Skip to content

Commit 4a3288f

Browse files
craig[bot]pav-kvmgartner
committed
142979: kvserver: update raft log stats with trunc state r=tbg a=pav-kv This PR iterates on the raft log truncation code, and makes it more consolidated. It also fixes one bug: the log size update is now done in the same `Replica.mu` critical section with the `RaftTruncatedState` update. This is achieved by moving the truncated files size computation from post-apply to pre-apply stage. The latter change bears no performance implications because the pre-apply stage already reads from FS when determining whether a truncation affects any sideloaded entries. Epic: none Release note: none 143087: go.mod: update hyperloglog to v0.2.5 r=mgartner a=mgartner The hyperloglog library has been upgraded from v0.2.0 to v0.2.5. See the commits in this upgrade here: axiomhq/hyperloglog@v0.2.0...v0.2.5 Fixes #140772 Release note: None Co-authored-by: Pavel Kalinnikov <[email protected]> Co-authored-by: Marcus Gartner <[email protected]>
3 parents 6b825e3 + 76a9b19 + 2d300f0 commit 4a3288f

14 files changed

+190
-198
lines changed

DEPS.bzl

+13-3
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,10 @@ def go_deps():
787787
name = "com_github_axiomhq_hyperloglog",
788788
build_file_proto_mode = "disable_global",
789789
importpath = "github.com/axiomhq/hyperloglog",
790-
sha256 = "fdbbf370d3ddb6b5463a88678b9e2d78d31dff46f11e8cbb9a9d58ba8af1af2f",
791-
strip_prefix = "github.com/axiomhq/[email protected].0",
790+
sha256 = "6125b12664bb5dd8614e82f0fe7528242dcb11649e1d7e051aabf3da471e14e1",
791+
strip_prefix = "github.com/axiomhq/[email protected].5",
792792
urls = [
793-
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/axiomhq/hyperloglog/com_github_axiomhq_hyperloglog-v0.2.0.zip",
793+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/axiomhq/hyperloglog/com_github_axiomhq_hyperloglog-v0.2.5.zip",
794794
],
795795
)
796796
go_repository(
@@ -5631,6 +5631,16 @@ def go_deps():
56315631
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/k0kubun/colorstring/com_github_k0kubun_colorstring-v0.0.0-20150214042306-9440f1994b88.zip",
56325632
],
56335633
)
5634+
go_repository(
5635+
name = "com_github_kamstrup_intmap",
5636+
build_file_proto_mode = "disable_global",
5637+
importpath = "github.com/kamstrup/intmap",
5638+
sha256 = "3cc752347b8e5ce47c148e9f81f31999942e4a2fae4c63f88d0bab4df6f1ec58",
5639+
strip_prefix = "github.com/kamstrup/[email protected]",
5640+
urls = [
5641+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/kamstrup/intmap/com_github_kamstrup_intmap-v0.5.1.zip",
5642+
],
5643+
)
56345644
go_repository(
56355645
name = "com_github_karrick_godirwalk",
56365646
build_file_proto_mode = "disable_global",

build/bazelutil/distdir_files.bzl

+2-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ DISTDIR_FILES = {
283283
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/aws/aws-sdk-go/com_github_aws_aws_sdk_go-v1.40.37.zip": "c0c481d28af88f621fb3fdeacc1e5d32f69a1bb83d0ee959f95ce89e4e2d0494",
284284
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/aws/smithy-go/com_github_aws_smithy_go-v1.22.3.zip": "572df48de9133d57f45909d3067b2053b97230268c2d28e4e44ea9644009ef11",
285285
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/axiomhq/hyperloglog/com_github_axiomhq_hyperloglog-v0.0.0-20181223111420-4b99d0c2c99e.zip": "812834322ee2ca50dc36f91f9ac3f2cde4631af2f9c330b1271c78b46024a540",
286-
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/axiomhq/hyperloglog/com_github_axiomhq_hyperloglog-v0.2.0.zip": "fdbbf370d3ddb6b5463a88678b9e2d78d31dff46f11e8cbb9a9d58ba8af1af2f",
286+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/axiomhq/hyperloglog/com_github_axiomhq_hyperloglog-v0.2.5.zip": "6125b12664bb5dd8614e82f0fe7528242dcb11649e1d7e051aabf3da471e14e1",
287287
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/aymanbagabas/go-osc52/com_github_aymanbagabas_go_osc52-v1.0.3.zip": "138e75a51599c2a8e4afe2bd6acdeaddbb73eb9ec796dfa2f577b16201660d9e",
288288
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/aymerick/douceur/com_github_aymerick_douceur-v0.2.0.zip": "dcbf69760cc1a8b32384495438e1086e4c3d669b2ebc0debd92e1865ffd6be60",
289289
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/aymerick/raymond/com_github_aymerick_raymond-v2.0.3-0.20180322193309-b565731e1464+incompatible.zip": "0a759716a73b587a436b3b4a95416a58bb1ffa1decf2cd7a92f1eeb2f9c654c1",
@@ -721,6 +721,7 @@ DISTDIR_FILES = {
721721
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/justinas/alice/com_github_justinas_alice-v1.2.0.zip": "b2d65d6a613d0fe33b4595b69855ab9d55bcfeee506a19d07d4585c566fe6587",
722722
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jwilder/encoding/com_github_jwilder_encoding-v0.0.0-20170811194829-b4e1701a28ef.zip": "91ab650780db18684a70137cbb34189c171c29a23aab48816c8bca74dbb012e9",
723723
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/k0kubun/colorstring/com_github_k0kubun_colorstring-v0.0.0-20150214042306-9440f1994b88.zip": "32a2eac0ffb69c6882b32ccfcdd76968cb9dfee9d9dc3d469fc405775399167c",
724+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/kamstrup/intmap/com_github_kamstrup_intmap-v0.5.1.zip": "3cc752347b8e5ce47c148e9f81f31999942e4a2fae4c63f88d0bab4df6f1ec58",
724725
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/karrick/godirwalk/com_github_karrick_godirwalk-v1.10.3.zip": "80518abce2eb573be7f1c529024f9a04cae142cd44995c59ccbffde40a5563d4",
725726
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/kataras/blocks/com_github_kataras_blocks-v0.0.7.zip": "bdc3d49ea54a2a2ef733ae701986be69fba7d735ae876ea736806e4f3ef00a8b",
726727
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/kataras/golog/com_github_kataras_golog-v0.1.8.zip": "3260bda850670a630c5a99d6501287096856efaf0126ab8f7a096b3b74f78b1c",

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ require (
117117
github.com/aws/aws-sdk-go-v2/service/s3 v1.78.1
118118
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.35.1
119119
github.com/aws/smithy-go v1.22.3
120-
github.com/axiomhq/hyperloglog v0.2.0
120+
github.com/axiomhq/hyperloglog v0.2.5
121121
github.com/axiomhq/hyperloglog/000 v0.0.0-20181223111420-4b99d0c2c99e
122122
github.com/bazelbuild/rules_go v0.26.0
123123
github.com/biogo/store v0.0.0-20160505134755-913427a1d5e8
@@ -378,6 +378,7 @@ require (
378378
github.com/jmespath/go-jmespath v0.4.0 // indirect
379379
github.com/josharian/intern v1.0.0 // indirect
380380
github.com/json-iterator/go v1.1.12 // indirect
381+
github.com/kamstrup/intmap v0.5.1 // indirect
381382
github.com/klauspost/asmfmt v1.3.2 // indirect
382383
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
383384
github.com/lestrrat-go/blackmagic v1.0.2 // indirect

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ github.com/aws/smithy-go v1.22.3 h1:Z//5NuZCSW6R4PhQ93hShNbyBbn8BWCmCVCt+Q8Io5k=
443443
github.com/aws/smithy-go v1.22.3/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI=
444444
github.com/axiomhq/hyperloglog v0.0.0-20181223111420-4b99d0c2c99e h1:190ugM9MsyFauTkR/UqcHG/mn5nmFe6SvHJqEHIrtrA=
445445
github.com/axiomhq/hyperloglog v0.0.0-20181223111420-4b99d0c2c99e/go.mod h1:IOXAcuKIFq/mDyuQ4wyJuJ79XLMsmLM+5RdQ+vWrL7o=
446-
github.com/axiomhq/hyperloglog v0.2.0 h1:u1XT3yyY1rjzlWuP6NQIrV4bRYHOaqZaovqjcBEvZJo=
447-
github.com/axiomhq/hyperloglog v0.2.0/go.mod h1:GcgMjz9gaDKZ3G0UMS6Fq/VkZ4l7uGgcJyxA7M+omIM=
446+
github.com/axiomhq/hyperloglog v0.2.5 h1:Hefy3i8nAs8zAI/tDp+wE7N+Ltr8JnwiW3875pvl0N8=
447+
github.com/axiomhq/hyperloglog v0.2.5/go.mod h1:DLUK9yIzpU5B6YFLjxTIcbHu1g4Y1WQb1m5RH3radaM=
448448
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
449449
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
450450
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
@@ -1605,6 +1605,8 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+
16051605
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
16061606
github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0=
16071607
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
1608+
github.com/kamstrup/intmap v0.5.1 h1:ENGAowczZA+PJPYYlreoqJvWgQVtAmX1l899WfYFVK0=
1609+
github.com/kamstrup/intmap v0.5.1/go.mod h1:gWUVWHKzWj8xpJVFf5GC0O26bWmv3GqdnIX/LMT6Aq4=
16081610
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
16091611
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
16101612
github.com/kataras/golog v0.0.10/go.mod h1:yJ8YKCmyL+nWjERB90Qwn+bdyBZsaQwU3bTVFgkFIp8=

pkg/kv/kvserver/logstore/logstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func (s *LogStore) ComputeSize(ctx context.Context) (int64, error) {
591591
if err != nil {
592592
return 0, err
593593
}
594-
totalSideloaded, err := s.Sideload.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
594+
_, totalSideloaded, err := s.Sideload.Stats(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
595595
if err != nil {
596596
return 0, err
597597
}

pkg/kv/kvserver/logstore/sideload.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,11 @@ type SideloadStorage interface {
4444
Purge(_ context.Context, index kvpb.RaftIndex, term kvpb.RaftTerm) (int64, error)
4545
// Clear files that may have been written by this SideloadStorage.
4646
Clear(context.Context) error
47-
// HasAnyEntry returns whether there is any entry in the given log span.
48-
HasAnyEntry(_ context.Context, _ kvpb.RaftSpan) (bool, error)
4947
// TruncateTo removes all files belonging to an index <= the given index.
50-
// Returns the number of bytes freed, or an error.
51-
TruncateTo(_ context.Context, index kvpb.RaftIndex) (freed int64, _ error)
52-
// BytesIfTruncatedFromTo returns the number of bytes that would be freed, if
53-
// one were to truncate the given (from, to] log span.
54-
// TODO(pav-kv): merge this with HasAnyEntry into a single "stats" call. The
55-
// truncation path needs both anyway.
56-
BytesIfTruncatedFromTo(_ context.Context, _ kvpb.RaftSpan) (freed int64, _ error)
48+
TruncateTo(_ context.Context, index kvpb.RaftIndex) error
49+
// Stats returns the number and the total size of the sideloaded entries in
50+
// the given log span.
51+
Stats(_ context.Context, _ kvpb.RaftSpan) (entries uint64, size int64, _ error)
5752
// Returns an absolute path to the file that Get() would return the contents
5853
// of. Does not check whether the file actually exists.
5954
Filename(_ context.Context, index kvpb.RaftIndex, term kvpb.RaftTerm) (string, error)

pkg/kv/kvserver/logstore/sideload_disk.go

+22-40
Original file line numberDiff line numberDiff line change
@@ -185,42 +185,26 @@ func (ss *DiskSideloadStorage) Clear(_ context.Context) error {
185185
}
186186

187187
// TruncateTo implements SideloadStorage.
188-
func (ss *DiskSideloadStorage) TruncateTo(
189-
ctx context.Context, lastIndex kvpb.RaftIndex,
190-
) (bytesFreed int64, _ error) {
191-
return ss.possiblyTruncateTo(ctx, kvpb.RaftSpan{
192-
After: 0, Last: lastIndex,
193-
}, true /* doTruncate */)
194-
}
195-
196-
// Helper for truncation or byte calculation for (from, to].
197-
func (ss *DiskSideloadStorage) possiblyTruncateTo(
198-
ctx context.Context, span kvpb.RaftSpan, doTruncate bool,
199-
) (bytesFreed int64, _ error) {
188+
func (ss *DiskSideloadStorage) TruncateTo(ctx context.Context, lastIndex kvpb.RaftIndex) error {
189+
span := kvpb.RaftSpan{Last: lastIndex}
200190
deletedAll := true
201191
if err := ss.forEach(ctx, func(index kvpb.RaftIndex, filename string) (bool, error) {
202192
if !span.Contains(index) {
203193
deletedAll = false
204194
return true, nil
205195
}
206196
// index is in (span.After, span.Last].
207-
var fileSize int64
208-
var err error
209-
if doTruncate {
210-
fileSize, err = ss.purgeFile(ctx, filename)
211-
} else {
212-
fileSize, err = ss.fileSize(filename)
213-
}
197+
// TODO(pav-kv): don't compute the size in purgeFile.
198+
_, err := ss.purgeFile(ctx, filename)
214199
if err != nil {
215200
return false, err
216201
}
217-
bytesFreed += fileSize
218202
return true, nil
219203
}); err != nil {
220-
return 0, err
204+
return err
221205
}
222206

223-
if deletedAll && doTruncate {
207+
if deletedAll {
224208
// The directory may not exist, or it may exist and have been empty.
225209
// Not worth trying to figure out which one, just try to delete.
226210
err := ss.eng.Env().Remove(ss.dir)
@@ -231,30 +215,28 @@ func (ss *DiskSideloadStorage) possiblyTruncateTo(
231215
err = nil // handled
232216
}
233217
}
234-
return bytesFreed, nil
218+
return nil
235219
}
236220

237-
// HasAnyEntry implements SideloadStorage.
238-
func (ss *DiskSideloadStorage) HasAnyEntry(ctx context.Context, span kvpb.RaftSpan) (bool, error) {
239-
// Find any file at index in (from, to].
240-
found := false
241-
if err := ss.forEach(ctx, func(index kvpb.RaftIndex, _ string) (bool, error) {
242-
if span.Contains(index) {
243-
found = true
244-
return false, nil // stop the iteration
221+
// Stats implements SideloadStorage.
222+
func (ss *DiskSideloadStorage) Stats(
223+
ctx context.Context, span kvpb.RaftSpan,
224+
) (entries uint64, size int64, _ error) {
225+
if err := ss.forEach(ctx, func(index kvpb.RaftIndex, filename string) (bool, error) {
226+
if !span.Contains(index) {
227+
return true, nil // skip
228+
}
229+
entries++
230+
fileSize, err := ss.fileSize(filename)
231+
if err != nil {
232+
return false, err
245233
}
234+
size += fileSize
246235
return true, nil
247236
}); err != nil {
248-
return false, err
237+
return 0, 0, err
249238
}
250-
return found, nil
251-
}
252-
253-
// BytesIfTruncatedFromTo implements SideloadStorage.
254-
func (ss *DiskSideloadStorage) BytesIfTruncatedFromTo(
255-
ctx context.Context, span kvpb.RaftSpan,
256-
) (freed int64, _ error) {
257-
return ss.possiblyTruncateTo(ctx, span, false /* doTruncate */)
239+
return entries, size, nil
258240
}
259241

260242
// forEach runs the given visit function for each file in the sideloaded storage

pkg/kv/kvserver/logstore/sideload_test.go

+41-40
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
166166
},
167167
{
168168
err: nil,
169-
fun: func() error {
170-
_, err := ss.TruncateTo(ctx, 122)
171-
return err
172-
},
169+
fun: func() error { return ss.TruncateTo(ctx, 122) },
173170
},
174171
{
175172
err: nil,
@@ -221,19 +218,17 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
221218

222219
for n := range payloads {
223220
index := payloads[n] // (0, index] + (index, ...]
224-
total, err := ss.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
221+
_, total, err := ss.Stats(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
225222
require.NoError(t, err)
226-
prefixBytes, err := ss.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{Last: index})
223+
_, prefixBytes, err := ss.Stats(ctx, kvpb.RaftSpan{Last: index})
227224
require.NoError(t, err)
228-
suffixBytes, err := ss.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{
225+
_, suffixBytes, err := ss.Stats(ctx, kvpb.RaftSpan{
229226
After: index, Last: math.MaxUint64,
230227
})
231228
require.NoError(t, err)
232229
require.Equal(t, total, prefixBytes+suffixBytes)
233230
// Truncate indexes <= payloads[n] (payloads is sorted in increasing order).
234-
freed, err := ss.TruncateTo(ctx, index)
235-
require.NoError(t, err)
236-
require.Equal(t, prefixBytes, freed)
231+
require.NoError(t, ss.TruncateTo(ctx, index))
237232
// Indexes > payloads[n] are still there at both terms.
238233
for _, term := range []kvpb.RaftTerm{lowTerm, highTerm} {
239234
for _, i := range payloads[n+1:] {
@@ -260,56 +255,64 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
260255
// we will be prevented from removing it below.
261256
require.NoError(t, f.Close())
262257

263-
_, err = ss.TruncateTo(ctx, math.MaxUint64)
258+
require.NoError(t, ss.TruncateTo(ctx, math.MaxUint64))
264259
// The sideloaded storage should not error out here; removing files
265260
// is optional. But the file should still be there!
266-
require.NoError(t, err)
267261
_, err = eng.Env().Stat(nonRemovableFile)
268262
require.NoError(t, err)
269263

270264
// Now remove extra file and let truncation proceed to remove directory.
271265
require.NoError(t, eng.Env().Remove(nonRemovableFile))
272266

273267
// Test that directory is removed when filepath.Glob returns 0 matches.
274-
_, err = ss.TruncateTo(ctx, math.MaxUint64)
275-
require.NoError(t, err)
268+
require.NoError(t, ss.TruncateTo(ctx, math.MaxUint64))
276269
// Ensure directory is removed, now that all files should be gone.
277270
_, err = eng.Env().Stat(ss.Dir())
278271
require.True(t, oserror.IsNotExist(err), "%v", err)
279-
// Ensure HasAnyEntry doesn't find anything.
280-
found, err := ss.HasAnyEntry(ctx, kvpb.RaftSpan{Last: 10000})
272+
// Ensure that Stats don't find anything.
273+
count, size, err := ss.Stats(ctx, kvpb.RaftSpan{Last: 10000})
281274
require.NoError(t, err)
282-
require.False(t, found)
275+
require.Zero(t, count)
276+
require.Zero(t, size)
283277

284278
// Repopulate with some random indexes to test deletion when there are a
285279
// non-zero number of filepath.Glob matches.
286280
payloads := []kvpb.RaftIndex{3, 5, 7, 9, 10}
281+
sizes := make([]int64, len(payloads)+1)
287282
for n := range rand.Perm(len(payloads)) {
288283
i := payloads[n]
289-
require.NoError(t, ss.Put(ctx, i, highTerm, file(i, highTerm)))
284+
content := file(i, highTerm)
285+
sizes[n+1] = sizes[n] + int64(len(content))
286+
require.NoError(t, ss.Put(ctx, i, highTerm, content))
287+
}
288+
spanSize := func(from, to int) int64 {
289+
return sizes[to] - sizes[from]
290290
}
291291
assertExists(true)
292-
// Verify the HasAnyEntry semantics.
292+
// Verify the Stats computation.
293293
for _, check := range []struct {
294-
from, to kvpb.RaftIndex
295-
want bool
294+
from, to kvpb.RaftIndex
295+
wantCount uint64
296+
wantSize int64
296297
}{
297-
{from: 0, to: 2, want: false}, // 2 is included
298-
{from: 0, to: 3, want: true}, // but included if to == 3
299-
{from: 2, to: 4, want: true}, // 2 is excluded
300-
{from: 3, to: 4, want: false},
301-
{from: 49, to: 59, want: false},
302-
{from: 0, to: 9, want: true},
298+
// Not found cases.
299+
{from: 0, to: 2}, // 2 is included
300+
{from: 3, to: 4},
301+
{from: 49, to: 59},
302+
// Found cases.
303+
{from: 0, to: 3, wantCount: 1, wantSize: spanSize(0, 1)}, // 3 is included
304+
{from: 2, to: 4, wantCount: 1, wantSize: spanSize(0, 1)}, // 2 is excluded
305+
{from: 0, to: 9, wantCount: 4, wantSize: spanSize(0, 4)},
306+
{from: 0, to: 10, wantCount: 5, wantSize: spanSize(0, 5)},
307+
{from: 0, to: 100, wantCount: 5, wantSize: spanSize(0, 5)},
308+
{from: 3, to: 10, wantCount: 4, wantSize: spanSize(1, 5)},
303309
} {
304-
found, err := ss.HasAnyEntry(ctx, kvpb.RaftSpan{After: check.from, Last: check.to})
310+
count, size, err := ss.Stats(ctx, kvpb.RaftSpan{After: check.from, Last: check.to})
305311
require.NoError(t, err)
306-
require.Equal(t, check.want, found)
312+
require.Equal(t, check.wantCount, count)
313+
require.Equal(t, check.wantSize, size)
307314
}
308-
prefixBytes, err := ss.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
309-
require.NoError(t, err)
310-
freed, err := ss.TruncateTo(ctx, math.MaxUint64)
311-
require.NoError(t, err)
312-
require.Equal(t, prefixBytes, freed)
315+
require.NoError(t, ss.TruncateTo(ctx, math.MaxUint64))
313316
// Ensure directory is removed when all records are removed.
314317
_, err = eng.Env().Stat(ss.Dir())
315318
require.True(t, oserror.IsNotExist(err), "%v", err)
@@ -319,14 +322,12 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
319322

320323
assertExists(false)
321324

322-
// Sanity check that we can call BytesIfTruncatedFromTo and TruncateTo
323-
// without the directory existing.
324-
size, err := ss.BytesIfTruncatedFromTo(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
325+
// Sanity check that Stats returns zeroes when the directory does not exist.
326+
count, size, err := ss.Stats(ctx, kvpb.RaftSpan{Last: math.MaxUint64})
325327
require.NoError(t, err)
328+
require.Zero(t, count)
326329
require.Zero(t, size)
327-
freed, err := ss.TruncateTo(ctx, 0)
328-
require.NoError(t, err)
329-
require.Zero(t, freed)
330+
require.NoError(t, ss.TruncateTo(ctx, 0))
330331

331332
assertExists(false)
332333

pkg/kv/kvserver/raft_log_truncator.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,7 @@ type replicaForTruncator interface {
241241
// Returns the current truncated state.
242242
getTruncatedState() kvserverpb.RaftTruncatedState
243243
// Updates the replica state after the truncation is enacted.
244-
setTruncatedStateAndSideEffects(
245-
_ context.Context, _ *kvserverpb.RaftTruncatedState, expectedFirstIndexPreTruncation kvpb.RaftIndex,
246-
) (expectedFirstIndexWasAccurate bool)
247-
// Updates the stats related to the raft log size after the truncation is
248-
// enacted.
249-
setTruncationDeltaAndTrusted(deltaBytes int64, isDeltaTrusted bool)
244+
handleTruncationResult(_ context.Context, _ pendingTruncation)
250245
// Returns the pending truncations queue. The caller is allowed to mutate
251246
// the return value by additionally acquiring pendingLogTruncations.mu.
252247
getPendingTruncs() *pendingLogTruncations
@@ -579,13 +574,7 @@ func (t *raftLogTruncator) tryEnactTruncations(
579574
if index > enactIndex {
580575
return
581576
}
582-
isDeltaTrusted := true
583-
expectedFirstIndexWasAccurate := r.setTruncatedStateAndSideEffects(
584-
ctx, &trunc.RaftTruncatedState, trunc.expectedFirstIndex)
585-
if !expectedFirstIndexWasAccurate || !trunc.isDeltaTrusted {
586-
isDeltaTrusted = false
587-
}
588-
r.setTruncationDeltaAndTrusted(trunc.logDeltaBytes, isDeltaTrusted)
577+
r.handleTruncationResult(ctx, trunc)
589578
})
590579
// Now remove the enacted truncations. It is the same iteration as the
591580
// previous one, but we do it while holding pendingTruncs.mu. Note that

0 commit comments

Comments
 (0)