From 2274c1a949f18b1254d9c9a54c4d69623864afc1 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 17 May 2024 15:07:21 +0800 Subject: [PATCH] core/region: fix the put sub tree may not consist with root tree Signed-off-by: nolouch --- pkg/core/region.go | 6 +++--- pkg/core/region_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/core/region.go b/pkg/core/region.go index be8fcddc179..affe6e14406 100644 --- a/pkg/core/region.go +++ b/pkg/core/region.go @@ -1041,10 +1041,10 @@ func (r *RegionsInfo) CheckAndPutRootTree(ctx *MetaProcessContext, region *Regio // Usually used with CheckAndPutRootTree together. func (r *RegionsInfo) CheckAndPutSubTree(region *RegionInfo) { // new region get from root tree again - var newRegion *RegionInfo - newRegion = r.GetRegion(region.GetID()) + newRegion := r.GetRegion(region.GetID()) if newRegion == nil { - newRegion = region + // Make sure there is this region in the root tree, so as to ensure the correctness of reference count + return } r.UpdateSubTreeOrderInsensitive(newRegion) } diff --git a/pkg/core/region_test.go b/pkg/core/region_test.go index 43629fccda0..4a418a16ebc 100644 --- a/pkg/core/region_test.go +++ b/pkg/core/region_test.go @@ -1021,3 +1021,12 @@ func TestUpdateRegionEventualConsistency(t *testing.T) { re.Equal(int32(2), item.GetRef()) } } + +func TestCheckAndPutSubTree(t *testing.T) { + re := require.New(t) + regions := NewRegionsInfo() + region := NewTestRegionInfo(1, 1, []byte("a"), []byte("b")) + regions.CheckAndPutSubTree(region) + // should failed to put because the root tree is missing + re.Equal(0, regions.tree.length()) +}