Skip to content

Commit 4d5850d

Browse files
committed
row/fetcher: clean up and harden TestRowFetcherMVCCMetadata
This commit refactors `TestRowFetcherMVCCMetadata` a bit to explicitly request `crdb_internal_mvcc_timestamp` as opposed to fishing it out of the fetcher, which allows removing an exported method from the latter. This commit also hardens the test to de-flake it when write buffering is enabled. The test scans the storage engine directly, so we need to tweak a testing knob to ensure that COMMIT blocks until the writes are committed into pebble. Release note: None
1 parent a338aa6 commit 4d5850d

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

pkg/sql/row/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ go_test(
127127
"//pkg/sql",
128128
"//pkg/sql/catalog",
129129
"//pkg/sql/catalog/bootstrap",
130+
"//pkg/sql/catalog/colinfo",
130131
"//pkg/sql/catalog/descpb",
131132
"//pkg/sql/catalog/descs",
132133
"//pkg/sql/catalog/desctestutils",

pkg/sql/row/fetcher.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,12 +1284,6 @@ func (rf *Fetcher) NextRowDecodedInto(
12841284
return true, spanID, nil
12851285
}
12861286

1287-
// RowLastModified may only be called after NextRow has returned a non-nil row
1288-
// and returns the timestamp of the last modification to that row.
1289-
func (rf *Fetcher) RowLastModified() hlc.Timestamp {
1290-
return rf.table.rowLastModified
1291-
}
1292-
12931287
// RowIsDeleted may only be called after NextRow has returned a non-nil row and
12941288
// returns true if that row was most recently deleted. This method is only
12951289
// meaningful when the configured KVBatchFetcher returns deletion tombstones, which
@@ -1306,7 +1300,7 @@ func (rf *Fetcher) finalizeRow() error {
13061300
// TODO (rohany): Datums are immutable, so we can't store a DDecimal on the
13071301
// fetcher and change its contents with each row. If that assumption gets
13081302
// lifted, then we can avoid an allocation of a new decimal datum here.
1309-
dec := rf.args.Alloc.NewDDecimal(tree.DDecimal{Decimal: eval.TimestampToDecimal(rf.RowLastModified())})
1303+
dec := rf.args.Alloc.NewDDecimal(tree.DDecimal{Decimal: eval.TimestampToDecimal(rf.table.rowLastModified)})
13101304
table.row[table.timestampOutputIdx] = rowenc.EncDatum{Datum: dec}
13111305
}
13121306
if table.oidOutputIdx != noOutputColumn {

pkg/sql/row/fetcher_mvcc_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
1616
"github.com/cockroachdb/cockroach/pkg/roachpb"
1717
"github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap"
18+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
1819
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
1920
"github.com/cockroachdb/cockroach/pkg/sql/catalog/fetchpb"
2021
"github.com/cockroachdb/cockroach/pkg/sql/row"
2122
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
22-
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
2323
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2424
"github.com/cockroachdb/cockroach/pkg/storage"
2525
"github.com/cockroachdb/cockroach/pkg/testutils"
@@ -74,7 +74,17 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
7474
defer log.Scope(t).Close(t)
7575

7676
ctx := context.Background()
77-
srv, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
77+
srv, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{
78+
Knobs: base.TestingKnobs{
79+
Store: &kvserver.StoreTestingKnobs{
80+
// With write buffering enabled on the KV client, we need to
81+
// ensure that committing a write to the raft log returns only
82+
// when it's applied to pebble (since we scan the engine
83+
// directly in slurpUserDataKVs).
84+
DisableCanAckBeforeApplication: true,
85+
},
86+
},
87+
})
7888
defer srv.Stopper().Stop(ctx)
7989
s := srv.ApplicationLayer()
8090
codec := s.Codec()
@@ -88,9 +98,13 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
8898
FAMILY (a, b, c), FAMILY (d)
8999
)`)
90100
desc := desctestutils.TestingGetPublicTableDescriptor(kvDB, codec, `d`, `parent`)
101+
// Request crdb_internal_mvcc_timestamp column in addition to all user
102+
// columns from the table.
103+
fetchColumnIDs := desc.PublicColumnIDs()
104+
fetchColumnIDs = append(fetchColumnIDs, colinfo.MVCCTimestampColumnID)
91105
var spec fetchpb.IndexFetchSpec
92106
if err := rowenc.InitIndexFetchSpec(
93-
&spec, codec, desc, desc.GetPrimaryIndex(), desc.PublicColumnIDs(),
107+
&spec, codec, desc, desc.GetPrimaryIndex(), fetchColumnIDs,
94108
); err != nil {
95109
t.Fatal(err)
96110
}
@@ -129,12 +143,13 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
129143
break
130144
}
131145
row := rowWithMVCCMetadata{
132-
RowIsDeleted: rf.RowIsDeleted(),
133-
RowLastModified: eval.TimestampToDecimalDatum(rf.RowLastModified()).String(),
146+
RowIsDeleted: rf.RowIsDeleted(),
134147
}
135148
for _, datum := range datums {
136149
if datum == tree.DNull {
137150
row.PrimaryKey = append(row.PrimaryKey, `NULL`)
151+
} else if d, ok := datum.(*tree.DDecimal); ok {
152+
row.RowLastModified = d.String()
138153
} else {
139154
row.PrimaryKey = append(row.PrimaryKey, string(*datum.(*tree.DString)))
140155
}
@@ -151,8 +166,8 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
151166
END;`).Scan(&ts1)
152167

153168
if actual, expected := kvsToRows(slurpUserDataKVs(t, store.TODOEngine(), codec)), []rowWithMVCCMetadata{
154-
{[]string{`1`, `a`, `a`, `a`}, false, ts1},
155-
{[]string{`2`, `b`, `b`, `b`}, false, ts1},
169+
{PrimaryKey: []string{`1`, `a`, `a`, `a`}, RowIsDeleted: false, RowLastModified: ts1},
170+
{PrimaryKey: []string{`2`, `b`, `b`, `b`}, RowIsDeleted: false, RowLastModified: ts1},
156171
}; !reflect.DeepEqual(expected, actual) {
157172
t.Errorf(`expected %v got %v`, expected, actual)
158173
}
@@ -165,8 +180,8 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
165180
END;`).Scan(&ts2)
166181

167182
if actual, expected := kvsToRows(slurpUserDataKVs(t, store.TODOEngine(), codec)), []rowWithMVCCMetadata{
168-
{[]string{`1`, `NULL`, `NULL`, `NULL`}, false, ts2},
169-
{[]string{`2`, `b`, `b`, `NULL`}, false, ts2},
183+
{PrimaryKey: []string{`1`, `NULL`, `NULL`, `NULL`}, RowIsDeleted: false, RowLastModified: ts2},
184+
{PrimaryKey: []string{`2`, `b`, `b`, `NULL`}, RowIsDeleted: false, RowLastModified: ts2},
170185
}; !reflect.DeepEqual(expected, actual) {
171186
t.Errorf(`expected %v got %v`, expected, actual)
172187
}
@@ -177,8 +192,8 @@ func TestRowFetcherMVCCMetadata(t *testing.T) {
177192
SELECT cluster_logical_timestamp();
178193
END;`).Scan(&ts3)
179194
if actual, expected := kvsToRows(slurpUserDataKVs(t, store.TODOEngine(), codec)), []rowWithMVCCMetadata{
180-
{[]string{`1`, `NULL`, `NULL`, `NULL`}, true, ts3},
181-
{[]string{`2`, `b`, `b`, `NULL`}, false, ts2},
195+
{PrimaryKey: []string{`1`, `NULL`, `NULL`, `NULL`}, RowIsDeleted: true, RowLastModified: ts3},
196+
{PrimaryKey: []string{`2`, `b`, `b`, `NULL`}, RowIsDeleted: false, RowLastModified: ts2},
182197
}; !reflect.DeepEqual(expected, actual) {
183198
t.Errorf(`expected %v got %v`, expected, actual)
184199
}

0 commit comments

Comments
 (0)