-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathstate.go
206 lines (164 loc) · 4.51 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mesh
import (
"fmt"
"sync"
"time"
"github.com/gogo/protobuf/proto"
"github.com/weaveworks/mesh"
"k8s.io/kops/protokube/pkg/gossip"
)
// state is an implementation of a LWW map
type state struct {
mtx sync.RWMutex
data KVState
self mesh.PeerName
lastSnapshot *gossip.GossipStateSnapshot
version uint64
}
//// state implements GossipData.
//var _ mesh.GossipData = &state{}
// Construct an empty state object, ready to receive updates.
// This is suitable to use at program start.
// Other peers will populate us with data.
func newState(self mesh.PeerName) *state {
return &state{
self: self,
}
}
func (s *state) now() uint64 {
// TODO: This relies on NTP. We could have a g-counter or something, but this is probably good enough for V1
// It's good enough for weave :-)
return uint64(time.Now().Unix())
}
func (s *state) snapshot() *gossip.GossipStateSnapshot {
s.mtx.Lock()
defer s.mtx.Unlock()
if s.lastSnapshot != nil && s.lastSnapshot.Version == s.version {
// Potential optimization - this common path only needs a read-lock
return s.lastSnapshot
}
values := make(map[string]string)
for k, v := range s.data.Records {
if v.Tombstone {
continue
}
values[k] = string(v.Data)
}
snapshot := &gossip.GossipStateSnapshot{
Values: values,
Version: s.version,
}
s.lastSnapshot = snapshot
return snapshot
}
func (s *state) updateValues(removeKeys []string, putEntries map[string]string) {
if len(removeKeys) == 0 && len(putEntries) == 0 {
return
}
s.mtx.Lock()
defer s.mtx.Unlock()
now := s.now()
if s.data.Records == nil {
s.data.Records = make(map[string]*KVStateRecord)
}
for _, k := range removeKeys {
v := &KVStateRecord{
Tombstone: true,
Version: now,
}
s.data.Records[k] = v
}
for k, v := range putEntries {
// TODO: Check that now > existing version?
s.data.Records[k] = &KVStateRecord{
Data: []byte(v),
Version: now,
}
}
s.version++
}
// getData returns a copy of our data, suitable for gossiping
func (s *state) getData() *KVState {
s.mtx.RLock()
defer s.mtx.RUnlock()
// make a deep-copy. To avoid a bunch of reflection etc. this simply marshals and unmarshals
b, _ := proto.Marshal(&s.data)
d, _ := DecodeKVState(b)
return d
}
func (s *state) merge(message *KVState, changes *KVState) {
s.mtx.Lock()
defer s.mtx.Unlock()
var c KVState
if s.data.Records != nil {
c.Records = make(map[string]*KVStateRecord)
for k, v := range s.data.Records {
c.Records[k] = v
}
}
changed := mergeKVState(&c, message, changes)
if changed {
s.version++
s.data = c
}
}
var _ mesh.GossipData = &KVState{}
func mergeKVState(dest *KVState, src *KVState, changes *KVState) bool {
changed := false
if dest.Records == nil {
dest.Records = make(map[string]*KVStateRecord)
}
if changes != nil && changes.Records == nil {
changes.Records = make(map[string]*KVStateRecord)
}
for k, update := range src.Records {
existing, found := dest.Records[k]
if found && existing.Version >= update.Version {
continue
}
dest.Records[k] = update
// this is required for deltas
if changes != nil {
changes.Records[k] = update
}
changed = true
}
return changed
}
// Encode serializes our complete state to a slice of byte-slices.
// In this simple example, we use a single gob-encoded
// buffer: see https://golang.org/pkg/encoding/gob/
func (s *KVState) Encode() [][]byte {
data, err := proto.Marshal(s)
if err != nil {
panic(fmt.Sprintf("error encoding gossip state: %v", err))
}
return [][]byte{data}
}
func DecodeKVState(data []byte) (*KVState, error) {
state := &KVState{}
err := proto.Unmarshal(data, state)
if err != nil {
return nil, fmt.Errorf("error decoding gossip state: %v", err)
}
return state, nil
}
// Merge merges the other GossipData into this one,
// and returns our resulting, complete state.
func (s *KVState) Merge(other mesh.GossipData) (complete mesh.GossipData) {
otherState := other.(*KVState)
mergeKVState(s, otherState, nil)
return s
}