Commit 37c2e806 authored by Kirill Smelkov's avatar Kirill Smelkov

X wcfs: Teach treediff to compute not only δtrack (set of nodes), but also δ for track-key coverage

This will, hopefully, be used to fix rebuild.

* t2:
  .
  .
  .
  .
  .
  .
  .
  X treediff ret += δtkeycov
  .
  .
parents 2c5f6517 175f92c9
...@@ -54,10 +54,11 @@ const tracePPSet = false ...@@ -54,10 +54,11 @@ const tracePPSet = false
type PPTreeSubSet map[zodb.Oid]*nodeInTree type PPTreeSubSet map[zodb.Oid]*nodeInTree
// nodeInTree represents tracking information about a node. // nodeInTree represents tracking information about a node.
// XXX -> treeNode ?
type nodeInTree struct { type nodeInTree struct {
parent zodb.Oid // parent node | InvalidOid for root parent zodb.Oid // parent node | InvalidOid for root
nchild int // number of direct children in PPTreeSubSet referring to this node nchild int // number of direct children in PPTreeSubSet referring to this node
// XXX + [lo,hi) range this node is coming under in its parent // XXX + [lo,hi) range this node is coming under in its parent XXX -> in its tree ?
} }
......
...@@ -398,11 +398,13 @@ func (S RangedKeySet) String() string { ...@@ -398,11 +398,13 @@ func (S RangedKeySet) String() string {
} }
func (r KeyRange) String() string { func (r KeyRange) String() string {
hi := r.hi_ var shi string
if hi < KeyMax { if r.hi_ == KeyMax {
hi += 1 shi = kstr(r.hi_) // ∞
} else {
shi = fmt.Sprintf("%d", r.hi_+1)
} }
return fmt.Sprintf("[%s,%s)", kstr(r.lo), kstr(hi)) return fmt.Sprintf("[%s,%s)", kstr(r.lo), shi)
} }
......
This diff is collapsed.
...@@ -36,13 +36,6 @@ type SetOid = set.SetOid ...@@ -36,13 +36,6 @@ type SetOid = set.SetOid
const debug = false
func tracef(format string, argv ...interface{}) {
if debug {
fmt.Printf(format, argv...)
}
}
// nodePathToPath converts path from []Node to []Oid. // nodePathToPath converts path from []Node to []Oid.
// XXX place = ? // XXX place = ?
func nodePathToPath(nodePath []Node) (path []zodb.Oid) { func nodePathToPath(nodePath []Node) (path []zodb.Oid) {
......
...@@ -22,6 +22,7 @@ package xbtree ...@@ -22,6 +22,7 @@ package xbtree
import ( import (
"context" "context"
"fmt"
"sort" "sort"
"strings" "strings"
...@@ -36,6 +37,10 @@ import ( ...@@ -36,6 +37,10 @@ import (
// TODO kill this after rebuild is finished // TODO kill this after rebuild is finished
const XXX_killWhenRebuildWorks = true const XXX_killWhenRebuildWorks = true
const traceΔBtail = false
const debugΔBtail = false
// ΔBtail represents tail of revisional changes to BTrees. // ΔBtail represents tail of revisional changes to BTrees.
// //
// It semantically consists of // It semantically consists of
...@@ -99,7 +104,7 @@ type ΔBtail struct { ...@@ -99,7 +104,7 @@ type ΔBtail struct {
// set of tracked nodes as of @head state. // set of tracked nodes as of @head state.
trackSet PPTreeSubSet trackSet PPTreeSubSet
// set of node that were requested to be tracked, but for which vδB was not yet rebuilt // set of nodes that were requested to be tracked, but for which vδB was not yet rebuilt
trackNew PPTreeSubSet trackNew PPTreeSubSet
} }
...@@ -217,18 +222,19 @@ func (δBtail *ΔBtail) Track(key Key, keyPresent bool, nodePath []Node) error { ...@@ -217,18 +222,19 @@ func (δBtail *ΔBtail) Track(key Key, keyPresent bool, nodePath []Node) error {
pathv := []string{} pathv := []string{}
for _, node := range nodePath { pathv = append(pathv, vnode(node)) } for _, node := range nodePath { pathv = append(pathv, vnode(node)) }
tracef("Track [%v] %s\n", key, strings.Join(pathv, " -> ")) tracefΔBtail("\nTrack [%v] %s\n", key, strings.Join(pathv, " -> "))
return δBtail.track(key, keyPresent, path) return δBtail.track(key, keyPresent, path)
} }
func (δBtail *ΔBtail) track(key Key, keyPresent bool, path []zodb.Oid) error { func (δBtail *ΔBtail) track(key Key, keyPresent bool, path []zodb.Oid) error {
// XXX locking
root := path[0] root := path[0]
δBtail.trackNew.AddPath(path) δBtail.trackNew.AddPath(path)
// track is track of path[-1] (i.e. leaf) // track is track of path[-1] (i.e. leaf)
// XXX hack - until rebuild is implemented // XXX hack - until rebuild is implemented
if XXX_killWhenRebuildWorks { if XXX_killWhenRebuildWorks {
_, ok := δBtail.byRoot[root] _, ok := δBtail.byRoot[root]
...@@ -248,6 +254,10 @@ func (δBtail *ΔBtail) rebuild() (err error) { ...@@ -248,6 +254,10 @@ func (δBtail *ΔBtail) rebuild() (err error) {
defer xerr.Context(&err, "ΔBtail rebuild") defer xerr.Context(&err, "ΔBtail rebuild")
// XXX locking // XXX locking
tracefΔBtail("\nRebuild @%s .. @%s\n", δBtail.Tail(), δBtail.Head())
tracefΔBtail("trackSet: %v\n", δBtail.trackSet)
tracefΔBtail("trackNew: %v\n", δBtail.trackNew)
trackNew := δBtail.trackNew trackNew := δBtail.trackNew
δBtail.trackNew = PPTreeSubSet{} δBtail.trackNew = PPTreeSubSet{}
...@@ -272,11 +282,11 @@ func (δBtail *ΔBtail) rebuild() (err error) { ...@@ -272,11 +282,11 @@ func (δBtail *ΔBtail) rebuild() (err error) {
δZTC, δtopsByRoot := δZConnectTracked(δZ.Changev, trackNew) δZTC, δtopsByRoot := δZConnectTracked(δZ.Changev, trackNew)
tracef("\nrebuild @%s <- @%s\n", atPrev, δZ.Rev) debugfΔBtail("\n rebuild @%s <- @%s\n", atPrev, δZ.Rev)
tracef("δZ:\t%v\n", δZ.Changev) debugfΔBtail(" δZ:\t%v\n", δZ.Changev)
tracef("trackNew: %v\n", trackNew) debugfΔBtail(" trackNew: %v\n", trackNew)
tracef("trackSet: %v\n", δBtail.trackSet) // XXX needed? debugfΔBtail(" trackSet: %v\n", δBtail.trackSet) // XXX needed?
defer tracef("\n\n") defer debugfΔBtail("\n\n")
// XXX len(δtopsByRoot) == 0 -> skip // XXX len(δtopsByRoot) == 0 -> skip
...@@ -296,12 +306,15 @@ func (δBtail *ΔBtail) rebuild() (err error) { ...@@ -296,12 +306,15 @@ func (δBtail *ΔBtail) rebuild() (err error) {
for root, δtops := range δtopsByRoot { for root, δtops := range δtopsByRoot {
// diff backwards curr -> prev // diff backwards curr -> prev
δT, δtrack, err := treediff(ctx, root, δtops, δZTC, trackNew, zconnCurr, zconnPrev) δT, δtrack, δtkeycov, err := treediff(ctx, root, δtops, δZTC, trackNew, zconnCurr, zconnPrev)
if err != nil { if err != nil {
return err return err
} }
tracef("-> root<%s> δkv*: %v δtrack*: %v\n", root, δT, δtrack) // FIXME use δtkeycov to recompute track coverage
_ = δtkeycov
debugfΔBtail(" -> root<%s> δkv*: %v δtrack*: %v\n", root, δT, δtrack)
trackNew.ApplyΔ(δtrack) trackNew.ApplyΔ(δtrack)
vδtrack = append([]*ΔPPTreeSubSet{δtrack}, vδtrack...) vδtrack = append([]*ΔPPTreeSubSet{δtrack}, vδtrack...)
...@@ -371,9 +384,24 @@ func (δBtail *ΔBtail) rebuild() (err error) { ...@@ -371,9 +384,24 @@ func (δBtail *ΔBtail) rebuild() (err error) {
// //
// TODO optionally accept zconnOld/zconnNew from client // TODO optionally accept zconnOld/zconnNew from client
func (δBtail *ΔBtail) Update(δZ *zodb.EventCommit) (_ ΔB, err error) { func (δBtail *ΔBtail) Update(δZ *zodb.EventCommit) (_ ΔB, err error) {
δB, _, err := δBtail._Update(δZ)
return δB, err
}
// _Update serves Update and also returns δtkeycov from treediff for testing.
type _ΔTrackKeyCov struct {
ByRoot map[zodb.Oid]*RangedKeySet // {} root -> δtrackedKeys (always grow)
}
func (δBtail *ΔBtail) _Update(δZ *zodb.EventCommit) (_ ΔB, δTKeyCov _ΔTrackKeyCov, err error) {
headOld := δBtail.Head() headOld := δBtail.Head()
defer xerr.Contextf(&err, "ΔBtail update %s -> %s", headOld, δZ.Tid) defer xerr.Contextf(&err, "ΔBtail update %s -> %s", headOld, δZ.Tid)
tracefΔBtail("\nUpdate @%s -> @%s δZ: %v\n", δBtail.Head(), δZ.Tid, δZ.Changev)
tracefΔBtail("trackSet: %v\n", δBtail.trackSet)
tracefΔBtail("trackNew: %v\n", δBtail.trackNew)
δTKeyCov = _ΔTrackKeyCov{ByRoot: make(map[zodb.Oid]*RangedKeySet)}
if XXX_killWhenRebuildWorks { if XXX_killWhenRebuildWorks {
// XXX hack - until vvv is reenabled // XXX hack - until vvv is reenabled
δBtail.trackSet.UnionInplace(δBtail.trackNew) δBtail.trackSet.UnionInplace(δBtail.trackNew)
...@@ -383,14 +411,10 @@ if XXX_killWhenRebuildWorks { ...@@ -383,14 +411,10 @@ if XXX_killWhenRebuildWorks {
// update .trackSet and vδB from .trackNew // update .trackSet and vδB from .trackNew
err = δBtail.rebuild() err = δBtail.rebuild()
if err != nil { if err != nil {
return ΔB{}, err return ΔB{}, δTKeyCov, err
} }
} }
tracef("Update @%s -> @%s\n", δBtail.Head(), δZ.Tid)
tracef("δZ:\t%v\n", δZ.Changev)
tracef("trackSet: %v\n", δBtail.trackSet)
// XXX dup wrt rebuild? // XXX dup wrt rebuild?
δBtail.δZtail.Append(δZ.Tid, δZ.Changev) δBtail.δZtail.Append(δZ.Tid, δZ.Changev)
...@@ -401,7 +425,7 @@ if XXX_killWhenRebuildWorks { ...@@ -401,7 +425,7 @@ if XXX_killWhenRebuildWorks {
// skip opening DB connections if there is no change to any tree node // skip opening DB connections if there is no change to any tree node
if len(δtopsByRoot) == 0 { if len(δtopsByRoot) == 0 {
return δB, nil return δB, δTKeyCov, nil
} }
// open ZODB connections corresponding to "old" and "new" states // open ZODB connections corresponding to "old" and "new" states
...@@ -410,20 +434,20 @@ if XXX_killWhenRebuildWorks { ...@@ -410,20 +434,20 @@ if XXX_killWhenRebuildWorks {
defer txn.Abort() defer txn.Abort()
zconnOld, err := δBtail.db.Open(ctx, &zodb.ConnOptions{At: headOld}) zconnOld, err := δBtail.db.Open(ctx, &zodb.ConnOptions{At: headOld})
if err != nil { if err != nil {
return ΔB{}, err return ΔB{}, δTKeyCov, err
} }
zconnNew, err := δBtail.db.Open(ctx, &zodb.ConnOptions{At: δZ.Tid}) zconnNew, err := δBtail.db.Open(ctx, &zodb.ConnOptions{At: δZ.Tid})
if err != nil { if err != nil {
return ΔB{}, err return ΔB{}, δTKeyCov, err
} }
for root, δtops := range δtopsByRoot { for root, δtops := range δtopsByRoot {
δT, δtrack, err := treediff(ctx, root, δtops, δZTC, δBtail.trackSet, zconnOld, zconnNew) δT, δtrack, δtkeycov, err := treediff(ctx, root, δtops, δZTC, δBtail.trackSet, zconnOld, zconnNew)
if err != nil { if err != nil {
return ΔB{}, err return ΔB{}, δTKeyCov, err
} }
tracef("\n-> root<%s> δkv: %v δtrack: %v\n", root, δT, δtrack) tracefΔBtail("\n-> root<%s> δkv: %v δtrack: %v δtkeycov: %v\n", root, δT, δtrack, δtkeycov)
if len(δT) > 0 { // an object might be resaved without change if len(δT) > 0 { // an object might be resaved without change
δB.ByRoot[root] = δT δB.ByRoot[root] = δT
...@@ -441,9 +465,10 @@ if XXX_killWhenRebuildWorks { ...@@ -441,9 +465,10 @@ if XXX_killWhenRebuildWorks {
} }
δBtail.trackSet.ApplyΔ(δtrack) δBtail.trackSet.ApplyΔ(δtrack)
δTKeyCov.ByRoot[root] = δtkeycov
} }
return δB, nil return δB, δTKeyCov, nil
} }
...@@ -563,3 +588,16 @@ func (δBtail *ΔBtail) SliceByRootRev(root zodb.Oid, lo, hi zodb.Tid) /*readonl ...@@ -563,3 +588,16 @@ func (δBtail *ΔBtail) SliceByRootRev(root zodb.Oid, lo, hi zodb.Tid) /*readonl
func (δBtail *ΔBtail) ΔZtail() /*readonly*/*zodb.ΔTail { func (δBtail *ΔBtail) ΔZtail() /*readonly*/*zodb.ΔTail {
return δBtail.δZtail return δBtail.δZtail
} }
func tracefΔBtail(format string, argv ...interface{}) {
if traceΔBtail {
fmt.Printf(format, argv...)
}
}
func debugfΔBtail(format string, argv ...interface{}) {
if debugΔBtail {
fmt.Printf(format, argv...)
}
}
...@@ -300,11 +300,19 @@ func (rbs RBucketSet) coverage() string { ...@@ -300,11 +300,19 @@ func (rbs RBucketSet) coverage() string {
return s return s
} }
// trackSet returns what should be ΔBtree.trackSet for specified tracked key set. // trackSet returns what should be ΔBtree.trackSet coverage for specified tracked key set.
func (rbs RBucketSet) trackSet(tracked SetKey) PPTreeSubSet { func (rbs RBucketSet) trackSet(tracked SetKey) PPTreeSubSet {
trackSet := PPTreeSubSet{} trackSet, _ := rbs.trackSetWithCov(tracked)
return trackSet
}
// trackSetWithCov returns what should be ΔBtree.trackSet and its key coverage for specified tracked key set.
func (rbs RBucketSet) trackSetWithCov(tracked SetKey) (trackSet PPTreeSubSet, keyCover *RangedKeySet) {
trackSet = PPTreeSubSet{}
keyCover = &RangedKeySet{}
for k := range tracked { for k := range tracked {
kb := rbs.Get(k) kb := rbs.Get(k)
keyCover.AddRange(KeyRange{kb.lo, kb.hi_})
// trackSet explicitly records only regular buckets. // trackSet explicitly records only regular buckets.
// embedded buckets all have oid=zodb.InvalidOid and would lead to z // embedded buckets all have oid=zodb.InvalidOid and would lead to z
newNode := false newNode := false
...@@ -345,7 +353,7 @@ func (rbs RBucketSet) trackSet(tracked SetKey) PPTreeSubSet { ...@@ -345,7 +353,7 @@ func (rbs RBucketSet) trackSet(tracked SetKey) PPTreeSubSet {
p = p.parent p = p.parent
} }
} }
return trackSet return trackSet, keyCover
} }
...@@ -666,7 +674,7 @@ func xverifyΔBTail_Update(t *testing.T, subj string, db *zodb.DB, treeRoot zodb ...@@ -666,7 +674,7 @@ func xverifyΔBTail_Update(t *testing.T, subj string, db *zodb.DB, treeRoot zodb
// tracked state defined by initialTrackedKeys. // tracked state defined by initialTrackedKeys.
func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zodb.Oid, at1,at2 zodb.Tid, xkv1,xkv2 RBucketSet, δZ *zodb.EventCommit, d12 map[Key]Δstring, initialTrackedKeys SetKey, kadj KAdjMatrix) { func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zodb.Oid, at1,at2 zodb.Tid, xkv1,xkv2 RBucketSet, δZ *zodb.EventCommit, d12 map[Key]Δstring, initialTrackedKeys SetKey, kadj KAdjMatrix) {
X := exc.Raiseif X := exc.Raiseif
tracef("\n>>> Track=%s\n", initialTrackedKeys) // t.Logf("\n>>> Track=%s\n", initialTrackedKeys)
var TrackedδZ SetKey = nil var TrackedδZ SetKey = nil
var kadjTrackedδZ SetKey = nil var kadjTrackedδZ SetKey = nil
...@@ -780,21 +788,18 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod ...@@ -780,21 +788,18 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod
// verify δbtail.trackSet against @at1 // verify δbtail.trackSet against @at1
// trackSet1 = xkv1[tracked1] // trackSet1 = xkv1[tracked1]
trackSet1 := xkv1.trackSet(initialTrackedKeys) trackSet1, tkeyCov1 := xkv1.trackSetWithCov(initialTrackedKeys)
// if !reflect.DeepEqual(trackSet1, δbtail.trackSet) {
// badf("δbtail.trackSet1 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, trackSet1)
// }
if !δbtail.trackSet.Equal(ø) { if !δbtail.trackSet.Equal(ø) {
badf("δbtail.trackSet1 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, ø) badf("δbtail.trackSet1 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, ø)
} }
if !δbtail.trackNew.Equal(trackSet1) { if !δbtail.trackNew.Equal(trackSet1) {
badf("δbtail.trackNew1 wrong:\n\thave: %v\n\twant: %v", δbtail.trackNew, trackSet1) badf("δbtail.trackNew1 wrong:\n\thave: %v\n\twant: %v", δbtail.trackNew, trackSet1)
} }
// δbtail.assertTrack(t, "1", /*ø*/PPTreeSubSet{}, trackSet1) // δbtail.assertTrack(t, "1", ø, trackSet1)
// δB <- δZ // δB <- δZ
δB, err := δbtail.Update(δZ); X(err) δB, δTKeyCov, err := δbtail._Update(δZ); X(err)
if δB.Rev != δZ.Tid { if δB.Rev != δZ.Tid {
badf("δB: rev != δZ.Tid ; rev=%s δZ.Tid=%s", δB.Rev, δZ.Tid) badf("δB: rev != δZ.Tid ; rev=%s δZ.Tid=%s", δB.Rev, δZ.Tid)
return return
...@@ -802,17 +807,14 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod ...@@ -802,17 +807,14 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod
// verify δbtail.trackSet against @at2 // verify δbtail.trackSet against @at2
// trackSet2 = xkv2[tracked2] ( = xkv2[kadj[tracked1]] // trackSet2 = xkv2[tracked2] ( = xkv2[kadj[tracked1]]
trackSet2 := xkv2.trackSet(initialTrackedKeys.Union(kadjTrackedδZ)) trackSet2, tkeyCov2 := xkv2.trackSetWithCov(initialTrackedKeys.Union(kadjTrackedδZ))
// if !reflect.DeepEqual(trackSet2, δbtail.trackSet) {
// badf("δbtail.trackSet2 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, trackSet2)
// }
if !δbtail.trackSet.Equal(trackSet2) { if !δbtail.trackSet.Equal(trackSet2) {
badf("δbtail.trackSet2 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, trackSet2) badf("δbtail.trackSet2 wrong:\n\thave: %v\n\twant: %v", δbtail.trackSet, trackSet2)
} }
if !δbtail.trackNew.Equal(ø) { if !δbtail.trackNew.Equal(ø) {
badf("δbtail.trackNew2 wrong:\n\thave: %v\n\twant: %v", δbtail.trackNew, ø) badf("δbtail.trackNew2 wrong:\n\thave: %v\n\twant: %v", δbtail.trackNew, ø)
} }
// δbtail.assertTrack(t, "2", trackSet2, /*ø*/PPTreeSubSet{}) // δbtail.assertTrack(t, "2", trackSet2, ø)
// assert δB.ByRoot == {treeRoot -> ...} if δTok != ø // assert δB.ByRoot == {treeRoot -> ...} if δTok != ø
...@@ -833,6 +835,13 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod ...@@ -833,6 +835,13 @@ func xverifyΔBTail_Update1(t *testing.T, subj string, db *zodb.DB, treeRoot zod
return return
} }
// assert δtkeycov == δ(tkeyCov1, tkeyCov2)
δtkeycovOK := tkeyCov2.Difference(tkeyCov1)
δtkeycov := δTKeyCov.ByRoot[treeRoot]
if !δtkeycov.Equal(δtkeycovOK) {
badf("δtkeycov wrong:\nhave: %s\nwant: %s", δtkeycov, δtkeycovOK)
}
// δT <- δB // δT <- δB
δToid := δB.ByRoot[treeRoot] // {} k -> δoid δToid := δB.ByRoot[treeRoot] // {} k -> δoid
...@@ -1354,7 +1363,7 @@ func testΔBTail(t *testing.T, testq chan ΔBTestEntry) { ...@@ -1354,7 +1363,7 @@ func testΔBTail(t *testing.T, testq chan ΔBTestEntry) {
t2.prev = t1 // XXX ----//---- t2.prev = t1 // XXX ----//----
subj := fmt.Sprintf("%s -> %s", t1.tree, t2.tree) subj := fmt.Sprintf("%s -> %s", t1.tree, t2.tree)
tracef("\n\n\n**** %s ****\n\n", subj) // t.Logf("\n\n\n**** %s ****\n\n", subj)
// KAdj // KAdj
if kadjOK := test.kadjOK; kadjOK != nil { if kadjOK := test.kadjOK; kadjOK != nil {
...@@ -1938,12 +1947,7 @@ func sortedKeys(kv map[Key]Δstring) []Key { ...@@ -1938,12 +1947,7 @@ func sortedKeys(kv map[Key]Δstring) []Key {
} }
func (b *RBucket) String() string { func (b *RBucket) String() string {
// XXX dup wrt nodeInRange.String return fmt.Sprintf("%sB%s{%s}", KeyRange{b.lo, b.hi_}, b.oid, kvtxt(b.kv))
hi := b.hi_
if hi < KeyMax {
hi += 1
}
return fmt.Sprintf("[%s,%s)B%s{%s}", kstr(b.lo), kstr(hi), b.oid, kvtxt(b.kv))
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment