diff --git a/weed/storage/disk_location_ec.go b/weed/storage/disk_location_ec.go index f08bf0cc5a..ac66d27c84 100644 --- a/weed/storage/disk_location_ec.go +++ b/weed/storage/disk_location_ec.go @@ -667,12 +667,13 @@ func (l *DiskLocation) validateEcVolume(collection string, vid needle.VolumeId) // On-disk .dat size, or -1 when absent (an empty <= superblock .dat is a // stub). A transient stat error keeps the shards rather than deleting. - var expectedShardSize int64 = -1 + var datSize, expectedShardSize int64 = -1, -1 datExists := false if datFileInfo, err := os.Stat(datFileName); err == nil { if datFileInfo.Size() > int64(super_block.SuperBlockSize) { datExists = true - expectedShardSize = calculateExpectedShardSize(datFileInfo.Size(), dataShards) + datSize = datFileInfo.Size() + expectedShardSize = calculateExpectedShardSize(datSize, dataShards) } } else if !os.IsNotExist(err) { glog.Warningf("EC volume %d: cannot stat .dat %s (%v); keeping EC shards", vid, datFileName, err) @@ -712,6 +713,17 @@ func (l *DiskLocation) validateEcVolume(collection string, vid needle.VolumeId) if shardCount == 0 { 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. + if recorded := l.ecRecordedDatFileSize(collection, vid); recorded > 0 && datSize != recorded && + actualShardSize == calculateExpectedShardSize(recorded, dataShards) { + 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 + } if expectedShardSize > 0 && actualShardSize > 0 && actualShardSize < expectedShardSize { glog.Warningf("EC volume %d: %d shards of %d bytes are smaller than the .dat's full encode (%d bytes); reclaiming the complete .dat", vid, shardCount, actualShardSize, expectedShardSize) @@ -751,6 +763,21 @@ func (l *DiskLocation) ecDataShardsFromVif(collection string, vid needle.VolumeI return erasure_coding.DataShardsCount } +// ecRecordedDatFileSize returns the encode-source .dat size recorded in the +// volume's .vif in the data dir or the idx dir, or 0 when none records it. +func (l *DiskLocation) ecRecordedDatFileSize(collection string, vid needle.VolumeId) int64 { + for _, dir := range [2]string{l.Directory, l.IdxDirectory} { + vifName := erasure_coding.EcShardFileName(collection, dir, int(vid)) + ".vif" + if vi, _, found, _ := volume_info.MaybeLoadVolumeInfo(vifName); found && vi.DatFileSize > 0 { + return vi.DatFileSize + } + if l.IdxDirectory == l.Directory { + break + } + } + return 0 +} + // ecDataShardsFromVifDir returns the .vif EcShardConfig data-shard count for // (collection, vid) under dir, or 0 when absent / not custom. func ecDataShardsFromVifDir(collection, dir string, vid needle.VolumeId) int { diff --git a/weed/storage/ec_startup_shard_safety_test.go b/weed/storage/ec_startup_shard_safety_test.go index ef6deeb5bf..28b6dde519 100644 --- a/weed/storage/ec_startup_shard_safety_test.go +++ b/weed/storage/ec_startup_shard_safety_test.go @@ -5,8 +5,11 @@ import ( "path/filepath" "testing" + "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/super_block" + "github.com/seaweedfs/seaweedfs/weed/storage/volume_info" ) func writeSizedFile(t *testing.T, path string, size int64) { @@ -101,3 +104,142 @@ func TestValidateEcVolume_TransientStatErrorKeeps(t *testing.T) { t.Fatal("validateEcVolume returned delete on a transient stat error; must keep on ambiguity") } } + +func writeEcVif(t *testing.T, base string, datFileSize int64) { + t.Helper() + vi := &volume_server_pb.VolumeInfo{ + Version: uint32(needle.GetCurrentVersion()), + DatFileSize: datFileSize, + EcShardConfig: &volume_server_pb.EcShardConfig{ + DataShards: uint32(erasure_coding.DataShardsCount), + ParityShards: uint32(erasure_coding.ParityShardsCount), + }, + } + if err := volume_info.SaveVolumeInfo(base+".vif", vi); err != nil { + t.Fatalf("save .vif: %v", err) + } +} + +// A .dat larger than the source the .vif records sits beside a full-size +// shard of that encode: it is not the shards' source, so the shards stay, the +// .dat is not loaded, and nothing on disk is touched. +func TestLoadExistingVolume_ForeignDatBesideEcShardsKeepsBoth(t *testing.T) { + store := newTestStore(t, 1) + l := store.Locations[0] + const vid = needle.VolumeId(74) + mountTestVolume(t, l, vid, "") + l.UnloadVolume(vid) + base := erasure_coding.EcShardFileName("", l.Directory, int(vid)) + + sourceSize := int64(30 * 1024 * 1024) + writeEcVif(t, base, sourceSize) + shardSize := calculateExpectedShardSize(sourceSize, erasure_coding.DataShardsCount) + writeSizedFile(t, base+erasure_coding.ToExt(9), shardSize) + writeSizedFile(t, base+".ecx", 20) + 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. + foreignSize := sourceSize + 10*1024*1024 + if calculateExpectedShardSize(foreignSize, erasure_coding.DataShardsCount) <= shardSize { + t.Fatal("test setup: the foreign .dat must encode to larger shards") + } + if err := os.Truncate(base+".dat", foreignSize); err != nil { + t.Fatal(err) + } + + want := map[string]int64{} + for _, ext := range []string{".dat", ".idx", ".vif", ".ecx", ".ecj", erasure_coding.ToExt(9)} { + fi, err := os.Stat(base + ext) + if err != nil { + t.Fatalf("stat %s: %v", ext, err) + } + want[ext] = fi.Size() + } + vifBefore, err := os.ReadFile(base + ".vif") + if err != nil { + t.Fatal(err) + } + + if !l.validateEcVolume("", vid) { + t.Fatal("validateEcVolume would delete shards whose .vif names a different source size than the .dat beside them") + } + l.loadExistingVolume(filepath.Base(base)+".idx", NeedleMapInMemory, true, 0, 0, false) + if _, found := l.FindVolume(vid); found { + t.Fatal("a .dat that is not the encode source was loaded beside its EC shards") + } + for ext, size := range want { + fi, err := os.Stat(base + ext) + if err != nil { + t.Fatalf("%s is gone: %v", ext, err) + } + if fi.Size() != size { + t.Fatalf("%s size changed: %d -> %d", ext, size, fi.Size()) + } + } + if vifAfter, _ := os.ReadFile(base + ".vif"); string(vifAfter) != string(vifBefore) { + t.Fatal(".vif was rewritten") + } +} + +// The .dat matches the recorded source size and the shards are short of its +// full encode: an interrupted local encode, still reclaimed. +func TestValidateEcVolume_InterruptedEncodeWithRecordedSizeReclaims(t *testing.T) { + l := newTestDiskLocation(t.TempDir()) + base := erasure_coding.EcShardFileName("", l.Directory, 75) + + datSize := int64(30 * 1024 * 1024) + writeSizedFile(t, base+".dat", datSize) + writeEcVif(t, base, datSize) + partialShardSize := calculateExpectedShardSize(datSize, erasure_coding.DataShardsCount) / 3 + for i := 0; i < erasure_coding.TotalShardsCount; i++ { + writeSizedFile(t, base+erasure_coding.ToExt(i), partialShardSize) + } + + if l.validateEcVolume("", 75) { + t.Fatal("validateEcVolume kept partial shards beside the recorded source .dat") + } +} + +// The .vif is written after the shards, so an interrupted re-encode of a +// volume that was once EC-decoded still has the old encode's .vif. Shards that +// are not that encode's full size are not described by it: reclaim as before. +func TestValidateEcVolume_InterruptedReEncodeWithStaleVifReclaims(t *testing.T) { + l := newTestDiskLocation(t.TempDir()) + base := erasure_coding.EcShardFileName("", l.Directory, 76) + + oldSourceSize := int64(20 * 1024 * 1024) + datSize := int64(60 * 1024 * 1024) + writeSizedFile(t, base+".dat", datSize) + writeEcVif(t, base, oldSourceSize) + partialShardSize := calculateExpectedShardSize(datSize, erasure_coding.DataShardsCount) / 2 + if partialShardSize == calculateExpectedShardSize(oldSourceSize, erasure_coding.DataShardsCount) { + t.Fatal("test setup: partial shards must not match the stale encode's size") + } + for i := 0; i < erasure_coding.TotalShardsCount; i++ { + writeSizedFile(t, base+erasure_coding.ToExt(i), partialShardSize) + } + + if l.validateEcVolume("", 76) { + t.Fatal("validateEcVolume kept partial shards of an interrupted re-encode because of a stale .vif") + } +} + +// A .vif without a recorded source size keeps the old size check against the +// .dat, in both directions. +func TestValidateEcVolume_NoRecordedSizeKeepsOldRule(t *testing.T) { + l := newTestDiskLocation(t.TempDir()) + base := erasure_coding.EcShardFileName("", l.Directory, 77) + + shardSize := calculateExpectedShardSize(30*1024*1024, erasure_coding.DataShardsCount) + writeEcVif(t, base, 0) + writeSizedFile(t, base+erasure_coding.ToExt(0), shardSize) + + writeSizedFile(t, base+".dat", 40*1024*1024) + if l.validateEcVolume("", 77) { + t.Fatal("without a recorded size, shards smaller than the .dat's full encode should be reclaimable") + } + writeSizedFile(t, base+".dat", 30*1024*1024) + if !l.validateEcVolume("", 77) { + t.Fatal("without a recorded size, shards matching the .dat's full encode must be kept") + } +}