From b380d4ab532a80bb0ddfa2a573af4d33645236a0 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 5 Jul 2022 09:25:23 +0300 Subject: [PATCH] [#542] Clear list cache on object removal Signed-off-by: Denis Kirillov --- api/handler/delete_test.go | 29 +++++++++++++++++++++++++++++ api/layer/layer.go | 3 +++ 2 files changed, 32 insertions(+) diff --git a/api/handler/delete_test.go b/api/handler/delete_test.go index 45ae8f7..1e3ae2b 100644 --- a/api/handler/delete_test.go +++ b/api/handler/delete_test.go @@ -134,6 +134,26 @@ func TestDeleteMarkers(t *testing.T) { require.Len(t, listOIDsFromMockedNeoFS(t, tc, bktName, objName), 0, "shouldn't be any object in neofs") } +func TestDeleteObjectFromListCache(t *testing.T) { + tc := prepareHandlerContext(t) + + bktName, objName := "bucket-for-removal", "object-to-delete" + bktInfo, objInfo := createVersionedBucketAndObject(t, tc, bktName, objName) + + versions := listObjectsV1(t, tc, bktName) + require.Len(t, versions.Contents, 1) + + checkFound(t, tc, bktName, objName, objInfo.Version()) + deleteObject(t, tc, bktName, objName, objInfo.Version()) + checkNotFound(t, tc, bktName, objName, objInfo.Version()) + + // check cache is clean after object removal + versions = listObjectsV1(t, tc, bktName) + require.Len(t, versions.Contents, 0) + + require.False(t, existInMockedNeoFS(tc, bktInfo, objInfo)) +} + func createBucketAndObject(t *testing.T, tc *handlerContext, bktName, objName string) (*data.BucketInfo, *data.ObjectInfo) { createTestBucket(tc.Context(), t, tc, bktName) bktInfo, err := tc.Layer().GetBucketInfo(tc.Context(), bktName) @@ -202,3 +222,12 @@ func listVersions(t *testing.T, tc *handlerContext, bktName string) *ListObjects parseTestResponse(t, w, res) return res } + +func listObjectsV1(t *testing.T, tc *handlerContext, bktName string) *ListObjectsV1Response { + w, r := prepareTestRequest(t, bktName, "", nil) + tc.Handler().ListObjectsV1Handler(w, r) + assertStatus(t, w, http.StatusOK) + res := &ListObjectsV1Response{} + parseTestResponse(t, w, res) + return res +} diff --git a/api/layer/layer.go b/api/layer/layer.go index 8c6dab4..bf73c84 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -486,6 +486,7 @@ func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, settings } obj.Error = n.treeService.RemoveVersion(ctx, bkt.CID, nodeVersion.ID) + n.listsCache.CleanCacheEntriesContainingObject(obj.Name, bkt.CID) return obj } @@ -528,7 +529,9 @@ func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, settings if obj.Error = n.treeService.AddVersion(ctx, bkt.CID, newVersion); obj.Error != nil { return obj } + n.namesCache.Delete(bkt.Name + "/" + obj.Name) + n.listsCache.CleanCacheEntriesContainingObject(obj.Name, bkt.CID) return obj }