Files
seaweedfs/weed/command/fix_test.go
T
Chris LuandClaude Opus 5.5 c7cf9db27c 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>
2026-11-23 13:58:26 -08:00

286 lines
9.1 KiB
Go

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
// append-ordered log (sorted by .dat offset), not sorted by key. A key-sorted
// .idx puts the highest-key needle last instead of the .dat-tail needle, which
// flipped volumes read-only on load (issue #9688). It must also carry every
// tombstone — including one whose live needle is gone — so the last entry is
// the real .dat tail.
func TestSaveToIdxWritesOffsetOrder(t *testing.T) {
nm := needle_map.NewMemDb()
defer nm.Close()
nmDeleted := needle_map.NewMemDb()
defer nmDeleted.Close()
// Live needles with high keys at low offsets (as if written first).
for _, e := range []struct {
key uint64
offset int64
}{{30, 8}, {20, 128}, {10, 256}} {
if err := nm.Set(types.Uint64ToNeedleId(e.key), types.ToOffset(e.offset), types.Size(100)); err != nil {
t.Fatalf("nm.Set: %v", err)
}
}
// A tombstone at the .dat tail whose live needle is gone; the old SaveToIdx
// dropped these because the key was absent from the live map.
if err := nmDeleted.Set(types.Uint64ToNeedleId(5), types.ToOffset(384), types.TombstoneFileSize); err != nil {
t.Fatalf("nmDeleted.Set: %v", err)
}
scanner := &VolumeFileScanner4Fix{nm: nm, nmDeleted: nmDeleted, includeDeleted: true}
idxPath := filepath.Join(t.TempDir(), "v.idx")
if err := SaveToIdx(scanner, idxPath); err != nil {
t.Fatalf("SaveToIdx: %v", err)
}
f, err := os.Open(idxPath)
if err != nil {
t.Fatalf("open idx: %v", err)
}
defer f.Close()
var offsets []int64
var lastKey types.NeedleId
if err := idx.WalkIndexFile(f, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error {
offsets = append(offsets, offset.ToActualOffset())
lastKey = key
return nil
}); err != nil {
t.Fatalf("walk idx: %v", err)
}
if len(offsets) != 4 {
t.Fatalf("expected 4 entries (3 live + 1 tombstone), got %d", len(offsets))
}
for i := 1; i < len(offsets); i++ {
if offsets[i] < offsets[i-1] {
t.Fatalf("entries not in offset order: %v", offsets)
}
}
// The .dat-tail needle (the orphan tombstone at the highest offset) must be last.
if lastKey != types.Uint64ToNeedleId(5) {
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)
}
}