Skip to content

Commit 219f3fc

Browse files
committed
tool: update compaction summary tool
1 parent ce94d05 commit 219f3fc

File tree

5 files changed

+360
-151
lines changed

5 files changed

+360
-151
lines changed

compaction.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ const (
154154
compactionKindRewrite
155155
compactionKindIngestedFlushable
156156
compactionKindBlobFileRewrite
157+
// compactionKindVirtualRewrite must be the last compactionKind.
158+
// If a new kind has to be added after VirtualRewrite,
159+
// update AllCompactionKindStrings() accordingly.
157160
compactionKindVirtualRewrite
158161
)
159162

@@ -196,6 +199,20 @@ func (k compactionKind) compactingOrFlushing() string {
196199
return "compacting"
197200
}
198201

202+
// AllCompactionKindStrings returns all compaction kind string representations
203+
// for testing purposes. Used by tool/logs/compaction_test.go to verify the
204+
// compaction summary tool stays in sync with new compaction types.
205+
//
206+
// NOTE: This function iterates up to compactionKindVirtualRewrite. If a new
207+
// compactionKind is added after VirtualRewrite, update this function accordingly.
208+
func AllCompactionKindStrings() map[string]bool {
209+
kinds := make(map[string]bool)
210+
for k := compactionKindDefault; k <= compactionKindVirtualRewrite; k++ {
211+
kinds[k.String()] = true
212+
}
213+
return kinds
214+
}
215+
199216
type compaction interface {
200217
AddInProgressLocked(*DB)
201218
BeganAt() time.Time

tool/logs/compaction.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ var (
4848
table.Div(),
4949
table.Int("virt", 4, table.AlignRight, func(r compactionTableRow) int { return r.Virtual }),
5050
table.Div(),
51+
table.Int("copy", 4, table.AlignRight, func(r compactionTableRow) int { return r.Copy }),
52+
table.Div(),
53+
table.Int("tomb", 4, table.AlignRight, func(r compactionTableRow) int { return r.Tombstone }),
54+
table.Div(),
55+
table.Int("rwrt", 4, table.AlignRight, func(r compactionTableRow) int { return r.Rewrite }),
56+
table.Div(),
5157
table.Int("cnt", 3, table.AlignRight, func(r compactionTableRow) int { return r.Count }),
5258
table.Div(),
5359
table.Bytes("in(B)", 5, table.AlignRight, func(r compactionTableRow) uint64 { return r.BytesIn }),
@@ -106,6 +112,9 @@ type compactionTableRow struct {
106112
Delete int
107113
Blob int
108114
Virtual int
115+
Copy int
116+
Tombstone int
117+
Rewrite int
109118
Count int
110119
BytesIn uint64
111120
BytesOut uint64
@@ -304,9 +313,12 @@ const (
304313
compactionTypeDefault compactionType = iota
305314
compactionTypeFlush
306315
compactionTypeMove
316+
compactionTypeCopy
307317
compactionTypeDeleteOnly
308318
compactionTypeElisionOnly
309319
compactionTypeRead
320+
compactionTypeTombstoneDensity
321+
compactionTypeRewrite
310322
compactionTypeBlobRewrite
311323
compactionTypeVirtualRewrite
312324
)
@@ -318,12 +330,18 @@ func (c compactionType) String() string {
318330
return "default"
319331
case compactionTypeMove:
320332
return "move"
333+
case compactionTypeCopy:
334+
return "copy"
321335
case compactionTypeDeleteOnly:
322336
return "delete-only"
323337
case compactionTypeElisionOnly:
324338
return "elision-only"
325339
case compactionTypeRead:
326340
return "read"
341+
case compactionTypeTombstoneDensity:
342+
return "tombstone-density"
343+
case compactionTypeRewrite:
344+
return "rewrite"
327345
case compactionTypeBlobRewrite:
328346
return "blob-rewrite"
329347
case compactionTypeVirtualRewrite:
@@ -341,12 +359,18 @@ func parseCompactionType(s string) (t compactionType, err error) {
341359
t = compactionTypeDefault
342360
case "move":
343361
t = compactionTypeMove
362+
case "copy":
363+
t = compactionTypeCopy
344364
case "delete-only":
345365
t = compactionTypeDeleteOnly
346366
case "elision-only":
347367
t = compactionTypeElisionOnly
348368
case "read":
349369
t = compactionTypeRead
370+
case "tombstone-density":
371+
t = compactionTypeTombstoneDensity
372+
case "rewrite":
373+
t = compactionTypeRewrite
350374
case "blob-rewrite":
351375
t = compactionTypeBlobRewrite
352376
case "virtual-sst-rewrite":
@@ -794,7 +818,7 @@ func (s windowSummary) String() string {
794818
// Print compactions statistics.
795819
if len(s.compactionCounts) > 0 {
796820
var compactionRows []compactionTableRow
797-
var totalDef, totalMove, totalElision, totalDel, totalBlob, totalVirtual int
821+
var totalDef, totalMove, totalElision, totalDel, totalBlob, totalVirtual, totalCopy, totalTombstone, totalRewrite int
798822
var totalBytesIn, totalBytesOut, totalBytesMoved, totalBytesDel uint64
799823
var totalTime time.Duration
800824

@@ -805,7 +829,10 @@ func (s windowSummary) String() string {
805829
del := p.counts[compactionTypeDeleteOnly]
806830
blob := p.counts[compactionTypeBlobRewrite]
807831
virtual := p.counts[compactionTypeVirtualRewrite]
808-
total := def + move + elision + del + blob + virtual
832+
copy := p.counts[compactionTypeCopy]
833+
tombstone := p.counts[compactionTypeTombstoneDensity]
834+
rewrite := p.counts[compactionTypeRewrite]
835+
total := def + move + elision + del + blob + virtual + copy + tombstone + rewrite
809836

810837
compactionRows = append(compactionRows, compactionTableRow{
811838
Kind: "compact",
@@ -817,6 +844,9 @@ func (s windowSummary) String() string {
817844
Delete: del,
818845
Blob: blob,
819846
Virtual: virtual,
847+
Copy: copy,
848+
Tombstone: tombstone,
849+
Rewrite: rewrite,
820850
Count: total,
821851
BytesIn: p.bytesIn,
822852
BytesOut: p.bytesOut,
@@ -831,6 +861,9 @@ func (s windowSummary) String() string {
831861
totalDel += del
832862
totalBlob += blob
833863
totalVirtual += virtual
864+
totalCopy += copy
865+
totalTombstone += tombstone
866+
totalRewrite += rewrite
834867
totalBytesIn += p.bytesIn
835868
totalBytesOut += p.bytesOut
836869
totalBytesMoved += p.bytesMoved
@@ -848,6 +881,9 @@ func (s windowSummary) String() string {
848881
Delete: totalDel,
849882
Blob: totalBlob,
850883
Virtual: totalVirtual,
884+
Copy: totalCopy,
885+
Tombstone: totalTombstone,
886+
Rewrite: totalRewrite,
851887
Count: s.eventCount,
852888
BytesIn: totalBytesIn,
853889
BytesOut: totalBytesOut,

tool/logs/compaction_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/cockroachdb/datadriven"
1818
"github.com/cockroachdb/errors"
19+
"github.com/cockroachdb/pebble"
1920
"github.com/stretchr/testify/require"
2021
)
2122

@@ -465,3 +466,66 @@ func TestParseInputBytes(t *testing.T) {
465466
})
466467
}
467468
}
469+
470+
// TestCompactionKindToolSupport verifies that the tool's compactionType enum
471+
// stays in sync with the main compactionKind enum in compaction.go.
472+
// This test will fail if a new compaction type is added to compaction.go
473+
// but not to this tool, preventing the tool from becoming out of date.
474+
func TestCompactionKindToolSupport(t *testing.T) {
475+
// Get all compaction kinds from compaction.go via the helper function
476+
allKinds := pebble.AllCompactionKindStrings()
477+
478+
// These kinds use flush logging format, not compaction logging format.
479+
// They are handled separately and should NOT be in the compactionType enum.
480+
flushLoggedKinds := map[string]string{
481+
"flush": "logged via FlushInfo, not CompactionInfo",
482+
"ingested-flushable": "logged as 'flushed N ingested flushables' via FlushInfo (see parseIngestDuringFlush)",
483+
}
484+
485+
var missingTypes []string
486+
handledCount := 0
487+
488+
// Verify each non-flush kind is supported by the tool
489+
for kindStr := range allKinds {
490+
if reason, excluded := flushLoggedKinds[kindStr]; excluded {
491+
t.Logf("Skipping %q: %s", kindStr, reason)
492+
continue
493+
}
494+
495+
// Special case: blob-file-rewrite logs as "blob-rewrite" in practice
496+
testStr := kindStr
497+
if kindStr == "blob-file-rewrite" {
498+
testStr = "blob-rewrite"
499+
}
500+
501+
_, err := parseCompactionType(testStr)
502+
if err != nil {
503+
missingTypes = append(missingTypes, kindStr)
504+
t.Errorf("SYNC ERROR: compaction kind %q from compaction.go is not supported by tool/logs/compaction.go", kindStr)
505+
} else {
506+
handledCount++
507+
}
508+
}
509+
510+
if len(missingTypes) > 0 {
511+
t.Errorf("\n"+
512+
"═══════════════════════════════════════════════════════════════════════════\n"+
513+
" COMPACTION TOOL OUT OF SYNC\n"+
514+
"═══════════════════════════════════════════════════════════════════════════\n"+
515+
"\n"+
516+
"A new compaction type was added to compaction.go but the compaction summary\n"+
517+
"tool (tool/logs/compaction.go) was not updated.\n"+
518+
"\n"+
519+
"Missing types: %v\n"+
520+
"═══════════════════════════════════════════════════════════════════════════\n",
521+
missingTypes)
522+
}
523+
524+
// Verify we're handling the expected number of types
525+
expectedHandled := len(allKinds) - len(flushLoggedKinds)
526+
if handledCount != expectedHandled {
527+
t.Errorf("Expected to handle %d compaction types but only handle %d", expectedHandled, handledCount)
528+
} else {
529+
t.Logf("Successfully verified %d compaction types are supported", handledCount)
530+
}
531+
}

0 commit comments

Comments
 (0)