From 3e8ff30c679eb5978e913f291c14204093cf86f6 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Fri, 3 Nov 2023 13:58:44 +0100 Subject: [PATCH 1/3] additional check for missed cids --- index/cids.go | 18 ++++++++++++++---- index/cids_test.go | 21 ++++++++++++++++++++- index/loader.go | 4 ++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/index/cids.go b/index/cids.go index 275972d..2e089d4 100644 --- a/index/cids.go +++ b/index/cids.go @@ -26,6 +26,7 @@ func (ri *redisIndex) CidEntries(ctx context.Context, cids []cid.Cid) (entries * entries = &CidEntries{} for _, c := range cids { if err = ri.getAndAddToEntries(ctx, entries, c); err != nil { + entries.Release() return nil, err } } @@ -63,14 +64,15 @@ func (ri *redisIndex) CidEntriesByBlocks(ctx context.Context, bs []blocks.Block) } func (ri *redisIndex) getAndAddToEntries(ctx context.Context, entries *CidEntries, c cid.Cid) (err error) { - ok, release, err := ri.AcquireKey(ctx, cidKey(c)) + _, release, err := ri.AcquireKey(ctx, cidKey(c)) if err != nil { return } - if !ok { + //temporarily ignore the exists check to make a deep check + /*if !ok { release() return ErrCidsNotExist - } + }*/ entry, err := ri.getCidEntry(ctx, c) if err != nil { release() @@ -130,7 +132,15 @@ func (ri *redisIndex) getCidEntry(ctx context.Context, c cid.Cid) (entry *cidEnt cidData, err := ri.cl.Get(ctx, ck).Result() if err != nil { if errors.Is(err, redis.Nil) { - err = ErrCidsNotExist + // temporary additional check: try to load data from store and restore cid + var b blocks.Block + if b, err = ri.persistStore.Get(ctx, c); err != nil { + log.WarnCtx(ctx, "restore cid entry error", zap.String("cid", c.String()), zap.Error(err)) + err = ErrCidsNotExist + return + } + log.InfoCtx(ctx, "restore cid entry", zap.String("cid", c.String())) + return ri.createCidEntry(ctx, b) } return } diff --git a/index/cids_test.go b/index/cids_test.go index 1610b20..0d23626 100644 --- a/index/cids_test.go +++ b/index/cids_test.go @@ -1,10 +1,12 @@ package index import ( + "fmt" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" "github.com/anyproto/any-sync-filenode/index/indexproto" "github.com/anyproto/any-sync-filenode/testutil" @@ -53,7 +55,7 @@ func TestRedisIndex_CidEntries(t *testing.T) { require.NoError(t, fx.BlocksAdd(ctx, bs[:3])) cids := testutil.BlocksToKeys(bs) - + fx.persistStore.EXPECT().Get(ctx, gomock.Any()).Return(nil, fmt.Errorf("err")).AnyTimes() _, err := fx.CidEntries(ctx, cids) assert.EqualError(t, err, ErrCidsNotExist.Error()) }) @@ -88,6 +90,23 @@ func TestRedisIndex_CidEntries(t *testing.T) { assert.NotEmpty(t, e.Version) } }) + t.Run("restore from store", func(t *testing.T) { + bs := testutil.NewRandBlocks(4) + fx := newFixture(t) + defer fx.Finish(t) + + require.NoError(t, fx.BlocksAdd(ctx, bs[:3])) + + cids := testutil.BlocksToKeys(bs) + + fx.persistStore.EXPECT().Get(ctx, bs[3].Cid()).Return(bs[3], nil) + + result, err := fx.CidEntries(ctx, cids) + defer result.Release() + require.NoError(t, err) + require.Len(t, result.entries, len(bs)) + t.Log(result.entries[3]) + }) } func TestRedisIndex_CidExistsInSpace(t *testing.T) { diff --git a/index/loader.go b/index/loader.go index db52b04..49c162c 100644 --- a/index/loader.go +++ b/index/loader.go @@ -11,6 +11,8 @@ import ( "github.com/cespare/xxhash/v2" "github.com/go-redsync/redsync/v4" + blocks "github.com/ipfs/go-block-format" + "github.com/ipfs/go-cid" "github.com/redis/go-redis/v9" "go.uber.org/zap" ) @@ -32,6 +34,8 @@ func init() { type persistentStore interface { IndexGet(ctx context.Context, key string) (value []byte, err error) IndexPut(ctx context.Context, key string, value []byte) (err error) + + Get(ctx context.Context, k cid.Cid) (blocks.Block, error) } func bloomFilterKey(key string) string { From a831dbf2663f9115796acf2a224e38fb8e1b9e3d Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Fri, 3 Nov 2023 13:59:29 +0100 Subject: [PATCH 2/3] migration: ignore missing cids --- index/migrate.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index/migrate.go b/index/migrate.go index 4885a80..2362345 100644 --- a/index/migrate.go +++ b/index/migrate.go @@ -2,6 +2,7 @@ package index import ( "context" + "errors" "strings" "time" @@ -63,6 +64,11 @@ func (ri *redisIndex) Migrate(ctx context.Context, key Key) (err error) { for _, k := range keys { if strings.HasPrefix(k, "f:") { if err = ri.migrateFile(ctx, key, migrateKey, k); err != nil { + if errors.Is(err, ErrCidsNotExist) { + err = nil + log.WarnCtx(ctx, "migrate cid no exists", zap.String("spaceId", key.SpaceId), zap.String("fileId", k)) + continue + } return } } From 14652c27fb08274236db423c531097c7a896192e Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Fri, 3 Nov 2023 14:01:37 +0100 Subject: [PATCH 3/3] index perist check for 0 --- index/loader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index/loader.go b/index/loader.go index 49c162c..c8d92b7 100644 --- a/index/loader.go +++ b/index/loader.go @@ -192,7 +192,7 @@ func (ri *redisIndex) persistKey(ctx context.Context, storeKey, key string, dead stat.errors.Add(1) return } - if int64(res[0]) > deadline { + if int64(res[0]) > deadline || res[0] == 0 { stat.missed.Add(1) return }