Skip to content

Commit 704a6c1

Browse files
committed
builtins: deflake TestGetSSTableMetricsSingleNode
Refactor and deflake TestGetSSTableMetricsSingleNode. Fixes: #151742 Epic: none Release note: none
1 parent 85e17c6 commit 704a6c1

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

pkg/sql/sem/builtins/generator_builtins_test.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"encoding/json"
1212
"fmt"
1313
"math/rand"
14+
"slices"
1415
"testing"
1516
"time"
1617

1718
"github.com/cockroachdb/cockroach/pkg/base"
1819
"github.com/cockroachdb/cockroach/pkg/kv"
1920
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
21+
"github.com/cockroachdb/cockroach/pkg/roachpb"
2022
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
2123
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
2224
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
@@ -157,27 +159,30 @@ func TestGetSSTableMetricsSingleNode(t *testing.T) {
157159
ts, hostDB, _ := serverutils.StartServer(t, base.TestServerArgs{})
158160
defer ts.Stopper().Stop(ctx)
159161

160-
nodeIDArg := 1
162+
nodeIDArg := int(ts.NodeID())
161163
storeIDArg := int(ts.GetFirstStoreID())
162164

163165
r := sqlutils.MakeSQLRunner(hostDB)
164-
r.Exec(t, `CREATE TABLE t(k INT PRIMARY KEY, v INT)`)
165-
r.Exec(t, `INSERT INTO t SELECT i, i*10 FROM generate_series(1, 10000) AS g(i)`)
166-
167-
r.Exec(t, fmt.Sprintf(`
168-
SELECT crdb_internal.compact_engine_span(
169-
%d, %d,
170-
(SELECT raw_start_key FROM [SHOW RANGES FROM TABLE t WITH KEYS] LIMIT 1),
171-
(SELECT raw_end_key FROM [SHOW RANGES FROM TABLE t WITH KEYS] LIMIT 1))`,
172-
nodeIDArg, storeIDArg))
173-
174-
rows := r.Query(t, fmt.Sprintf(`
175-
SELECT * FROM crdb_internal.sstable_metrics(
176-
%d, %d,
177-
(SELECT raw_start_key FROM [SHOW RANGES FROM TABLE t WITH KEYS] LIMIT 1),
178-
(SELECT raw_end_key FROM [SHOW RANGES FROM TABLE t WITH KEYS] LIMIT 1))`,
179-
nodeIDArg, storeIDArg))
180166

167+
// Seed some data.
168+
r.Exec(t, `CREATE TABLE t(k INT PRIMARY KEY, v STRING)`)
169+
r.Exec(t, `INSERT INTO t SELECT i, CAST(gen_random_uuid() AS STRING) FROM generate_series(1, 10000) AS g(i)`)
170+
171+
// Construct min and max engine keys. We append a 0x00 byte to the end of
172+
// each to make them valid engine keys (the last byte indicates the length
173+
// of the version, in this case none).
174+
minEngineKey := append(slices.Clone(roachpb.KeyMin), 0x00)
175+
maxEngineKey := append(slices.Clone(roachpb.KeyMax), 0x00)
176+
177+
// Manually compact the entire user key space. This will trigger a memtable
178+
// flush if needed, ensuring that the data written above has been flushed to
179+
// sstables.
180+
r.Exec(t, `SELECT crdb_internal.compact_engine_span($1, $2, $3, $4)`,
181+
nodeIDArg, storeIDArg, minEngineKey, maxEngineKey)
182+
183+
// Now there must exist at least one sstable.
184+
rows := r.Query(t, `SELECT * FROM crdb_internal.sstable_metrics($1, $2, $3, $4)`,
185+
nodeIDArg, storeIDArg, minEngineKey, maxEngineKey)
181186
count := 0
182187
var nodeID int
183188
var storeID int
@@ -195,6 +200,7 @@ func TestGetSSTableMetricsSingleNode(t *testing.T) {
195200
require.NotEqual(t, approximateSpanBytes, 0)
196201
count++
197202
}
203+
require.NoError(t, rows.Err())
198204
require.GreaterOrEqual(t, count, 1)
199205
}
200206

0 commit comments

Comments
 (0)