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

fix(subscriptions): fix subscription to use the kv with the max version #7349

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,8 @@ func listenForCorsUpdate(closer *z.Closer) {
// Remove uid from the key, to get the correct prefix
prefix = prefix[:len(prefix)-8]
worker.SubscribeForUpdates([][]byte{prefix}, func(kvs *badgerpb.KVList) {
// Last update contains the latest value. So, taking the last update.
lastIdx := len(kvs.GetKv()) - 1
kv := kvs.GetKv()[lastIdx]

kv := x.KvWithMaxVersion(kvs, [][]byte{prefix}, "CORS Subscription")
glog.Infof("Updating cors from subscription.")
// Unmarshal the incoming posting list.
pl := &pb.PostingList{}
Expand Down
3 changes: 2 additions & 1 deletion edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ func RefreshAcls(closer *z.Closer) {
if kvs == nil || len(kvs.Kv) == 0 {
return
}
NamanJain8 marked this conversation as resolved.
Show resolved Hide resolved
if err := retrieveAcls(kvs.Kv[0].Version); err != nil {
kv := x.KvWithMaxVersion(kvs, aclPrefixes, "ACL Subscription")
if err := retrieveAcls(kv.GetVersion()); err != nil {
glog.Errorf("Error while retrieving acls: %v", err)
}
}, 1, closer)
Expand Down
4 changes: 1 addition & 3 deletions graphql/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,8 @@ func newAdminResolver(
prefix = prefix[:len(prefix)-8]
// Listen for graphql schema changes in group 1.
go worker.SubscribeForUpdates([][]byte{prefix}, func(kvs *badgerpb.KVList) {
// Last update contains the latest value. So, taking the last update.
lastIdx := len(kvs.GetKv()) - 1
kv := kvs.GetKv()[lastIdx]

kv := x.KvWithMaxVersion(kvs, [][]byte{prefix}, "GraphQL Schema Subscription")
glog.Infof("Updating GraphQL schema from subscription.")

// Unmarshal the incoming posting list.
Expand Down
28 changes: 28 additions & 0 deletions x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"github.com/dgraph-io/badger/v3"
bo "github.com/dgraph-io/badger/v3/options"
badgerpb "github.com/dgraph-io/badger/v3/pb"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/ristretto/z"
Expand Down Expand Up @@ -1202,3 +1203,30 @@ func ToHex(i uint64, rdf bool) []byte {

return out
}

// KvWithMaxVersion returns a KV with the max version from the list of KVs.
func KvWithMaxVersion(kvs *badgerpb.KVList, prefixes [][]byte, tag string) *badgerpb.KV {
hasAnyPrefix := func(key []byte) bool {
for _, prefix := range prefixes {
if bytes.HasPrefix(key, prefix) {
return true
}
}
return false
}
// Iterate over kvs to get the KV with the latest version. It is not necessary that the last
// KV contain the latest value.
var maxKv *badgerpb.KV
for _, kv := range kvs.GetKv() {
if !hasAnyPrefix(kv.GetKey()) {
// Verify that we got the key which was subscribed. This shouldn't happen, but added for
// robustness.
glog.Errorf("[%s] Got key: %x which was not subscribed", tag, kv.GetKey())
continue
}
if maxKv.GetVersion() <= kv.GetVersion() {
maxKv = kv
}
}
return maxKv
}