Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[contiv] Watchers may get closed after server restart if db was compacted #266

Merged
merged 1 commit into from
May 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 39 additions & 8 deletions db/keyval/etcdv3/bytes_broker_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,56 @@ func (db *BytesConnectionEtcd) Watch(resp func(keyval.BytesWatchResp), closeChan
}

// watchInternal starts the watch subscription for the key.
func watchInternal(log logging.Logger, watcher clientv3.Watcher, closeCh chan string, key string, resp func(keyval.BytesWatchResp)) error {
recvChan := watcher.Watch(context.Background(), key, clientv3.WithPrefix(), clientv3.WithPrevKV())
func watchInternal(log logging.Logger, watcher clientv3.Watcher, closeCh chan string, prefix string, resp func(keyval.BytesWatchResp)) error {
recvChan := watcher.Watch(context.Background(), prefix, clientv3.WithPrefix(), clientv3.WithPrevKV())

go func() {
registeredKey := key
go func(registeredKey string) {
var compactRev int64
for {
select {
case wresp := <-recvChan:
case wresp, ok := <-recvChan:
if !ok {
log.WithField("prefix", prefix).Warn("Watch recv channel was closed")
if compactRev != 0 {
recvChan = watcher.Watch(context.Background(), prefix,
clientv3.WithPrefix(), clientv3.WithPrevKV(), clientv3.WithRev(compactRev))
log.WithFields(logging.Fields{
"prefix": prefix,
"rev": compactRev,
}).Warn("Watch recv channel was re-created")
compactRev = 0
continue
}
return
}
if wresp.Canceled {
log.WithField("prefix", prefix).Warn("Watch was canceled")
}
err := wresp.Err()
if err != nil {
log.WithFields(logging.Fields{
"prefix": prefix,
"err": err,
}).Warn("Watch returned error")
}
compactRev = wresp.CompactRevision
if compactRev != 0 {
log.WithFields(logging.Fields{
"prefix": prefix,
"rev": compactRev,
}).Warn("Watched data were compacted ")
}
for _, ev := range wresp.Events {
handleWatchEvent(log, resp, ev)
}
case closeVal, ok := <-closeCh:
if !ok || registeredKey == closeVal {
log.WithField("key", key).Debug("Watch ended")
if !ok || closeVal == registeredKey {
log.WithField("prefix", prefix).Debug("Watch ended")
return
}
}
}
}()
}(prefix)
return nil
}

Expand Down