mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
fix/compact: stop at records past EOF, report -ignoreError failures, clean up failed compaction (#12915)
* fix: do not index a record that runs past the end of the .dat A truncated .dat, or a header whose size points past the file end, left weed fix indexing a needle the volume cannot serve. Stop the scan at such a record instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: exit non-zero under -ignoreError when a volume fails -ignoreError kept going but always exited 0, so an empty or partial index looked like success. Record the failure in the exit status, and keep the existing .idx when the super block could not be read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * compact: remove the partial .cpd/.cpx when compaction fails A failed weed compact exited through glog.Fatalf and left a partial .cpd next to the volume. Remove both outputs and report the failure in the exit status instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: skip the end-of-file check for a volume on a remote tier The scan reads the remote object when the .vif lists one, so a retained local .dat is the wrong length to check records against. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep a key deleted when its last record is truncated Stopping before a truncated delete record left the key's earlier live record in the index, so a deleted file came back. Drop the key instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: return the .idx open error from SaveToIdx The bare return gave back the nil named result, so a failure to open the index looked like a successful save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: exit non-zero when a record is cut off by the end of the .dat A record past EOF ends the scan like a bad header does, so report it the same way while keeping the index of the records before it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * compact: close the volume before removing a failed compaction's output Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: remove a failed .idx only after SaveToIdx opened it An open failure no longer reaches the caller's cleanup, so an existing .idx that could not be opened is left in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(s3vectors): drop the worker and CLI flags #12906 removed (#12911) * test(s3vectors): start weed-worker with only --admin weed-worker no longer accepts --namespace, --masters or --filer-group; it takes the Lance Namespace and the filers from admin. The worker jobs passed all three, so the worker exited on its arguments before registering. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(s3vectors): let weed-vector-cli find the namespace through the masters weed-vector-cli dropped --namespace and looks the Lance Namespace up among the gateways the masters list, so the index job test failed on its arguments once the worker could register. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a98047e2a
commit
c7cf9db27c
4 files changed
+293
-10
No files matched your search
+13
-6
@@ -1,6 +1,7 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
@@ -56,16 +57,22 @@ func runCompact(cmd *Command, args []string) bool {
|
||||
}
|
||||
switch strings.ToLower(*compactMethod) {
|
||||
case "data":
|
||||
if err = v.CompactByVolumeData(opts); err != nil {
|
||||
glog.Fatalf("Compact Volume [ERROR] %s\n", err)
|
||||
}
|
||||
err = v.CompactByVolumeData(opts)
|
||||
case "index":
|
||||
if err = v.CompactByIndex(opts); err != nil {
|
||||
glog.Fatalf("Compact Volume [ERROR] %s\n", err)
|
||||
}
|
||||
err = v.CompactByIndex(opts)
|
||||
default:
|
||||
glog.Fatalf("unsupported compaction method %q", *compactMethod)
|
||||
}
|
||||
if err != nil {
|
||||
v.Close()
|
||||
for _, ext := range []string{".cpd", ".cpx"} {
|
||||
if rmErr := os.Remove(v.FileName(ext)); rmErr != nil && !os.IsNotExist(rmErr) {
|
||||
glog.Errorf("remove %s: %v", v.FileName(ext), rmErr)
|
||||
}
|
||||
}
|
||||
glog.Errorf("Compact Volume [ERROR] %s", err)
|
||||
SetCommandExitStatus(1)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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))
|
||||
|
||||
savedDir, savedCollection, savedId, savedMethod, savedStatus := *compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus
|
||||
defer func() {
|
||||
*compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus = savedDir, savedCollection, savedId, savedMethod, savedStatus
|
||||
}()
|
||||
*compactVolumePath, *compactVolumeCollection, *compactVolumeId, *compactMethod, commandExitStatus = dir, "fix", 7, "data", 0
|
||||
|
||||
runCompact(cmdCompact, nil)
|
||||
|
||||
if CommandExitStatus() == 0 {
|
||||
t.Error("expected a non-zero exit status")
|
||||
}
|
||||
for _, ext := range []string{".cpd", ".cpx"} {
|
||||
if _, err := os.Stat(base + ext); !os.IsNotExist(err) {
|
||||
t.Errorf("%s left behind: %v", ext, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
+39
-4
@@ -33,6 +33,8 @@ var cmdFix = &Command{
|
||||
Long: `Fix runs the SeaweedFS fix command on local dat files ( or remote files) or whole folders to re-create the index .idx file. If fixing remote files, you need to synchronize master.toml to the same directory on the current node as on the master node.
|
||||
You Need to stop the volume server when running this command.
|
||||
Use -ecx to rebuild a lost EC index (.ecx) — and the .vif when missing — from the local .ec## shards.
|
||||
On a damaged .dat, fix indexes only the needles before the first unreadable header or a record cut off by the end of the file, and exits non-zero; repair from a replica, or run fix on a copy, before compacting or loading the volume.
|
||||
With -ignoreError, fix keeps going and writes what it could index, but still exits non-zero if any volume failed.
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -50,6 +52,7 @@ var (
|
||||
|
||||
type VolumeFileScanner4Fix struct {
|
||||
version needle.Version
|
||||
datSize int64
|
||||
nm *needle_map.MemDb
|
||||
nmDeleted *needle_map.MemDb
|
||||
includeDeleted bool
|
||||
@@ -66,6 +69,15 @@ func (scanner *VolumeFileScanner4Fix) ReadNeedleBody() bool {
|
||||
|
||||
func (scanner *VolumeFileScanner4Fix) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
|
||||
glog.V(2).Infof("key %v offset %d size %d disk_size %d compressed %v", n.Id, offset, n.Size, n.DiskSize(scanner.version), n.IsCompressed())
|
||||
if scanner.datSize > 0 && offset+n.DiskSize(scanner.version) > scanner.datSize {
|
||||
glog.Errorf("needle %v at offset %d needs %d bytes but the .dat ends at %d; not indexing it", n.Id, offset, n.DiskSize(scanner.version), scanner.datSize)
|
||||
SetCommandExitStatus(1)
|
||||
// The key was rewritten or deleted here, so an older record must not become current again.
|
||||
if pe := scanner.nm.Delete(n.Id); pe != nil {
|
||||
return fmt.Errorf("delete %v with error %v", n.Id, pe)
|
||||
}
|
||||
return io.EOF
|
||||
}
|
||||
if n.Size.IsValid() {
|
||||
if pe := scanner.nm.Set(n.Id, types.ToOffset(offset), n.Size); pe != nil {
|
||||
return fmt.Errorf("saved %d with error %v", n.Size, pe)
|
||||
@@ -186,10 +198,15 @@ func fixEcxFromShardsInDir(basePath string, files []fs.DirEntry) bool {
|
||||
func SaveToIdx(scaner *VolumeFileScanner4Fix, idxName string) (ret error) {
|
||||
idxFile, err := os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
idxFile.Close()
|
||||
if ret != nil && !*fixIgnoreError {
|
||||
if err := os.Remove(idxName); err != nil {
|
||||
glog.Errorf("failed to cleanup file %s:%v", idxName, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Emit entries in .dat offset (append) order so the .idx stays the
|
||||
@@ -222,6 +239,18 @@ func SaveToIdx(scaner *VolumeFileScanner4Fix, idxName string) (ret error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// localDatSize is the length of the .dat the scan reads, or 0 when the volume
|
||||
// is read from a remote tier.
|
||||
func localDatSize(basepath, baseFileName string) int64 {
|
||||
if _, hasRemoteFile, _, _ := volume_info.MaybeLoadVolumeInfo(path.Join(basepath, baseFileName+".vif")); hasRemoteFile {
|
||||
return 0
|
||||
}
|
||||
if info, err := os.Stat(path.Join(basepath, baseFileName+".dat")); err == nil {
|
||||
return info.Size()
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64, fixIncludeDeleted bool) {
|
||||
indexFileName := path.Join(basepath, baseFileName+".idx")
|
||||
|
||||
@@ -235,6 +264,7 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol
|
||||
err := fmt.Errorf("volume ID out of range: %d", volumeId)
|
||||
if *fixIgnoreError {
|
||||
glog.Error(err)
|
||||
SetCommandExitStatus(1)
|
||||
return
|
||||
} else {
|
||||
glog.Fatal(err)
|
||||
@@ -248,11 +278,17 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol
|
||||
nmDeleted: nmDeleted,
|
||||
includeDeleted: fixIncludeDeleted,
|
||||
}
|
||||
scanner.datSize = localDatSize(basepath, baseFileName)
|
||||
|
||||
if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
|
||||
err := fmt.Errorf("scan .dat File: %w", err)
|
||||
if *fixIgnoreError {
|
||||
glog.Error(err)
|
||||
SetCommandExitStatus(1)
|
||||
// Without a super block nothing was scanned; keep the existing .idx.
|
||||
if scanner.version == 0 {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
glog.Fatal(err)
|
||||
}
|
||||
@@ -262,10 +298,8 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol
|
||||
err := fmt.Errorf("save to .idx File: %w", err)
|
||||
if *fixIgnoreError {
|
||||
glog.Error(err)
|
||||
SetCommandExitStatus(1)
|
||||
} else {
|
||||
if err := os.Remove(indexFileName); err != nil {
|
||||
glog.Errorf("failed to cleanup file %s:%v", indexFileName, err)
|
||||
}
|
||||
glog.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -286,6 +320,7 @@ func doFixEcxFromShards(basePath, baseFileName, collection string, volumeId int6
|
||||
fail := func(err error) {
|
||||
if *fixIgnoreError {
|
||||
glog.Error(err)
|
||||
SetCommandExitStatus(1)
|
||||
} else {
|
||||
glog.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
||||
"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/needle_map"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/volume_info"
|
||||
)
|
||||
|
||||
// TestSaveToIdxWritesOffsetOrder guards that weed fix rebuilds the .idx as an
|
||||
@@ -73,3 +80,206 @@ func TestSaveToIdxWritesOffsetOrder(t *testing.T) {
|
||||
t.Errorf("last entry should be the .dat-tail tombstone (key 5), got key %v", lastKey)
|
||||
}
|
||||
}
|
||||
|
||||
// writeFixTestVolume writes a .dat holding needles 1..count and returns its
|
||||
// base path and the offset of each needle, in key order.
|
||||
func writeFixTestVolume(t *testing.T, dir string, count int) (string, []int64) {
|
||||
t.Helper()
|
||||
base := filepath.Join(dir, "fix_7")
|
||||
version := needle.GetCurrentVersion()
|
||||
f, err := os.OpenFile(base+".dat", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
datBackend := backend.NewDiskFile(f)
|
||||
defer datBackend.Close()
|
||||
sb := super_block.SuperBlock{Version: version, ReplicaPlacement: &super_block.ReplicaPlacement{}, Ttl: &needle.TTL{}}
|
||||
if _, err := datBackend.WriteAt(sb.Bytes(), 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var offsets []int64
|
||||
for i := 1; i <= count; i++ {
|
||||
n := &needle.Needle{Id: types.Uint64ToNeedleId(uint64(i)), Data: bytes.Repeat([]byte{byte(i)}, 1000*i)}
|
||||
n.Checksum = needle.NewCRC(n.Data)
|
||||
offset, _, _, err := n.Append(datBackend, version)
|
||||
if err != nil {
|
||||
t.Fatalf("append needle %d: %v", i, err)
|
||||
}
|
||||
offsets = append(offsets, int64(offset))
|
||||
}
|
||||
return base, offsets
|
||||
}
|
||||
|
||||
func indexedKeys(t *testing.T, idxPath string) []uint64 {
|
||||
t.Helper()
|
||||
f, err := os.Open(idxPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open idx: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
var keys []uint64
|
||||
if err := idx.WalkIndexFile(f, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error {
|
||||
keys = append(keys, uint64(key))
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("walk idx: %v", err)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func overwriteAt(t *testing.T, path string, offset int64, b []byte) {
|
||||
t.Helper()
|
||||
f, err := os.OpenFile(path, os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := f.WriteAt(b, offset); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixSkipsRecordPastEndOfDat(t *testing.T) {
|
||||
savedStatus := commandExitStatus
|
||||
defer func() { commandExitStatus = savedStatus }()
|
||||
sizeAt := types.NeedleIdSize + types.CookieSize
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
corrupt func(t *testing.T, dat string, off int64)
|
||||
}{
|
||||
{"truncated", func(t *testing.T, dat string, off int64) {
|
||||
if err := os.Truncate(dat, off+100); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"huge size", func(t *testing.T, dat string, off int64) {
|
||||
overwriteAt(t, dat, off+int64(sizeAt), []byte{0x7f, 0xff, 0xff, 0xf0})
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, offsets := writeFixTestVolume(t, dir, 5)
|
||||
tc.corrupt(t, base+".dat", offsets[4])
|
||||
commandExitStatus = 0
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
||||
if CommandExitStatus() == 0 {
|
||||
t.Error("expected a non-zero exit status")
|
||||
}
|
||||
if got := indexedKeys(t, base+".idx"); !slices.Equal(got, []uint64{1, 2, 3, 4}) {
|
||||
t.Errorf("indexed keys %v, want [1 2 3 4]", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixIgnoreErrorReportsIncompleteScan(t *testing.T) {
|
||||
savedIgnore, savedStatus := *fixIgnoreError, commandExitStatus
|
||||
*fixIgnoreError = true
|
||||
defer func() { *fixIgnoreError, commandExitStatus = savedIgnore, savedStatus }()
|
||||
|
||||
t.Run("bad header", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, offsets := writeFixTestVolume(t, dir, 5)
|
||||
overwriteAt(t, base+".dat", offsets[2], bytes.Repeat([]byte{0xa5}, 16))
|
||||
commandExitStatus = 0
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, false)
|
||||
if CommandExitStatus() == 0 {
|
||||
t.Error("expected a non-zero exit status for a scan that stopped before the end of the .dat")
|
||||
}
|
||||
if got := indexedKeys(t, base+".idx"); !slices.Equal(got, []uint64{1, 2}) {
|
||||
t.Errorf("indexed keys %v, want [1 2]", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("bad super block keeps the existing index", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, _ := writeFixTestVolume(t, dir, 3)
|
||||
overwriteAt(t, base+".dat", 0, bytes.Repeat([]byte{0xa5}, 8))
|
||||
if err := os.WriteFile(base+".idx", []byte("previous index"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commandExitStatus = 0
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, true)
|
||||
if CommandExitStatus() == 0 {
|
||||
t.Error("expected a non-zero exit status for an unreadable super block")
|
||||
}
|
||||
if b, err := os.ReadFile(base + ".idx"); err != nil || string(b) != "previous index" {
|
||||
t.Errorf("existing .idx changed: %q, %v", b, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalDatSizeIgnoresRemoteTier(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
base, _ := writeFixTestVolume(t, dir, 2)
|
||||
info, err := os.Stat(base + ".dat")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := localDatSize(dir, "fix_7"); got != info.Size() {
|
||||
t.Fatalf("localDatSize = %d, want %d", got, info.Size())
|
||||
}
|
||||
vi := &volume_server_pb.VolumeInfo{Files: []*volume_server_pb.RemoteFile{{BackendType: "s3", Key: "fix_7.dat", FileSize: uint64(info.Size()) * 2}}}
|
||||
if err := volume_info.SaveVolumeInfo(base+".vif", vi); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := localDatSize(dir, "fix_7"); got != 0 {
|
||||
t.Errorf("localDatSize with a remote .dat = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixTruncatedDeleteKeepsKeyDeleted(t *testing.T) {
|
||||
savedStatus := commandExitStatus
|
||||
defer func() { commandExitStatus = savedStatus }()
|
||||
dir := t.TempDir()
|
||||
base, _ := writeFixTestVolume(t, dir, 3)
|
||||
f, err := os.OpenFile(base+".dat", os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
datBackend := backend.NewDiskFile(f)
|
||||
deletion := &needle.Needle{Id: types.Uint64ToNeedleId(2)}
|
||||
offset, _, _, err := deletion.Append(datBackend, needle.GetCurrentVersion())
|
||||
datBackend.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Truncate(base+".dat", int64(offset)+types.NeedleHeaderSize+2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, includeDeleted := range []bool{true, false} {
|
||||
doFixOneVolume(dir, "fix_7", "fix", 7, includeDeleted)
|
||||
f, err := os.Open(base + ".idx")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
live := map[uint64]bool{}
|
||||
err = idx.WalkIndexFile(f, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error {
|
||||
live[uint64(key)] = !offset.IsZero() && size.IsValid()
|
||||
return nil
|
||||
})
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if live[2] || !live[1] || !live[3] {
|
||||
t.Errorf("includeDeleted=%v: live keys %v, want 1 and 3 only", includeDeleted, live)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveToIdxReportsOpenError(t *testing.T) {
|
||||
nm := needle_map.NewMemDb()
|
||||
defer nm.Close()
|
||||
idxPath := filepath.Join(t.TempDir(), "fix_7.idx")
|
||||
if err := os.Mkdir(idxPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SaveToIdx(&VolumeFileScanner4Fix{nm: nm}, idxPath); err == nil {
|
||||
t.Error("expected an error when the .idx path cannot be opened")
|
||||
}
|
||||
if _, err := os.Stat(idxPath); err != nil {
|
||||
t.Errorf("a failed open must leave the existing .idx path alone: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user