|
| 1 | +package rocksdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "iter" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/linxGnu/grocksdb" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// pairs yields key/value pairs given as alternating strings. |
| 18 | +func pairs(kv ...string) iter.Seq2[[]byte, []byte] { |
| 19 | + return func(yield func(key, value []byte) bool) { |
| 20 | + for i := 0; i < len(kv); i += 2 { |
| 21 | + if !yield([]byte(kv[i]), []byte(kv[i+1])) { |
| 22 | + return |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// numbered yields n ascending keys, each with value. |
| 29 | +func numbered(n int, value []byte) iter.Seq2[[]byte, []byte] { |
| 30 | + return func(yield func(key, value []byte) bool) { |
| 31 | + for i := range n { |
| 32 | + if !yield(fmt.Appendf(nil, "key-%08d", i), value) { |
| 33 | + return |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func requireValue(t *testing.T, s *Store, cf, key, want string) { |
| 40 | + t.Helper() |
| 41 | + got, found, err := s.Get(cf, []byte(key)) |
| 42 | + require.NoError(t, err) |
| 43 | + require.True(t, found, "key %q", key) |
| 44 | + require.Equal(t, want, string(got)) |
| 45 | +} |
| 46 | + |
| 47 | +func loadFiles(t *testing.T, s *Store) []string { |
| 48 | + t.Helper() |
| 49 | + files, err := filepath.Glob(filepath.Join(s.cfg.Path, loadDirName, "*")) |
| 50 | + require.NoError(t, err) |
| 51 | + return files |
| 52 | +} |
| 53 | + |
| 54 | +func uintProperty(t *testing.T, s *Store, cf, name string) uint64 { |
| 55 | + t.Helper() |
| 56 | + n, err := strconv.ParseUint(s.db.GetPropertyCF(name, s.cfHandles[cf]), 10, 64) |
| 57 | + require.NoError(t, err) |
| 58 | + return n |
| 59 | +} |
| 60 | + |
| 61 | +func TestStore_LoadSorted_RoundTrip(t *testing.T) { |
| 62 | + const loaded, other = "loaded", "other" |
| 63 | + cfg := Config{Path: t.TempDir(), ColumnFamilies: []string{loaded, other}, Logger: silentLogger()} |
| 64 | + s, err := New(cfg) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + require.NoError(t, s.LoadSorted(loaded, pairs("a1", "one", "a2", "two"))) |
| 68 | + // A batch to another CF sits in the memtable while a second, disjoint |
| 69 | + // file is loaded. |
| 70 | + require.NoError(t, s.Batch(func(b *BatchWriter) error { |
| 71 | + b.Put(other, []byte("k"), []byte("batched")) |
| 72 | + return nil |
| 73 | + })) |
| 74 | + require.NoError(t, s.LoadSorted(loaded, pairs("b1", "three"))) |
| 75 | + require.Error(t, s.LoadSorted(loaded, pairs("a2", "x")), "a key a loaded file holds") |
| 76 | + require.Empty(t, loadFiles(t, s)) |
| 77 | + |
| 78 | + check := func(s *Store) { |
| 79 | + requireValue(t, s, loaded, "a1", "one") |
| 80 | + requireValue(t, s, loaded, "a2", "two") |
| 81 | + requireValue(t, s, loaded, "b1", "three") |
| 82 | + requireValue(t, s, other, "k", "batched") |
| 83 | + _, found, err := s.Get(other, []byte("a1")) |
| 84 | + require.NoError(t, err) |
| 85 | + require.False(t, found, "a loaded key stays in its column family") |
| 86 | + last, ok, err := s.LastKey(loaded) |
| 87 | + require.NoError(t, err) |
| 88 | + require.True(t, ok) |
| 89 | + require.Equal(t, "b1", string(last)) |
| 90 | + } |
| 91 | + check(s) |
| 92 | + |
| 93 | + require.NoError(t, s.Close()) |
| 94 | + reopened, err := New(cfg) |
| 95 | + require.NoError(t, err) |
| 96 | + t.Cleanup(func() { _ = reopened.Close() }) |
| 97 | + check(reopened) |
| 98 | +} |
| 99 | + |
| 100 | +func TestStore_LoadSorted_FailedLoadLeavesNothing(t *testing.T) { |
| 101 | + s := openTestStore(t, nil) |
| 102 | + require.NoError(t, s.Put("", []byte("kept"), []byte("v"))) |
| 103 | + |
| 104 | + require.Error(t, s.LoadSorted("", pairs("b", "1", "a", "2")), "descending keys") |
| 105 | + require.Error(t, s.LoadSorted("", pairs()), "no entries") |
| 106 | + require.Error(t, s.LoadSorted("", pairs("kept", "x")), "a key the column family holds") |
| 107 | + require.ErrorIs(t, s.LoadSorted("not-configured", pairs("a", "1")), ErrCFNotFound) |
| 108 | + |
| 109 | + require.Empty(t, loadFiles(t, s)) |
| 110 | + for _, key := range []string{"a", "b"} { |
| 111 | + _, found, err := s.Get("", []byte(key)) |
| 112 | + require.NoError(t, err) |
| 113 | + require.False(t, found, "key %q", key) |
| 114 | + } |
| 115 | + requireValue(t, s, "", "kept", "v") |
| 116 | +} |
| 117 | + |
| 118 | +func TestStore_LoadSorted_ClosedAndReadOnlyStores(t *testing.T) { |
| 119 | + path := t.TempDir() |
| 120 | + s, err := New(Config{Path: path, Logger: silentLogger()}) |
| 121 | + require.NoError(t, err) |
| 122 | + require.NoError(t, s.Close()) |
| 123 | + require.ErrorIs(t, s.LoadSorted("", pairs("a", "1")), ErrStoreClosed) |
| 124 | + |
| 125 | + readOnly, err := New(Config{Path: path, Logger: silentLogger(), ReadOnly: true}) |
| 126 | + require.NoError(t, err) |
| 127 | + t.Cleanup(func() { _ = readOnly.Close() }) |
| 128 | + require.Error(t, readOnly.LoadSorted("", pairs("a", "1"))) |
| 129 | + require.NoDirExists(t, filepath.Join(path, loadDirName)) |
| 130 | +} |
| 131 | + |
| 132 | +// Close waits for a load in flight, so the load is durable or never started. |
| 133 | +func TestStore_LoadSorted_CloseWaitsForLoad(t *testing.T) { |
| 134 | + path := t.TempDir() |
| 135 | + s, err := New(Config{Path: path, Logger: silentLogger()}) |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + loading, release := make(chan struct{}), make(chan struct{}) |
| 139 | + loadErr := make(chan error, 1) |
| 140 | + go func() { |
| 141 | + loadErr <- s.LoadSorted("", func(yield func(key, value []byte) bool) { |
| 142 | + close(loading) |
| 143 | + <-release |
| 144 | + yield([]byte("a"), []byte("1")) |
| 145 | + }) |
| 146 | + }() |
| 147 | + <-loading |
| 148 | + closed := make(chan struct{}) |
| 149 | + go func() { |
| 150 | + _ = s.Close() |
| 151 | + close(closed) |
| 152 | + }() |
| 153 | + select { |
| 154 | + case <-closed: |
| 155 | + t.Fatal("Close returned while a load was in flight") |
| 156 | + case <-time.After(50 * time.Millisecond): |
| 157 | + } |
| 158 | + close(release) |
| 159 | + require.NoError(t, <-loadErr) |
| 160 | + <-closed |
| 161 | + |
| 162 | + reopened, err := New(Config{Path: path, Logger: silentLogger(), MustExist: true}) |
| 163 | + require.NoError(t, err) |
| 164 | + t.Cleanup(func() { _ = reopened.Close() }) |
| 165 | + requireValue(t, reopened, "", "a", "1") |
| 166 | +} |
| 167 | + |
| 168 | +// The file is written with the column family's own options: a compressed |
| 169 | +// column family gets a compressed file. |
| 170 | +func TestStore_LoadSorted_UsesTheColumnFamilyOptions(t *testing.T) { |
| 171 | + const raw, zstd = "raw", "zstd" |
| 172 | + s, err := New(Config{ |
| 173 | + Path: t.TempDir(), |
| 174 | + ColumnFamilies: []string{raw, zstd}, |
| 175 | + Logger: silentLogger(), |
| 176 | + PerCFOptions: map[string]CFOptions{zstd: {Compression: grocksdb.ZSTDCompression}}, |
| 177 | + }) |
| 178 | + require.NoError(t, err) |
| 179 | + t.Cleanup(func() { _ = s.Close() }) |
| 180 | + |
| 181 | + value := bytes.Repeat([]byte("compressible "), 100) |
| 182 | + for _, cf := range []string{raw, zstd} { |
| 183 | + require.NoError(t, s.LoadSorted(cf, numbered(1000, value))) |
| 184 | + } |
| 185 | + const size = "rocksdb.total-sst-files-size" |
| 186 | + require.Less(t, 4*uintProperty(t, s, zstd, size), uintProperty(t, s, raw, size)) |
| 187 | +} |
| 188 | + |
| 189 | +// With CacheIndexAndFilterBlocks, an open file's index and filter blocks |
| 190 | +// live in the block cache, not on the heap. |
| 191 | +func TestStore_CacheIndexAndFilterBlocks_KeepsThemOffTheHeap(t *testing.T) { |
| 192 | + const cached, plain = "cached", "plain" |
| 193 | + s, err := New(Config{ |
| 194 | + Path: t.TempDir(), |
| 195 | + ColumnFamilies: []string{cached, plain}, |
| 196 | + Logger: silentLogger(), |
| 197 | + Tuning: Tuning{BlockCacheMB: 4}, |
| 198 | + PerCFOptions: map[string]CFOptions{ |
| 199 | + cached: {BloomFilterBitsPerKey: 10, CacheIndexAndFilterBlocks: true}, |
| 200 | + plain: {BloomFilterBitsPerKey: 10}, |
| 201 | + }, |
| 202 | + }) |
| 203 | + require.NoError(t, err) |
| 204 | + t.Cleanup(func() { _ = s.Close() }) |
| 205 | + |
| 206 | + for _, cf := range []string{cached, plain} { |
| 207 | + require.NoError(t, s.LoadSorted(cf, numbered(20_000, []byte("v")))) |
| 208 | + requireValue(t, s, cf, "key-00000007", "v") |
| 209 | + } |
| 210 | + const heap = "rocksdb.estimate-table-readers-mem" |
| 211 | + require.Less(t, 4*uintProperty(t, s, cached, heap), uintProperty(t, s, plain, heap)) |
| 212 | +} |
| 213 | + |
| 214 | +// Files a stopped process was still building are removed by the next open |
| 215 | +// that takes the DB lock, and by no other open. |
| 216 | +func TestNew_RemovesUnfinishedLoadFiles(t *testing.T) { |
| 217 | + path := t.TempDir() |
| 218 | + cfg := Config{Path: path, Logger: silentLogger()} |
| 219 | + s, err := New(cfg) |
| 220 | + require.NoError(t, err) |
| 221 | + |
| 222 | + // An open that fails on the lock leaves a live store's load alone. |
| 223 | + require.NoError(t, s.LoadSorted("", func(yield func(key, value []byte) bool) { |
| 224 | + _, err := New(cfg) |
| 225 | + require.Error(t, err) |
| 226 | + yield([]byte("a"), []byte("1")) |
| 227 | + })) |
| 228 | + require.NoError(t, s.Close()) |
| 229 | + |
| 230 | + orphan := filepath.Join(path, loadDirName, "orphan.sst") |
| 231 | + require.NoError(t, os.WriteFile(orphan, []byte("half a file"), 0o600)) |
| 232 | + readOnly, err := New(Config{Path: path, Logger: silentLogger(), ReadOnly: true}) |
| 233 | + require.NoError(t, err) |
| 234 | + require.NoError(t, readOnly.Close()) |
| 235 | + require.FileExists(t, orphan) |
| 236 | + |
| 237 | + reopened, err := New(Config{Path: path, Logger: silentLogger(), MustExist: true}) |
| 238 | + require.NoError(t, err) |
| 239 | + t.Cleanup(func() { _ = reopened.Close() }) |
| 240 | + require.NoFileExists(t, orphan) |
| 241 | + requireValue(t, reopened, "", "a", "1") |
| 242 | +} |
0 commit comments