mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
storage: resume data compaction past an unreadable record
Compacting by data follows record headers through the .dat, so one corrupt header ended the scan: the compaction failed, or, when the header's size jumped past the end, every record after it was dropped. Compacting by index copies past such a record. When the scan stops on a corrupt header or jumps past live records, resume it at each live record the index places further on. A read error still fails the compaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
85fcde9b0d
commit
30c2b07dc6
3 files changed
+166
-18
No files matched your search
@@ -3,22 +3,98 @@ package command
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/idx"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
||||
)
|
||||
|
||||
func TestCompactDataMethodBadHeaderLeavesNoOutput(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, offsets := writeFixTestVolume(t, dir, 5)
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
||||
overwriteAt(t, base+".dat", offsets[2], bytes.Repeat([]byte{0xa5}, 16))
|
||||
|
||||
func runCompactOnFixTestVolume(t *testing.T, dir, method string) {
|
||||
t.Helper()
|
||||
savedDir, savedCollection, savedId, savedMethod, savedStatus := *compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus
|
||||
defer func() {
|
||||
t.Cleanup(func() {
|
||||
*compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus = savedDir, savedCollection, savedId, savedMethod, savedStatus
|
||||
}()
|
||||
*compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus = dir, "fix", 7, "data", 0
|
||||
|
||||
})
|
||||
*compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus = dir, "fix", 7, method, 0
|
||||
runCompact(cmdCompact, nil)
|
||||
}
|
||||
|
||||
// compactedKeys returns the keys in the .cpx, and those whose .cpd record
|
||||
// holds the data writeFixTestVolume wrote for them.
|
||||
func compactedKeys(t *testing.T, base string) (indexed, intact []uint64) {
|
||||
t.Helper()
|
||||
datFile, err := os.Open(base + ".cpd")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dat := backend.NewDiskFile(datFile)
|
||||
defer dat.Close()
|
||||
idxFile, err := os.Open(base + ".cpx")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer idxFile.Close()
|
||||
if err := idx.WalkIndexFile(idxFile, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error {
|
||||
indexed = append(indexed, uint64(key))
|
||||
n := new(needle.Needle)
|
||||
if n.ReadData(dat, offset.ToActualOffset(), size, needle.GetCurrentVersion()) == nil && bytes.Equal(n.Data, bytes.Repeat([]byte{byte(key)}, 1000*int(key))) {
|
||||
intact = append(intact, uint64(key))
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
slices.Sort(indexed)
|
||||
slices.Sort(intact)
|
||||
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 TestCompactDataMethodSkipsUnreadableRecord(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
at int64
|
||||
damage []byte
|
||||
}{
|
||||
{"garbage header", 0, bytes.Repeat([]byte{0xa5}, 16)},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
for _, method := range []string{"data", "index"} {
|
||||
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)
|
||||
|
||||
runCompactOnFixTestVolume(t, dir, method)
|
||||
|
||||
if CommandExitStatus() != 0 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompactFailureLeavesNoOutput(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, _ := writeFixTestVolume(t, dir, 5)
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
||||
if err := os.Mkdir(base+".cpd", 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
runCompactOnFixTestVolume(t, dir, "data")
|
||||
|
||||
if CommandExitStatus() == 0 {
|
||||
t.Error("expected a non-zero exit status")
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
@@ -644,6 +645,8 @@ type VolumeFileScanner4Vacuum struct {
|
||||
newOffset int64
|
||||
now uint64
|
||||
writeThrottler *util.WriteThrottler
|
||||
visited int64
|
||||
visitErr error
|
||||
}
|
||||
|
||||
func (scanner *VolumeFileScanner4Vacuum) VisitSuperBlock(superBlock super_block.SuperBlock) error {
|
||||
@@ -658,7 +661,9 @@ func (scanner *VolumeFileScanner4Vacuum) ReadNeedleBody() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (scanner *VolumeFileScanner4Vacuum) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
|
||||
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
|
||||
}
|
||||
@@ -697,6 +702,9 @@ func (v *Volume) copyDataAndGenerateIndexFile(opts *CompactOptions) (err error)
|
||||
writeThrottler: util.NewWriteThrottler(opts.MaxBytesPerSecond),
|
||||
}
|
||||
err = ScanVolumeFile(v.dir, v.Collection, v.Id, v.needleMapKind, scanner)
|
||||
if scanner.newOffset > 0 && scanner.visitErr == nil {
|
||||
err = v.copyPastUnreadableRecords(scanner, err)
|
||||
}
|
||||
if err != nil {
|
||||
v.checkReadWriteError(err)
|
||||
return err
|
||||
@@ -708,6 +716,55 @@ func (v *Volume) copyDataAndGenerateIndexFile(opts *CompactOptions) (err error)
|
||||
return v.loadTmpNeedleMap(opts.destIdxPath)
|
||||
}
|
||||
|
||||
// copyPastUnreadableRecords resumes the scan at each live record the index
|
||||
// places past where it stopped. The scan follows record headers, so a corrupt
|
||||
// one, or one whose size jumps over the records after it, ends it early;
|
||||
// compacting by index copies past such a record, and so does this. A read
|
||||
// error still fails the compaction.
|
||||
func (v *Volume) copyPastUnreadableRecords(scanner *VolumeFileScanner4Vacuum, scanErr error) error {
|
||||
if scanErr != nil && !errors.Is(scanErr, needle.ErrorCorrupted) {
|
||||
return scanErr
|
||||
}
|
||||
pending, err := v.liveRecordsAfter(scanner.visited)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, offset := range pending {
|
||||
if offset <= scanner.visited {
|
||||
continue
|
||||
}
|
||||
glog.Warningf("compact volume %d: scan stopped after offset %d (%v), resuming at live record %d", v.Id, scanner.visited, scanErr, offset)
|
||||
scanErr = ScanVolumeFileFrom(v.Version(), v.DataBackend, offset, scanner)
|
||||
if scanErr != nil && (scanner.visitErr != nil || !errors.Is(scanErr, needle.ErrorCorrupted)) {
|
||||
return scanErr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// liveRecordsAfter returns the .dat offsets, in order, of the live records the
|
||||
// index places past offset.
|
||||
func (v *Volume) liveRecordsAfter(offset int64) ([]int64, error) {
|
||||
indexFile, err := os.Open(v.FileName(".idx"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open %s: %w", v.FileName(".idx"), err)
|
||||
}
|
||||
defer indexFile.Close()
|
||||
var offsets []int64
|
||||
err = idx2.WalkIndexFile(indexFile, 0, func(key NeedleId, recordOffset Offset, size Size) error {
|
||||
actual := recordOffset.ToActualOffset()
|
||||
if actual <= offset {
|
||||
return nil
|
||||
}
|
||||
if nv, ok := v.nm.Get(key); ok && nv.Offset == recordOffset && nv.Size > 0 && nv.Size.IsValid() {
|
||||
offsets = append(offsets, actual)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
slices.Sort(offsets)
|
||||
return offsets, err
|
||||
}
|
||||
|
||||
func (v *Volume) copyDataBasedOnIndexFile(opts *CompactOptions) (err error) {
|
||||
var (
|
||||
srcDatBackend, dstDatBackend backend.BackendStorageFile
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -726,9 +727,9 @@ func TestCompactByVolumeData_SkipsNegativeSizeHeader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A corrupt header the scan cannot advance past must fail compaction and
|
||||
// leave the original volume untouched.
|
||||
func TestCompactByVolumeData_FailsOnHeaderThatCannotAdvance(t *testing.T) {
|
||||
// A corrupt header the scan cannot advance past is skipped: compaction resumes
|
||||
// at the next live record and leaves the original volume untouched.
|
||||
func TestCompactByVolumeData_SkipsHeaderThatCannotAdvance(t *testing.T) {
|
||||
for _, size := range []types.Size{-36, -100} {
|
||||
t.Run(fmt.Sprintf("size%d", size), func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
@@ -770,12 +771,26 @@ func TestCompactByVolumeData_FailsOnHeaderThatCannotAdvance(t *testing.T) {
|
||||
stuck = true
|
||||
t.Fatalf("CompactByVolumeData: %v", err)
|
||||
}
|
||||
if !errors.Is(err, needle.ErrorCorrupted) {
|
||||
t.Fatalf("CompactByVolumeData error = %v, want one wrapping ErrorCorrupted", err)
|
||||
if err != nil {
|
||||
t.Fatalf("CompactByVolumeData: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(v.FileName(".cpx")); !os.IsNotExist(err) {
|
||||
t.Errorf("failed compaction left a compacted index: %v", err)
|
||||
cpx, err := os.Open(v.FileName(".cpx"))
|
||||
if err != nil {
|
||||
t.Fatalf("open .cpx: %v", err)
|
||||
}
|
||||
var kept []types.NeedleId
|
||||
if err := idx.WalkIndexFile(cpx, 0, func(key types.NeedleId, _ types.Offset, size types.Size) error {
|
||||
if size.IsValid() {
|
||||
kept = append(kept, key)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("walk .cpx: %v", err)
|
||||
}
|
||||
cpx.Close()
|
||||
if want := []types.NeedleId{1, 2}; !slices.Equal(kept, want) {
|
||||
t.Errorf("compacted index holds %v, want %v", kept, want)
|
||||
}
|
||||
datAfter, err := os.ReadFile(v.FileName(".dat"))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user