Files
seaweedfs/weed/filer/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

263 lines
6.7 KiB
Go

package filer
import (
"os"
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/util"
)
type Attr struct {
Mtime time.Time // time of last modification
Crtime time.Time // time of creation (OS X only)
Ctime time.Time // time of last inode change
Atime time.Time // time of last access
Mode os.FileMode // file mode
Uid uint32 // owner uid
Gid uint32 // group gid
Mime string // mime type
TtlSec int32 // ttl in seconds
UserName string
GroupNames []string
SymlinkTarget string
Md5 []byte
FileSize uint64
Rdev uint32
Inode uint64
}
func (attr Attr) IsDirectory() bool {
return attr.Mode&os.ModeDir > 0
}
type Entry struct {
util.FullPath
Attr
Extended map[string][]byte
// the following is for files
Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"`
HardLinkId HardLinkId
HardLinkCounter int32
Content []byte
Remote *filer_pb.RemoteEntry
Quota int64
WORMEnforcedAtTsNs int64
// CommitTsNs is the meta log event timestamp of the mutation that
// produced this version. Zero for pre-field versions; they order by mtime.
CommitTsNs int64
}
func (entry *Entry) Size() uint64 {
return maxUint64(maxUint64(TotalSize(entry.GetChunks()), entry.FileSize), uint64(len(entry.Content)))
}
func (entry *Entry) Timestamp() time.Time {
if entry.IsDirectory() {
return entry.Crtime
} else {
return entry.Mtime
}
}
func (entry *Entry) ShallowClone() *Entry {
if entry == nil {
return nil
}
newEntry := &Entry{}
newEntry.FullPath = entry.FullPath
newEntry.Attr = entry.Attr
newEntry.Chunks = entry.Chunks
newEntry.Extended = entry.Extended
newEntry.HardLinkId = entry.HardLinkId
newEntry.HardLinkCounter = entry.HardLinkCounter
newEntry.Content = entry.Content
newEntry.Remote = entry.Remote
newEntry.Quota = entry.Quota
newEntry.WORMEnforcedAtTsNs = entry.WORMEnforcedAtTsNs
newEntry.CommitTsNs = entry.CommitTsNs
return newEntry
}
func (entry *Entry) ToProtoEntry() *filer_pb.Entry {
if entry == nil {
return nil
}
message := &filer_pb.Entry{}
message.Name = entry.FullPath.Name()
entry.ToExistingProtoEntry(message)
return message
}
func (entry *Entry) ToExistingProtoEntry(message *filer_pb.Entry) {
if entry == nil {
return
}
message.IsDirectory = entry.IsDirectory()
// Reuse pre-allocated attributes if available, otherwise allocate
if message.Attributes != nil {
EntryAttributeToExistingPb(entry, message.Attributes)
} else {
message.Attributes = EntryAttributeToPb(entry)
}
message.Chunks = entry.GetChunks()
message.Extended = entry.Extended
message.HardLinkId = entry.HardLinkId
message.HardLinkCounter = entry.HardLinkCounter
message.Content = entry.Content
message.RemoteEntry = entry.Remote
message.Quota = entry.Quota
message.WormEnforcedAtTsNs = entry.WORMEnforcedAtTsNs
message.CommitTsNs = entry.CommitTsNs
}
func FromPbEntryToExistingEntry(message *filer_pb.Entry, fsEntry *Entry) {
fsEntry.Attr = PbToEntryAttribute(message.Attributes)
fsEntry.Chunks = message.Chunks
fsEntry.Extended = message.Extended
fsEntry.HardLinkId = HardLinkId(message.HardLinkId)
fsEntry.HardLinkCounter = message.HardLinkCounter
fsEntry.Content = message.Content
fsEntry.Remote = message.RemoteEntry
fsEntry.Quota = message.Quota
fsEntry.FileSize = FileSize(message)
fsEntry.WORMEnforcedAtTsNs = message.WormEnforcedAtTsNs
fsEntry.CommitTsNs = message.CommitTsNs
}
func (entry *Entry) ToProtoFullEntry() *filer_pb.FullEntry {
if entry == nil {
return nil
}
dir, _ := entry.FullPath.DirAndName()
return &filer_pb.FullEntry{
Dir: dir,
Entry: entry.ToProtoEntry(),
}
}
func (entry *Entry) GetChunks() []*filer_pb.FileChunk {
return entry.Chunks
}
func FromPbEntry(dir string, entry *filer_pb.Entry) *Entry {
t := &Entry{}
t.FullPath = util.NewFullPath(dir, entry.Name)
FromPbEntryToExistingEntry(entry, t)
return t
}
func maxUint64(x, y uint64) uint64 {
if x > y {
return x
}
return y
}
// IsDirectoryKeyObject mirrors filer_pb.Entry.IsDirectoryKeyObject, so a
// promoted file's data is kept.
func (entry *Entry) IsDirectoryKeyObject() bool {
_, isPrefixObject := entry.Extended[s3_constants.SeaweedFSPrefixObject]
return entry.IsDirectory() && (entry.Mime != "" || len(entry.GetChunks()) > 0 || len(entry.Content) > 0 || entry.IsInRemoteOnly() || isPrefixObject)
}
func (entry *Entry) IsExpireS3Enabled() (exist bool) {
if entry.Extended != nil {
_, exist = entry.Extended[s3_constants.SeaweedFSExpiresS3]
}
return exist
}
func (entry *Entry) isS3Entry() bool {
if entry.Extended == nil {
return false
}
if _, found := entry.Extended[s3_constants.ExtAmzOwnerKey]; found {
return true
}
if _, found := entry.Extended[s3_constants.ExtETagKey]; found {
return true
}
for key := range entry.Extended {
if strings.HasPrefix(key, s3_constants.ExtAmzPrefix) {
return true
}
}
return false
}
// ApplyStorageTtl stamps the TTL the entry's data was placed with. Remote
// entries never expire locally; the remote storage owns their lifecycle.
func (entry *Entry) ApplyStorageTtl(ttlSec int32) {
if entry.Remote != nil {
ttlSec = 0
}
entry.TtlSec = ttlSec
entry.ApplyS3ExpiryMetadata()
}
func (entry *Entry) ApplyS3ExpiryMetadata() {
if entry.TtlSec == 0 {
return
}
if _, found := entry.Extended[s3_constants.SeaweedFSExpiresS3]; found {
return
}
// The s3 expiry path skips versioned entries, so stamping one would drop its
// expiry entirely instead of moving it onto mtime.
if entry.IsS3Versioning() {
return
}
if !entry.isS3Entry() {
return
}
entry.Extended[s3_constants.SeaweedFSExpiresS3] = []byte("true")
return
}
func (entry *Entry) IsS3Versioning() (exist bool) {
if entry.Extended != nil {
_, exist = entry.Extended[s3_constants.ExtVersionIdKey]
}
return exist
}
// ExpiresAt is when listings start leaving entry out; ok is false for an
// entry they never leave out: a directory, one with no TTL, and a version of
// a versioned S3 object.
func (entry *Entry) ExpiresAt() (expiresAt time.Time, ok bool) {
if entry.TtlSec <= 0 || entry.IsDirectory() {
return time.Time{}, false
}
if entry.IsExpireS3Enabled() {
if entry.IsS3Versioning() {
return time.Time{}, false
}
return entry.GetS3ExpireTime(), true
}
return entry.Crtime.Add(time.Duration(entry.TtlSec) * time.Second), true
}
func (entry *Entry) GetS3ExpireTime() (expireTime time.Time) {
if entry.Mtime.IsZero() {
expireTime = entry.Crtime
} else {
expireTime = entry.Mtime
}
return expireTime.Add(time.Duration(entry.TtlSec) * time.Second)
}