From e5646d55d5efb695b03283dfad991657808352db Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 11:48:18 -0800 Subject: [PATCH] redis3: an update that finds its value expired lists the name again UpdateEntry only wrote the value. When redis expired the value between the caller's read and the update, a lookup or listing could take the name out of the directory in between, and the update left a live entry that no listing shows. The value is now written by a script that also reports whether one was stored, and an update that found none adds the name back, under the list's lock as an insert does. Co-Authored-By: Claude Opus 5.5 --- weed/filer/redis3/universal_redis_store.go | 42 +++++++++++++------ .../redis3/universal_redis_store_test.go | 29 +++++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/weed/filer/redis3/universal_redis_store.go b/weed/filer/redis3/universal_redis_store.go index d85d9c6ad3..aba957b055 100644 --- a/weed/filer/redis3/universal_redis_store.go +++ b/weed/filer/redis3/universal_redis_store.go @@ -63,40 +63,58 @@ func (store *UniversalRedis3Store) RollbackTransaction(ctx context.Context) erro func (store *UniversalRedis3Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) { - if err = store.doInsertEntry(ctx, entry); err != nil { + if _, err = store.doInsertEntry(ctx, entry); err != nil { return err } + return store.insertIntoParent(ctx, entry.FullPath) +} - dir, name := entry.FullPath.DirAndName() - +func (store *UniversalRedis3Store) insertIntoParent(ctx context.Context, fullpath util.FullPath) error { + dir, name := fullpath.DirAndName() if name != "" { - if err = insertChild(ctx, store, store.getKey(genDirectoryListKey(dir)), name); err != nil { - return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err) + if err := insertChild(ctx, store, store.getKey(genDirectoryListKey(dir)), name); err != nil { + return fmt.Errorf("persisting %s in parent dir: %v", fullpath, err) } } - return nil } -func (store *UniversalRedis3Store) doInsertEntry(ctx context.Context, entry *filer.Entry) error { +// setEntryScript stores an entry's value and reports whether one was stored before. +const setEntryScript = ` +local existed = redis.call('EXISTS', KEYS[1]) +if tonumber(ARGV[2]) > 0 then + redis.call('SET', KEYS[1], ARGV[1], 'EX', ARGV[2]) +else + redis.call('SET', KEYS[1], ARGV[1]) +end +return existed` + +func (store *UniversalRedis3Store) doInsertEntry(ctx context.Context, entry *filer.Entry) (existed bool, err error) { value, err := entry.EncodeAttributesAndChunks() if err != nil { - return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err) + return false, fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err) } if len(entry.GetChunks()) > filer.CountEntryChunksForGzip { value = util.MaybeGzipData(value) } - if err = store.Client.Set(ctx, store.getKey(string(entry.FullPath)), value, time.Duration(entry.TtlSec)*time.Second).Err(); err != nil { - return fmt.Errorf("persisting %s : %v", entry.FullPath, err) + n, err := store.Client.Eval(ctx, setEntryScript, []string{store.getKey(string(entry.FullPath))}, value, entry.TtlSec).Int() + if err != nil { + return false, fmt.Errorf("persisting %s : %v", entry.FullPath, err) } - return nil + return n != 0, nil } func (store *UniversalRedis3Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) { - return store.doInsertEntry(ctx, entry) + existed, err := store.doInsertEntry(ctx, entry) + if err != nil || existed { + return err + } + // The value expired since the caller read it, and a lookup or listing may + // have taken its name out of the directory since. + return store.insertIntoParent(ctx, entry.FullPath) } func (store *UniversalRedis3Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) { diff --git a/weed/filer/redis3/universal_redis_store_test.go b/weed/filer/redis3/universal_redis_store_test.go index 3682b8a308..c00dba2875 100644 --- a/weed/filer/redis3/universal_redis_store_test.go +++ b/weed/filer/redis3/universal_redis_store_test.go @@ -326,3 +326,32 @@ func TestRemoveExpiredLeftover(t *testing.T) { t.Fatalf("DeleteFolderChildren: %v", err) } } + +// An update that finds the value expired writes it again, and has to list the +// name again too: the cleanup of the expired value may have taken it out. +func TestUpdateEntryOfAnExpiredValueListsItAgain(t *testing.T) { + store, dir := newTestStore(t) + ctx := context.Background() + now := time.Now() + path := dir.Child("updated") + insertTestEntry(t, store, path, now, 3600) + if err := store.Client.Del(ctx, store.getKey(string(path))).Err(); err != nil { + t.Fatalf("del: %v", err) + } + if removed, err := store.RemoveExpiredLeftover(ctx, path); err != nil || !removed { + t.Fatalf("RemoveExpiredLeftover = %v, %v", removed, err) + } + + if err := store.UpdateEntry(ctx, &filer.Entry{ + FullPath: path, + Attr: filer.Attr{Crtime: now, Mtime: now, Mode: 0644, TtlSec: 7200}, + }); err != nil { + t.Fatalf("UpdateEntry: %v", err) + } + if names := listNames(t, store, dir); len(names) != 1 || names[0] != "updated" { + t.Fatalf("listed %v, want [updated]", names) + } + if ttl, err := store.Client.TTL(ctx, store.getKey(string(path))).Result(); err != nil || ttl <= time.Hour || ttl > 2*time.Hour { + t.Fatalf("TTL = %v, %v; want the update's 2h", ttl, err) + } +}