Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

region: fix the potential panic . (#7143) #8916

Open
wants to merge 1 commit into
base: release-6.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
"sort"
"strings"
"sync/atomic"
"time"
"unsafe"

"github.com/docker/go-units"
"github.com/gogo/protobuf/proto"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/kvproto/pkg/replication_modepb"
Expand Down Expand Up @@ -885,7 +887,16 @@
}

// UpdateSubTree updates the subtree.
<<<<<<< HEAD:server/core/region.go

Check failure on line 890 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: non-declaration statement outside function body
func (r *RegionsInfo) UpdateSubTree(region, origin *RegionInfo, toRemove []*RegionInfo, rangeChanged bool) {
=======

Check failure on line 892 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: unexpected ==, expecting }
func (r *RegionsInfo) UpdateSubTree(region, origin *RegionInfo, overlaps []*RegionInfo, rangeChanged bool) {
failpoint.Inject("UpdateSubTree", func() {
if origin == nil {
time.Sleep(time.Second)
}
})
>>>>>>> 54219d649 (region: fix the potential panic . (#7143)):pkg/core/region.go

Check failure on line 899 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: unexpected >>, expecting }

Check failure on line 899 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

invalid character U+0023 '#'
r.st.Lock()
defer r.st.Unlock()
if origin != nil {
Expand All @@ -894,8 +905,17 @@
// TODO: Improve performance by deleting only the different peers.
r.removeRegionFromSubTreeLocked(origin)
} else {
r.updateSubTreeStat(origin, region)
r.subRegions[region.GetID()].RegionInfo = region
// The region tree and the subtree update is not atomic and the region tree is updated first.
// If there are two thread needs to update region tree,
// t1: thread-A update region tree
// t2: thread-B: update region tree again
// t3: thread-B: update subtree
// t4: thread-A: update region subtree
// to keep region tree consistent with subtree, we need to drop this update.
if tree, ok := r.subRegions[region.GetID()]; ok {
r.updateSubTreeStat(origin, region)
tree.RegionInfo = region
}
return
}
}
Expand All @@ -909,7 +929,7 @@
r.subRegions[region.GetID()] = item
// It has been removed and all information needs to be updated again.
// Set peers then.
setPeer := func(peersMap map[uint64]*regionTree, storeID uint64, item *regionItem) {

Check failure on line 932 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

method has multiple receivers

Check failure on line 932 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: unexpected {, expecting name or (
store, ok := peersMap[storeID]
if !ok {
store = newRegionTree()
Expand All @@ -930,7 +950,7 @@
}
}

setPeers := func(peersMap map[uint64]*regionTree, peers []*metapb.Peer) {

Check failure on line 953 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

method has multiple receivers

Check failure on line 953 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: unexpected {, expecting name or (
for _, peer := range peers {
storeID := peer.GetStoreId()
setPeer(peersMap, storeID, item)
Expand Down
13 changes: 13 additions & 0 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -451,6 +452,18 @@ func TestRegionKey(t *testing.T) {
}
}

func TestSetRegionConcurrence(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/core/UpdateSubTree", `return()`))
regions := NewRegionsInfo()
region := NewTestRegionInfo(1, 1, []byte("a"), []byte("b"))
go func() {
regions.AtomicCheckAndPutRegion(region)
}()
regions.AtomicCheckAndPutRegion(region)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/core/UpdateSubTree"))
}

func TestSetRegion(t *testing.T) {
re := require.New(t)
regions := NewRegionsInfo()
Expand Down
Loading