diff --git a/weed/filer/redis3/kv_directory_children.go b/weed/filer/redis3/kv_directory_children.go index 62b5ae6345..b9d6a9e3f8 100644 --- a/weed/filer/redis3/kv_directory_children.go +++ b/weed/filer/redis3/kv_directory_children.go @@ -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 } diff --git a/weed/filer/redis3/universal_redis_store.go b/weed/filer/redis3/universal_redis_store.go index 184df38f61..6316dafe20 100644 --- a/weed/filer/redis3/universal_redis_store.go +++ b/weed/filer/redis3/universal_redis_store.go @@ -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) { diff --git a/weed/filer/redis3/universal_redis_store_test.go b/weed/filer/redis3/universal_redis_store_test.go index 23662bfacf..191cafca09 100644 --- a/weed/filer/redis3/universal_redis_store_test.go +++ b/weed/filer/redis3/universal_redis_store_test.go @@ -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 {