Skip to content

Commit 644e966

Browse files
authored
CI: Address our two flakiest tests (#19587)
Signed-off-by: Matt Lord <mattalord@gmail.com>
1 parent e73f9aa commit 644e966

4 files changed

Lines changed: 46 additions & 14 deletions

File tree

go/vt/vttablet/tabletmanager/framework_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strconv"
2424
"strings"
2525
"sync"
26+
"sync/atomic"
2627
"testing"
2728

2829
"github.com/stretchr/testify/require"
@@ -59,6 +60,8 @@ const (
5960
gtidPosition = "16b1039f-22b6-11ed-b765-0a43f95f28a3:1-220"
6061
)
6162

63+
var testEnvCounter atomic.Int64
64+
6265
func init() {
6366
tabletconn.RegisterDialer("grpc", func(ctx context.Context, tablet *topodatapb.Tablet, failFast grpcclient.FailFast) (queryservice.QueryService, error) {
6467
return &tabletconntest.FakeQueryService{
@@ -90,12 +93,13 @@ type testEnv struct {
9093

9194
func newTestEnv(t *testing.T, ctx context.Context, sourceKeyspace string, sourceShards []string) *testEnv {
9295
vttablet.InitVReplicationConfigDefaults()
96+
protoName := fmt.Sprintf("%s_%d", t.Name(), testEnvCounter.Add(1))
9397
tenv := &testEnv{
9498
ctx: context.Background(),
9599
tmc: newFakeTMClient(),
96100
cells: []string{"zone1"},
97101
dbName: "tmtestdb",
98-
protoName: t.Name(),
102+
protoName: protoName,
99103
}
100104
tenv.mu.Lock()
101105
defer tenv.mu.Unlock()
@@ -104,19 +108,19 @@ func newTestEnv(t *testing.T, ctx context.Context, sourceKeyspace string, source
104108
tenv.tmc.sourceShards = sourceShards
105109
tenv.tmc.schema = defaultSchema
106110

107-
tabletconn.RegisterDialer(t.Name(), func(ctx context.Context, tablet *topodatapb.Tablet, failFast grpcclient.FailFast) (queryservice.QueryService, error) {
111+
tabletconn.RegisterDialer(protoName, func(ctx context.Context, tablet *topodatapb.Tablet, failFast grpcclient.FailFast) (queryservice.QueryService, error) {
108112
tenv.mu.Lock()
109113
defer tenv.mu.Unlock()
110114
if qs, ok := tenv.tmc.tablets[int(tablet.Alias.Uid)]; ok {
111115
return qs, nil
112116
}
113117
return nil, fmt.Errorf("tablet %d not found", tablet.Alias.Uid)
114118
})
115-
tabletconntest.SetProtocol("go.vt.vttablet.tabletmanager.framework_test_"+t.Name(), tenv.protoName)
116-
tmclient.RegisterTabletManagerClientFactory(t.Name(), func() tmclient.TabletManagerClient {
119+
tabletconntest.SetProtocol("go.vt.vttablet.tabletmanager.framework_test_"+protoName, protoName)
120+
tmclient.RegisterTabletManagerClientFactory(protoName, func() tmclient.TabletManagerClient {
117121
return tenv.tmc
118122
})
119-
tmclienttest.SetProtocol("go.vt.vttablet.tabletmanager.framework_test_"+t.Name(), tenv.protoName)
123+
tmclienttest.SetProtocol("go.vt.vttablet.tabletmanager.framework_test_"+protoName, protoName)
120124

121125
tenv.db = fakesqldb.New(t)
122126
tenv.mysqld = mysqlctl.NewFakeMysqlDaemon(tenv.db)

go/vt/vttablet/tabletmanager/rpc_vreplication_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ func addInvariants(dbClient *binlogplayer.MockDBClient, vreplID, sourceTabletUID
18401840
"0",
18411841
))
18421842
dbClient.AddInvariant(fmt.Sprintf(updatePickedSourceTablet, cell, sourceTabletUID, vreplID), &sqltypes.Result{})
1843-
dbClient.AddInvariant("update _vt.vreplication set state='Running', message='' where id=1", &sqltypes.Result{})
1843+
dbClient.AddInvariant("update _vt.vreplication set state='Running', message=left('', 1000) where id=1", &sqltypes.Result{})
18441844
dbClient.AddInvariant(vreplication.SqlMaxAllowedPacket, sqltypes.MakeTestResult(
18451845
sqltypes.MakeTestFields(
18461846
"max_allowed_packet",

go/vt/vttablet/tabletmanager/vreplication/framework_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var (
7474
vrepldb = "vrepl"
7575
globalDBQueries = make(chan string, 1000)
7676
lastMultiExecQuery = ""
77+
lastMultiExecQueryMu sync.Mutex
7778
testForeignKeyQueries = false
7879
testSetForeignKeyQueries = false
7980
doNotLogDBQueries = false
@@ -608,10 +609,18 @@ func (dbc *realDBClient) ExecuteFetchMulti(query string, maxrows int) ([]*sqltyp
608609
}
609610
results = append(results, qr)
610611
}
612+
lastMultiExecQueryMu.Lock()
611613
lastMultiExecQuery = query
614+
lastMultiExecQueryMu.Unlock()
612615
return results, nil
613616
}
614617

618+
func getLastMultiExecQuery() string {
619+
lastMultiExecQueryMu.Lock()
620+
defer lastMultiExecQueryMu.Unlock()
621+
return lastMultiExecQuery
622+
}
623+
615624
func (dbc *realDBClient) SupportsCapability(capability capabilities.FlavorCapability) (bool, error) {
616625
return dbc.conn.SupportsCapability(capability)
617626
}

go/vt/vttablet/tabletmanager/vreplication/vplayer_flaky_test.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,7 +3810,6 @@ func TestPlayerBatchMode(t *testing.T) {
38103810
require.LessOrEqual(t, len(stmt), maxBatchSize, "expected output statement is longer than the max batch size (%d): %s", maxBatchSize, stmt)
38113811
}
38123812
expectNontxQueries(t, output, recvTimeout)
3813-
time.Sleep(1 * time.Second)
38143813
if tcase.table != "" {
38153814
expectData(t, tcase.table, tcase.data)
38163815
}
@@ -3822,15 +3821,35 @@ func TestPlayerBatchMode(t *testing.T) {
38223821
expectedBulkInserts += tcase.expectedBulkInserts
38233822
expectedTrxBatchCommits++ // Should only ever be 1 per test case
38243823
expectedTrxBatchExecs += tcase.expectedNonCommitBatches
3825-
if tcase.expectedInLastBatch != "" { // We expect the trx to be split
3826-
require.Regexpf(t, regexp.MustCompile(fmt.Sprintf(trxLastBatchExpectRE, regexp.QuoteMeta(tcase.expectedInLastBatch))), lastMultiExecQuery, "Unexpected batch statement: %s", lastMultiExecQuery)
3824+
3825+
// Poll until the batch query and stats counters are updated.
3826+
// These are set asynchronously on the vplayer goroutine after
3827+
// the commit completes, so we poll rather than using a fixed sleep.
3828+
var batchRE *regexp.Regexp
3829+
if tcase.expectedInLastBatch != "" {
3830+
batchRE = regexp.MustCompile(fmt.Sprintf(trxLastBatchExpectRE, regexp.QuoteMeta(tcase.expectedInLastBatch)))
38273831
} else {
3828-
require.Regexpf(t, regexp.MustCompile(fmt.Sprintf(trxFullBatchExpectRE, regexp.QuoteMeta(strings.Join(tcase.output, ";")))), lastMultiExecQuery, "Unexpected batch statement: %s", lastMultiExecQuery)
3832+
batchRE = regexp.MustCompile(fmt.Sprintf(trxFullBatchExpectRE, regexp.QuoteMeta(strings.Join(tcase.output, ";"))))
38293833
}
3830-
require.Equal(t, expectedBulkInserts, stats.BulkQueryCount.Counts()["insert"], "expected %d bulk inserts but got %d", expectedBulkInserts, stats.BulkQueryCount.Counts()["insert"])
3831-
require.Equal(t, expectedBulkDeletes, stats.BulkQueryCount.Counts()["delete"], "expected %d bulk deletes but got %d", expectedBulkDeletes, stats.BulkQueryCount.Counts()["delete"])
3832-
require.Equal(t, expectedTrxBatchExecs, stats.TrxQueryBatchCount.Counts()["without_commit"], "expected %d trx batch execs but got %d", expectedTrxBatchExecs, stats.TrxQueryBatchCount.Counts()["without_commit"])
3833-
require.Equal(t, expectedTrxBatchCommits, stats.TrxQueryBatchCount.Counts()["with_commit"], "expected %d trx batch commits but got %d", expectedTrxBatchCommits, stats.TrxQueryBatchCount.Counts()["with_commit"])
3834+
require.Eventually(t, func() bool {
3835+
got := getLastMultiExecQuery()
3836+
if !batchRE.MatchString(got) {
3837+
return false
3838+
}
3839+
if stats.BulkQueryCount.Counts()["insert"] != expectedBulkInserts {
3840+
return false
3841+
}
3842+
if stats.BulkQueryCount.Counts()["delete"] != expectedBulkDeletes {
3843+
return false
3844+
}
3845+
if stats.TrxQueryBatchCount.Counts()["without_commit"] != expectedTrxBatchExecs {
3846+
return false
3847+
}
3848+
if stats.TrxQueryBatchCount.Counts()["with_commit"] != expectedTrxBatchCommits {
3849+
return false
3850+
}
3851+
return true
3852+
}, 10*time.Second, 100*time.Millisecond, "batch query or stats mismatch after timeout; lastMultiExecQuery: %s", getLastMultiExecQuery())
38343853
})
38353854
}
38363855
}

0 commit comments

Comments
 (0)