Skip to content

Commit 1d6e033

Browse files
authored
Merge pull request #154691 from cockroachdb/blathers/backport-release-25.4-154076
release-25.4: cmd/roachtest: fix cdc/multi-region-execution-locality-tpcc
2 parents 84bab66 + 75ff3ab commit 1d6e033

File tree

1 file changed

+39
-52
lines changed

1 file changed

+39
-52
lines changed

pkg/cmd/roachtest/tests/cdc.go

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/pem"
2121
"fmt"
2222
"io"
23+
"maps"
2324
"math/big"
2425
"math/rand"
2526
"net"
@@ -31,6 +32,7 @@ import (
3132
"path/filepath"
3233
"regexp"
3334
"runtime"
35+
"slices"
3436
"sort"
3537
"strconv"
3638
"strings"
@@ -1947,26 +1949,18 @@ func getDiagramProcessors(ctx context.Context, db *gosql.DB) ([]any, error) {
19471949
}
19481950

19491951
type ChangefeedDistribution struct {
1950-
NodeToSpansWatched map[int]int
19511952
ZoneToSpansWatched map[string]int
19521953
TotalSpansWatched int
19531954
TotalAggregators int
1954-
TotalLeaseHolders int
1955-
TotalRanges int
1956-
NodeToZone map[int]string
19571955
}
19581956

19591957
func getChangefeedDistribution(
19601958
processors []any, nodeToZone map[int]string, t test.Test,
19611959
) ChangefeedDistribution {
19621960
changefeedDistribution := ChangefeedDistribution{
1963-
NodeToSpansWatched: make(map[int]int),
19641961
ZoneToSpansWatched: make(map[string]int),
19651962
TotalSpansWatched: 0,
19661963
TotalAggregators: 0,
1967-
TotalLeaseHolders: 0,
1968-
TotalRanges: 0,
1969-
NodeToZone: nodeToZone,
19701964
}
19711965
for _, p := range processors {
19721966
procMap, ok := p.(map[string]any)
@@ -1989,10 +1983,8 @@ func getChangefeedDistribution(
19891983
if len(matches) > 1 {
19901984
numWatches, err := strconv.Atoi(matches[1])
19911985
require.NoError(t, err)
1992-
changefeedDistribution.NodeToSpansWatched[int(nodeIdx)] += numWatches
19931986
changefeedDistribution.TotalSpansWatched += numWatches
1994-
changefeedDistribution.ZoneToSpansWatched[changefeedDistribution.NodeToZone[int(nodeIdx)]] += numWatches
1995-
1987+
changefeedDistribution.ZoneToSpansWatched[nodeToZone[int(nodeIdx)]] += numWatches
19961988
}
19971989
}
19981990
}
@@ -2001,42 +1993,36 @@ func getChangefeedDistribution(
20011993
return changefeedDistribution
20021994
}
20031995

2004-
func veryifyLeaseHolderDistribution(
2005-
db *gosql.DB, t test.Test, nodeToZone map[int]string,
2006-
) map[string]int {
2007-
var rows *gosql.Rows
2008-
// Get lease holders for all ranges in tpcc database.
2009-
leaseHolderQuery := `SELECT r.start_pretty, r.replicas, r.replica_localities, r.lease_holder
2010-
FROM crdb_internal.ranges r
2011-
JOIN crdb_internal.tables t ON r.start_pretty like concat('/Table/', t.table_id::STRING,'%')
2012-
WHERE t.database_name = 'tpcc'`
2013-
rows, err := db.Query(leaseHolderQuery)
2014-
zoneToLeaseHolderCount := make(map[string]int)
2015-
require.NoError(t, err)
2016-
defer rows.Close()
2017-
for rows.Next() {
2018-
var startKeyPretty string
2019-
var replicas []uint8
2020-
var replicaLocalities []uint8
2021-
var leaseHolder int
2022-
require.NoError(t, rows.Scan(&startKeyPretty, &replicas, &replicaLocalities, &leaseHolder))
2023-
for indx := range replicas {
2024-
require.NotEqual(t, replicas[indx], 0)
2025-
replicas[indx]--
1996+
func verifyLeaseHolderLocality(db *gosql.DB, t test.Test, primaryRegion string) {
1997+
leaseHolderQuery := `SELECT NOT EXISTS (
1998+
SELECT 1
1999+
FROM [SHOW CLUSTER RANGES WITH TABLES, DETAILS]
2000+
WHERE database_name = 'tpcc'
2001+
AND (lease_holder_locality IS DISTINCT FROM $1::STRING OR lease_holder_locality IS NULL)
2002+
)`
2003+
t.L().Printf("Waiting for all lease holders to be in region %s", primaryRegion)
2004+
start := timeutil.Now()
2005+
ok := false
2006+
for {
2007+
if timeutil.Since(start) > 5*time.Minute {
2008+
t.Fatalf("Timeout waiting for lease holders to be in region %s; waited for %s", primaryRegion, timeutil.Since(start))
20262009
}
2027-
leaseHolder--
2028-
zoneToLeaseHolderCount[nodeToZone[leaseHolder]]++
2010+
require.NoError(t, db.QueryRow(leaseHolderQuery, primaryRegion).Scan(&ok))
2011+
if ok {
2012+
break
2013+
}
2014+
time.Sleep(time.Second)
20292015
}
2030-
return zoneToLeaseHolderCount
20312016
}
20322017

20332018
func registerCDC(r registry.Registry) {
20342019
r.Add(registry.TestSpec{
20352020
// This test
2036-
// 1. Creates a cluster with 3 nodes each in us-east and us-west
2037-
// 2. Runs a tpcc workload, then sets tpcc database to primary region us-west
2038-
// 3. Creates a changefeed with execution locality set to us-east
2039-
// 4. Gets the changefeed diagram and creates mappings
2021+
// 1. Creates a cluster with 3 nodes each in us-east and us-west;
2022+
// 2. Runs a tpcc workload, then congigures tpcc database to have lease holders in region us-west;
2023+
// 3. Creates a changefeed with execution locality set to us-east;
2024+
// 4. Gets the changefeed diagram and creates mappings;
2025+
// 5. Verifies that spans are assigned to multiple change aggregators in region us-east.
20402026

20412027
// This test is used to verify that ranges are evenly distributed across
20422028
// change aggregators in the execution_locality region while targeting tables
@@ -2048,7 +2034,7 @@ func registerCDC(r registry.Registry) {
20482034
Owner: registry.OwnerCDC,
20492035
Cluster: r.MakeClusterSpec(7, spec.Geo(), spec.GatherCores(), spec.GCEZones("us-east1-b,us-west1-b")),
20502036
CompatibleClouds: registry.OnlyGCE,
2051-
Suites: registry.Suites(),
2037+
Suites: registry.Suites(registry.Nightly),
20522038
Run: func(ctx context.Context, t test.Test, c cluster.Cluster) {
20532039
nodeToZone := map[int]string{
20542040
0: "us-east1-b",
@@ -2061,17 +2047,24 @@ func registerCDC(r registry.Registry) {
20612047
ct := newCDCTester(ctx, t, c)
20622048
defer ct.Close()
20632049

2064-
ct.runTPCCWorkload(tpccArgs{warehouses: 100})
2050+
ct.runTPCCWorkload(tpccArgs{warehouses: 20})
20652051

20662052
var err error
2067-
_, err = ct.DB().Exec("ALTER DATABASE tpcc SET PRIMARY REGION 'us-west1'")
2053+
_, err = ct.DB().Exec(`ALTER DATABASE tpcc
2054+
CONFIGURE ZONE USING
2055+
constraints = '{+region=us-west1: 1, +region=us-east1: 1}',
2056+
lease_preferences = '[[+region=us-west1]]', num_replicas = 3`)
20682057
require.NoError(t, err)
20692058

2059+
// Verify lease holders are in us-west1-b.
2060+
verifyLeaseHolderLocality(ct.DB(), t, "cloud=gce,region=us-west1,zone=us-west1-b")
2061+
20702062
feed := ct.newChangefeed(feedArgs{
20712063
sinkType: cloudStorageSink,
20722064
targets: allTpccTargets,
20732065
opts: map[string]string{
20742066
"execution_locality": "'region=us-east1'",
2067+
"initial_scan": "'only'",
20752068
},
20762069
})
20772070
ct.waitForWorkload()
@@ -2080,18 +2073,12 @@ func registerCDC(r registry.Registry) {
20802073
processors, err := getDiagramProcessors(ctx, ct.DB())
20812074
require.NoError(t, err)
20822075

2076+
// Verify changefeed aggregators are distributed across nodes in region us-east.
20832077
changefeedDistribution := getChangefeedDistribution(processors, nodeToZone, t)
20842078
require.Greater(t, changefeedDistribution.TotalAggregators, 1)
2085-
for nodeIdx, spansWatched := range changefeedDistribution.NodeToSpansWatched {
2086-
require.LessOrEqual(t, spansWatched, changefeedDistribution.TotalSpansWatched/2, "nodeIdx %d watched %d spans, total spans watched %d", nodeIdx, spansWatched, changefeedDistribution.TotalSpansWatched)
2087-
}
2088-
require.Equal(t, 1, len(changefeedDistribution.ZoneToSpansWatched))
2079+
require.ElementsMatch(t, []string{"us-east1-b"}, slices.Collect(maps.Keys(changefeedDistribution.ZoneToSpansWatched)))
20892080
require.Equal(t, changefeedDistribution.ZoneToSpansWatched["us-east1-b"], changefeedDistribution.TotalSpansWatched)
2090-
zoneToLeaseHolderCount := veryifyLeaseHolderDistribution(ct.DB(), t, nodeToZone)
2091-
// Majority of lease holders should be in us-west1-b. Some may not, but most should.
2092-
if zoneToLeaseHolderCount["us-east1-b"] != 0 {
2093-
require.Greater(t, zoneToLeaseHolderCount["us-west1-b"]/zoneToLeaseHolderCount["us-east1-b"], 10)
2094-
}
2081+
require.Greater(t, changefeedDistribution.TotalSpansWatched, 0)
20952082
},
20962083
})
20972084
r.Add(registry.TestSpec{

0 commit comments

Comments
 (0)