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

executor: sync deletable columns to binlog when remove record #53617

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions pkg/ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func onAddColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error)
case model.StateWriteReorganization:
// reorganization -> public
// Adjust table column offset.
failpoint.InjectCall("onAddColumnStateWriteReorg")
offset, err := LocateOffsetToMove(columnInfo.Offset, pos, tblInfo)
if err != nil {
return ver, errors.Trace(err)
Expand Down Expand Up @@ -271,6 +272,7 @@ func onDropColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error)
}
case model.StateWriteOnly:
// write only -> delete only
failpoint.InjectCall("onDropColumnStateWriteOnly")
colInfo.State = model.StateDeleteOnly
tblInfo.MoveColumnInfo(colInfo.Offset, len(tblInfo.Columns)-1)
if len(idxInfos) > 0 {
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ go_test(
"//pkg/testkit",
"//pkg/testkit/external",
"//pkg/testkit/testdata",
"//pkg/testkit/testfailpoint",
"//pkg/testkit/testmain",
"//pkg/testkit/testsetup",
"//pkg/types",
Expand Down
40 changes: 40 additions & 0 deletions pkg/executor/executor_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
"fmt"
"strconv"
"strings"
"sync"
"testing"
"time"

"github.com/pingcap/tidb/pkg/errno"
"github.com/pingcap/tidb/pkg/executor"
"github.com/pingcap/tidb/pkg/sessionctx/binloginfo"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -694,3 +697,40 @@ func TestSavepointWithBinlog(t *testing.T) {
tk.MustExec("commit")
tk.MustQuery("select * from t").Check(testkit.Rows("1 1"))
}

func TestColumnNotMatchError(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.Session().GetSessionVars().BinlogClient = binloginfo.MockPumpsClient(&testkit.MockPumpClient{})
tk.MustExec("set @@global.tidb_enable_metadata_lock=0")
tk.MustExec("use test")
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
tk.MustExec("create table t(id int primary key, a int)")
tk.MustExec("insert into t values(1, 2)")

testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onAddColumnStateWriteReorg", func() {
tk.MustExec("begin;")
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
tk2.MustExec("alter table t add column wait_notify int")
wg.Done()
}()
wg.Wait()
tk.MustExec("delete from t where id=1")
tk.MustGetErrCode("commit", errno.ErrInfoSchemaChanged)

testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onDropColumnStateWriteOnly", func() {
tk.MustExec("begin;")
})
wg.Add(1)
go func() {
tk2.MustExec("alter table t drop column wait_notify")
wg.Done()
}()
wg.Wait()
tk.MustExec("delete from t where id=1")
tk.MustGetErrCode("commit", errno.ErrInfoSchemaChanged)
}
27 changes: 19 additions & 8 deletions pkg/table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,23 +1368,19 @@ func (t *TableCommon) RemoveRecord(ctx table.MutateContext, h kv.Handle, r []typ
memBuffer.Release(sh)

if shouldWriteBinlog(ctx.GetSessionVars(), t.meta) {
cols := t.Cols()
colIDs := make([]int64, 0, len(cols)+1)
for _, col := range cols {
colIDs = append(colIDs, col.ID)
}
publicDt, colIDs := projectPublicColData(t, r)
var binlogRow []types.Datum
if !t.meta.PKIsHandle && !t.meta.IsCommonHandle {
colIDs = append(colIDs, model.ExtraHandleID)
binlogRow = make([]types.Datum, 0, len(r)+1)
binlogRow = append(binlogRow, r...)
binlogRow = make([]types.Datum, 0, len(publicDt)+1)
binlogRow = append(binlogRow, publicDt...)
handleData, err := h.Data()
if err != nil {
return err
}
binlogRow = append(binlogRow, handleData...)
} else {
binlogRow = r
binlogRow = publicDt
}
err = t.addDeleteBinlog(ctx, binlogRow, colIDs)
}
Expand All @@ -1403,6 +1399,21 @@ func (t *TableCommon) RemoveRecord(ctx table.MutateContext, h kv.Handle, r []typ
return err
}

func projectPublicColData(t *TableCommon, deletableData []types.Datum) (publicData []types.Datum, colIDs []int64) {
publicColLen := len(t.Cols())
publicData = make([]types.Datum, 0, publicColLen)
colIDs = make([]int64, 0, publicColLen+1)
deletableCols := t.DeletableCols()
for i, d := range deletableData {
dCol := deletableCols[i]
if dCol.State == model.StatePublic {
Copy link
Contributor

@D3Hunter D3Hunter May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suppose we are dropping/lossy-modify a column, this list won't contains the changed column while binlog think the column exist, not sure if binlog can handle it.

Copy link
Contributor Author

@tangenta tangenta May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For lossy modifying a column, it consists of two steps:

1. add a hidden column
2. drop the old column

Since DDL is only synced when the job state is done, the hidden column does not exists in downstream.

publicData = append(publicData, d)
colIDs = append(colIDs, dCol.ID)
}
}
return publicData, colIDs
}

func (t *TableCommon) addInsertBinlog(ctx table.MutateContext, h kv.Handle, row []types.Datum, colIDs []int64) error {
mutation := t.getMutation(ctx)
handleData, err := h.Data()
Expand Down