worker: reject a task at capacity without blocking the manager loop (#13453)

* worker: reject a task at capacity without blocking the manager loop

* worker: gofmt the capacity test and trim its comment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* worker: report a task rejected at capacity back to the admin

The admin marks a task assigned before sending it, has no assignment
timeout, and only reclaims tasks from workers whose heartbeats stop.
A worker that rejects a task at capacity keeps heartbeating, so the
task stayed assigned to it and blocked further work on its volume.
Report the rejection as a failure so the admin reschedules it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-12-05 11:15:01 -08:00
1 parent 2637ff09c5
commit 6e91dc97b8
2 files changed
+106

No files matched your search

+3
View File
@@ -248,6 +248,7 @@ out:
currentLoad := len(w.state.currentTasks)
if currentLoad >= w.config.MaxConcurrent {
cmd.resp <- fmt.Errorf("worker is at capacity")
continue
}
task := cmd.data.(*types.TaskInput)
w.state.currentTasks[task.ID] = task
@@ -545,6 +546,8 @@ func (w *Worker) HandleTask(task *types.TaskInput) error {
w.id, task.ID, task.Type, task.VolumeID)
if err := w.setTask(task); err != nil {
// The admin already marked the task assigned to this worker.
w.completeTask(task.ID, false, err.Error())
return err
}
+103
View File
@@ -0,0 +1,103 @@
package worker
import (
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/worker/types"
)
// HandleTask does not pre-check load the way requestTasks does, so a task can
// arrive at capacity. The manager loop must reject it and keep serving.
func TestSetTaskAtCapacityDoesNotBlockManagerLoop(t *testing.T) {
w, err := NewWorker(&types.WorkerConfig{
BaseWorkingDir: t.TempDir(),
MaxConcurrent: 2,
})
if err != nil {
t.Fatalf("NewWorker: %v", err)
}
for _, id := range []string{"t1", "t2"} {
if err := w.setTask(&types.TaskInput{ID: id}); err != nil {
t.Fatalf("setTask(%s): %v", id, err)
}
}
rejected := make(chan error, 1)
go func() {
rejected <- w.setTask(&types.TaskInput{ID: "t3"})
}()
select {
case err := <-rejected:
if err == nil {
t.Fatalf("setTask at capacity = nil error, want capacity error")
}
case <-time.After(5 * time.Second):
t.Fatalf("setTask at capacity blocked: manager loop deadlocked on the rejected task")
}
load := make(chan int, 1)
go func() { load <- w.getTaskLoad() }()
select {
case got := <-load:
if got != 2 {
t.Errorf("load after rejection = %d, want 2", got)
}
case <-time.After(5 * time.Second):
t.Fatalf("getTaskLoad blocked after rejection")
}
// Drain so Stop does not wait for tasks that never execute.
w.removeTask(&types.TaskInput{ID: "t1"})
w.removeTask(&types.TaskInput{ID: "t2"})
if err := w.Stop(); err != nil {
t.Fatalf("Stop: %v", err)
}
}
type completionRecorder struct {
AdminClient
completed chan string
}
func (c *completionRecorder) CompleteTask(taskID string, success bool, errorMsg string) error {
if !success {
c.completed <- taskID
}
return nil
}
func (c *completionRecorder) Disconnect() error { return nil }
func TestHandleTaskAtCapacityReportsRejectionToAdmin(t *testing.T) {
w, err := NewWorker(&types.WorkerConfig{
BaseWorkingDir: t.TempDir(),
MaxConcurrent: 1,
})
if err != nil {
t.Fatalf("NewWorker: %v", err)
}
admin := &completionRecorder{completed: make(chan string, 1)}
w.SetAdminClient(admin)
if err := w.setTask(&types.TaskInput{ID: "t1"}); err != nil {
t.Fatalf("setTask(t1): %v", err)
}
if err := w.HandleTask(&types.TaskInput{ID: "t2"}); err == nil {
t.Fatalf("HandleTask at capacity = nil error, want capacity error")
}
select {
case id := <-admin.completed:
if id != "t2" {
t.Errorf("failure reported for %q, want t2", id)
}
default:
t.Fatalf("rejected task was not reported to the admin, which keeps it assigned")
}
w.removeTask(&types.TaskInput{ID: "t1"})
if err := w.Stop(); err != nil {
t.Fatalf("Stop: %v", err)
}
}