From 005ddc5e6a6be4c2ddf458c4c5ac349bceb2dfe8 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 24 Nov 2026 18:14:42 -0800 Subject: [PATCH] storage: verify a live record's body instead of comparing its size with the index Data compaction copied a record whose body failed to parse, and the size check dropped an intact record when only its index row was wrong. Read the body only for the record the index points at, drop it on the same errors -method index drops on, fail on any other read error, and keep the size the record itself carries. Co-Authored-By: Claude Opus 5.5 --- weed/command/compact_test.go | 40 ++++++++++++++++++++++------------- weed/storage/volume_vacuum.go | 17 ++++++++++----- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/weed/command/compact_test.go b/weed/command/compact_test.go index 5cde934e6d..52af4ebb78 100644 --- a/weed/command/compact_test.go +++ b/weed/command/compact_test.go @@ -52,25 +52,38 @@ func compactedKeys(t *testing.T, base string) (indexed, intact []uint64) { return indexed, intact } -// A damaged header in the middle of the .dat: -method data copies every live -// record past it and drops the damaged one, where -method index copies it as is. +func damageDat(at int64, b []byte) func(t *testing.T, base string, offsets []int64) { + return func(t *testing.T, base string, offsets []int64) { + overwriteAt(t, base+".dat", offsets[2]+at, b) + } +} + +// Damage to the middle record of five: both methods drop it and keep the +// others. -method data trusts an intact record over a wrong index size. func TestCompactDataMethodSkipsUnreadableRecord(t *testing.T) { sizeAt := int64(types.NeedleIdSize + types.CookieSize) + others := []uint64{1, 2, 4, 5} for _, tc := range []struct { - name string - at int64 - damage []byte + name string + corrupt func(t *testing.T, base string, offsets []int64) + wantData []uint64 + wantIndex []uint64 }{ - {"garbage header", 0, bytes.Repeat([]byte{0xa5}, 16)}, - {"size past the end", sizeAt, []byte{0x7f, 0xff, 0xff, 0xf0}}, - {"size into the next record", sizeAt, []byte{0x00, 0x00, 0x00, 0x10}}, + {"garbage header", damageDat(0, bytes.Repeat([]byte{0xa5}, 16)), others, others}, + {"size past the end", damageDat(sizeAt, []byte{0x7f, 0xff, 0xff, 0xf0}), others, others}, + {"size into the next record", damageDat(sizeAt, []byte{0x00, 0x00, 0x00, 0x10}), others, others}, + {"data", damageDat(types.NeedleHeaderSize+10, []byte{0xee}), others, others}, + {"index size", func(t *testing.T, base string, _ []int64) { + row := int64(slices.Index(indexedKeys(t, base+".idx"), 3)) + overwriteAt(t, base+".idx", row*types.NeedleMapEntrySize+types.NeedleIdSize+types.OffsetSize, []byte{0x00, 0x00, 0x00, 0x01}) + }, []uint64{1, 2, 3, 4, 5}, others}, } { t.Run(tc.name, func(t *testing.T) { - for _, method := range []string{"data", "index"} { + for method, want := range map[string][]uint64{"data": tc.wantData, "index": tc.wantIndex} { dir := t.TempDir() base, offsets := writeFixTestVolume(t, dir, 5) doFixOneVolume(dir, "fix_7", "fix", 7, true) - overwriteAt(t, base+".dat", offsets[2]+tc.at, tc.damage) + tc.corrupt(t, base, offsets) runCompactOnFixTestVolume(t, dir, method) @@ -78,11 +91,8 @@ func TestCompactDataMethodSkipsUnreadableRecord(t *testing.T) { t.Fatalf("-method %s: exit status %d", method, CommandExitStatus()) } indexed, intact := compactedKeys(t, base) - if !slices.Equal(intact, []uint64{1, 2, 4, 5}) { - t.Errorf("-method %s: intact keys %v, want [1 2 4 5]", method, intact) - } - if method == "data" && !slices.Equal(indexed, intact) { - t.Errorf("-method data: indexed keys %v, want only the intact ones", indexed) + if !slices.Equal(indexed, want) || !slices.Equal(intact, want) { + t.Errorf("-method %s: indexed keys %v, intact keys %v, want %v", method, indexed, intact, want) } } }) diff --git a/weed/storage/volume_vacuum.go b/weed/storage/volume_vacuum.go index 08c0c81fff..d3d6ca1b00 100644 --- a/weed/storage/volume_vacuum.go +++ b/weed/storage/volume_vacuum.go @@ -658,18 +658,25 @@ func (scanner *VolumeFileScanner4Vacuum) VisitSuperBlock(superBlock super_block. } func (scanner *VolumeFileScanner4Vacuum) ReadNeedleBody() bool { - return true + return false } func (scanner *VolumeFileScanner4Vacuum) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) (err error) { scanner.visited = offset defer func() { scanner.visitErr = err }() - if n.HasTtl() && scanner.now >= n.LastModified+uint64(scanner.v.Ttl.Minutes()*60) { - return nil - } nv, ok := scanner.v.nm.Get(n.Id) glog.V(4).Infoln("needle expected offset ", offset, "ok", ok, "nv", nv) - if ok && n.Size != nv.Size { + if !ok || nv.Offset.ToActualOffset() != offset { + return nil + } + if err := n.ReadData(scanner.v.DataBackend, offset, n.Size, scanner.v.Version()); err != nil { + if !isSkippableNeedleReadError(err) { + return fmt.Errorf("read needle %d at offset %d: %w", n.Id, offset, err) + } + glog.Errorf("compact volume %d: dropping unreadable needle key=%d offset=%d size=%d: %v", scanner.v.Id, n.Id, offset, n.Size, err) + return nil + } + if n.HasTtl() && scanner.now >= n.LastModified+uint64(scanner.v.Ttl.Minutes()*60) { return nil } if ok && nv.Offset.ToActualOffset() == offset && nv.Size > 0 && nv.Size.IsValid() {