mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* admin/plugin: fail a restored job its worker no longer lists An admin restart reloads the jobs that were running from the persisted state, but the workers drop those jobs when the old admin stream closes. No pending execution covers a restored job, so only the 24h stale sweep ended it, and until then it blocked dedupe and scheduling of its job type. Track the active jobs loaded at startup by worker and fail one after two itemized heartbeats from its worker leave it out, as is already done for a dispatch whose worker restarted. A listed job is left to adoption. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: keep tracking a listed restored job; skip queued ones - A restored job its worker lists only resets its miss count, so one it drops later is still failed. - Track only jobs that were sent to a worker (request id set, not pending). - Match restored ids against the trimmed heartbeat ids. - Fail the restored attempt under the jobs lock that applies the expiry (handleJobCompletedIf / expireActiveJobIf), so a newer attempt of the same job is neither failed nor canceled. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: keep a newer attempt's detail from a restored-job expiry - The conditional completion writes the job detail through persistJobDetailSnapshotIf and skips it when the stored detail already names another request; the check runs under jobDetailsMu with the write. - mergeTerminalDetailIntoTracked ignores a terminal detail of another request, so a restart does not end the loaded attempt with it. - Tests wait for the restored-job expiries instead of sleeping. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: restore a finished retry from its detail The job detail is saved as each attempt changes and the job list only periodically, so a terminal detail naming another request is the newer attempt. Apply it with its request id instead of ignoring it; the expiry's own detail write already refuses to overwrite another request. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: restore whichever attempt is newer The job list and a job's detail can name different attempts either way round: the list is saved periodically, and a detail write can fail. When their request ids differ, apply the terminal detail only if it is the newer attempt (higher attempt number, else later update). Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: fill a restored job's empty request id from its detail A job the list saved while still queued has no request id; take the finished detail's, as the merge did before the newer-attempt check. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: take the worker and attempt of a newer restored detail When the restore takes a finished retry's detail over the job list's attempt, the record kept the older attempt's worker and attempt number next to the retry's request id and state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin/plugin: fold the conditional completion into the existing functions handleJobCompleted, expireActiveJob, trackExecutionCompletion and persistJobDetailSnapshot take the accept check themselves instead of gaining *If twins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin/plugin: ignore a late earlier-attempt completion; replace an older detail - A completion whose request id differs from the tracked attempt's is a late report of an earlier attempt and no longer ends a retry. - A conditional completion skips the detail write only when the stored detail is a newer attempt (detailIsNewerAttempt), so an older finished attempt's detail left behind by a failed write is replaced. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: take only the dispatched attempt's reports A late progress report of a timed-out attempt replaced the retry's request id, after which the retry's own completion was rejected as foreign. One rule now gates both: a record the admin dispatched (attempt > 0) takes progress and completion only for its own request (reportFromOtherAttempt). When an expiry replaces an older attempt's detail it also takes the closing attempt's worker, attempt number and times. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: restore a newer running attempt's identity from its detail The job list can name attempt A while the detail shows retry B running when the admin stopped. The restore now takes B's request id, worker and attempt number from such a newer running detail, so B's own reports are accepted. Test helper keeps a given attempt number. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin/plugin: keep attemptEnded's doc comment on attemptEnded Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin/plugin: dispatch an attempt-less job as attempt 1 The late-report guard only covers records with a positive attempt, and ExecuteJob passed a zero attempt through, so a direct caller's dispatch could be closed by another request's completion. The HTTP API already raised zero to one; the dispatch path now does the same for every caller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
277 lines
8.5 KiB
Go
277 lines
8.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/plugin_pb"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func TestRunDetectionSendsCancelOnContextDone(t *testing.T) {
|
|
t.Parallel()
|
|
pluginSvc, err := New(Options{})
|
|
if err != nil {
|
|
t.Fatalf("New plugin error: %v", err)
|
|
}
|
|
defer pluginSvc.Shutdown()
|
|
|
|
const workerID = "worker-detect"
|
|
const jobType = "vacuum"
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: workerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: jobType, CanDetect: true, MaxDetectionConcurrency: 1},
|
|
},
|
|
})
|
|
session := &streamSession{workerID: workerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 4), done: make(chan struct{})}
|
|
pluginSvc.putSession(session)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.RunDetection(ctx, jobType, &plugin_pb.ClusterContext{}, 10)
|
|
errCh <- runErr
|
|
}()
|
|
|
|
first := <-session.outgoing
|
|
if first.GetRunDetectionRequest() == nil {
|
|
t.Fatalf("expected first message to be run_detection_request")
|
|
}
|
|
|
|
cancel()
|
|
|
|
second := <-session.outgoing
|
|
cancelReq := second.GetCancelRequest()
|
|
if cancelReq == nil {
|
|
t.Fatalf("expected second message to be cancel_request")
|
|
}
|
|
if cancelReq.TargetId != first.RequestId {
|
|
t.Fatalf("unexpected cancel target id: got=%s want=%s", cancelReq.TargetId, first.RequestId)
|
|
}
|
|
if cancelReq.TargetKind != plugin_pb.WorkKind_WORK_KIND_DETECTION {
|
|
t.Fatalf("unexpected cancel target kind: %v", cancelReq.TargetKind)
|
|
}
|
|
|
|
runErr := <-errCh
|
|
if !errors.Is(runErr, context.Canceled) {
|
|
t.Fatalf("expected context canceled error, got %v", runErr)
|
|
}
|
|
}
|
|
|
|
func TestExecuteJobSendsCancelOnContextDone(t *testing.T) {
|
|
t.Parallel()
|
|
pluginSvc, err := New(Options{})
|
|
if err != nil {
|
|
t.Fatalf("New plugin error: %v", err)
|
|
}
|
|
defer pluginSvc.Shutdown()
|
|
|
|
const workerID = "worker-exec"
|
|
const jobType = "vacuum"
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: workerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: jobType, CanExecute: true, MaxExecutionConcurrency: 1},
|
|
},
|
|
})
|
|
session := &streamSession{workerID: workerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 4), done: make(chan struct{})}
|
|
pluginSvc.putSession(session)
|
|
|
|
job := &plugin_pb.JobSpec{JobId: "job-1", JobType: jobType}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.ExecuteJob(ctx, job, &plugin_pb.ClusterContext{}, 1)
|
|
errCh <- runErr
|
|
}()
|
|
|
|
first := <-session.outgoing
|
|
if first.GetExecuteJobRequest() == nil {
|
|
t.Fatalf("expected first message to be execute_job_request")
|
|
}
|
|
|
|
cancel()
|
|
|
|
second := <-session.outgoing
|
|
cancelReq := second.GetCancelRequest()
|
|
if cancelReq == nil {
|
|
t.Fatalf("expected second message to be cancel_request")
|
|
}
|
|
if cancelReq.TargetId != first.RequestId {
|
|
t.Fatalf("unexpected cancel target id: got=%s want=%s", cancelReq.TargetId, first.RequestId)
|
|
}
|
|
if cancelReq.TargetKind != plugin_pb.WorkKind_WORK_KIND_EXECUTION {
|
|
t.Fatalf("unexpected cancel target kind: %v", cancelReq.TargetKind)
|
|
}
|
|
|
|
runErr := <-errCh
|
|
if !errors.Is(runErr, context.Canceled) {
|
|
t.Fatalf("expected context canceled error, got %v", runErr)
|
|
}
|
|
}
|
|
|
|
func TestAdminScriptExecutionBlocksOtherDetection(t *testing.T) {
|
|
pluginSvc, err := New(Options{})
|
|
if err != nil {
|
|
t.Fatalf("New plugin error: %v", err)
|
|
}
|
|
defer pluginSvc.Shutdown()
|
|
|
|
const adminWorkerID = "worker-admin-script"
|
|
const otherWorkerID = "worker-vacuum"
|
|
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: adminWorkerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: "admin_script", CanExecute: true, MaxExecutionConcurrency: 1},
|
|
},
|
|
})
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: otherWorkerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: "vacuum", CanDetect: true, MaxDetectionConcurrency: 1},
|
|
},
|
|
})
|
|
adminSession := &streamSession{workerID: adminWorkerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 8), done: make(chan struct{})}
|
|
otherSession := &streamSession{workerID: otherWorkerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 8), done: make(chan struct{})}
|
|
pluginSvc.putSession(adminSession)
|
|
pluginSvc.putSession(otherSession)
|
|
|
|
adminErrCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.ExecuteJob(context.Background(), &plugin_pb.JobSpec{
|
|
JobId: "job-admin-script-1",
|
|
JobType: "admin_script",
|
|
}, &plugin_pb.ClusterContext{}, 1)
|
|
adminErrCh <- runErr
|
|
}()
|
|
|
|
adminExecMessage := <-adminSession.outgoing
|
|
if adminExecMessage.GetExecuteJobRequest() == nil {
|
|
t.Fatalf("expected admin_script execute request")
|
|
}
|
|
|
|
detectErrCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.RunDetection(context.Background(), "vacuum", &plugin_pb.ClusterContext{}, 10)
|
|
detectErrCh <- runErr
|
|
}()
|
|
|
|
select {
|
|
case unexpected := <-otherSession.outgoing:
|
|
t.Fatalf("expected vacuum detection to wait while admin_script runs, got message: %+v", unexpected)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
|
|
pluginSvc.handleJobCompleted(&plugin_pb.JobCompleted{
|
|
RequestId: adminExecMessage.RequestId,
|
|
JobId: "job-admin-script-1",
|
|
JobType: "admin_script",
|
|
Success: true,
|
|
CompletedAt: timestamppb.Now(),
|
|
}, nil)
|
|
if runErr := <-adminErrCh; runErr != nil {
|
|
t.Fatalf("admin_script ExecuteJob error: %v", runErr)
|
|
}
|
|
|
|
detectMessage := <-otherSession.outgoing
|
|
detectRequest := detectMessage.GetRunDetectionRequest()
|
|
if detectRequest == nil {
|
|
t.Fatalf("expected vacuum detection request after admin_script completion")
|
|
}
|
|
pluginSvc.handleDetectionComplete(otherWorkerID, &plugin_pb.DetectionComplete{
|
|
RequestId: detectMessage.RequestId,
|
|
JobType: "vacuum",
|
|
Success: true,
|
|
})
|
|
if runErr := <-detectErrCh; runErr != nil {
|
|
t.Fatalf("vacuum RunDetection error: %v", runErr)
|
|
}
|
|
}
|
|
|
|
func TestAdminScriptExecutionBlocksOtherExecution(t *testing.T) {
|
|
pluginSvc, err := New(Options{})
|
|
if err != nil {
|
|
t.Fatalf("New plugin error: %v", err)
|
|
}
|
|
defer pluginSvc.Shutdown()
|
|
|
|
const adminWorkerID = "worker-admin-script"
|
|
const otherWorkerID = "worker-vacuum"
|
|
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: adminWorkerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: "admin_script", CanExecute: true, MaxExecutionConcurrency: 1},
|
|
},
|
|
})
|
|
pluginSvc.registry.UpsertFromHello(&plugin_pb.WorkerHello{
|
|
WorkerId: otherWorkerID,
|
|
Capabilities: []*plugin_pb.JobTypeCapability{
|
|
{JobType: "vacuum", CanExecute: true, MaxExecutionConcurrency: 1},
|
|
},
|
|
})
|
|
adminSession := &streamSession{workerID: adminWorkerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 8), done: make(chan struct{})}
|
|
otherSession := &streamSession{workerID: otherWorkerID, outgoing: make(chan *plugin_pb.AdminToWorkerMessage, 8), done: make(chan struct{})}
|
|
pluginSvc.putSession(adminSession)
|
|
pluginSvc.putSession(otherSession)
|
|
|
|
adminErrCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.ExecuteJob(context.Background(), &plugin_pb.JobSpec{
|
|
JobId: "job-admin-script-2",
|
|
JobType: "admin_script",
|
|
}, &plugin_pb.ClusterContext{}, 1)
|
|
adminErrCh <- runErr
|
|
}()
|
|
|
|
adminExecMessage := <-adminSession.outgoing
|
|
if adminExecMessage.GetExecuteJobRequest() == nil {
|
|
t.Fatalf("expected admin_script execute request")
|
|
}
|
|
|
|
otherErrCh := make(chan error, 1)
|
|
go func() {
|
|
_, runErr := pluginSvc.ExecuteJob(context.Background(), &plugin_pb.JobSpec{
|
|
JobId: "job-vacuum-1",
|
|
JobType: "vacuum",
|
|
}, &plugin_pb.ClusterContext{}, 1)
|
|
otherErrCh <- runErr
|
|
}()
|
|
|
|
select {
|
|
case unexpected := <-otherSession.outgoing:
|
|
t.Fatalf("expected vacuum execute to wait while admin_script runs, got message: %+v", unexpected)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
|
|
pluginSvc.handleJobCompleted(&plugin_pb.JobCompleted{
|
|
RequestId: adminExecMessage.RequestId,
|
|
JobId: "job-admin-script-2",
|
|
JobType: "admin_script",
|
|
Success: true,
|
|
CompletedAt: timestamppb.Now(),
|
|
}, nil)
|
|
if runErr := <-adminErrCh; runErr != nil {
|
|
t.Fatalf("admin_script ExecuteJob error: %v", runErr)
|
|
}
|
|
|
|
otherExecMessage := <-otherSession.outgoing
|
|
if otherExecMessage.GetExecuteJobRequest() == nil {
|
|
t.Fatalf("expected vacuum execute request after admin_script completion")
|
|
}
|
|
pluginSvc.handleJobCompleted(&plugin_pb.JobCompleted{
|
|
RequestId: otherExecMessage.RequestId,
|
|
JobId: "job-vacuum-1",
|
|
JobType: "vacuum",
|
|
Success: true,
|
|
CompletedAt: timestamppb.Now(),
|
|
}, nil)
|
|
if runErr := <-otherErrCh; runErr != nil {
|
|
t.Fatalf("vacuum ExecuteJob error: %v", runErr)
|
|
}
|
|
}
|