Files
seaweedfs/weed/s3api/s3lifecycle/scheduler/scheduler.go
T
Chris Lu 7f254e158e feat(worker/s3_lifecycle): plugin handler with admin UI config (#9362)
* feat(s3/lifecycle): scheduler — N pipelines over an even shard split

Scheduler.Run spawns Workers Pipeline goroutines plus one engine-refresh
ticker. Each worker owns a contiguous AssignShards(idx, total) slice of
[0, ShardCount) and runs Pipeline.Run with EventBudget bounding each
iteration; brief RetryBackoff between iterations avoids hot-loop on
errors. The refresh ticker rebuilds the engine snapshot from the filer's
bucket configs every RefreshInterval.

LoadCompileInputs / IsBucketVersioned / AllActivePriorStates are
exported from a configload.go sibling so the shell command can move to
this shared implementation in a follow-up.

* refactor(shell): reuse scheduler.LoadCompileInputs in run-shard

Drop the local copies of loadLifecycleCompileInputs / isBucketVersioned
/ allActivePriorStates / lifecycleParseError that the new
scheduler package now exports. Same behavior, one source of truth.

* feat(worker/s3_lifecycle): plugin handler with admin UI config

Registers a JobHandler for s3_lifecycle via pluginworker.RegisterHandler.
Admin pulls the descriptor over the worker plugin gRPC and renders the
AdminConfigForm + WorkerConfigForm in the existing UI:

  Admin form (cluster shape):
    - workers (1..16, default 1)
    - s3_grpc_endpoints (comma list)

  Worker form (operational tuning):
    - dispatch_tick_ms (default 5000)
    - checkpoint_tick_ms (default 30000)
    - refresh_interval_ms (default 300000)
    - event_budget (default 0 = unbounded)

Detect emits a single proposal whenever S3 endpoints + filer addresses
are configured. MaxExecutionConcurrency=1 so admin only ever runs one
lifecycle daemon per worker; a fresh proposal next cycle restarts it
if the prior Execute exits.

Execute dials the configured S3 endpoint + filer, builds a
scheduler.Scheduler with the parsed config, and runs it until
ctx cancellation. Reuses the existing scheduler / dispatcher /
reader / engine packages — the handler is the thin glue that
parses descriptor values and wires the long-running daemon.

* proto(plugin): add s3_grpc_addresses to ClusterContext

So workers can dial s3 servers discovered by the master rather than a
hand-typed list in the admin form.

* feat(admin): populate ClusterContext.s3_grpc_addresses from master

ListClusterNodes(S3Type) returns the live S3 servers; the plugin
scheduler now hands these to job handlers alongside filer/volume
addresses.

* feat(worker/s3_lifecycle): discover s3 endpoints from cluster context

Drop the s3_grpc_endpoints admin form field and read the master-supplied
ClusterContext.S3GrpcAddresses instead. Operators no longer maintain a
hand-typed list, and a stale entry self-heals when the master's view
updates.

* feat(worker/s3_lifecycle): time-based runtime cap, friendlier cadence units

- dispatch_tick_minutes (was *_ms): minutes is the natural granularity
  for a daily batch; default 1 minute.
- checkpoint_tick_seconds: seconds for the durable cursor write; default
  30 seconds.
- refresh_interval_minutes: minutes for the engine snapshot rebuild.
- max_runtime_minutes replaces event_budget. Each daily run is bounded
  by wall clock — typical run wraps in well under an hour because the
  cursor persists and the meta-log streams fast. Default 60 minutes.
- AdminRuntimeDefaults.DetectionIntervalSeconds = 86400 so the admin
  schedules one job per day.
2026-05-08 10:30:02 -07:00

167 lines
4.0 KiB
Go

package scheduler
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/dispatcher"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/engine"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/reader"
)
const (
defaultRefreshInterval = 5 * time.Minute
defaultRetryBackoff = 5 * time.Second
)
// Scheduler runs N pipeline goroutines, one per worker, each owning a
// contiguous shard slice of [0, ShardCount). It periodically rebuilds the
// engine snapshot from the filer's bucket configs.
type Scheduler struct {
BucketsPath string
Engine *engine.Engine
Persister reader.Persister
Client dispatcher.LifecycleClient
FilerClient filer_pb.SeaweedFilerClient
ClientID int32
ClientName string
Workers int
DispatchTick time.Duration
CheckpointTick time.Duration
RefreshInterval time.Duration
RetryBackoff time.Duration
}
// Run blocks until ctx is canceled. Spawns Workers + 1 goroutines: one
// engine-refresh ticker plus one Pipeline runner per worker.
func (s *Scheduler) Run(ctx context.Context) error {
if s.Engine == nil || s.Persister == nil || s.Client == nil || s.FilerClient == nil {
return errors.New("scheduler: missing required dependency")
}
if s.BucketsPath == "" {
return errors.New("scheduler: BucketsPath required")
}
workers := s.Workers
if workers <= 0 {
workers = 1
}
refresh := s.RefreshInterval
if refresh <= 0 {
refresh = defaultRefreshInterval
}
retry := s.RetryBackoff
if retry <= 0 {
retry = defaultRetryBackoff
}
s.refreshEngine(ctx)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
t := time.NewTicker(refresh)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
s.refreshEngine(ctx)
}
}
}()
for i := 0; i < workers; i++ {
shards := AssignShards(i, workers)
if len(shards) == 0 {
continue
}
wg.Add(1)
go func(idx int, shardSet []int) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
}
p := &dispatcher.Pipeline{
Shards: shardSet,
BucketsPath: s.BucketsPath,
Engine: s.Engine,
Persister: s.Persister,
Client: s.Client,
FilerClient: s.FilerClient,
ClientID: s.ClientID,
ClientName: fmt.Sprintf("%s-w%02d", s.ClientName, idx),
DispatchTick: s.DispatchTick,
CheckpointTick: s.CheckpointTick,
}
if err := p.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
glog.Warningf("lifecycle scheduler: worker=%d shards=%v: %v", idx, shardSet, err)
}
select {
case <-ctx.Done():
return
case <-time.After(retry):
}
}
}(i, shards)
}
wg.Wait()
return nil
}
func (s *Scheduler) refreshEngine(ctx context.Context) {
inputs, parseErrors, err := LoadCompileInputs(ctx, s.FilerClient, s.BucketsPath)
if err != nil {
glog.Warningf("lifecycle scheduler: refresh engine: %v", err)
return
}
for _, pe := range parseErrors {
glog.Warningf("lifecycle scheduler: malformed config in bucket %s: %v", pe.Bucket, pe.Err)
}
s.Engine.Compile(inputs, engine.CompileOptions{PriorStates: AllActivePriorStates(inputs)})
}
// AssignShards returns the contiguous shard slice for worker idx of total.
// Shards distribute as evenly as possible: with ShardCount=16 and total=3,
// workers receive [0..5], [6..10], [11..15].
func AssignShards(idx, total int) []int {
if total <= 0 || idx < 0 || idx >= total {
return nil
}
shardCount := s3lifecycle.ShardCount
base := shardCount / total
extra := shardCount % total
var lo, hi int
if idx < extra {
lo = idx * (base + 1)
hi = lo + base + 1
} else {
lo = idx*base + extra
hi = lo + base
}
if hi > shardCount {
hi = shardCount
}
if lo >= hi {
return nil
}
out := make([]int, 0, hi-lo)
for i := lo; i < hi; i++ {
out = append(out, i)
}
return out
}