diff --git a/weed/admin/dash/plugin_api.go b/weed/admin/dash/plugin_api.go index 1ed90db806..6c47b7e938 100644 --- a/weed/admin/dash/plugin_api.go +++ b/weed/admin/dash/plugin_api.go @@ -70,6 +70,47 @@ func filterActivitiesByLane(activities []plugin.JobActivity, laneFilter string) return filtered } +func parseVolumeIDFilter(raw string) (uint32, error) { + raw = strings.TrimSpace(raw) + if raw == "" { + return 0, nil + } + value, err := strconv.ParseUint(raw, 10, 32) + if err != nil || value == 0 { + return 0, fmt.Errorf("invalid volume_id %q", raw) + } + return uint32(value), nil +} + +func filterTrackedJobsByVolume(jobs []plugin.TrackedJob, volumeID uint32) []plugin.TrackedJob { + if volumeID == 0 { + return jobs + } + + filtered := make([]plugin.TrackedJob, 0, len(jobs)) + for _, job := range jobs { + if job.VolumeID == volumeID { + filtered = append(filtered, job) + } + } + return filtered +} + +func filterActivitiesByJobs(activities []plugin.JobActivity, jobs []plugin.TrackedJob) []plugin.JobActivity { + jobIDs := make(map[string]struct{}, len(jobs)) + for _, job := range jobs { + jobIDs[job.JobID] = struct{}{} + } + + filtered := make([]plugin.JobActivity, 0, len(activities)) + for _, activity := range activities { + if _, ok := jobIDs[activity.JobID]; ok { + filtered = append(filtered, activity) + } + } + return filtered +} + // GetPluginStatusAPI returns plugin status. func (s *AdminServer) GetPluginStatusAPI(w http.ResponseWriter, r *http.Request) { plugin := s.GetPlugin() @@ -153,10 +194,15 @@ func (s *AdminServer) GetPluginJobsAPI(w http.ResponseWriter, r *http.Request) { jobType := strings.TrimSpace(query.Get("job_type")) state := strings.TrimSpace(query.Get("state")) laneFilter := strings.TrimSpace(query.Get("lane")) + volumeID, err := parseVolumeIDFilter(query.Get("volume_id")) + if err != nil { + writeJSONError(w, http.StatusBadRequest, err.Error()) + return + } limit := parsePositiveInt(query.Get("limit"), 200) - // Apply the lane before the limit, or a busy lane's newest rows crowd out a quiet lane. + // Apply the filters before the limit, or a busy lane's newest rows crowd out a quiet lane. listLimit := limit - if laneFilter != "" { + if laneFilter != "" || volumeID != 0 { listLimit = 0 } jobs := s.ListPluginJobs(jobType, state, listLimit) @@ -165,6 +211,7 @@ func (s *AdminServer) GetPluginJobsAPI(w http.ResponseWriter, r *http.Request) { return } jobs = filterTrackedJobsByLane(jobs, laneFilter) + jobs = filterTrackedJobsByVolume(jobs, volumeID) if len(jobs) > limit { jobs = jobs[:limit] } @@ -261,9 +308,14 @@ func (s *AdminServer) GetPluginActivitiesAPI(w http.ResponseWriter, r *http.Requ query := r.URL.Query() jobType := strings.TrimSpace(query.Get("job_type")) laneFilter := strings.TrimSpace(query.Get("lane")) + volumeID, err := parseVolumeIDFilter(query.Get("volume_id")) + if err != nil { + writeJSONError(w, http.StatusBadRequest, err.Error()) + return + } limit := parsePositiveInt(query.Get("limit"), 500) listLimit := limit - if laneFilter != "" { + if laneFilter != "" || volumeID != 0 { listLimit = 0 } activities := s.ListPluginActivities(jobType, listLimit) @@ -272,6 +324,9 @@ func (s *AdminServer) GetPluginActivitiesAPI(w http.ResponseWriter, r *http.Requ return } activities = filterActivitiesByLane(activities, laneFilter) + if volumeID != 0 { + activities = filterActivitiesByJobs(activities, filterTrackedJobsByVolume(s.ListPluginJobs(jobType, "", 0), volumeID)) + } if len(activities) > limit { activities = activities[:limit] } diff --git a/weed/admin/dash/plugin_api_test.go b/weed/admin/dash/plugin_api_test.go index a88e215a5b..790146ab59 100644 --- a/weed/admin/dash/plugin_api_test.go +++ b/weed/admin/dash/plugin_api_test.go @@ -290,3 +290,75 @@ func TestPluginJobsAndActivitiesAPILimitAfterLaneFilter(t *testing.T) { t.Fatalf("default lane activities: got %d rows, want the limit of 2", len(rows)) } } + +func TestPluginJobsAndActivitiesAPIVolumeIDFilter(t *testing.T) { + dataDir := t.TempDir() + store, err := plugin.NewConfigStore(dataDir) + if err != nil { + t.Fatalf("NewConfigStore: %v", err) + } + now := time.Now() + var jobs []plugin.TrackedJob + var activities []plugin.JobActivity + for i := 0; i < 6; i++ { + jobType, jobID, volumeID := "erasure_coding", fmt.Sprintf("ec-%d", i), uint32(7) + if i%2 == 1 { + jobType, jobID, volumeID = "vacuum", fmt.Sprintf("vacuum-%d", i), uint32(8) + } + at := now.Add(-time.Duration(i) * time.Minute) + jobs = append(jobs, plugin.TrackedJob{JobID: jobID, JobType: jobType, VolumeID: volumeID, State: plugin.StateSucceeded, UpdatedAt: &at}) + activities = append(activities, plugin.JobActivity{JobID: jobID, JobType: jobType, Message: "done", OccurredAt: &at}) + } + if err := store.SaveTrackedJobs(jobs); err != nil { + t.Fatalf("SaveTrackedJobs: %v", err) + } + if err := store.SaveActivities(activities); err != nil { + t.Fatalf("SaveActivities: %v", err) + } + p, err := plugin.New(plugin.Options{DataDir: dataDir}) + if err != nil { + t.Fatalf("plugin.New: %v", err) + } + defer p.Shutdown() + s := &AdminServer{plugin: p} + + get := func(handler http.HandlerFunc, url string, wantStatus int) []map[string]any { + recorder := httptest.NewRecorder() + handler(recorder, httptest.NewRequest(http.MethodGet, url, nil)) + if recorder.Code != wantStatus { + t.Fatalf("%s: status %d, want %d", url, recorder.Code, wantStatus) + } + if wantStatus != http.StatusOK { + return nil + } + var rows []map[string]any + if err := json.Unmarshal(recorder.Body.Bytes(), &rows); err != nil { + t.Fatalf("%s: %v", url, err) + } + return rows + } + idsOf := func(rows []map[string]any) []string { + ids := make([]string, 0, len(rows)) + for _, row := range rows { + ids = append(ids, fmt.Sprint(row["job_id"])) + } + return ids + } + + if ids := idsOf(get(s.GetPluginJobsAPI, "/api/plugin/jobs?limit=2&volume_id=8", http.StatusOK)); fmt.Sprint(ids) != "[vacuum-1 vacuum-3]" { + t.Fatalf("volume 8 jobs: got %v", ids) + } + if ids := idsOf(get(s.GetPluginJobsAPI, "/api/plugin/jobs?volume_id=7&job_type=vacuum", http.StatusOK)); len(ids) != 0 { + t.Fatalf("volume 7 vacuum jobs: got %v, want none", ids) + } + if ids := idsOf(get(s.GetPluginActivitiesAPI, "/api/plugin/activities?limit=2&volume_id=7", http.StatusOK)); fmt.Sprint(ids) != "[ec-0 ec-2]" { + t.Fatalf("volume 7 activities: got %v", ids) + } + if ids := idsOf(get(s.GetPluginActivitiesAPI, "/api/plugin/activities?volume_id=9", http.StatusOK)); len(ids) != 0 { + t.Fatalf("volume 9 activities: got %v, want none", ids) + } + for _, bad := range []string{"0", "-1", "abc", "4294967296"} { + get(s.GetPluginJobsAPI, "/api/plugin/jobs?volume_id="+bad, http.StatusBadRequest) + get(s.GetPluginActivitiesAPI, "/api/plugin/activities?volume_id="+bad, http.StatusBadRequest) + } +} diff --git a/weed/admin/plugin/plugin_monitor.go b/weed/admin/plugin/plugin_monitor.go index 6013acf3d0..253ffba223 100644 --- a/weed/admin/plugin/plugin_monitor.go +++ b/weed/admin/plugin/plugin_monitor.go @@ -10,6 +10,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb/plugin_pb" + pluginworker "github.com/seaweedfs/seaweedfs/weed/plugin/worker" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -805,6 +806,9 @@ func mergeTrackedStatusIntoDetail(detail *TrackedJob, tracked *TrackedJob) { if detail.DedupeKey == "" { detail.DedupeKey = tracked.DedupeKey } + if detail.VolumeID == 0 { + detail.VolumeID = tracked.VolumeID + } if detail.Summary == "" { detail.Summary = tracked.Summary } @@ -942,6 +946,7 @@ func (r *Plugin) trackExecutionStart(requestID, workerID string, job *plugin_pb. tracked.RequestID = requestID tracked.WorkerID = workerID tracked.DedupeKey = job.DedupeKey + tracked.VolumeID = pluginworker.ReadUint32Config(job.Parameters, "volume_id", 0) tracked.Summary = job.Summary tracked.State = strings.ToLower(plugin_pb.JobState_JOB_STATE_ASSIGNED.String()) tracked.Progress = 0 @@ -962,6 +967,7 @@ func (r *Plugin) trackExecutionStart(requestID, workerID string, job *plugin_pb. detail.RequestID = requestID detail.WorkerID = workerID detail.DedupeKey = job.DedupeKey + detail.VolumeID = trackedSnapshot.VolumeID detail.Summary = job.Summary detail.Detail = job.Detail detail.Parameters = enrichTrackedJobParameters(job.JobType, configValueMapToPlain(job.Parameters)) @@ -1017,6 +1023,7 @@ func (r *Plugin) trackExecutionQueued(job *plugin_pb.JobSpec) { tracked.JobType = job.JobType tracked.DedupeKey = job.DedupeKey + tracked.VolumeID = pluginworker.ReadUint32Config(job.Parameters, "volume_id", 0) tracked.Summary = job.Summary tracked.State = strings.ToLower(plugin_pb.JobState_JOB_STATE_PENDING.String()) tracked.Progress = 0 @@ -1034,6 +1041,7 @@ func (r *Plugin) trackExecutionQueued(job *plugin_pb.JobSpec) { detail.JobID = job.JobId detail.JobType = job.JobType detail.DedupeKey = job.DedupeKey + detail.VolumeID = trackedSnapshot.VolumeID detail.Summary = job.Summary detail.Detail = job.Detail detail.Parameters = enrichTrackedJobParameters(job.JobType, configValueMapToPlain(job.Parameters)) diff --git a/weed/admin/plugin/plugin_monitor_test.go b/weed/admin/plugin/plugin_monitor_test.go index fd564edb39..b853a9778f 100644 --- a/weed/admin/plugin/plugin_monitor_test.go +++ b/weed/admin/plugin/plugin_monitor_test.go @@ -170,6 +170,51 @@ func TestTrackExecutionQueuedMarksPendingState(t *testing.T) { } } +func TestTrackedJobsKeepVolumeID(t *testing.T) { + t.Parallel() + + pluginSvc, err := New(Options{}) + if err != nil { + t.Fatalf("New: %v", err) + } + defer pluginSvc.Shutdown() + + pluginSvc.trackExecutionQueued(&plugin_pb.JobSpec{ + JobId: "job-ec-17", + JobType: "erasure_coding", + Parameters: map[string]*plugin_pb.ConfigValue{ + "volume_id": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 17}}, + }, + }) + pluginSvc.trackExecutionStart("req-1", "worker-a", &plugin_pb.JobSpec{ + JobId: "job-vacuum-23", + JobType: "vacuum", + Parameters: map[string]*plugin_pb.ConfigValue{ + "volume_id": {Kind: &plugin_pb.ConfigValue_StringValue{StringValue: "23"}}, + }, + }, 1) + pluginSvc.trackExecutionQueued(&plugin_pb.JobSpec{JobId: "job-no-volume", JobType: "iceberg"}) + + want := map[string]uint32{"job-ec-17": 17, "job-vacuum-23": 23, "job-no-volume": 0} + jobs := pluginSvc.ListTrackedJobs("", "", 0) + if len(jobs) != len(want) { + t.Fatalf("expected %d tracked jobs, got %d", len(want), len(jobs)) + } + for _, job := range jobs { + if job.VolumeID != want[job.JobID] { + t.Fatalf("job %s: volume id %d, want %d", job.JobID, job.VolumeID, want[job.JobID]) + } + } + + detail, found, err := pluginSvc.BuildJobDetail("job-ec-17", 10, 0) + if err != nil || !found || detail.Job == nil { + t.Fatalf("BuildJobDetail: found=%v err=%v", found, err) + } + if detail.Job.VolumeID != 17 { + t.Fatalf("detail volume id %d, want 17", detail.Job.VolumeID) + } +} + func TestHandleJobProgressUpdateCarriesWorkerIDInActivities(t *testing.T) { t.Parallel() diff --git a/weed/admin/plugin/types.go b/weed/admin/plugin/types.go index 8a7df25fc7..7a4331a95f 100644 --- a/weed/admin/plugin/types.go +++ b/weed/admin/plugin/types.go @@ -44,6 +44,7 @@ type TrackedJob struct { RequestID string `json:"request_id"` WorkerID string `json:"worker_id"` DedupeKey string `json:"dedupe_key,omitempty"` + VolumeID uint32 `json:"volume_id,omitempty"` Summary string `json:"summary,omitempty"` Detail string `json:"detail,omitempty"` Parameters map[string]interface{} `json:"parameters,omitempty"` diff --git a/weed/admin/view/app/plugin.templ b/weed/admin/view/app/plugin.templ index d5e41029b3..d96aa81223 100644 --- a/weed/admin/view/app/plugin.templ +++ b/weed/admin/view/app/plugin.templ @@ -383,6 +383,7 @@ templ Plugin(page string, initialJob string, lane string) {
Job Queue
+ States: pending/assigned/running
@@ -451,6 +452,7 @@ templ Plugin(page string, initialJob string, lane string) {
Execution Jobs
+
How often to check for new work.
Per-attempt deadline for one job. Size-aware tasks may extend this automatically.
Next scheduled check
Scheduler
-
Not scheduled
Run History
Keep last 10 success + last 10 errors
Successful Runs
TimeJob IDWorkerDuration
No data
Error Runs
TimeJob IDWorkerError
No data
Detection Results
Run detection to see proposals.
Job Queue
States: pending/assigned/running
Job IDTypeStateProgressWorkerUpdatedMessage
Loading...
Detection Jobs
Detection activities for selected job type
TimeJob TypeRequest IDWorkerStageSourceMessage
Loading...
Execution Jobs
Job IDTypeStateProgressWorkerUpdatedMessage
Loading...
Execution Activities
Non-detection events only
TimeJob TypeJob IDSourceStageMessage
Loading...
Job Detail
Select a job to view details.
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "
Workers

0

Active Jobs

0

Activities (recent)

0

Next scheduled check

-

Per Job Type Summary
Job TypeActive JobsRecent Activities
Loading...
Scheduler State
Sequential scheduler with per-job runtime limits
Job TypeEnabledDetectorIn FlightMax RuntimeExec GlobalExec/WorkerExecutor WorkersEffective ExecLast Run
Loading...
Workers
WorkerAddressCapabilitiesLoad
Loading...
Job Type Configuration
Not loaded
Selected Job Type
-
Descriptor
Select a job type to load schema and config.
Admin Config Form
No admin form loaded.
Worker Config Form
No worker form loaded.
Job Scheduling Settings
How often to check for new work.
Per-attempt deadline for one job. Size-aware tasks may extend this automatically.
Next scheduled check
Scheduler
-
Not scheduled
Run History
Keep last 10 success + last 10 errors
Successful Runs
TimeJob IDWorkerDuration
No data
Error Runs
TimeJob IDWorkerError
No data
Detection Results
Run detection to see proposals.
Job Queue
States: pending/assigned/running
Job IDTypeStateProgressWorkerUpdatedMessage
Loading...
Detection Jobs
Detection activities for selected job type
TimeJob TypeRequest IDWorkerStageSourceMessage
Loading...
Execution Jobs
Job IDTypeStateProgressWorkerUpdatedMessage
Loading...
Execution Activities
Non-detection events only
TimeJob TypeJob IDSourceStageMessage
Loading...
Job Detail
Select a job to view details.
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }