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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-04 11:48:18 -08:00
1 parent 7f0cb0f038
commit e5646d55d5
2 files changed
+59 -12

No files matched your search

+30 -12
View File
@@ -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) {
@@ -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)
}
}