mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-08 15:41:15 +02:00
* 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
99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"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"
|
|
)
|
|
|
|
// VolumeFileScanner4RebuildIdx writes one .idx row per .dat record, in .dat
|
|
// append order, which is the shape the volume server's own writes leave behind.
|
|
type VolumeFileScanner4RebuildIdx struct {
|
|
writer *bufio.Writer
|
|
datSize int64
|
|
version needle.Version
|
|
}
|
|
|
|
func (scanner *VolumeFileScanner4RebuildIdx) VisitSuperBlock(superBlock super_block.SuperBlock) error {
|
|
return nil
|
|
}
|
|
|
|
func (scanner *VolumeFileScanner4RebuildIdx) ReadNeedleBody() bool {
|
|
return false
|
|
}
|
|
|
|
func (scanner *VolumeFileScanner4RebuildIdx) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
|
|
// Stop at the first thing that is not a record: an all-zero header is
|
|
// unwritten space, a negative size is a corrupt header, and a record
|
|
// reaching past the end of .dat is a torn append. Indexing any of them
|
|
// fabricates rows, and io.EOF here stops the walk before it advances by a
|
|
// bad size -- a negative one moves the offset backwards.
|
|
if (n.Size == 0 && n.Id == 0) || n.Size < 0 {
|
|
return io.EOF
|
|
}
|
|
if needleDiskEnd(types.ToOffset(offset), n.Size, scanner.version) > scanner.datSize {
|
|
return io.EOF
|
|
}
|
|
size := n.Size
|
|
if !size.IsValid() {
|
|
size = types.TombstoneFileSize
|
|
}
|
|
_, err := scanner.writer.Write(needle_map.ToBytes(n.Id, types.ToOffset(offset), size))
|
|
return err
|
|
}
|
|
|
|
// rebuildIdxFile regenerates the volume's .idx from its .dat. The whole index
|
|
// is derivable from the data file, so a volume whose index directory has no
|
|
// .idx -- a -dir.idx pointed at an empty directory, or a lost index -- comes
|
|
// back on its own instead of taking the volume server down. The rows go to a
|
|
// temp file that is renamed in, so an interrupted rebuild leaves no partial
|
|
// index behind.
|
|
func (v *Volume) rebuildIdxFile() error {
|
|
if v.DataBackend == nil {
|
|
return fmt.Errorf("volume %d has no data backend", v.Id)
|
|
}
|
|
|
|
datSize, _, err := v.DataBackend.GetStat()
|
|
if err != nil {
|
|
return fmt.Errorf("stat %s: %w", v.FileName(".dat"), err)
|
|
}
|
|
|
|
idxFileName := v.FileName(".idx")
|
|
if err := os.MkdirAll(filepath.Dir(idxFileName), 0755); err != nil {
|
|
return fmt.Errorf("create idx dir for %s: %w", idxFileName, err)
|
|
}
|
|
tmpFileName := idxFileName + ".tmp"
|
|
tmpFile, err := os.OpenFile(tmpFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("create %s: %w", tmpFileName, err)
|
|
}
|
|
defer os.Remove(tmpFileName)
|
|
|
|
scanner := &VolumeFileScanner4RebuildIdx{writer: bufio.NewWriter(tmpFile), datSize: datSize, version: v.Version()}
|
|
err = ScanVolumeFileFrom(scanner.version, v.DataBackend, int64(v.SuperBlock.BlockSize()), scanner)
|
|
if err == nil {
|
|
err = scanner.writer.Flush()
|
|
}
|
|
if err == nil {
|
|
err = tmpFile.Sync()
|
|
}
|
|
if closeErr := tmpFile.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("rebuild %s from %s: %w", idxFileName, v.FileName(".dat"), err)
|
|
}
|
|
|
|
if err := os.Rename(tmpFileName, idxFileName); err != nil {
|
|
return fmt.Errorf("rename %s: %w", tmpFileName, err)
|
|
}
|
|
return fsyncDir(filepath.Dir(idxFileName))
|
|
}
|