From 6ded6f8db51bae0af5c948a532545792f7543959 Mon Sep 17 00:00:00 2001 From: ShuNing Date: Sat, 18 May 2024 13:52:13 +0800 Subject: [PATCH] core/region: fix sub tree may not consist with root tree (#8187) ref tikv/pd#7897 core/region: fix the sub tree may not consist of 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 7084eb36261..54091968d1a 100644 --- a/pkg/core/region.go +++ b/pkg/core/region.go @@ -1044,10 +1044,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 7feb4e432c4..b15e8f38b93 100644 --- a/pkg/core/region_test.go +++ b/pkg/core/region_test.go @@ -1072,3 +1072,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()) +}