Files
seaweedfs/weed/filer/filer_expired_entry.go
T
e9ea21340b filer: a lookup removes what a redis-expired entry left behind (#13397)
* filer: a lookup removes what a redis-expired entry left behind

Redis expires an entry's value on its own and leaves the name in the
parent directory's list. A listing drops such a member when it meets it,
but a lookup finds nothing and returns, so the names of entries nobody
lists stay, and so do the directories they leave empty. The filer_ttl
job expires entries by looking them up and cannot reach them either.

A lookup that finds nothing under a filer.conf TTL rule now asks the
store for what expiry left of that name. The redis stores remove the
list member, guarded against a concurrent recreate as on a listing, and
when there was one the filer removes the directories this leaves empty,
as a listing that meets an expired entry does. A miss with no member to
remove costs one command, and only under a TTL rule.

* filer: keep a directory that holds an object when removing empty parents

A file promoted to a directory by a child write keeps its own object data.
Once its last child expires the directory is empty, and the cleanup
deleted it along with that object.

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

* filer: report a failed removal of a redis-expired leftover

RemoveExpiredLeftover returned a nil error even when the redis SREM or
ZREM failed, so the filer could not log why an expired name stayed.

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

* filer: check a redis orphan repair's keys on the master

With useReadOnly or routeByLatency the cluster client sends EXISTS to a
replica. A lagging replica misreads a just-written value as absent, and
the repair then drops a live entry from its directory list. A lookup miss
now reaches this repair, so check the keys with a script, which runs on
the master, as the redis2 store already does.

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

* filer: a lookup's cleanup leaves directories alone and asks the master first

Removing the directory a redis-expired entry left empty from the lookup
went through DeleteEmptyParentDirectories, which makes none of the empty
folder cleaner's checks. With a leftover as the trigger it removed a
multipart upload's staging directory together with .uploads, a directory
of a bucket that keeps empty folders, an S3 Tables catalog directory,
and the directory of a file written between its emptiness check and its
delete, which strands that file. The lookup now only has the store
remove the name; the directory stays.

The redis stores also check the master for the value before they touch
the list. A lookup that missed on a lagging replica used to remove a live
entry's member and put it back, and a failure in between lost it.

* redis: a lookup's cleanup looks for the member before it asks the master

Most lookups that find nothing are of a name that was never written.
Those now cost one read of the directory list instead of a script and
a removal; only a name the list still holds is checked on the master.

* redis: retry the restore of a directory list member, and report a failure

The orphan repair removes a member and puts it back when the value turns
out to be stored, because an insert got in between. Nothing else puts
that member back, and a failed restore was only logged, so the entry
stayed out of its directory's listing until it was written again. The
restore is now retried a few times, and a restore that still fails is
returned as an error.

* redis: keep restoring a directory list member until redis takes it

A restore that failed every attempt left a live entry out of its directory
list for good, since nothing else puts the member back. Retry it in the
background until it lands or the store closes; the repair still reports
the failure.

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

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 11:30:04 -08:00

104 lines
3.4 KiB
Go

package filer
import (
"context"
"errors"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// deleteExpiredEntry removes an entry a listing found expired. The row is
// re-read under the lock writers take, so a replacement written after the
// listing read the row is kept; a contended path is left to a later listing.
func (f *Filer) deleteExpiredEntry(ctx context.Context, p util.FullPath) error {
if lt := f.EntryLockTable; lt != nil {
lock, acquired := lt.TryAcquireLock("deleteExpiredEntry", p, util.ExclusiveLock)
if !acquired {
return nil
}
defer lt.ReleaseLock(p, lock)
}
current, err := f.Store.FindEntry(ctx, p)
if err != nil {
if errors.Is(err, filer_pb.ErrNotFound) {
return nil
}
return err
}
if !current.IsExpireS3Enabled() {
_, err = f.deleteIfExpired(ctx, p)
return err
}
// Its remote delete and event would outlive a refused commit, so it is
// removed outside a transaction.
if ctx.Value(storeTransactionKey{}) != nil || current.IsDirectory() || current.TtlSec <= 0 ||
current.IsS3Versioning() || !current.GetS3ExpireTime().Before(time.Now()) {
return nil
}
return f.doDeleteEntryMetaAndData(ctx, current, true, false, nil)
}
// deleteIfExpired removes p's row if its TTL has passed, and reports whether
// the store then holds nothing at p. On a store that refuses conflicting
// commits the re-read and the delete share a transaction, so a replacement
// written through another filer is kept.
func (f *Filer) deleteIfExpired(ctx context.Context, p util.FullPath) (gone bool, err error) {
if !f.RefusesConflictingCommits(p) || ctx.Value(storeTransactionKey{}) != nil {
return f.deleteRowIfExpired(ctx, p)
}
txCtx, err := f.BeginTransaction(ctx)
if err != nil {
return false, err
}
if gone, err = f.deleteRowIfExpired(txCtx, p); err == nil {
err = f.CommitTransaction(txCtx)
}
if err != nil {
f.RollbackTransaction(txCtx)
if errors.Is(err, ErrTransactionConflict) {
return false, nil
}
return false, err
}
return gone, nil
}
func (f *Filer) deleteRowIfExpired(ctx context.Context, p util.FullPath) (gone bool, err error) {
current, err := f.Store.FindEntry(ctx, p)
if err != nil {
if errors.Is(err, filer_pb.ErrNotFound) {
return true, nil
}
return false, err
}
if current.IsDirectory() || current.TtlSec <= 0 || current.IsExpireS3Enabled() || !current.Crtime.Add(time.Duration(current.TtlSec)*time.Second).Before(time.Now()) {
return false, nil
}
if err := f.Store.DeleteOneEntry(ctx, current); err != nil {
return false, err
}
return true, nil
}
// RemoveExpiredLeftover is for a lookup that found nothing at p under a
// filer.conf TTL rule. On a store whose rows expire on their own it removes
// what such a row left behind. The directory this may leave empty stays:
// removing one safely takes the empty folder cleaner's checks, which
// DeleteEmptyParentDirectories does not make.
func (f *Filer) RemoveExpiredLeftover(ctx context.Context, p util.FullPath) {
fsw, ok := f.Store.(*FilerStoreWrapper)
if !ok {
return
}
store := fsw.expiredLeftoverStore(p)
if store == nil || f.FilerConf == nil || f.FilerConf.MatchStorageRule(string(p)).GetTtl() == "" {
return
}
if _, err := store.RemoveExpiredLeftover(ctx, p); err != nil {
glog.V(1).InfofCtx(ctx, "remove what expired at %s: %v", p, err)
}
}