Skip to content

Commit 6db50f7

Browse files
committed
kv: deflake TestFollowerReadsWithStaleDescriptor
Fixes cockroachdb#108087. This fix avoids a data race in the test. The race was harmless, but could cause the test to fail when run with the race detector. Release note: None
1 parent ab13de5 commit 6db50f7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"fmt"
1515
"math"
1616
"strings"
17+
"sync/atomic"
1718
"testing"
1819
"time"
1920

@@ -697,7 +698,8 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) {
697698
// The test uses follower_read_timestamp().
698699
defer utilccl.TestingEnableEnterprise()()
699700

700-
historicalQuery := `SELECT * FROM test AS OF SYSTEM TIME follower_read_timestamp() WHERE k=2`
701+
var historicalQuery atomic.Value
702+
historicalQuery.Store(`SELECT * FROM test AS OF SYSTEM TIME follower_read_timestamp() WHERE k=2`)
701703
recCh := make(chan tracingpb.Recording, 1)
702704

703705
tc := testcluster.StartTestCluster(t, 4,
@@ -732,7 +734,7 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) {
732734
},
733735
SQLExecutor: &sql.ExecutorTestingKnobs{
734736
WithStatementTrace: func(trace tracingpb.Recording, stmt string) {
735-
if stmt == historicalQuery {
737+
if stmt == historicalQuery.Load().(string) {
736738
recCh <- trace
737739
}
738740
},
@@ -786,7 +788,7 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) {
786788
// not be executed as a follower read since it attempts to use n2 which
787789
// doesn't have a replica any more and then it tries n1 which returns an
788790
// updated descriptor.
789-
n4.Exec(t, historicalQuery)
791+
n4.Exec(t, historicalQuery.Load().(string))
790792
// As a sanity check, verify that this was not a follower read.
791793
rec := <-recCh
792794
require.False(t, kv.OnlyFollowerReads(rec), "query was served through follower reads: %s", rec)
@@ -812,7 +814,7 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) {
812814
// Run a historical query and assert that it's served from the follower (n3).
813815
// n4 should attempt to route to n3 because we pretend n3 has a lower latency
814816
// (see testing knob).
815-
n4.Exec(t, historicalQuery)
817+
n4.Exec(t, historicalQuery.Load().(string))
816818
rec = <-recCh
817819

818820
// Look at the trace and check that we've served a follower read.
@@ -855,8 +857,8 @@ func TestFollowerReadsWithStaleDescriptor(t *testing.T) {
855857
// the ReplicaInfo twice for the same range. This allows us to verify that
856858
// the cached - in the spanResolverIterator - information is correctly
857859
// preserved.
858-
historicalQuery = `SELECT * FROM [SELECT * FROM test WHERE k=2 UNION ALL SELECT * FROM test WHERE k=3] AS OF SYSTEM TIME follower_read_timestamp()`
859-
n4.Exec(t, historicalQuery)
860+
historicalQuery.Store(`SELECT * FROM [SELECT * FROM test WHERE k=2 UNION ALL SELECT * FROM test WHERE k=3] AS OF SYSTEM TIME follower_read_timestamp()`)
861+
n4.Exec(t, historicalQuery.Load().(string))
860862
rec = <-recCh
861863

862864
// Sanity check that the plan was distributed.

0 commit comments

Comments
 (0)