Skip to content

Commit e5ebbdf

Browse files
committed
tool: fix db scan crash, add tests
`db scan` crashes when the database uses columnar blocks because it incorrectly does a `SeekGE(nil)`. This commit fixes this and adds some fixtures and tests with CRDB schema.
1 parent c5a2e25 commit e5ebbdf

16 files changed

+268
-14
lines changed

tool/data_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/datadriven"
1717
"github.com/cockroachdb/errors"
1818
"github.com/cockroachdb/pebble"
19+
"github.com/cockroachdb/pebble/cockroachkvs"
1920
"github.com/cockroachdb/pebble/internal/base"
2021
"github.com/cockroachdb/pebble/internal/testkeys"
2122
"github.com/cockroachdb/pebble/vfs"
@@ -132,10 +133,12 @@ func runTests(t *testing.T, path string) {
132133

133134
tool := New(
134135
DefaultComparer(comparer),
135-
Comparers(altComparer, testkeys.Comparer),
136+
Comparers(altComparer, testkeys.Comparer, &cockroachkvs.Comparer),
136137
Mergers(merger),
137138
FS(fs),
138139
OpenErrEnhancer(openErrEnhancer),
140+
KeySchema(cockroachkvs.KeySchema.Name),
141+
KeySchemas(&cockroachkvs.KeySchema),
139142
)
140143

141144
c := &cobra.Command{}

tool/db.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,13 @@ func (d *dbT) runScan(cmd *cobra.Command, args []string) {
527527
iter, _ := db.NewIter(&pebble.IterOptions{
528528
UpperBound: d.end,
529529
})
530-
for valid := iter.SeekGE(d.start); valid; valid = iter.Next() {
530+
var valid bool
531+
if len(d.start) == 0 {
532+
valid = iter.First()
533+
} else {
534+
valid = iter.SeekGE(d.start)
535+
}
536+
for ; valid; valid = iter.Next() {
531537
if fmtKeys || fmtValues {
532538
needDelimiter := false
533539
if fmtKeys {

tool/make_test_sstables.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@ package main
99

1010
import (
1111
"log"
12+
"math/rand/v2"
1213

13-
"github.com/cockroachdb/pebble/internal/private"
14+
"github.com/cockroachdb/pebble/cockroachkvs"
15+
"github.com/cockroachdb/pebble/internal/sstableinternal"
1416
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
1517
"github.com/cockroachdb/pebble/sstable"
1618
"github.com/cockroachdb/pebble/vfs"
1719
)
1820

19-
func makeOutOfOrder() {
21+
func makeOutOfOrderSST() {
2022
fs := vfs.Default
21-
f, err := fs.Create("tool/testdata/out-of-order.sst")
23+
f, err := fs.Create("tool/testdata/out-of-order.sst", vfs.WriteCategoryUnspecified)
2224
if err != nil {
2325
log.Fatal(err)
2426
}
2527
opts := sstable.WriterOptions{
2628
TableFormat: sstable.TableFormatPebblev1,
2729
}
30+
opts.SetInternal(sstableinternal.WriterOptions{
31+
DisableKeyOrderChecks: true,
32+
})
2833
w := sstable.NewWriter(objstorageprovider.NewFileWritable(f), opts)
29-
private.SSTableWriterDisableKeyOrderChecks(w)
3034

3135
set := func(key string) {
3236
if err := w.Set([]byte(key), nil); err != nil {
@@ -43,6 +47,48 @@ func makeOutOfOrder() {
4347
}
4448
}
4549

50+
func makeCockroachSchemaSST() {
51+
fs := vfs.Default
52+
f, err := fs.Create("tool/testdata/cr-schema.sst", vfs.WriteCategoryUnspecified)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
opts := sstable.WriterOptions{
57+
TableFormat: sstable.TableFormatMax,
58+
Comparer: &cockroachkvs.Comparer,
59+
KeySchema: &cockroachkvs.KeySchema,
60+
BlockSize: 32,
61+
}
62+
w := sstable.NewWriter(objstorageprovider.NewFileWritable(f), opts)
63+
64+
// 2025/04/03 15:38:23 background error: pebble: keys must be added in strictly increasing order:
65+
// tcestilr\x00\x00\x00\x00\x1a5\xaddx\x09#20,SET
66+
// tcestilr\x00\x00\x00\x01\xc8\xc0@\xe4%\x09#19,SET
67+
68+
cfg := cockroachkvs.KeyGenConfig{
69+
PrefixAlphabetLen: 20,
70+
PrefixLenShared: 2,
71+
RoachKeyLen: 8,
72+
AvgKeysPerPrefix: 2,
73+
BaseWallTime: 1,
74+
PercentLogical: 10,
75+
PercentEmptySuffix: 5,
76+
PercentLockSuffix: 5,
77+
}
78+
rng := rand.New(rand.NewPCG(1, 1))
79+
keys, vals := cockroachkvs.RandomKVs(rng, 20, cfg, 16)
80+
81+
for i := range keys {
82+
if err := w.Set(keys[i], vals[i]); err != nil {
83+
log.Fatal(err)
84+
}
85+
}
86+
if err := w.Close(); err != nil {
87+
log.Fatal(err)
88+
}
89+
}
90+
4691
func main() {
47-
makeOutOfOrder()
92+
makeOutOfOrderSST()
93+
makeCockroachSchemaSST()
4894
}
2.94 KB
Binary file not shown.
11 Bytes
Binary file not shown.
2.51 KB
Binary file not shown.

tool/testdata/cr-schema-db/LOCK

Whitespace-only changes.
198 Bytes
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[Version]
2+
pebble_version=0.1
3+
4+
[Options]
5+
bytes_per_sync=524288
6+
cache_size=8388608
7+
cleaner=delete
8+
compaction_debt_concurrency=1073741824
9+
comparer=cockroach_comparator
10+
disable_wal=false
11+
flush_delay_delete_range=0s
12+
flush_delay_range_key=0s
13+
flush_split_bytes=4194304
14+
format_major_version=20
15+
key_schema=crdb1
16+
l0_compaction_concurrency=10
17+
l0_compaction_file_threshold=500
18+
l0_compaction_threshold=4
19+
l0_stop_writes_threshold=12
20+
lbase_max_bytes=67108864
21+
max_concurrent_compactions=1
22+
max_concurrent_downloads=1
23+
max_manifest_file_size=134217728
24+
max_open_files=1000
25+
mem_table_size=4194304
26+
mem_table_stop_writes_threshold=2
27+
min_deletion_rate=0
28+
free_space_threshold_bytes=17179869184
29+
free_space_timeframe=10s
30+
obsolete_bytes_max_ratio=0.200000
31+
obsolete_bytes_timeframe=5m0s
32+
merger=pebble.concatenate
33+
multilevel_compaction_heuristic=wamp(0.00, false)
34+
read_compaction_rate=16000
35+
read_sampling_multiplier=16
36+
num_deletions_threshold=100
37+
deletion_size_ratio_threshold=0.500000
38+
tombstone_dense_compaction_threshold=0.100000
39+
strict_wal_tail=true
40+
table_cache_shards=10
41+
validate_on_ingest=false
42+
wal_dir=
43+
wal_bytes_per_sync=0
44+
secondary_cache_size_bytes=0
45+
create_on_shared=0
46+
47+
[Level "0"]
48+
block_restart_interval=16
49+
block_size=4096
50+
block_size_threshold=90
51+
compression=Snappy
52+
filter_policy=none
53+
filter_type=table
54+
index_block_size=4096
55+
target_file_size=2097152

tool/testdata/cr-schema-db/marker.format-version.000007.020

Whitespace-only changes.

0 commit comments

Comments
 (0)