diff --git a/weed/worker/tasks/erasure_coding/detection.go b/weed/worker/tasks/erasure_coding/detection.go index ab3cb8a9f9..dc60bdeb1e 100644 --- a/weed/worker/tasks/erasure_coding/detection.go +++ b/weed/worker/tasks/erasure_coding/detection.go @@ -22,6 +22,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding" "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding/ecbalancer" "github.com/seaweedfs/seaweedfs/weed/storage/idx" + "github.com/seaweedfs/seaweedfs/weed/storage/needle_map" "github.com/seaweedfs/seaweedfs/weed/storage/super_block" storagetypes "github.com/seaweedfs/seaweedfs/weed/storage/types" "github.com/seaweedfs/seaweedfs/weed/util/wildcard" @@ -801,7 +802,7 @@ func cleanupOrphanSourceReplicas(ctx context.Context, clusterInfo *types.Cluster continue } if missing > 0 { - glog.Warningf("EC Detection: source replica for volume %d on %s holds %d needles the EC volume lacks; keeping it", metric.VolumeID, serverAddress, missing) + glog.Warningf("EC Detection: source replica for volume %d on %s holds %d needles the EC volume lacks or holds differently; keeping it", metric.VolumeID, serverAddress, missing) continue } deleted++ @@ -883,20 +884,23 @@ func pruneOrphanSourceSightings(activeTopology *topology.ActiveTopology) { } } -// needlesMissingFromEc counts the live needles in the replica's .idx whose key -// is absent from the EC volume's .ecx. A key in the .ecx in any state existed -// at encode time; a live key absent from it was written afterwards. +// needlesMissingFromEc counts the live needles in the replica's .idx that the +// EC volume's .ecx does not account for. The .ecx lists the needles live at +// encode time with their sizes; a later delete marks its entry in place and +// keeps the offset. A live replica needle is accounted for when the .ecx holds +// it at the same size, or deleted at the same offset (the encoded record, +// deleted since). Anything else may be data the EC volume never had. func needlesMissingFromEc(ctx context.Context, client volume_server_pb.VolumeServerClient, volumeID uint32, collection string, ecx *ecxReader) (int, error) { idxBytes, err := readVolumeFile(ctx, client, volumeID, collection, ".idx", false) if err != nil { return 0, fmt.Errorf("read source replica .idx: %w", err) } - live := make(map[storagetypes.NeedleId]struct{}) + live := make(map[storagetypes.NeedleId]needle_map.NeedleValue) err = idx.WalkIndexFile(bytes.NewReader(idxBytes), 0, func(key storagetypes.NeedleId, offset storagetypes.Offset, size storagetypes.Size) error { if offset.IsZero() || !size.IsValid() { delete(live, key) } else { - live[key] = struct{}{} + live[key] = needle_map.NeedleValue{Key: key, Offset: offset, Size: size} } return nil }) @@ -910,8 +914,10 @@ func needlesMissingFromEc(ctx context.Context, client volume_server_pb.VolumeSer if err != nil { return 0, err } - err = idx.WalkIndexFile(bytes.NewReader(ecxBytes), 0, func(key storagetypes.NeedleId, _ storagetypes.Offset, _ storagetypes.Size) error { - delete(live, key) + err = idx.WalkIndexFile(bytes.NewReader(ecxBytes), 0, func(key storagetypes.NeedleId, offset storagetypes.Offset, size storagetypes.Size) error { + if replica, found := live[key]; found && (size == replica.Size || size.IsDeleted() && offset == replica.Offset) { + delete(live, key) + } return nil }) if err != nil { diff --git a/weed/worker/tasks/erasure_coding/ec_task_orphan_cleanup_test.go b/weed/worker/tasks/erasure_coding/ec_task_orphan_cleanup_test.go index 2da5bbe721..145bbc3d29 100644 --- a/weed/worker/tasks/erasure_coding/ec_task_orphan_cleanup_test.go +++ b/weed/worker/tasks/erasure_coding/ec_task_orphan_cleanup_test.go @@ -163,15 +163,21 @@ func startFakeOrphanServer(t *testing.T, server *fakeOrphanServer) string { return "127.0.0.1:0." + port } +// indexEntry is one index record; an unset offset is derived from the key. type indexEntry struct { - key uint64 - size storagetypes.Size + key uint64 + size storagetypes.Size + offset int64 } func indexBytes(entries ...indexEntry) []byte { var buf bytes.Buffer - for i, e := range entries { - buf.Write(needle_map.ToBytes(storagetypes.NeedleId(e.key), storagetypes.ToOffset(int64(i+1)*8), e.size)) + for _, e := range entries { + offset := e.offset + if offset == 0 { + offset = int64(e.key) * 8 + } + buf.Write(needle_map.ToBytes(storagetypes.NeedleId(e.key), storagetypes.ToOffset(offset), e.size)) } return buf.Bytes() } @@ -207,8 +213,9 @@ func buildOrphanTopology(t *testing.T, replicaAddr, holderAddr string, volumeID return at } -// A read-only source replica is deleted only when every live needle in its -// .idx is a key of the EC volume's .ecx, in any state there. +// A read-only source replica is deleted only when the EC volume's .ecx accounts +// for every live needle in its .idx: held at the same size, or deleted at the +// same offset. func TestCleanupOrphanChecksSourceNeedlesAgainstEcx(t *testing.T) { const volumeID = uint32(956) ecx := indexBytes( @@ -225,10 +232,23 @@ func TestCleanupOrphanChecksSourceNeedlesAgainstEcx(t *testing.T) { wantErr bool }{ { - name: "live needles all in ecx", + name: "live needles held in ecx or deleted there in place", idx: indexBytes(indexEntry{key: 1, size: 100}, indexEntry{key: 2, size: 100}, indexEntry{key: 3, size: 100}), wantDeleted: true, }, + { + name: "live needles in ecx at other offsets", + idx: indexBytes(indexEntry{key: 1, size: 100, offset: 800}, indexEntry{key: 3, size: 100, offset: 808}), + wantDeleted: true, + }, + { + name: "needle rewritten at another size", + idx: indexBytes(indexEntry{key: 1, size: 100}, indexEntry{key: 3, size: 100}, indexEntry{key: 3, size: 150, offset: 800}), + }, + { + name: "needle deleted in ecx but rewritten in the replica", + idx: indexBytes(indexEntry{key: 1, size: 100}, indexEntry{key: 2, size: 100, offset: 800}), + }, { name: "live needle absent from ecx", idx: indexBytes(indexEntry{key: 1, size: 100}, indexEntry{key: 4, size: 100}, indexEntry{key: 5, size: 100}),