-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codec(ticdc): simple protocol DDL message encode 2 table schema (#10294)
close #10293
- Loading branch information
1 parent
f0ab88a
commit 964df36
Showing
6 changed files
with
222 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ func TestEncodeCheckpoint(t *testing.T) { | |
require.NoError(t, err) | ||
enc := builder.Build() | ||
|
||
checkpoint := 23 | ||
checkpoint := 446266400629063682 | ||
m, err := enc.EncodeCheckpointEvent(uint64(checkpoint)) | ||
require.NoError(t, err) | ||
|
||
|
@@ -69,6 +69,20 @@ func TestEncodeDDLEvent(t *testing.T) { | |
helper := entry.NewSchemaTestHelper(t) | ||
defer helper.Close() | ||
|
||
sql := `create table test.t(id int primary key, name varchar(255) not null, gender enum('male', 'female'), email varchar(255) not null, key idx_name_email(name, email))` | ||
createTableDDLEvent := helper.DDL2Event(sql) | ||
|
||
sql = `insert into test.t values (1, "jack", "male", "[email protected]")` | ||
insertEvent := helper.DML2Event(sql, "test", "t") | ||
|
||
sql = `rename table test.t to test.abc` | ||
renameTableDDLEvent := helper.DDL2Event(sql) | ||
|
||
sql = `insert into test.abc values (2, "anna", "female", "[email protected]")` | ||
insertEvent2 := helper.DML2Event(sql, "test", "abc") | ||
|
||
helper.Tk().MustExec("drop table test.abc") | ||
|
||
ctx := context.Background() | ||
for _, compressionType := range []string{ | ||
compression.None, | ||
|
@@ -81,18 +95,10 @@ func TestEncodeDDLEvent(t *testing.T) { | |
require.NoError(t, err) | ||
enc := builder.Build() | ||
|
||
sql := `create table test.t( | ||
id int primary key, | ||
name varchar(255) not null, | ||
gender enum('male', 'female'), | ||
email varchar(255) not null, | ||
key idx_name_email(name, email))` | ||
ddlEvent := helper.DDL2Event(sql) | ||
|
||
m, err := enc.EncodeDDLEvent(ddlEvent) | ||
dec, err := NewDecoder(ctx, codecConfig, nil) | ||
require.NoError(t, err) | ||
|
||
dec, err := NewDecoder(ctx, codecConfig, nil) | ||
m, err := enc.EncodeDDLEvent(createTableDDLEvent) | ||
require.NoError(t, err) | ||
|
||
err = dec.AddKeyValue(m.Key, m.Value) | ||
|
@@ -106,22 +112,20 @@ func TestEncodeDDLEvent(t *testing.T) { | |
|
||
event, err := dec.NextDDLEvent() | ||
require.NoError(t, err) | ||
require.Equal(t, ddlEvent.CommitTs, event.CommitTs) | ||
require.Equal(t, createTableDDLEvent.CommitTs, event.CommitTs) | ||
// because we don't we don't set startTs in the encoded message, | ||
// so the startTs is equal to commitTs | ||
require.Equal(t, ddlEvent.CommitTs, event.StartTs) | ||
require.Equal(t, ddlEvent.Query, event.Query) | ||
require.Equal(t, len(ddlEvent.TableInfo.Columns), len(event.TableInfo.Columns)) | ||
require.Equal(t, len(ddlEvent.TableInfo.Indices)+1, len(event.TableInfo.Indices)) | ||
|
||
item := dec.memo.Read(ddlEvent.TableInfo.TableName.Schema, | ||
ddlEvent.TableInfo.TableName.Table, ddlEvent.TableInfo.UpdateTS) | ||
require.Equal(t, createTableDDLEvent.CommitTs, event.StartTs) | ||
require.Equal(t, createTableDDLEvent.Query, event.Query) | ||
require.Equal(t, len(createTableDDLEvent.TableInfo.Columns), len(event.TableInfo.Columns)) | ||
require.Equal(t, len(createTableDDLEvent.TableInfo.Indices)+1, len(event.TableInfo.Indices)) | ||
require.Nil(t, event.PreTableInfo) | ||
|
||
item := dec.memo.Read(createTableDDLEvent.TableInfo.TableName.Schema, | ||
createTableDDLEvent.TableInfo.TableName.Table, createTableDDLEvent.TableInfo.UpdateTS) | ||
require.NotNil(t, item) | ||
|
||
sql = `insert into test.t values (1, "jack", "male", "[email protected]")` | ||
row := helper.DML2Event(sql, "test", "t") | ||
|
||
err = enc.AppendRowChangedEvent(context.Background(), "", row, func() {}) | ||
err = enc.AppendRowChangedEvent(context.Background(), "", insertEvent, func() {}) | ||
require.NoError(t, err) | ||
|
||
messages := enc.Build() | ||
|
@@ -138,12 +142,59 @@ func TestEncodeDDLEvent(t *testing.T) { | |
|
||
decodedRow, err := dec.NextRowChangedEvent() | ||
require.NoError(t, err) | ||
require.Equal(t, decodedRow.CommitTs, row.CommitTs) | ||
require.Equal(t, decodedRow.Table.Schema, row.Table.Schema) | ||
require.Equal(t, decodedRow.Table.Table, row.Table.Table) | ||
require.Equal(t, decodedRow.CommitTs, insertEvent.CommitTs) | ||
require.Equal(t, decodedRow.Table.Schema, insertEvent.Table.Schema) | ||
require.Equal(t, decodedRow.Table.Table, insertEvent.Table.Table) | ||
require.Nil(t, decodedRow.PreColumns) | ||
|
||
helper.Tk().MustExec("drop table test.t") | ||
m, err = enc.EncodeDDLEvent(renameTableDDLEvent) | ||
require.NoError(t, err) | ||
|
||
err = dec.AddKeyValue(m.Key, m.Value) | ||
require.NoError(t, err) | ||
|
||
messageType, hasNext, err = dec.HasNext() | ||
require.NoError(t, err) | ||
require.True(t, hasNext) | ||
require.Equal(t, model.MessageTypeDDL, messageType) | ||
require.NotEqual(t, 0, dec.msg.BuildTs) | ||
|
||
event, err = dec.NextDDLEvent() | ||
require.NoError(t, err) | ||
require.Equal(t, renameTableDDLEvent.CommitTs, event.CommitTs) | ||
// because we don't we don't set startTs in the encoded message, | ||
// so the startTs is equal to commitTs | ||
require.Equal(t, renameTableDDLEvent.CommitTs, event.StartTs) | ||
require.Equal(t, renameTableDDLEvent.Query, event.Query) | ||
require.Equal(t, len(renameTableDDLEvent.TableInfo.Columns), len(event.TableInfo.Columns)) | ||
require.Equal(t, len(renameTableDDLEvent.TableInfo.Indices)+1, len(event.TableInfo.Indices)) | ||
require.NotNil(t, event.PreTableInfo) | ||
|
||
item = dec.memo.Read(renameTableDDLEvent.TableInfo.TableName.Schema, | ||
renameTableDDLEvent.TableInfo.TableName.Table, renameTableDDLEvent.TableInfo.UpdateTS) | ||
require.NotNil(t, item) | ||
|
||
err = enc.AppendRowChangedEvent(context.Background(), "", insertEvent2, func() {}) | ||
require.NoError(t, err) | ||
|
||
messages = enc.Build() | ||
require.Len(t, messages, 1) | ||
|
||
err = dec.AddKeyValue(messages[0].Key, messages[0].Value) | ||
require.NoError(t, err) | ||
|
||
messageType, hasNext, err = dec.HasNext() | ||
require.NoError(t, err) | ||
require.True(t, hasNext) | ||
require.Equal(t, model.MessageTypeRow, messageType) | ||
require.NotEqual(t, 0, dec.msg.BuildTs) | ||
|
||
decodedRow, err = dec.NextRowChangedEvent() | ||
require.NoError(t, err) | ||
require.Equal(t, decodedRow.CommitTs, insertEvent2.CommitTs) | ||
require.Equal(t, decodedRow.Table.Schema, insertEvent2.Table.Schema) | ||
require.Equal(t, decodedRow.Table.Table, insertEvent2.Table.Table) | ||
require.Nil(t, decodedRow.PreColumns) | ||
} | ||
} | ||
|
||
|
@@ -223,6 +274,20 @@ func TestEncodeBootstrapEvent(t *testing.T) { | |
helper := entry.NewSchemaTestHelper(t) | ||
defer helper.Close() | ||
|
||
sql := `create table test.t( | ||
id int primary key, | ||
name varchar(255) not null, | ||
age int, | ||
email varchar(255) not null, | ||
key idx_name_email(name, email))` | ||
ddlEvent := helper.DDL2Event(sql) | ||
ddlEvent.IsBootstrap = true | ||
|
||
sql = `insert into test.t values (1, "jack", 23, "[email protected]")` | ||
row := helper.DML2Event(sql, "test", "t") | ||
|
||
helper.Tk().MustExec("drop table test.t") | ||
|
||
ctx := context.Background() | ||
for _, compressionType := range []string{ | ||
compression.None, | ||
|
@@ -235,15 +300,6 @@ func TestEncodeBootstrapEvent(t *testing.T) { | |
require.NoError(t, err) | ||
enc := builder.Build() | ||
|
||
sql := `create table test.t( | ||
id int primary key, | ||
name varchar(255) not null, | ||
age int, | ||
email varchar(255) not null, | ||
key idx_name_email(name, email))` | ||
ddlEvent := helper.DDL2Event(sql) | ||
ddlEvent.IsBootstrap = true | ||
|
||
m, err := enc.EncodeDDLEvent(ddlEvent) | ||
require.NoError(t, err) | ||
|
||
|
@@ -274,9 +330,6 @@ func TestEncodeBootstrapEvent(t *testing.T) { | |
ddlEvent.TableInfo.TableName.Table, ddlEvent.TableInfo.UpdateTS) | ||
require.NotNil(t, item) | ||
|
||
sql = `insert into test.t values (1, "jack", 23, "[email protected]")` | ||
row := helper.DML2Event(sql, "test", "t") | ||
|
||
err = enc.AppendRowChangedEvent(context.Background(), "", row, func() {}) | ||
require.NoError(t, err) | ||
|
||
|
@@ -298,8 +351,6 @@ func TestEncodeBootstrapEvent(t *testing.T) { | |
require.Equal(t, decodedRow.Table.Schema, row.Table.Schema) | ||
require.Equal(t, decodedRow.Table.Table, row.Table.Table) | ||
require.Nil(t, decodedRow.PreColumns) | ||
|
||
helper.Tk().MustExec("drop table test.t") | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.