|
| 1 | +// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use |
| 2 | +// of this source code is governed by a BSD-style license that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package pebble |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/cockroachdb/datadriven" |
| 12 | + "github.com/cockroachdb/pebble/objstorage/remote" |
| 13 | + "github.com/cockroachdb/pebble/vfs" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// Test that the EstimateDiskUsage and EstimateDiskUsageByBackingType should panic when the DB is closed |
| 18 | +func TestEstimateDiskUsageClosedDB(t *testing.T) { |
| 19 | + mem := vfs.NewMem() |
| 20 | + d, err := Open("", &Options{FS: mem}) |
| 21 | + require.NoError(t, err) |
| 22 | + require.NoError(t, d.Set([]byte("key"), []byte("value"), nil)) |
| 23 | + require.NoError(t, d.Close()) |
| 24 | + // Attempting to estimate on closed DB should panic |
| 25 | + require.Panics(t, func() { |
| 26 | + d.EstimateDiskUsage([]byte("a"), []byte("z")) |
| 27 | + }) |
| 28 | + require.Panics(t, func() { |
| 29 | + d.EstimateDiskUsageByBackingType([]byte("a"), []byte("z")) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +// Test the EstimateDiskUsage and EstimateDiskUsageByBackingType data driven tests |
| 34 | +func TestEstimateDiskUsageDataDriven(t *testing.T) { |
| 35 | + fs := vfs.NewMem() |
| 36 | + remoteStorage := remote.NewInMem() |
| 37 | + var d *DB |
| 38 | + defer func() { |
| 39 | + if d != nil { |
| 40 | + _ = d.Close() |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | + datadriven.RunTest(t, "testdata/disk_usage", func(t *testing.T, td *datadriven.TestData) string { |
| 45 | + switch td.Cmd { |
| 46 | + case "open": |
| 47 | + if d != nil { |
| 48 | + require.NoError(t, d.Close()) |
| 49 | + d = nil |
| 50 | + } |
| 51 | + opts := &Options{FS: fs, FormatMajorVersion: FormatExciseBoundsRecord, DisableAutomaticCompactions: true} |
| 52 | + opts.Experimental.RemoteStorage = remote.MakeSimpleFactory(map[remote.Locator]remote.Storage{ |
| 53 | + "external-locator": remoteStorage, |
| 54 | + }) |
| 55 | + require.NoError(t, parseDBOptionsArgs(opts, td.CmdArgs)) |
| 56 | + var err error |
| 57 | + d, err = Open("", opts) |
| 58 | + require.NoError(t, err) |
| 59 | + return "" |
| 60 | + |
| 61 | + case "close": |
| 62 | + if d != nil { |
| 63 | + require.NoError(t, d.Close()) |
| 64 | + d = nil |
| 65 | + } |
| 66 | + return "" |
| 67 | + |
| 68 | + case "batch": |
| 69 | + b := d.NewBatch() |
| 70 | + if err := runBatchDefineCmd(td, b); err != nil { |
| 71 | + return err.Error() |
| 72 | + } |
| 73 | + if err := b.Commit(nil); err != nil { |
| 74 | + return err.Error() |
| 75 | + } |
| 76 | + return "" |
| 77 | + |
| 78 | + case "flush": |
| 79 | + if err := d.Flush(); err != nil { |
| 80 | + return err.Error() |
| 81 | + } |
| 82 | + return "" |
| 83 | + |
| 84 | + case "build": |
| 85 | + if err := runBuildCmd(td, d, fs); err != nil { |
| 86 | + return err.Error() |
| 87 | + } |
| 88 | + return "" |
| 89 | + |
| 90 | + case "ingest": |
| 91 | + if err := runIngestCmd(td, d, fs); err != nil { |
| 92 | + return err.Error() |
| 93 | + } |
| 94 | + return "" |
| 95 | + case "build-remote": |
| 96 | + if err := runBuildRemoteCmd(td, d, remoteStorage); err != nil { |
| 97 | + return err.Error() |
| 98 | + } |
| 99 | + return "" |
| 100 | + case "ingest-external": |
| 101 | + if err := runIngestExternalCmd(t, td, d, remoteStorage, "external-locator"); err != nil { |
| 102 | + return err.Error() |
| 103 | + } |
| 104 | + return "" |
| 105 | + case "compact": |
| 106 | + if err := runCompactCmd(td, d); err != nil { |
| 107 | + return err.Error() |
| 108 | + } |
| 109 | + return runLSMCmd(td, d) |
| 110 | + |
| 111 | + case "estimate-disk-usage": |
| 112 | + // Parse range arguments, default to "a" and "z" if not specified |
| 113 | + start := []byte("a") |
| 114 | + end := []byte("z") |
| 115 | + if len(td.CmdArgs) >= 2 { |
| 116 | + start = []byte(td.CmdArgs[0].Key) |
| 117 | + end = []byte(td.CmdArgs[1].Key) |
| 118 | + } |
| 119 | + size, err := d.EstimateDiskUsage(start, end) |
| 120 | + if err != nil { |
| 121 | + return err.Error() |
| 122 | + } |
| 123 | + return fmt.Sprintf("size: %d", size) |
| 124 | + |
| 125 | + case "estimate-disk-usage-by-backing-type": |
| 126 | + // Parse range arguments, default to "a" and "z" if not specified |
| 127 | + start := []byte("a") |
| 128 | + end := []byte("z") |
| 129 | + if len(td.CmdArgs) >= 2 { |
| 130 | + start = []byte(td.CmdArgs[0].Key) |
| 131 | + end = []byte(td.CmdArgs[1].Key) |
| 132 | + } |
| 133 | + total, remote, external, err := d.EstimateDiskUsageByBackingType(start, end) |
| 134 | + if err != nil { |
| 135 | + return err.Error() |
| 136 | + } |
| 137 | + // remote and external should be less than or equal to total |
| 138 | + require.LessOrEqual(t, remote, total) |
| 139 | + require.LessOrEqual(t, external, remote) |
| 140 | + return fmt.Sprintf("total: %d, remote: %d, external: %d", total, remote, external) |
| 141 | + |
| 142 | + default: |
| 143 | + return fmt.Sprintf("unknown command: %s", td.Cmd) |
| 144 | + } |
| 145 | + }) |
| 146 | +} |
0 commit comments