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

[Merged by Bors] - sync2: fptree: don't get next in FingerprintInterval if not needed #6558

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions sync2/fptree/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (

var ErrEasySplitFailed = errEasySplitFailed

func (ft *FPTree) FingerprintIntervalInternal(
fasmat marked this conversation as resolved.
Show resolved Hide resolved
x, y rangesync.KeyBytes,
limit int,
needNext bool,
) (fpr FPResult, err error) {
ft.np.lockRead()
defer ft.np.unlockRead()
return ft.fingerprintInterval(x, y, limit, needNext)
}

func (ft *FPTree) EasySplit(x, y rangesync.KeyBytes, limit int) (sr SplitResult, err error) {
return ft.easySplit(x, y, limit)
}
Expand Down
33 changes: 23 additions & 10 deletions sync2/fptree/fptree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,14 @@
func (ft *FPTree) FingerprintInterval(x, y rangesync.KeyBytes, limit int) (fpr FPResult, err error) {
ft.np.lockRead()
defer ft.np.unlockRead()
return ft.fingerprintInterval(x, y, limit)
return ft.fingerprintInterval(x, y, limit, false)
}

func (ft *FPTree) fingerprintInterval(x, y rangesync.KeyBytes, limit int) (fpr FPResult, err error) {
func (ft *FPTree) fingerprintInterval(
x, y rangesync.KeyBytes,
limit int,
needNext bool,
) (fpr FPResult, err error) {
ft.enter("fingerprintInterval: x %s y %s limit %d", x, y, limit)
defer func() {
ft.leave(fpr.FP, fpr.Count, fpr.IType, fpr.Items, fpr.Next, err)
Expand All @@ -1110,16 +1114,23 @@

if ac.items.Seq != nil {
ft.log("fingerprintInterval: items %v", ac.items)
fpr.Items = ac.items.Limit(int(ac.count))
fpr.Items = ac.items
} else {
fpr.Items = ft.from(x, 1).Limit(int(ac.count))
fpr.Items = ft.from(x, 1)
ft.log("fingerprintInterval: start from x: %v", fpr.Items)
}

if ac.next != nil {
switch {
case !needNext:
// The next item is only needed for splitting in case if easy split is not
// feasible, and it's better to avoid getting it as that may incur
// database access
fpr.Items = fpr.Items.Limit(int(fpr.Count))
return fpr, nil
case ac.next != nil:
ft.log("fingerprintInterval: next %s", ac.next)
fpr.Next = ac.next
} else if (fpr.IType == 0 && limit < 0) || fpr.Count == 0 {
case (fpr.IType == 0 && limit < 0) || fpr.Count == 0:
next, err := fpr.Items.First()
if err != nil {
return FPResult{}, err
Expand All @@ -1128,10 +1139,10 @@
fpr.Next = next.Clone()
}
ft.log("fingerprintInterval: next at start %s", fpr.Next)
} else if ac.lastPrefix != nil {
case ac.lastPrefix != nil:
fpr.Next, err = ft.nextFromPrefix(&ac, *ac.lastPrefix)
ft.log("fingerprintInterval: next at lastPrefix %s -> %s", *ac.lastPrefix, fpr.Next)
} else {
default:

Check warning on line 1145 in sync2/fptree/fptree.go

View check run for this annotation

Codecov / codecov/patch

sync2/fptree/fptree.go#L1145

Added line #L1145 was not covered by tests
next, err := ft.from(y, 1).First()
if err != nil {
return FPResult{}, err
Expand All @@ -1140,6 +1151,8 @@
ft.log("fingerprintInterval: next at y: %s", fpr.Next)
}

// We apply limit after we have retrieved the next item
fpr.Items = fpr.Items.Limit(int(fpr.Count))
return fpr, nil
}

Expand Down Expand Up @@ -1219,7 +1232,7 @@
return SplitResult{}, err
}

fpr0, err := ft.fingerprintInterval(x, y, limit)
fpr0, err := ft.fingerprintInterval(x, y, limit, true)
if err != nil {
return SplitResult{}, err
}
Expand All @@ -1228,7 +1241,7 @@
return SplitResult{}, errors.New("can't split empty range")
}

fpr1, err := ft.fingerprintInterval(fpr0.Next, y, -1)
fpr1, err := ft.fingerprintInterval(fpr0.Next, y, -1, false)
if err != nil {
return SplitResult{}, err
}
Expand Down
38 changes: 23 additions & 15 deletions sync2/fptree/fptree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func testFPTree(t *testing.T, makeFPTrees mkFPTreesFunc) {
count: 0,
itype: 0,
startIdx: -1,
endIdx: -1,
endIdx: 0,
},
{
xIdx: 0,
Expand Down Expand Up @@ -194,7 +194,7 @@ func testFPTree(t *testing.T, makeFPTrees mkFPTreesFunc) {
count: 0,
itype: -1,
startIdx: -1,
endIdx: -1,
endIdx: 0,
},
{
xIdx: 1,
Expand Down Expand Up @@ -234,7 +234,7 @@ func testFPTree(t *testing.T, makeFPTrees mkFPTreesFunc) {
count: 0,
itype: 1,
startIdx: -1,
endIdx: -1,
endIdx: 2,
},
{
xIdx: 3,
Expand Down Expand Up @@ -274,7 +274,7 @@ func testFPTree(t *testing.T, makeFPTrees mkFPTreesFunc) {
count: 0,
itype: -1,
startIdx: -1,
endIdx: -1,
endIdx: 0,
},
},
},
Expand Down Expand Up @@ -581,7 +581,7 @@ func testFPTree(t *testing.T, makeFPTrees mkFPTreesFunc) {
name = fmt.Sprintf("%d-%d_%d", rtc.xIdx, rtc.yIdx, rtc.limit)
}
t.Run(name, func(t *testing.T) {
fpr, err := ft.FingerprintInterval(x, y, rtc.limit)
fpr, err := ft.FingerprintIntervalInternal(x, y, rtc.limit, true)
require.NoError(t, err)
assert.Equal(t, rtc.fp, fpr.FP.String(), "fp")
assert.Equal(t, rtc.count, fpr.Count, "count")
Expand Down Expand Up @@ -963,25 +963,29 @@ func dumbFP(hs hashList, x, y rangesync.KeyBytes, limit int) fpResultWithBounds
case -1:
p := hs.findGTE(x)
pY := hs.findGTE(y)
fpr.start = hs.keyAt(p)
for {
if p >= pY || limit == 0 {
fpr.next = hs.keyAt(p)
break
}
if fpr.start == nil {
fpr.start = hs.keyAt(p)
}
fpr.fp.Update(hs.keyAt(p))
limit--
fpr.count++
p++
}
case 1:
p := hs.findGTE(x)
fpr.start = hs.keyAt(p)
for {
if p >= len(hs) || limit == 0 {
fpr.next = hs.keyAt(p)
break
}
if fpr.start == nil {
fpr.start = hs.keyAt(p)
}
fpr.fp.Update(hs.keyAt(p))
limit--
fpr.count++
Expand All @@ -997,6 +1001,9 @@ func dumbFP(hs hashList, x, y rangesync.KeyBytes, limit int) fpResultWithBounds
fpr.next = hs.keyAt(p)
break
}
if fpr.start == nil {
fpr.start = hs.keyAt(p)
}
fpr.fp.Update(hs.keyAt(p))
limit--
fpr.count++
Expand All @@ -1005,13 +1012,15 @@ func dumbFP(hs hashList, x, y rangesync.KeyBytes, limit int) fpResultWithBounds
default:
pX := hs.findGTE(x)
p := pX
fpr.start = hs.keyAt(p)
fpr.next = fpr.start
fpr.next = hs.keyAt(p)
for {
if limit == 0 {
fpr.next = hs.keyAt(p)
break
}
if fpr.start == nil {
fpr.start = hs.keyAt(p)
}
fpr.fp.Update(hs.keyAt(p))
limit--
fpr.count++
Expand All @@ -1026,14 +1035,15 @@ func dumbFP(hs hashList, x, y rangesync.KeyBytes, limit int) fpResultWithBounds

func verifyInterval(t *testing.T, hs hashList, ft *fptree.FPTree, x, y rangesync.KeyBytes, limit int) fptree.FPResult {
expFPR := dumbFP(hs, x, y, limit)
fpr, err := ft.FingerprintInterval(x, y, limit)
fpr, err := ft.FingerprintIntervalInternal(x, y, limit, true)
require.NoError(t, err)
require.Equal(t, expFPR, toFPResultWithBounds(t, fpr),
"x=%s y=%s limit=%d", x.String(), y.String(), limit)

require.Equal(t, expFPR, toFPResultWithBounds(t, fpr),
fprNoNext, err := ft.FingerprintInterval(x, y, limit)
require.NoError(t, err)
expFPR.next = nil
require.Equal(t, expFPR, toFPResultWithBounds(t, fprNoNext),
"x=%s y=%s limit=%d", x.String(), y.String(), limit)

return fpr
}

Expand Down Expand Up @@ -1154,8 +1164,6 @@ func verifyEasySplit(
}
a := firstKey(t, fpr.Items)
require.NoError(t, err)
b := fpr.Next
require.NotNil(t, b)

m := fpr.Count / 2
sr, err := ft.EasySplit(x, y, int(m))
Expand Down
Loading