Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txn: bump checksum version (#57205) #57220

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ func TestRowFormatWithChecksums(t *testing.T) {
data, err := h.GetMvccByEncodedKey(encodedKey)
require.NoError(t, err)
// row value with checksums
<<<<<<< HEAD
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}
=======
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}
>>>>>>> e2505e95a03 (txn: bump checksum version (#57205))
require.Equal(t, expected, data.Info.Writes[0].ShortValue)
tk.MustExec("drop table if exists t")
}
Expand All @@ -284,7 +288,11 @@ func TestRowLevelChecksumWithMultiSchemaChange(t *testing.T) {
data, err := h.GetMvccByEncodedKey(encodedKey)
require.NoError(t, err)
// checksum skipped and with a null col vv
<<<<<<< HEAD
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}
=======
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}
>>>>>>> e2505e95a03 (txn: bump checksum version (#57205))
require.Equal(t, expected, data.Info.Writes[0].ShortValue)
tk.MustExec("drop table if exists t")
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/util/rowcodec/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,14 @@ func (NoChecksum) encode(encoder *Encoder, buf []byte) ([]byte, error) {
return encoder.toBytes(buf), nil
}

const checksumVersionRaw byte = 1
// introduced since v7.1.0
const checksumVersionColumn byte = 0

// introduced since v8.3.0
const checksumVersionRawKey byte = 1

// introduced since v8.4.0
const checksumVersionRawHandle byte = 2

// RawChecksum indicates encode the raw bytes checksum and append it to the raw bytes.
type RawChecksum struct {
Expand All @@ -247,9 +254,9 @@ type RawChecksum struct {

func (c RawChecksum) encode(encoder *Encoder, buf []byte) ([]byte, error) {
encoder.flags |= rowFlagChecksum
encoder.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
encoder.checksumHeader &^= checksumMaskVersion // revert checksum version
encoder.checksumHeader |= checksumVersionRaw // set checksum version
encoder.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
encoder.checksumHeader &^= checksumMaskVersion // revert checksum version
encoder.checksumHeader |= checksumVersionRawHandle // set checksum version
valueBytes := encoder.toBytes(buf)
valueBytes = append(valueBytes, encoder.checksumHeader)
encoder.checksum1 = crc32.Checksum(valueBytes, crc32.IEEETable)
Expand Down
21 changes: 16 additions & 5 deletions pkg/util/rowcodec/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (r *row) fromBytes(rowData []byte) error {
r.checksumHeader = rowData[cursor]
checksumVersion := r.ChecksumVersion()
// make sure it can be read previous version checksum to support backward compatibility.
if checksumVersion != 0 && checksumVersion != 1 {
switch checksumVersion {
case 0, 1, 2:
default:
return errInvalidChecksumVer
}
cursor++
Expand Down Expand Up @@ -303,12 +305,12 @@ func (r *row) initOffsets32() {
// CalculateRawChecksum calculates the bytes-level checksum by using the given elements.
// this is mainly used by the TiCDC to implement E2E checksum functionality.
func (r *row) CalculateRawChecksum(
<<<<<<< HEAD
loc *time.Location, colIDs []int64, values []*types.Datum, key kv.Key, buf []byte,
=======
loc *time.Location, colIDs []int64, values []*types.Datum, key kv.Key, handle kv.Handle, buf []byte,
>>>>>>> e2505e95a03 (txn: bump checksum version (#57205))
) (uint32, error) {
r.flags |= rowFlagChecksum
r.checksumHeader &^= checksumFlagExtra // revert extra checksum flag
r.checksumHeader &^= checksumMaskVersion // revert checksum version
r.checksumHeader |= checksumVersionRaw // set checksum version
for idx, colID := range colIDs {
data, err := encodeValueDatum(loc, values[idx], nil)
if err != nil {
Expand All @@ -325,6 +327,15 @@ func (r *row) CalculateRawChecksum(
buf = r.toBytes(buf)
buf = append(buf, r.checksumHeader)
rawChecksum := crc32.Checksum(buf, crc32.IEEETable)
<<<<<<< HEAD
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, key)
=======
// keep backward compatibility to v8.3.0
if r.ChecksumVersion() == int(checksumVersionRawKey) {
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, key)
} else {
rawChecksum = crc32.Update(rawChecksum, crc32.IEEETable, handle.Encoded())
}
>>>>>>> e2505e95a03 (txn: bump checksum version (#57205))
return rawChecksum, nil
}
2 changes: 1 addition & 1 deletion pkg/util/rowcodec/rowcodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ func TestEncodeDecodeRowWithChecksum(t *testing.T) {
require.Equal(t, expected, checksum)

version := dec.ChecksumVersion()
require.Equal(t, 1, version)
require.Equal(t, 2, version)
}

var (
Expand Down