From 9efd3951fed7a0e8a3f7ad7b3c4f5ef90a2080bc Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 6 Dec 2026 00:44:46 -0800 Subject: [PATCH] storage: require the .ecx to fit the recorded source before trusting it A re-encode of a grown .dat interrupted under an earlier encode's .vif can leave partial shards that happen to be exactly as long as that earlier encode's full shards, and the length check alone then trusted the stale .vif and kept them. The re-encode writes the .ecx of the grown .dat before any shard, so it indexes needles past the recorded size, which no .ecx of a completed encode does. Trust the recorded size only when no .ecx beside the shards indexes past it; an unreadable .ecx or .dat keeps the shards. FindDatFileSize now shares the .ecx extent scan. --- weed/storage/disk_location_ec.go | 41 +++++++++++-- weed/storage/ec_startup_shard_safety_test.go | 63 +++++++++++++++++++- weed/storage/erasure_coding/ec_decoder.go | 19 +++--- 3 files changed, 110 insertions(+), 13 deletions(-) diff --git a/weed/storage/disk_location_ec.go b/weed/storage/disk_location_ec.go index ed88f33588..87b51dffd4 100644 --- a/weed/storage/disk_location_ec.go +++ b/weed/storage/disk_location_ec.go @@ -719,12 +719,15 @@ func (l *DiskLocation) validateEcVolume(collection string, vid needle.VolumeId) return false } // Shards that are exactly the full encode of the source size the .vif - // records were made from that source. A .dat of any other size is not it - // (e.g. a replica that kept taking writes), so its size says nothing about - // these shards: keep them, and leave the .dat unloaded so it cannot - // collide with the EC volume. + // records, under an .ecx indexing nothing past that size, were made from + // that source. A .dat of any other size is not it (e.g. a replica that + // kept taking writes), so its size says nothing about these shards: keep + // them, and leave the .dat unloaded so it cannot collide with the EC + // volume. The .ecx check catches a re-encode interrupted under an earlier + // encode's .vif: it writes the .ecx of the grown .dat before the shards. if recorded := encodeInfo.GetDatFileSize(); recorded > 0 && datSize != recorded && - actualShardSize == calculateExpectedShardSize(recorded, dataShards) { + actualShardSize == calculateExpectedShardSize(recorded, dataShards) && + !l.ecxIndexesPast(collection, vid, datFileName, recorded) { glog.Warningf("EC volume %d: .dat is %d bytes but the .vif records an encode source of %d bytes; keeping the EC shards and leaving the .dat in place untouched", vid, datSize, recorded) return true @@ -737,6 +740,34 @@ func (l *DiskLocation) validateEcVolume(collection string, vid needle.VolumeId) return true } +// ecxIndexesPast reports whether an .ecx of (collection, vid) in the data or +// idx dir indexes a live needle ending past size, sized with the needle +// version of the .dat beside it. A missing or unreadable file reports false: +// a doubt keeps the shards. +func (l *DiskLocation) ecxIndexesPast(collection string, vid needle.VolumeId, datFileName string, size int64) bool { + f, err := os.Open(datFileName) + if err != nil { + return false + } + var versionByte [1]byte + _, err = f.ReadAt(versionByte[:], 0) + f.Close() + if err != nil { + return false + } + version := needle.Version(versionByte[0]) + for _, dir := range [2]string{l.Directory, l.IdxDirectory} { + // A torn tail still reports the whole entries before it. + if extent, _ := erasure_coding.EcxNeedleExtent(erasure_coding.EcShardFileName(collection, dir, int(vid)), version); extent > size { + return true + } + if l.IdxDirectory == l.Directory { + break + } + } + return false +} + // ecEncodeVolumeInfo returns the .vif describing the encode of an EC volume: // the first with an EC shard config in the data dir, the idx dir, then the // sibling data dir the mount takes it from under a shared idx dir. Without diff --git a/weed/storage/ec_startup_shard_safety_test.go b/weed/storage/ec_startup_shard_safety_test.go index 2232a64919..58dd8f8b3a 100644 --- a/weed/storage/ec_startup_shard_safety_test.go +++ b/weed/storage/ec_startup_shard_safety_test.go @@ -8,7 +8,9 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding" "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" ) @@ -135,7 +137,8 @@ func TestLoadExistingVolume_ForeignDatBesideEcShardsKeepsBoth(t *testing.T) { writeEcVif(t, base, sourceSize) shardSize := calculateExpectedShardSize(sourceSize, erasure_coding.DataShardsCount) writeSizedFile(t, base+erasure_coding.ToExt(9), shardSize) - writeSizedFile(t, base+".ecx", 20) + // The .ecx indexes a needle ending exactly at the source size. + writeEcxEndingAt(t, base, sourceSize) writeSizedFile(t, base+".ecj", 0) // Grown past the source by later writes: its full encode would be larger // than the shard, which used to read as an interrupted local encode. @@ -271,3 +274,61 @@ func TestValidateEcVolume_ForeignDatKeepsShardsWithSiblingVif(t *testing.T) { t.Fatal("validateEcVolume ignored the sibling .vif's source size and would delete the shards") } } + +// writeEcxEndingAt writes an .ecx with one live needle ending at end in a +// current-version .dat. +func writeEcxEndingAt(t *testing.T, base string, end int64) { + t.Helper() + size := types.Size(1000) + offset := end - needle.GetActualSize(size, needle.GetCurrentVersion()) + entry := needle_map.ToBytes(1, types.ToOffset(offset), size) + if err := os.WriteFile(base+".ecx", entry, 0o644); err != nil { + t.Fatalf("write .ecx: %v", err) + } +} + +// writeSizedDat writes a .dat of size whose superblock names the current +// needle version. +func writeSizedDat(t *testing.T, path string, size int64) { + t.Helper() + writeSizedFile(t, path, size) + f, err := os.OpenFile(path, os.O_WRONLY, 0) + if err != nil { + t.Fatal(err) + } + defer f.Close() + if _, err := f.WriteAt([]byte{byte(needle.GetCurrentVersion())}, 0); err != nil { + t.Fatal(err) + } +} + +// A re-encode of a grown .dat interrupted under an earlier encode's .vif, +// with every partial shard exactly as long as that earlier encode's full +// shards. The re-encode wrote the .ecx of the grown .dat first, so it indexes +// needles past the recorded size: the shards are not that encode's, and the +// complete .dat is reclaimed. +func TestValidateEcVolume_InterruptedReEncodeMatchingStaleLengthReclaims(t *testing.T) { + l := newTestDiskLocation(t.TempDir()) + const vid = 79 + base := erasure_coding.EcShardFileName("", l.Directory, vid) + + oldSourceSize := int64(20 * 1024 * 1024) + datSize := int64(60 * 1024 * 1024) + writeSizedDat(t, base+".dat", datSize) + writeEcVif(t, base, oldSourceSize) + writeEcxEndingAt(t, base, datSize) + for i := 0; i < erasure_coding.TotalShardsCount; i++ { + writeSizedFile(t, base+erasure_coding.ToExt(i), calculateExpectedShardSize(oldSourceSize, erasure_coding.DataShardsCount)) + } + + if l.validateEcVolume("", vid) { + t.Fatal("validateEcVolume trusted a stale .vif for partial shards under an .ecx of the grown .dat") + } + + // The same shards under an .ecx that stays within the recorded size are + // that encode's: keep them. + writeEcxEndingAt(t, base, oldSourceSize) + if !l.validateEcVolume("", vid) { + t.Fatal("validateEcVolume dropped shards whose .ecx and length match the recorded source") + } +} diff --git a/weed/storage/erasure_coding/ec_decoder.go b/weed/storage/erasure_coding/ec_decoder.go index 96494c4a5a..c8d3df73e9 100644 --- a/weed/storage/erasure_coding/ec_decoder.go +++ b/weed/storage/erasure_coding/ec_decoder.go @@ -109,20 +109,25 @@ func FindDatFileSize(shard0FileName, indexBaseFileName string) (datSize int64, e // when all needles are deleted (see issue #7748). datSize = int64(super_block.SuperBlockSize) - err = iterateEcxFile(indexBaseFileName, func(key types.NeedleId, offset types.Offset, size types.Size) error { + extent, err := EcxNeedleExtent(indexBaseFileName, version) + if extent > datSize { + datSize = extent + } + return +} +// EcxNeedleExtent returns where the furthest live needle indexed by +// indexBaseFileName.ecx ends in the source .dat, or 0 when it indexes none. +func EcxNeedleExtent(indexBaseFileName string, version needle.Version) (extent int64, err error) { + err = iterateEcxFile(indexBaseFileName, func(key types.NeedleId, offset types.Offset, size types.Size) error { if size.IsDeleted() { return nil } - - entryStopOffset := offset.ToActualOffset() + needle.GetActualSize(size, version) - if datSize < entryStopOffset { - datSize = entryStopOffset + if end := offset.ToActualOffset() + needle.GetActualSize(size, version); end > extent { + extent = end } - return nil }) - return }