Skip to content

Commit

Permalink
Use BitSchemaPosting for schema keys.
Browse files Browse the repository at this point in the history
- Make `dgraph debug --jepsen` work across predicate stripes.
- Sometimes it gets hard to debug, why we can't parse the PostingList.
Almost always, it is due to the fact that we came across a schema key.
We were not setting any UserMeta for it, so it was hard to determine the
root cause as well. This PR sets the UserMeta for schema keys, so
debugging is easier.
  • Loading branch information
manishrjain committed Nov 27, 2018
1 parent c4bb411 commit 5c97853
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
26 changes: 19 additions & 7 deletions dgraph/cmd/debug/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"math"
"strconv"
"strings"

"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
Expand Down Expand Up @@ -91,9 +92,13 @@ func readAmount(txn *badger.Txn, uid uint64) int {
itr := txn.NewIterator(iopt)
defer itr.Close()

key := x.DataKey("amount_0", uid)
for itr.Seek(key); itr.Valid(); {
for itr.Rewind(); itr.Valid(); {
item := itr.Item()
pk := x.Parse(item.Key())
if !pk.IsData() || pk.Uid != uid || !strings.HasPrefix(pk.Attr, "amount_") {
itr.Next()
continue
}
pl, err := posting.ReadPostingList(item.KeyCopy(nil), itr)
if err != nil {
log.Fatalf("Unable to read posting list: %v", err)
Expand All @@ -105,8 +110,12 @@ func readAmount(txn *badger.Txn, uid uint64) int {
times++
return nil
})
x.AssertTrue(times <= 1)
x.Check(err)
if times == 0 {
itr.Next()
continue
}
x.AssertTrue(times <= 1)
return amount
}
return 0
Expand All @@ -116,9 +125,6 @@ func seekTotal(db *badger.DB, readTs uint64) int {
txn := db.NewTransactionAt(readTs, false)
defer txn.Discard()

pk := x.ParsedKey{Attr: "key_0"}
prefix := pk.DataPrefix()

iopt := badger.DefaultIteratorOptions
iopt.AllVersions = true
iopt.PrefetchValues = false
Expand All @@ -127,14 +133,20 @@ func seekTotal(db *badger.DB, readTs uint64) int {

keys := make(map[uint64]int)
var lastKey []byte
for itr.Seek(prefix); itr.ValidForPrefix(prefix); {
for itr.Rewind(); itr.Valid(); {
item := itr.Item()
if bytes.Equal(lastKey, item.Key()) {
itr.Next()
continue
}
lastKey = append(lastKey[:0], item.Key()...)
pk := x.Parse(item.Key())
if !pk.IsData() || !strings.HasPrefix(pk.Attr, "key_") {
continue
}
if pk.IsSchema() {
continue
}

pl, err := posting.ReadPostingList(item.KeyCopy(nil), itr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
Del uint32 = 0x02

// Metadata Bit which is stored to find out whether the stored value is pl or byte slice.
BitSchemaPosting byte = 0x01
BitDeltaPosting byte = 0x04
BitCompletePosting byte = 0x08
BitEmptyPosting byte = 0x10 | BitCompletePosting
Expand Down
2 changes: 1 addition & 1 deletion worker/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func updateSchema(attr string, s pb.SchemaUpdate) error {
defer txn.Discard()
data, err := s.Marshal()
x.Check(err)
if err := txn.Set(x.SchemaKey(attr), data); err != nil {
if err := txn.SetWithMeta(x.SchemaKey(attr), data, posting.BitSchemaPosting); err != nil {
return err
}
return txn.CommitAt(1, nil)
Expand Down

0 comments on commit 5c97853

Please sign in to comment.