Files
seaweedfs/weed/storage/volume_idx_rebuild_test.go
Chris Lu 31fb46f693 volume: rebuild a missing .idx from the .dat (#11115)
* volume: rebuild a missing .idx from the .dat

Pointing -dir.idx at a directory that holds no index aborted the whole
volume server: checkIdxFile found no .idx and load() called glog.Fatalf.
Every row of the index is derivable from the .dat, so walk it in append
order and write the index back, which reproduces byte for byte what the
server's own writes had left in the old directory.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: keep the index co-located with the data in the Rust server

Go's load() drops back to the data directory when an .idx already sits
beside the .dat, so naming a --dir.idx does not strand a pre-existing
index. Rust had no such adjustment: it opened the new directory with
create, and the volume came up on an empty index with every needle
invisible.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: rebuild a missing .idx from the .dat in the Rust server

Mirrors the Go side. Rust did not abort on a missing index the way
checkIdxFile did; it opened the new directory with create and mounted the
volume on an empty index, so every needle read as missing while the .dat
still held the data. Walk the .dat in append order and write the index
back, byte for byte what the server's own writes had left behind.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: stop the idx rebuild at a zero-padded .dat tail

An all-zero needle header is unwritten space, not a record. Go's .dat walk
keeps reading past it and would index a truncated data file's tail as
millions of needle 0 rows; the Rust walk already stops there. Stop the Go
rebuild at the same place.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: create the -dir.idx directory when it does not exist

Rust's DiskLocation creates the index directory as it takes it; Go only
resolved the path, so naming a directory that does not exist yet left every
volume unable to open or rebuild its index and took the server down.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: stop the idx rebuild at a torn .dat record

A crash between writing a needle's header and its body leaves a record
whose declared size runs past the end of .dat. Indexing it puts a row in
the .idx that points at bytes that do not exist, which fails every read of
that needle and trips the past-EOF check on the next load. Stop at the
first record that does not fit, in both servers.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: stop the idx rebuild at a negative-size header

A corrupt header whose size field is negative makes the .dat walk advance
backwards: NeedleBodyLength adds the negative size, so the next offset is
lower than the current one. The Go walk then reads at a negative offset and
the rebuild fails, which puts the volume server right back to exiting at
startup; the Rust walk seeks past EOF and truncates the index instead.
A negative size is never a record, so stop there.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: skip a volume whose index cannot be rebuilt, do not exit

glog.Fatalf calls os.Exit(255), so a rebuild that could not write -- a full
or read-only index directory -- put the server right back to dying at
startup for one bad volume. Return the error instead: loadExistingVolume
logs it and skips that volume, which is what the remote-volume branch just
above already does and what the Rust loader has always done.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* volume: create the index directory from the rebuild too

The rebuild is the first thing to write into a fresh -dir.idx, and it runs
before the loaders that create the directory on their way to opening .idx.
Create it in both rebuilds so the ordering does not matter.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ

* ci: let codespell past the sme variable in the mount tests

weedfs_stream_mutate_error_test.go names its *streamMutateError local
sme, which codespell reads as a misspelling of same/some. It is an
identifier, so exempt it beside the other variable-name entries.

Claude-Session: https://claude.ai/code/session_01BYrb2AdJSckq9FdHuqseDJ
2026-09-03 08:43:10 -07:00

266 lines
8.8 KiB
Go

package storage
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
)
// Pointing -dir.idx at a directory with no .idx used to abort the whole volume
// server in checkIdxFile; the index is derivable from the .dat, so it must be
// rebuilt in place instead.
func TestLoad_MovedIdxDirectory_RebuildsIdx(t *testing.T) {
root := t.TempDir()
dataDir := filepath.Join(root, "data")
oldIdxDir := filepath.Join(root, "idxA")
newIdxDir := filepath.Join(root, "idxB")
for _, dir := range []string{dataDir, oldIdxDir, newIdxDir} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir %s: %v", dir, err)
}
}
v, err := NewVolume(dataDir, oldIdxDir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
for id := uint64(1); id <= 3; id++ {
if _, _, _, err := v.writeNeedle2(newRandomNeedle(id), true, false, false); err != nil {
t.Fatalf("seed write %d: %v", id, err)
}
}
if _, err := v.deleteNeedle2(newRandomNeedle(2)); err != nil {
t.Fatalf("seed delete: %v", err)
}
wantCount, wantDeleted := v.nm.FileCount(), v.nm.DeletedCount()
v.Close()
if _, err := os.Stat(filepath.Join(oldIdxDir, "1.idx")); err != nil {
t.Fatalf("seeded idx should live in the old idx dir: %v", err)
}
v2, err := NewVolume(dataDir, newIdxDir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("reload against the new idx dir: %v", err)
}
defer v2.Close()
if _, err := os.Stat(filepath.Join(newIdxDir, "1.idx")); err != nil {
t.Fatalf("idx not rebuilt in the new idx dir: %v", err)
}
if got := v2.nm.FileCount(); got != wantCount {
t.Errorf("file count = %d, want %d", got, wantCount)
}
if got := v2.nm.DeletedCount(); got != wantDeleted {
t.Errorf("deleted count = %d, want %d", got, wantDeleted)
}
old, err := os.ReadFile(filepath.Join(oldIdxDir, "1.idx"))
if err != nil {
t.Fatalf("read the seeded idx: %v", err)
}
rebuilt, err := os.ReadFile(filepath.Join(newIdxDir, "1.idx"))
if err != nil {
t.Fatalf("read the rebuilt idx: %v", err)
}
if !bytes.Equal(old, rebuilt) {
t.Errorf("rebuilt idx (%d bytes) differs from the one the server wrote (%d bytes)", len(rebuilt), len(old))
}
if v2.noWriteOrDelete {
t.Errorf("volume marked read-only after the rebuild")
}
}
// A .dat padded with zeros must not be indexed as needle 0 rows: the walk stops
// where the records do.
func TestRebuildIdx_StopsAtZeroPaddedDatTail(t *testing.T) {
dir := t.TempDir()
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(1), true, false, false); err != nil {
t.Fatalf("seed write: %v", err)
}
v.Close()
base := VolumeFileName(dir, "", 1)
seeded, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the seeded idx: %v", err)
}
datSize, err := os.Stat(base + ".dat")
if err != nil {
t.Fatalf("stat dat: %v", err)
}
if err := os.Truncate(base+".dat", datSize.Size()+4096); err != nil {
t.Fatalf("pad dat: %v", err)
}
if err := os.Remove(base + ".idx"); err != nil {
t.Fatalf("drop idx: %v", err)
}
v2, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("reload: %v", err)
}
defer v2.Close()
rebuilt, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the rebuilt idx: %v", err)
}
if !bytes.Equal(seeded, rebuilt) {
t.Errorf("rebuilt idx has %d bytes, want the %d the server wrote", len(rebuilt), len(seeded))
}
}
// A .dat whose last append was torn mid-body must not gain an index row that
// points past the end of the file.
func TestRebuildIdx_SkipsTruncatedDatTail(t *testing.T) {
dir := t.TempDir()
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(1), true, false, false); err != nil {
t.Fatalf("seed write: %v", err)
}
base := VolumeFileName(dir, "", 1)
kept, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the seeded idx: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(2), true, false, false); err != nil {
t.Fatalf("seed torn write: %v", err)
}
v.Close()
// Chop the second needle's body, leaving its header intact.
datSize, err := os.Stat(base + ".dat")
if err != nil {
t.Fatalf("stat dat: %v", err)
}
if err := os.Truncate(base+".dat", datSize.Size()-8); err != nil {
t.Fatalf("tear dat: %v", err)
}
if err := os.Remove(base + ".idx"); err != nil {
t.Fatalf("drop idx: %v", err)
}
v2, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("reload: %v", err)
}
defer v2.Close()
rebuilt, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the rebuilt idx: %v", err)
}
if !bytes.Equal(kept, rebuilt) {
t.Errorf("rebuilt idx has %d bytes, want the %d covering only the intact needle", len(rebuilt), len(kept))
}
if maxEnd := v2.nm.MaxNeedleEnd(); maxEnd > datSize.Size()-8 {
t.Errorf("rebuilt idx reaches %d, past the %d-byte .dat", maxEnd, datSize.Size()-8)
}
}
// A corrupt header carrying a negative size advances the .dat walk backwards,
// which cycles forever between it and the record before it.
func TestRebuildIdx_StopsAtNegativeSizeHeader(t *testing.T) {
dir := t.TempDir()
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(1), true, false, false); err != nil {
t.Fatalf("seed write: %v", err)
}
base := VolumeFileName(dir, "", 1)
kept, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the seeded idx: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(2), true, false, false); err != nil {
t.Fatalf("seed second write: %v", err)
}
nv, ok := v.nm.Get(2)
if !ok {
t.Fatalf("second needle missing from the index")
}
corruptAt := nv.Offset.ToActualOffset()
v.Close()
// Overwrite the second needle's size field with a negative int32.
dat, err := os.OpenFile(base+".dat", os.O_WRONLY, 0644)
if err != nil {
t.Fatalf("open dat: %v", err)
}
if _, err := dat.WriteAt([]byte{0xff, 0xff, 0xf0, 0x00}, corruptAt+types.CookieSize+types.NeedleIdSize); err != nil {
t.Fatalf("corrupt size field: %v", err)
}
dat.Close()
if err := os.Remove(base + ".idx"); err != nil {
t.Fatalf("drop idx: %v", err)
}
// Pre-fix the rebuild walked backwards from here and never terminated.
v2, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("reload: %v", err)
}
defer v2.Close()
rebuilt, err := os.ReadFile(base + ".idx")
if err != nil {
t.Fatalf("read the rebuilt idx: %v", err)
}
if !bytes.Equal(kept, rebuilt) {
t.Errorf("rebuilt idx has %d bytes, want the %d covering only the intact needle", len(rebuilt), len(kept))
}
}
// A rebuild that cannot write skips just this volume; it used to call
// glog.Fatalf and take the whole volume server down with it.
func TestRebuildIdx_UnwritableIdxDirSkipsOnlyThisVolume(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root ignores directory permissions")
}
root := t.TempDir()
dataDir := filepath.Join(root, "data")
idxDir := filepath.Join(root, "idx")
for _, dir := range []string{dataDir, idxDir} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir %s: %v", dir, err)
}
}
v, err := NewVolume(dataDir, dataDir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
if err != nil {
t.Fatalf("create volume: %v", err)
}
if _, _, _, err := v.writeNeedle2(newRandomNeedle(1), true, false, false); err != nil {
t.Fatalf("seed write: %v", err)
}
v.Close()
if err := os.Remove(VolumeFileName(dataDir, "", 1) + ".idx"); err != nil {
t.Fatalf("drop idx: %v", err)
}
if err := os.Chmod(idxDir, 0555); err != nil {
t.Fatalf("seal idx dir: %v", err)
}
defer os.Chmod(idxDir, 0755)
if _, err := NewVolume(dataDir, idxDir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0); err == nil {
t.Errorf("expected a load error for an unwritable idx dir")
}
}