Fix race in gossip initialization

This commit is contained in:
Justin Santa Barbara 2017-06-19 11:12:08 -04:00
parent 5955467be0
commit 734289043a
1 changed files with 18 additions and 1 deletions

View File

@ -90,8 +90,8 @@ func (s *state) snapshot() *gossip.GossipStateSnapshot {
} }
s.lastSnapshot = snapshot s.lastSnapshot = snapshot
return snapshot return snapshot
} }
func (s *state) put(key string, data []byte) { func (s *state) put(key string, data []byte) {
s.mtx.Lock() s.mtx.Lock()
defer s.mtx.Unlock() defer s.mtx.Unlock()
@ -102,6 +102,11 @@ func (s *state) put(key string, data []byte) {
Data: data, Data: data,
Version: now, Version: now,
} }
if s.data.Records == nil {
s.data.Records = make(map[string]*KVStateRecord)
}
s.data.Records[key] = v s.data.Records[key] = v
s.version++ s.version++
} }
@ -116,6 +121,10 @@ func (s *state) updateValues(removeKeys []string, putEntries map[string]string)
now := s.now() now := s.now()
if s.data.Records == nil {
s.data.Records = make(map[string]*KVStateRecord)
}
for _, k := range removeKeys { for _, k := range removeKeys {
v := &KVStateRecord{ v := &KVStateRecord{
Tombstone: true, Tombstone: true,
@ -161,6 +170,14 @@ var _ mesh.GossipData = &KVState{}
func mergeKVState(dest *KVState, src *KVState, changes *KVState) bool { func mergeKVState(dest *KVState, src *KVState, changes *KVState) bool {
changed := false 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 { for k, update := range src.Records {
existing, found := dest.Records[k] existing, found := dest.Records[k]
if found && existing.Version >= update.Version { if found && existing.Version >= update.Version {