Skip to content

Commit

Permalink
Fix test issue in TestRandomResignLeader. (#6410)
Browse files Browse the repository at this point in the history
close #6404

We need to make sure the selected keyspaces are from different keyspace groups, otherwise multiple goroutines below could try to resign the primary of the same keyspace group and cause race condition.

Signed-off-by: Bin Shi <binshi.bing@gmail.com>
  • Loading branch information
binshi-bing authored May 5, 2023
1 parent 62b51dd commit 09e6531
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions tests/integrations/tso/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type tsoClientTestSuite struct {
// The TSO service in microservice mode.
tsoCluster *mcs.TestTSOCluster

keyspaceGroups []struct {
keyspaceGroupID uint32
keyspaceIDs []uint32
}

backendEndpoints string
keyspaceIDs []uint32
clients []pd.Client
Expand Down Expand Up @@ -99,7 +104,7 @@ func (suite *tsoClientTestSuite) SetupSuite() {
suite.tsoCluster, err = mcs.NewTestTSOCluster(suite.ctx, 3, suite.backendEndpoints)
re.NoError(err)

params := []struct {
suite.keyspaceGroups = []struct {
keyspaceGroupID uint32
keyspaceIDs []uint32
}{
Expand All @@ -108,7 +113,7 @@ func (suite *tsoClientTestSuite) SetupSuite() {
{2, []uint32{2}},
}

for _, param := range params {
for _, param := range suite.keyspaceGroups {
if param.keyspaceGroupID == 0 {
// we have already created default keyspace group, so we can skip it.
// keyspace 10 isn't assigned to any keyspace group, so they will be
Expand All @@ -127,8 +132,8 @@ func (suite *tsoClientTestSuite) SetupSuite() {
})
}

for _, param := range params {
suite.keyspaceIDs = append(suite.keyspaceIDs, param.keyspaceIDs...)
for _, keyspaceGroup := range suite.keyspaceGroups {
suite.keyspaceIDs = append(suite.keyspaceIDs, keyspaceGroup.keyspaceIDs...)
}

suite.clients = mcs.WaitForMultiKeyspacesTSOAvailable(
Expand Down Expand Up @@ -242,10 +247,15 @@ func (suite *tsoClientTestSuite) TestRandomResignLeader() {
time.Sleep(time.Duration(n) * time.Second)
if !suite.legacy {
wg := sync.WaitGroup{}
// Select the default keyspace and a randomly picked keyspace to test
keyspaceIDs := []uint32{mcsutils.DefaultKeyspaceID}
selectIdx := uint32(r.Intn(len(suite.keyspaceIDs)-1) + 1)
keyspaceIDs = append(keyspaceIDs, suite.keyspaceIDs[selectIdx])
// Select the first keyspace from all keyspace groups. We need to make sure the selected
// keyspaces are from different keyspace groups, otherwise multiple goroutines below could
// try to resign the primary of the same keyspace group and cause race condition.
keyspaceIDs := make([]uint32, 0)
for _, keyspaceGroup := range suite.keyspaceGroups {
if len(keyspaceGroup.keyspaceIDs) > 0 {
keyspaceIDs = append(keyspaceIDs, keyspaceGroup.keyspaceIDs[0])
}
}
wg.Add(len(keyspaceIDs))
for _, keyspaceID := range keyspaceIDs {
go func(keyspaceID uint32) {
Expand Down

0 comments on commit 09e6531

Please sign in to comment.