redis3: list an expired update's name before writing its value back

An update that wrote back an expired value and then failed to list the
name deleted the value again when it still held the update's bytes. A
concurrent write of the same bytes was deleted with it, and a failed
delete or a concurrent update of other bytes left the value without its
name for a retry that no longer looked.

The update now writes with SET XX. When the value is missing, it lists
the name and then writes the value under the list's lock, which the
removal of an expired name also takes. A failure leaves the value
missing, so the retry lists the name again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-04 16:56:36 -08:00
1 parent bce4934cca
commit f2fd66a96b
3 files changed
+54 -50

No files matched your search

+10 -6
View File
@@ -10,7 +10,10 @@ import (
const maxNameBatchSizeLimit = 1000000
func insertChild(ctx context.Context, redisStore *UniversalRedis3Store, key string, name string) error {
// insertChild adds name to the list at key. A non-nil write runs under the
// list's lock once the name is listed, so no removeChild of the name runs
// between the two.
func insertChild(ctx context.Context, redisStore *UniversalRedis3Store, key string, name string, write func() error) error {
// lock and unlock
mutex := redisStore.redsync.NewMutex(key + "lock")
@@ -36,14 +39,15 @@ func insertChild(ctx context.Context, redisStore *UniversalRedis3Store, key stri
return err
}
if !nameList.HasChanges() {
return nil
if nameList.HasChanges() {
if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
return err
}
}
if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
return err
if write != nil {
return write()
}
return nil
}
+39 -40
View File
@@ -74,71 +74,70 @@ 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)
}
func (store *UniversalRedis3Store) insertIntoParent(ctx context.Context, fullpath util.FullPath) error {
dir, name := fullpath.DirAndName()
dir, name := entry.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", fullpath, err)
if err = insertChild(ctx, store, store.getKey(genDirectoryListKey(dir)), name, nil); err != nil {
return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
}
}
return nil
}
// 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`
// deleteEntryScript deletes an entry's value if it is still the given one.
const deleteEntryScript = `
if redis.call('GET', KEYS[1]) == ARGV[1] then
return redis.call('DEL', KEYS[1])
end
return 0`
func (store *UniversalRedis3Store) doInsertEntry(ctx context.Context, entry *filer.Entry) (value []byte, existed bool, err error) {
value, err = entry.EncodeAttributesAndChunks()
func encodeEntry(entry *filer.Entry) ([]byte, error) {
value, err := entry.EncodeAttributesAndChunks()
if err != nil {
return nil, false, fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
return nil, fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
}
if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
value = util.MaybeGzipData(value)
}
return value, nil
}
n, err := store.Client.Eval(ctx, setEntryScript, []string{store.getKey(string(entry.FullPath))}, value, entry.TtlSec).Int()
func (store *UniversalRedis3Store) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
value, err := encodeEntry(entry)
if err != nil {
return nil, false, fmt.Errorf("persisting %s : %v", entry.FullPath, err)
return err
}
return value, n != 0, nil
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)
}
return nil
}
func (store *UniversalRedis3Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
value, existed, err := store.doInsertEntry(ctx, entry)
if err != nil || existed {
value, err := encodeEntry(entry)
if err != nil {
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.
if err = store.insertIntoParent(ctx, entry.FullPath); err != nil {
// leave the value missing, so that a retry lists the name again
if delErr := store.Client.Eval(ctx, deleteEntryScript, []string{store.getKey(string(entry.FullPath))}, value).Err(); delErr != nil {
glog.WarningfCtx(ctx, "update %s: take back the value: %v", entry.FullPath, delErr)
}
updated, err := store.Client.SetXX(ctx, store.getKey(string(entry.FullPath)), value, time.Duration(entry.TtlSec)*time.Second).Result()
if err != nil {
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
}
return err
if updated {
return nil
}
// The value expired since the caller read it, and a lookup or listing may
// have taken its name out of the directory since. The name is listed before
// the value is written back, under the lock such a removal takes, so a failure
// leaves the value missing and a retry lists the name again.
dir, name := entry.FullPath.DirAndName()
if name == "" {
return store.doInsertEntry(ctx, entry)
}
return insertChild(ctx, store, store.getKey(genDirectoryListKey(dir)), name, func() error {
return store.doInsertEntry(ctx, entry)
})
}
func (store *UniversalRedis3Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
@@ -356,8 +356,8 @@ func TestUpdateEntryOfAnExpiredValueListsItAgain(t *testing.T) {
}
}
// An update that writes an expired value again and then fails to list the name
// takes the value back, so that its retry lists the name.
// An update that finds the value expired lists the name before it writes the
// value back. Failing to list it leaves the value missing, so a retry lists it.
func TestUpdateEntryFailingToListTheNameRetries(t *testing.T) {
store, dir := newTestStore(t)
ctx := context.Background()
@@ -375,16 +375,17 @@ func TestUpdateEntryFailingToListTheNameRetries(t *testing.T) {
FullPath: path,
Attr: filer.Attr{Crtime: now, Mtime: now, Mode: 0644, TtlSec: 7200},
}
// the list lock is taken with SET NX
// let the update's SET XX through, then fail the SET NX that takes the list lock
failing := &failingCommands{name: "set"}
store.Client.AddHook(failing)
failing.skip.Store(1)
failing.left.Store(1 << 20)
if err := store.UpdateEntry(ctx, update); err == nil {
t.Fatalf("UpdateEntry succeeded although the list lock failed")
}
failing.left.Store(0)
if n, err := store.Client.Exists(ctx, store.getKey(string(path))).Result(); err != nil || n != 0 {
t.Fatalf("value exists = %d, %v; want it taken back", n, err)
t.Fatalf("value exists = %d, %v; want it left missing", n, err)
}
if err := store.UpdateEntry(ctx, update); err != nil {