Skip to content

Commit 4bb071b

Browse files
authored
Merge branch 'main' into qiang/support-multi-checksum-algo
2 parents 0d8da25 + f697d22 commit 4bb071b

10 files changed

Lines changed: 96 additions & 62 deletions

File tree

.github/workflows/trunk.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Trunk Code Quality
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
branches: main
6+
7+
permissions:
8+
contents: read
9+
actions: write
10+
checks: write
11+
12+
jobs:
13+
trunk-code-quality:
14+
name: Trunk Code Quality
15+
uses: hypermodeinc/.github/.github/workflows/trunk.yml@main

badger/cmd/info.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"strings"
1818
"time"
1919

20-
humanize "github.com/dustin/go-humanize"
20+
"github.com/dustin/go-humanize"
2121
"github.com/spf13/cobra"
2222

2323
"github.com/dgraph-io/badger/v4"
@@ -121,7 +121,7 @@ func handleInfo(cmd *cobra.Command, args []string) error {
121121
return nil
122122
}
123123

124-
if err := printInfo(sstDir, vlogDir); err != nil {
124+
if err := printInfo(sstDir, vlogDir, bopt); err != nil {
125125
return y.Wrap(err, "failed to print information in MANIFEST file")
126126
}
127127

@@ -332,7 +332,7 @@ func readDir(dir string) ([]fs.FileInfo, error) {
332332
return infos, err
333333
}
334334

335-
func printInfo(dir, valueDir string) error {
335+
func printInfo(dir, valueDir string, bopt badger.Options) error {
336336
if dir == "" {
337337
return fmt.Errorf("--dir not supplied")
338338
}
@@ -348,7 +348,7 @@ func printInfo(dir, valueDir string) error {
348348
fp.Close()
349349
}
350350
}()
351-
manifest, truncOffset, err := badger.ReplayManifestFile(fp, opt.externalMagicVersion)
351+
manifest, truncOffset, err := badger.ReplayManifestFile(fp, opt.externalMagicVersion, bopt)
352352
if err != nil {
353353
return err
354354
}

db2_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,24 @@ func TestCompactionFilePicking(t *testing.T) {
427427
for i := 1; i <= 10; i++ {
428428
// Each table has difference of 1 between smallest and largest key.
429429
tab := createTableWithRange(t, db, 2*i-1, 2*i)
430-
addToManifest(t, db, tab, 3)
430+
addToManifest(t, db, tab, 3, db.opt)
431431
require.NoError(t, l3.replaceTables([]*table.Table{}, []*table.Table{tab}))
432432
}
433433

434434
l2 := db.lc.levels[2]
435435
// First table has keys 1 and 4.
436436
tab := createTableWithRange(t, db, 1, 4)
437-
addToManifest(t, db, tab, 2)
437+
addToManifest(t, db, tab, 2, db.opt)
438438
require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
439439

440440
// Second table has keys 5 and 12.
441441
tab = createTableWithRange(t, db, 5, 12)
442-
addToManifest(t, db, tab, 2)
442+
addToManifest(t, db, tab, 2, db.opt)
443443
require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
444444

445445
// Third table has keys 13 and 18.
446446
tab = createTableWithRange(t, db, 13, 18)
447-
addToManifest(t, db, tab, 2)
447+
addToManifest(t, db, tab, 2, db.opt)
448448
require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
449449

450450
cdef := &compactDef{
@@ -479,14 +479,14 @@ func TestCompactionFilePicking(t *testing.T) {
479479
}
480480

481481
// addToManifest function is used in TestCompactionFilePicking. It adds table to db manifest.
482-
func addToManifest(t *testing.T, db *DB, tab *table.Table, level uint32) {
482+
func addToManifest(t *testing.T, db *DB, tab *table.Table, level uint32, opt Options) {
483483
change := &pb.ManifestChange{
484484
Id: tab.ID(),
485485
Op: pb.ManifestChange_CREATE,
486486
Level: level,
487487
Compression: uint32(tab.CompressionType()),
488488
}
489-
require.NoError(t, db.manifest.addChanges([]*pb.ManifestChange{change}),
489+
require.NoError(t, db.manifest.addChanges([]*pb.ManifestChange{change}, opt),
490490
"unable to add to manifest")
491491
}
492492

@@ -679,7 +679,7 @@ func TestWindowsDataLoss(t *testing.T) {
679679
v := []byte("barValuebarValuebarValuebarValuebarValue")
680680
require.Greater(t, len(v), db.valueThreshold())
681681

682-
//32 bytes length and now it's not working
682+
// 32 bytes length and now it's not working
683683
err := txn.Set(key, v)
684684
require.NoError(t, err)
685685
keyList = append(keyList, key)

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/dgraph-io/badger/v4
22

33
go 1.23.0
44

5-
toolchain go1.24.2
5+
toolchain go1.24.3
66

77
require (
88
github.com/cespare/xxhash/v2 v2.3.0
@@ -12,9 +12,9 @@ require (
1212
github.com/klauspost/compress v1.18.0
1313
github.com/spf13/cobra v1.9.1
1414
github.com/stretchr/testify v1.10.0
15-
go.opentelemetry.io/contrib/zpages v0.60.0
16-
go.opentelemetry.io/otel v1.35.0
17-
golang.org/x/net v0.39.0
15+
go.opentelemetry.io/contrib/zpages v0.61.0
16+
go.opentelemetry.io/otel v1.36.0
17+
golang.org/x/net v0.40.0
1818
golang.org/x/sys v0.33.0
1919
google.golang.org/protobuf v1.36.6
2020
)
@@ -28,9 +28,9 @@ require (
2828
github.com/pmezard/go-difflib v1.0.0 // indirect
2929
github.com/spf13/pflag v1.0.6 // indirect
3030
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
31-
go.opentelemetry.io/otel/metric v1.35.0 // indirect
32-
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
33-
go.opentelemetry.io/otel/trace v1.35.0 // indirect
31+
go.opentelemetry.io/otel/metric v1.36.0 // indirect
32+
go.opentelemetry.io/otel/sdk v1.36.0 // indirect
33+
go.opentelemetry.io/otel/trace v1.36.0 // indirect
3434
gopkg.in/yaml.v3 v3.0.1 // indirect
3535
)
3636

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
4141
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4242
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
4343
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
44-
go.opentelemetry.io/contrib/zpages v0.60.0 h1:wOM9ie1Hz4H88L9KE6GrGbKJhfm+8F1NfW/Y3q9Xt+8=
45-
go.opentelemetry.io/contrib/zpages v0.60.0/go.mod h1:xqfToSRGh2MYUsfyErNz8jnNDPlnpZqWM/y6Z2Cx7xw=
46-
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
47-
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
48-
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
49-
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
50-
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
51-
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
52-
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
53-
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
44+
go.opentelemetry.io/contrib/zpages v0.61.0 h1:tYvUj377Dn3k1wf1le/f8YWSNQ8k0byS3jK8PiIXu9Y=
45+
go.opentelemetry.io/contrib/zpages v0.61.0/go.mod h1:MFNPHMJOGA1P6m5501ANjOJDp4A9BUQja1Y53CDL8LQ=
46+
go.opentelemetry.io/otel v1.36.0 h1:UumtzIklRBY6cI/lllNZlALOF5nNIzJVb16APdvgTXg=
47+
go.opentelemetry.io/otel v1.36.0/go.mod h1:/TcFMXYjyRNh8khOAO9ybYkqaDBb/70aVwkNML4pP8E=
48+
go.opentelemetry.io/otel/metric v1.36.0 h1:MoWPKVhQvJ+eeXWHFBOPoBOi20jh6Iq2CcCREuTYufE=
49+
go.opentelemetry.io/otel/metric v1.36.0/go.mod h1:zC7Ks+yeyJt4xig9DEw9kuUFe5C3zLbVjV2PzT6qzbs=
50+
go.opentelemetry.io/otel/sdk v1.36.0 h1:b6SYIuLRs88ztox4EyrvRti80uXIFy+Sqzoh9kFULbs=
51+
go.opentelemetry.io/otel/sdk v1.36.0/go.mod h1:+lC+mTgD+MUWfjJubi2vvXWcVxyr9rmlshZni72pXeY=
52+
go.opentelemetry.io/otel/trace v1.36.0 h1:ahxWNuqZjpdiFAyrIoQ4GIiAIhxAunQR6MUoKrsNd4w=
53+
go.opentelemetry.io/otel/trace v1.36.0/go.mod h1:gQ+OnDZzrybY4k4seLzPAWNwVBBVlF2szhehOBB/tGA=
5454
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5555
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
56-
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
57-
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
56+
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
57+
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
5858
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
5959
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
6060
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=

levels.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (s *levelsController) dropTree() (int, error) {
226226
}
227227
}
228228
changeSet := pb.ManifestChangeSet{Changes: changes}
229-
if err := s.kv.manifest.addChanges(changeSet.Changes); err != nil {
229+
if err := s.kv.manifest.addChanges(changeSet.Changes, s.kv.opt); err != nil {
230230
return 0, err
231231
}
232232

@@ -1437,7 +1437,7 @@ func (s *levelsController) runCompactDef(id, l int, cd compactDef) (err error) {
14371437
changeSet := buildChangeSet(&cd, newTables)
14381438

14391439
// We write to the manifest _before_ we delete files (and after we created files)
1440-
if err := s.kv.manifest.addChanges(changeSet.Changes); err != nil {
1440+
if err := s.kv.manifest.addChanges(changeSet.Changes, s.kv.opt); err != nil {
14411441
return err
14421442
}
14431443

@@ -1566,7 +1566,7 @@ func (s *levelsController) addLevel0Table(t *table.Table) error {
15661566
// deletes the table.)
15671567
err := s.kv.manifest.addChanges([]*pb.ManifestChange{
15681568
newCreateChange(t.ID(), 0, t.KeyID(), t.CompressionType()),
1569-
})
1569+
}, s.kv.opt)
15701570
if err != nil {
15711571
return err
15721572
}

levels_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func createAndOpen(db *DB, td []keyValVersion, level int) {
4545
}
4646
if err := db.manifest.addChanges([]*pb.ManifestChange{
4747
newCreateChange(tab.ID(), level, 0, tab.CompressionType()),
48-
}); err != nil {
48+
}, db.opt); err != nil {
4949
panic(err)
5050
}
5151
db.lc.levels[level].Lock()
@@ -1193,7 +1193,7 @@ func TestFillTableCleanup(t *testing.T) {
11931193
tab := buildTable(i)
11941194
require.NoError(t, db.manifest.addChanges([]*pb.ManifestChange{
11951195
newCreateChange(tab.ID(), level, 0, tab.CompressionType()),
1196-
}))
1196+
}, db.opt))
11971197
tab.CreatedAt = time.Now().Add(-10 * time.Hour)
11981198
// Add table to the given level.
11991199
lh.addTable(tab)
@@ -1270,7 +1270,7 @@ func TestStaleDataCleanup(t *testing.T) {
12701270
tab := buildStaleTable(i)
12711271
require.NoError(t, db.manifest.addChanges([]*pb.ManifestChange{
12721272
newCreateChange(tab.ID(), level, 0, tab.CompressionType()),
1273-
}))
1273+
}, db.opt))
12741274
tab.CreatedAt = time.Now().Add(-10 * time.Hour)
12751275
// Add table to the given level.
12761276
lh.addTable(tab)

manifest.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func (m *Manifest) asChanges() []*pb.ManifestChange {
105105
return changes
106106
}
107107

108-
func (m *Manifest) clone() Manifest {
108+
func (m *Manifest) clone(opt Options) Manifest {
109109
changeSet := pb.ManifestChangeSet{Changes: m.asChanges()}
110110
ret := createManifest()
111-
y.Check(applyChangeSet(&ret, &changeSet))
111+
y.Check(applyChangeSet(&ret, &changeSet, opt))
112112
return ret
113113
}
114114

@@ -120,11 +120,11 @@ func openOrCreateManifestFile(opt Options) (
120120
return &manifestFile{inMemory: true}, Manifest{}, nil
121121
}
122122
return helpOpenOrCreateManifestFile(opt.Dir, opt.ReadOnly, opt.ExternalMagicVersion,
123-
manifestDeletionsRewriteThreshold)
123+
manifestDeletionsRewriteThreshold, opt)
124124
}
125125

126126
func helpOpenOrCreateManifestFile(dir string, readOnly bool, extMagic uint16,
127-
deletionsThreshold int) (*manifestFile, Manifest, error) {
127+
deletionsThreshold int, opt Options) (*manifestFile, Manifest, error) {
128128

129129
path := filepath.Join(dir, ManifestFilename)
130130
var flags y.Flags
@@ -149,13 +149,13 @@ func helpOpenOrCreateManifestFile(dir string, readOnly bool, extMagic uint16,
149149
fp: fp,
150150
directory: dir,
151151
externalMagic: extMagic,
152-
manifest: m.clone(),
152+
manifest: m.clone(opt),
153153
deletionsRewriteThreshold: deletionsThreshold,
154154
}
155155
return mf, m, nil
156156
}
157157

158-
manifest, truncOffset, err := ReplayManifestFile(fp, extMagic)
158+
manifest, truncOffset, err := ReplayManifestFile(fp, extMagic, opt)
159159
if err != nil {
160160
_ = fp.Close()
161161
return nil, Manifest{}, err
@@ -177,7 +177,7 @@ func helpOpenOrCreateManifestFile(dir string, readOnly bool, extMagic uint16,
177177
fp: fp,
178178
directory: dir,
179179
externalMagic: extMagic,
180-
manifest: manifest.clone(),
180+
manifest: manifest.clone(opt),
181181
deletionsRewriteThreshold: deletionsThreshold,
182182
}
183183
return mf, manifest, nil
@@ -194,7 +194,7 @@ func (mf *manifestFile) close() error {
194194
// we replay the MANIFEST file, we'll either replay all the changes or none of them. (The truth of
195195
// this depends on the filesystem -- some might append garbage data if a system crash happens at
196196
// the wrong time.)
197-
func (mf *manifestFile) addChanges(changesParam []*pb.ManifestChange) error {
197+
func (mf *manifestFile) addChanges(changesParam []*pb.ManifestChange, opt Options) error {
198198
if mf.inMemory {
199199
return nil
200200
}
@@ -207,7 +207,7 @@ func (mf *manifestFile) addChanges(changesParam []*pb.ManifestChange) error {
207207
// Maybe we could use O_APPEND instead (on certain file systems)
208208
mf.appendLock.Lock()
209209
defer mf.appendLock.Unlock()
210-
if err := applyChangeSet(&mf.manifest, &changes); err != nil {
210+
if err := applyChangeSet(&mf.manifest, &changes, opt); err != nil {
211211
return err
212212
}
213213
// Rewrite manifest if it'd shrink by 1/10 and it's big enough to care
@@ -350,7 +350,7 @@ var (
350350
// Also, returns the last offset after a completely read manifest entry -- the file must be
351351
// truncated at that point before further appends are made (if there is a partial entry after
352352
// that). In normal conditions, truncOffset is the file size.
353-
func ReplayManifestFile(fp *os.File, extMagic uint16) (Manifest, int64, error) {
353+
func ReplayManifestFile(fp *os.File, extMagic uint16, opt Options) (Manifest, int64, error) {
354354
r := countingReader{wrapped: bufio.NewReader(fp)}
355355

356356
var magicBuf [8]byte
@@ -418,15 +418,15 @@ func ReplayManifestFile(fp *os.File, extMagic uint16) (Manifest, int64, error) {
418418
return Manifest{}, 0, err
419419
}
420420

421-
if err := applyChangeSet(&build, &changeSet); err != nil {
421+
if err := applyChangeSet(&build, &changeSet, opt); err != nil {
422422
return Manifest{}, 0, err
423423
}
424424
}
425425

426426
return build, offset, nil
427427
}
428428

429-
func applyManifestChange(build *Manifest, tc *pb.ManifestChange) error {
429+
func applyManifestChange(build *Manifest, tc *pb.ManifestChange, opt Options) error {
430430
switch tc.Op {
431431
case pb.ManifestChange_CREATE:
432432
if _, ok := build.Tables[tc.Id]; ok {
@@ -445,10 +445,14 @@ func applyManifestChange(build *Manifest, tc *pb.ManifestChange) error {
445445
case pb.ManifestChange_DELETE:
446446
tm, ok := build.Tables[tc.Id]
447447
if !ok {
448-
return fmt.Errorf("MANIFEST removes non-existing table %d", tc.Id)
448+
opt.Warningf("MANIFEST delete: table %d has already been removed", tc.Id)
449+
for _, level := range build.Levels {
450+
delete(level.Tables, tc.Id)
451+
}
452+
} else {
453+
delete(build.Levels[tm.Level].Tables, tc.Id)
454+
delete(build.Tables, tc.Id)
449455
}
450-
delete(build.Levels[tm.Level].Tables, tc.Id)
451-
delete(build.Tables, tc.Id)
452456
build.Deletions++
453457
default:
454458
return fmt.Errorf("MANIFEST file has invalid manifestChange op")
@@ -458,9 +462,9 @@ func applyManifestChange(build *Manifest, tc *pb.ManifestChange) error {
458462

459463
// This is not a "recoverable" error -- opening the KV store fails because the MANIFEST file is
460464
// just plain broken.
461-
func applyChangeSet(build *Manifest, changeSet *pb.ManifestChangeSet) error {
465+
func applyChangeSet(build *Manifest, changeSet *pb.ManifestChangeSet, opt Options) error {
462466
for _, change := range changeSet.Changes {
463-
if err := applyManifestChange(build, change); err != nil {
467+
if err := applyManifestChange(build, change, opt); err != nil {
464468
return err
465469
}
466470
}

0 commit comments

Comments
 (0)