From 68b595d7cab3e89d45187763346442eccd9763d2 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 6 Dec 2026 00:48:11 -0800 Subject: [PATCH] erasure_coding: bound the orphan cleanup's index reads Detection runs with a background context, so a server that stalled while streaming an .idx or .ecx held up the whole scan. Give each read a deadline; a timed-out .ecx read moves on to the next shard holder, and the replica is kept when none answers. --- weed/worker/tasks/erasure_coding/detection.go | 6 +++ .../ec_task_orphan_cleanup_test.go | 51 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/weed/worker/tasks/erasure_coding/detection.go b/weed/worker/tasks/erasure_coding/detection.go index dc60bdeb1e..f9ccb13a2d 100644 --- a/weed/worker/tasks/erasure_coding/detection.go +++ b/weed/worker/tasks/erasure_coding/detection.go @@ -40,6 +40,10 @@ const ( minOrphanSourceCoexistence = 30 * time.Minute ) +// orphanSourceIndexReadTimeout bounds each .idx/.ecx read of the orphan +// cleanup, so a stalled server cannot hold up detection. +var orphanSourceIndexReadTimeout = 5 * time.Minute + // Detection implements the detection logic for erasure coding tasks. // It respects ctx cancellation and can stop early once maxResults is reached. func Detection(ctx context.Context, metrics []*types.VolumeHealthMetrics, clusterInfo *types.ClusterInfo, config base.TaskConfig, maxResults int) ([]*types.TaskDetectionResult, bool, error) { @@ -962,6 +966,8 @@ func (r *ecxReader) read(ctx context.Context) ([]byte, error) { // readVolumeFile reads a whole volume file over the CopyFile RPC. func readVolumeFile(ctx context.Context, client volume_server_pb.VolumeServerClient, volumeID uint32, collection, ext string, isEcVolume bool) ([]byte, error) { + ctx, cancel := context.WithTimeout(ctx, orphanSourceIndexReadTimeout) + defer cancel() stream, err := client.CopyFile(ctx, &volume_server_pb.CopyFileRequest{ VolumeId: volumeID, Collection: collection, 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 145bbc3d29..d962b3dc44 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 @@ -111,6 +111,7 @@ type fakeOrphanServer struct { mu sync.Mutex files map[string][]byte copyErr error + stall bool deleted bool } @@ -129,6 +130,10 @@ func (f *fakeOrphanServer) CopyFile(req *volume_server_pb.CopyFileRequest, strea if f.copyErr != nil { return f.copyErr } + if f.stall { + <-stream.Context().Done() + return stream.Context().Err() + } f.mu.Lock() content, found := f.files[req.GetExt()] f.mu.Unlock() @@ -375,3 +380,49 @@ func TestOrphanSourceSightingResetsWhenCoexistenceEnds(t *testing.T) { require.Equal(t, 0, deleted) require.False(t, replica.wasDeleted(), "a renewed coexistence must wait again") } + +// A holder that stalls mid-read is given up on after orphanSourceIndexReadTimeout +// and the .ecx is read from the next holder instead. +func TestCleanupOrphanReadsEcxPastStalledHolder(t *testing.T) { + const volumeID = uint32(959) + saved := orphanSourceIndexReadTimeout + orphanSourceIndexReadTimeout = 200 * time.Millisecond + t.Cleanup(func() { orphanSourceIndexReadTimeout = saved }) + + ecx := indexBytes(indexEntry{key: 1, size: 100}) + replica := &fakeOrphanServer{files: map[string][]byte{".idx": ecx}} + stalled := &fakeOrphanServer{stall: true} + holder := &fakeOrphanServer{files: map[string][]byte{".ecx": ecx}} + replicaAddr := startFakeOrphanServer(t, replica) + half := func(addr string, bits uint32) *master_pb.DataNodeInfo { + return &master_pb.DataNodeInfo{Id: addr, DiskInfos: map[string]*master_pb.DiskInfo{"hdd": { + EcShardInfos: []*master_pb.VolumeEcShardInformationMessage{{Id: volumeID, EcIndexBits: bits, EncodeTsNs: 1}}, + }}} + } + at := topology.NewActiveTopology(10) + require.NoError(t, at.UpdateTopology(&master_pb.TopologyInfo{ + DataCenterInfos: []*master_pb.DataCenterInfo{{ + Id: "dc1", + RackInfos: []*master_pb.RackInfo{{ + Id: "rack1", + DataNodeInfos: []*master_pb.DataNodeInfo{ + {Id: replicaAddr, DiskInfos: map[string]*master_pb.DiskInfo{"hdd": { + VolumeInfos: []*master_pb.VolumeInformationMessage{{Id: volumeID}}, + }}}, + half(startFakeOrphanServer(t, stalled), uint32(1)<