Skip to content

Commit 70b8a30

Browse files
authored
txn: use handle to encode checksum instead of the key (#57139) (#57186)
close #57174
1 parent 7d9ee81 commit 70b8a30

File tree

8 files changed

+36
-25
lines changed

8 files changed

+36
-25
lines changed

pkg/ddl/column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ func (w *updateColumnWorker) getRowRecord(handle kv.Handle, recordKey []byte, ra
779779
ec := w.exprCtx.GetEvalCtx().ErrCtx()
780780
var checksum rowcodec.Checksum
781781
if w.checksumNeeded {
782-
checksum = rowcodec.RawChecksum{Key: recordKey}
782+
checksum = rowcodec.RawChecksum{Handle: handle}
783783
}
784784
newRowVal, err := tablecodec.EncodeRow(sysTZ, newRow, newColumnIDs, nil, nil, checksum, rd)
785785
err = ec.HandleError(err)

pkg/ddl/column_type_change_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func TestRowFormatWithChecksums(t *testing.T) {
260260
data, err := h.GetMvccByEncodedKey(encodedKey)
261261
require.NoError(t, err)
262262
// row value with checksums
263-
expected := []byte{0x80, 0x2, 0x3, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x1, 0x0, 0x4, 0x0, 0x7, 0x0, 0x1, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x1, 0xa9, 0x7a, 0xf4, 0xc8}
263+
expected := []byte{0x80, 0x2, 0x3, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x1, 0x0, 0x4, 0x0, 0x7, 0x0, 0x1, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x2, 0x9e, 0x56, 0xf5, 0x45}
264264
require.Equal(t, expected, data.Info.Writes[0].ShortValue)
265265
tk.MustExec("drop table if exists t")
266266
}
@@ -284,7 +284,7 @@ func TestRowLevelChecksumWithMultiSchemaChange(t *testing.T) {
284284
data, err := h.GetMvccByEncodedKey(encodedKey)
285285
require.NoError(t, err)
286286
// checksum skipped and with a null col vv
287-
expected := []byte{0x80, 0x2, 0x3, 0x0, 0x1, 0x0, 0x1, 0x2, 0x4, 0x3, 0x1, 0x0, 0x4, 0x0, 0x7, 0x0, 0x1, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x1, 0xeb, 0x42, 0xda, 0x20}
287+
expected := []byte{0x80, 0x2, 0x3, 0x0, 0x1, 0x0, 0x1, 0x2, 0x4, 0x3, 0x1, 0x0, 0x4, 0x0, 0x7, 0x0, 0x1, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x2, 0x0, 0x4f, 0xd2, 0x26}
288288
require.Equal(t, expected, data.Info.Writes[0].ShortValue)
289289
tk.MustExec("drop table if exists t")
290290
}

pkg/table/tables/tables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func (t *TableCommon) updateRecord(sctx table.MutateContext, txn kv.Transaction,
518518

519519
key := t.RecordKey(h)
520520
tc, ec := evalCtx.TypeCtx(), evalCtx.ErrCtx()
521-
err = encodeRowBuffer.WriteMemBufferEncoded(sctx.GetRowEncodingConfig(), tc.Location(), ec, memBuffer, key)
521+
err = encodeRowBuffer.WriteMemBufferEncoded(sctx.GetRowEncodingConfig(), tc.Location(), ec, memBuffer, key, h)
522522
if err != nil {
523523
return err
524524
}
@@ -886,7 +886,7 @@ func (t *TableCommon) addRecord(sctx table.MutateContext, txn kv.Transaction, r
886886
}
887887
}
888888

889-
err = encodeRowBuffer.WriteMemBufferEncoded(sctx.GetRowEncodingConfig(), tc.Location(), ec, memBuffer, key, flags...)
889+
err = encodeRowBuffer.WriteMemBufferEncoded(sctx.GetRowEncodingConfig(), tc.Location(), ec, memBuffer, key, recordID, flags...)
890890
if err != nil {
891891
return nil, err
892892
}

pkg/table/tblctx/buffers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func (b *EncodeRowBuffer) AddColVal(colID int64, val types.Datum) {
5252
// WriteMemBufferEncoded writes the encoded row to the memBuffer.
5353
func (b *EncodeRowBuffer) WriteMemBufferEncoded(
5454
cfg RowEncodingConfig, loc *time.Location, ec errctx.Context,
55-
memBuffer kv.MemBuffer, key kv.Key, flags ...kv.FlagsOp,
55+
memBuffer kv.MemBuffer, key kv.Key, handle kv.Handle, flags ...kv.FlagsOp,
5656
) error {
5757
var checksum rowcodec.Checksum
5858
if cfg.IsRowLevelChecksumEnabled {
59-
checksum = rowcodec.RawChecksum{Key: key}
59+
checksum = rowcodec.RawChecksum{Handle: handle}
6060
}
6161

6262
stmtBufs := b.writeStmtBufs

pkg/table/tblctx/buffers_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestEncodeRow(t *testing.T) {
106106

107107
var checksum rowcodec.Checksum
108108
if cfg.IsRowLevelChecksumEnabled {
109-
checksum = rowcodec.RawChecksum{Key: kv.Key("key1")}
109+
checksum = rowcodec.RawChecksum{Handle: kv.IntHandle(1)}
110110
}
111111

112112
expectedVal, err := tablecodec.EncodeRow(
@@ -125,7 +125,7 @@ func TestEncodeRow(t *testing.T) {
125125
}
126126
err = buffer.WriteMemBufferEncoded(
127127
cfg, c.loc, errctx.StrictNoWarningContext,
128-
memBuffer, kv.Key("key1"), c.flags...,
128+
memBuffer, kv.Key("key1"), kv.IntHandle(1), c.flags...,
129129
)
130130
require.NoError(t, err)
131131
memBuffer.AssertExpectations(t)
@@ -166,7 +166,7 @@ func TestEncodeBufferReserve(t *testing.T) {
166166
require.Equal(t, 2, len(buffer.row))
167167
require.NoError(t, buffer.WriteMemBufferEncoded(RowEncodingConfig{
168168
RowEncoder: &rowcodec.Encoder{Enable: true},
169-
}, time.UTC, errctx.StrictNoWarningContext, mb, kv.Key("key1")))
169+
}, time.UTC, errctx.StrictNoWarningContext, mb, kv.Key("key1"), kv.IntHandle(1)))
170170
encodedCap := cap(buffer.writeStmtBufs.RowValBuf)
171171
require.Greater(t, encodedCap, 0)
172172
require.Equal(t, 4, len(buffer.writeStmtBufs.AddRowValues))

pkg/util/rowcodec/encoder.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/pingcap/errors"
25+
"github.com/pingcap/tidb/pkg/kv"
2526
"github.com/pingcap/tidb/pkg/parser/mysql"
2627
"github.com/pingcap/tidb/pkg/types"
2728
"github.com/pingcap/tidb/pkg/util/codec"
@@ -238,22 +239,29 @@ func (NoChecksum) encode(encoder *Encoder, buf []byte) ([]byte, error) {
238239
return encoder.toBytes(buf), nil
239240
}
240241

241-
const checksumVersionRaw byte = 1
242+
// introduced since v7.1.0
243+
const checksumVersionColumn byte = 0
244+
245+
// introduced since v8.3.0
246+
const checksumVersionRawKey byte = 1
247+
248+
// introduced since v8.4.0
249+
const checksumVersionRawHandle byte = 2
242250

243251
// RawChecksum indicates encode the raw bytes checksum and append it to the raw bytes.
244252
type RawChecksum struct {
245-
Key []byte
253+
Handle kv.Handle
246254
}
247255

248256
func (c RawChecksum) encode(encoder *Encoder, buf []byte) ([]byte, error) {
249257
encoder.flags |= rowFlagChecksum
250-
encoder.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
251-
encoder.checksumHeader &^= checksumMaskVersion // revert checksum version
252-
encoder.checksumHeader |= checksumVersionRaw // set checksum version
258+
encoder.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
259+
encoder.checksumHeader &^= checksumMaskVersion // revert checksum version
260+
encoder.checksumHeader |= checksumVersionRawHandle // set checksum version
253261
valueBytes := encoder.toBytes(buf)
254262
valueBytes = append(valueBytes, encoder.checksumHeader)
255263
encoder.checksum1 = crc32.Checksum(valueBytes, crc32.IEEETable)
256-
encoder.checksum1 = crc32.Update(encoder.checksum1, crc32.IEEETable, c.Key)
264+
encoder.checksum1 = crc32.Update(encoder.checksum1, crc32.IEEETable, c.Handle.Encoded())
257265
valueBytes = binary.LittleEndian.AppendUint32(valueBytes, encoder.checksum1)
258266
return valueBytes, nil
259267
}

pkg/util/rowcodec/row.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ func (r *row) fromBytes(rowData []byte) error {
152152
r.checksumHeader = rowData[cursor]
153153
checksumVersion := r.ChecksumVersion()
154154
// make sure it can be read previous version checksum to support backward compatibility.
155-
if checksumVersion != 0 && checksumVersion != 1 {
155+
switch checksumVersion {
156+
case 0, 1, 2:
157+
default:
156158
return errInvalidChecksumVer
157159
}
158160
cursor++
@@ -303,12 +305,8 @@ func (r *row) initOffsets32() {
303305
// CalculateRawChecksum calculates the bytes-level checksum by using the given elements.
304306
// this is mainly used by the TiCDC to implement E2E checksum functionality.
305307
func (r *row) CalculateRawChecksum(
306-
loc *time.Location, colIDs []int64, values []*types.Datum, key kv.Key, buf []byte,
308+
loc *time.Location, colIDs []int64, values []*types.Datum, key kv.Key, handle kv.Handle, buf []byte,
307309
) (uint32, error) {
308-
r.flags |= rowFlagChecksum
309-
r.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
310-
r.checksumHeader &^= checksumMaskVersion // revert checksum version
311-
r.checksumHeader |= checksumVersionRaw // set checksum version
312310
for idx, colID := range colIDs {
313311
data, err := encodeValueDatum(loc, values[idx], nil)
314312
if err != nil {
@@ -325,6 +323,11 @@ func (r *row) CalculateRawChecksum(
325323
buf = r.toBytes(buf)
326324
buf = append(buf, r.checksumHeader)
327325
rawChecksum := crc32.Checksum(buf, crc32.IEEETable)
328-
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, key)
326+
// keep backward compatibility to v8.3.0
327+
if r.ChecksumVersion() == int(checksumVersionRawKey) {
328+
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, key)
329+
} else {
330+
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, handle.Encoded())
331+
}
329332
return rawChecksum, nil
330333
}

pkg/util/rowcodec/rowcodec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ func TestEncodeDecodeRowWithChecksum(t *testing.T) {
12091209
require.Zero(t, checksum)
12101210

12111211
rawChecksum := rowcodec.RawChecksum{
1212-
Key: []byte("0x1"),
1212+
Handle: kv.IntHandle(1),
12131213
}
12141214
raw, err = enc.Encode(time.UTC, nil, nil, rawChecksum, nil)
12151215
require.NoError(t, err)
@@ -1226,7 +1226,7 @@ func TestEncodeDecodeRowWithChecksum(t *testing.T) {
12261226
require.Equal(t, expected, checksum)
12271227

12281228
version := dec.ChecksumVersion()
1229-
require.Equal(t, 1, version)
1229+
require.Equal(t, 2, version)
12301230
}
12311231

12321232
var (

0 commit comments

Comments
 (0)