| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package etcd3 |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"go.etcd.io/etcd/api/v3/mvccpb" |
| 22 |
clientv3 "go.etcd.io/etcd/client/v3" |
| 23 |
) |
| 24 |
|
| 25 |
type event struct { |
| 26 |
key string |
| 27 |
value []byte |
| 28 |
prevValue []byte |
| 29 |
rev int64 |
| 30 |
isDeleted bool |
| 31 |
isCreated bool |
| 32 |
isProgressNotify bool |
| 33 |
// isInitialEventsEndBookmark helps us keep track |
| 34 |
// of whether we have sent an annotated bookmark event. |
| 35 |
// |
| 36 |
// when this variable is set to true, |
| 37 |
// a special annotation will be added |
| 38 |
// to the bookmark event. |
| 39 |
// |
| 40 |
// note that we decided to extend the event |
| 41 |
// struct field to eliminate contention |
| 42 |
// between startWatching and processEvent |
| 43 |
isInitialEventsEndBookmark bool |
| 44 |
} |
| 45 |
|
| 46 |
// parseKV converts a KeyValue retrieved from an initial sync() listing to a synthetic isCreated event. |
| 47 |
func parseKV(kv *mvccpb.KeyValue) *event { |
| 48 |
return &event{ |
| 49 |
key: string(kv.Key), |
| 50 |
value: kv.Value, |
| 51 |
prevValue: nil, |
| 52 |
rev: kv.ModRevision, |
| 53 |
isDeleted: false, |
| 54 |
isCreated: true, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func parseEvent(e *clientv3.Event) (*event, error) { |
| 59 |
if !e.IsCreate() && e.PrevKv == nil { |
| 60 |
// If the previous value is nil, error. One example of how this is possible is if the previous value has been compacted already. |
| 61 |
return nil, fmt.Errorf("etcd event received with PrevKv=nil (key=%q, modRevision=%d, type=%s)", string(e.Kv.Key), e.Kv.ModRevision, e.Type.String()) |
| 62 |
|
| 63 |
} |
| 64 |
ret := &event{ |
| 65 |
key: string(e.Kv.Key), |
| 66 |
value: e.Kv.Value, |
| 67 |
rev: e.Kv.ModRevision, |
| 68 |
isDeleted: e.Type == clientv3.EventTypeDelete, |
| 69 |
isCreated: e.IsCreate(), |
| 70 |
} |
| 71 |
if e.PrevKv != nil { |
| 72 |
ret.prevValue = e.PrevKv.Value |
| 73 |
} |
| 74 |
return ret, nil |
| 75 |
} |
| 76 |
|
| 77 |
func progressNotifyEvent(rev int64) *event { |
| 78 |
return &event{ |
| 79 |
rev: rev, |
| 80 |
isProgressNotify: true, |
| 81 |
} |
| 82 |
} |