package filer_ttl import ( "context" "fmt" "math" "strings" "time" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/pb/plugin_pb" pluginworker "github.com/seaweedfs/seaweedfs/weed/plugin/worker" "google.golang.org/grpc" ) const ( jobType = "filer_ttl" filerAddressParam = "filer_address" defaultDetectionMinutes = 24 * 60 ) func init() { pluginworker.RegisterHandler(pluginworker.HandlerFactory{ JobType: jobType, Category: pluginworker.CategoryDefault, Aliases: []string{"filer-ttl", "filer.ttl"}, Build: func(opts pluginworker.HandlerBuildOptions) (pluginworker.JobHandler, error) { return NewHandler(opts.GrpcDialOption), nil }, }) } // Handler removes the filer rows of entries whose filer.conf path TTL has // passed. A store without native TTL keeps such rows until something looks // them up, long after the TTL volumes holding the data are gone. type Handler struct { grpcDialOption grpc.DialOption } func NewHandler(grpcDialOption grpc.DialOption) *Handler { return &Handler{grpcDialOption: grpcDialOption} } func (h *Handler) Capability() *plugin_pb.JobTypeCapability { return &plugin_pb.JobTypeCapability{ JobType: jobType, CanDetect: true, CanExecute: true, MaxDetectionConcurrency: 1, MaxExecutionConcurrency: 1, DisplayName: "Filer TTL", Description: "Replay filer metadata events under filer.conf TTL paths so the filer removes the rows of expired entries.", Weight: 20, } } func (h *Handler) Descriptor() *plugin_pb.JobTypeDescriptor { return &plugin_pb.JobTypeDescriptor{ JobType: jobType, DisplayName: "Filer TTL", Description: "Remove the filer rows of entries past their filer.conf path TTL, once per filer store.", Icon: "bi bi-hourglass-split", DescriptorVersion: 1, AdminRuntimeDefaults: &plugin_pb.AdminRuntimeDefaults{ Enabled: true, DetectionIntervalMinutes: defaultDetectionMinutes, DetectionTimeoutSeconds: 60, ExecutionTimeoutSeconds: math.MaxInt32, JobTypeMaxRuntimeSeconds: math.MaxInt32, }, WorkerDefaultValues: map[string]*plugin_pb.ConfigValue{}, } } func (h *Handler) Detect(ctx context.Context, request *plugin_pb.RunDetectionRequest, sender pluginworker.DetectionSender) error { if request == nil || sender == nil { return fmt.Errorf("detect: nil request or sender") } if request.JobType != "" && request.JobType != jobType { return fmt.Errorf("job type %q is not handled by filer ttl handler", request.JobType) } filers := pluginworker.ClusterFilerAddresses(request.GetClusterContext()) if len(filers) == 0 { _ = sender.SendActivity(pluginworker.BuildDetectorActivity("skipped", "no filer addresses in cluster context", nil)) return sender.SendComplete(&plugin_pb.DetectionComplete{JobType: jobType, Success: true}) } var proposals []*plugin_pb.JobProposal var readErrors []string for _, address := range pluginworker.OneFilerPerStore(ctx, filers, h.grpcDialOption, sender) { client := &pluginworker.AdminFilerClient{Address: address, GrpcDialOption: h.grpcDialOption} fc, err := filer.ReadFilerConfFromClient(ctx, client, client) if err != nil { readErrors = append(readErrors, fmt.Sprintf("filer %s: %v", address, err)) continue } rules := ttlRules(fc) if len(rules) == 0 { continue } proposals = append(proposals, &plugin_pb.JobProposal{ JobType: jobType, ProposalId: fmt.Sprintf("filer-ttl-%s-%d", address, time.Now().UnixNano()), DedupeKey: fmt.Sprintf("%s:%s", jobType, address), Priority: plugin_pb.JobPriority_JOB_PRIORITY_LOW, Summary: fmt.Sprintf("Expire entries under %d TTL path(s) on the store of filer %s", len(rules), address), Parameters: map[string]*plugin_pb.ConfigValue{ filerAddressParam: {Kind: &plugin_pb.ConfigValue_StringValue{StringValue: string(address)}}, }, }) } if len(proposals) > 0 { if err := sender.SendProposals(&plugin_pb.DetectionProposals{JobType: jobType, Proposals: proposals}); err != nil { return err } } return sender.SendComplete(&plugin_pb.DetectionComplete{ JobType: jobType, Success: len(readErrors) == 0, ErrorMessage: strings.Join(readErrors, "; "), TotalProposals: int32(len(proposals)), }) } func (h *Handler) Execute(ctx context.Context, request *plugin_pb.ExecuteJobRequest, sender pluginworker.ExecutionSender) error { if request == nil || request.Job == nil || sender == nil { return fmt.Errorf("execute: nil request/job/sender") } if request.Job.JobType != "" && request.Job.JobType != jobType { return fmt.Errorf("job type %q is not handled by filer ttl handler", request.Job.JobType) } address := pluginworker.ReadStringConfig(request.Job.Parameters, filerAddressParam, "") if address == "" { return fmt.Errorf("execute: missing %s in job parameters", filerAddressParam) } client := &pluginworker.AdminFilerClient{Address: pb.ServerAddress(address), GrpcDialOption: h.grpcDialOption} _ = sender.SendProgress(&plugin_pb.JobProgressUpdate{ JobId: request.Job.JobId, JobType: jobType, State: plugin_pb.JobState_JOB_STATE_RUNNING, Stage: "replaying", Message: fmt.Sprintf("filer %s", address), }) stats := &runStats{} var rules []ttlRule fc, err := filer.ReadFilerConfFromClient(ctx, client, client) if err == nil { rules = ttlRules(fc) err = client.WithFilerClient(true, func(c filer_pb.SeaweedFilerClient) error { return expireByEvents(ctx, c, rules, stats) }) } summary := fmt.Sprintf("filer %s: %d TTL path(s), %d events, %d expired entries looked up, %d still waiting", address, len(rules), stats.events, stats.expired, stats.pending) if stats.directories > 0 { summary += fmt.Sprintf(", listed %d directories and %d live entries", stats.directories, stats.entries) } completed := &plugin_pb.JobCompleted{ JobId: request.Job.JobId, JobType: jobType, Success: err == nil, Result: &plugin_pb.JobResult{ Summary: summary, OutputValues: map[string]*plugin_pb.ConfigValue{ "events": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: stats.events}}, "expired": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: stats.expired}}, "pending": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: stats.pending}}, "directories": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: stats.directories}}, "entries": {Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: stats.entries}}, }, }, } if err != nil { completed.ErrorMessage = err.Error() } return sender.SendCompleted(completed) }