From 075b1990114d554bbccbc1655fc180fe86eedc5f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 30 Dec 2021 09:59:50 +0800 Subject: [PATCH] ddl: don't write binlog for 'ALTER TABLE t CACHE|NOCACHE' (#31106) close pingcap/tidb#31105 --- ddl/ddl_worker.go | 4 +++ sessionctx/binloginfo/binloginfo_test.go | 34 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index 39d564ef2a..c2b3dce8cc 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -621,6 +621,10 @@ func skipWriteBinlog(job *model.Job) bool { // it's used to update table's TiFlash replica available status. case model.ActionUpdateTiFlashReplicaStatus: return true + // Don't sync 'alter table cache|nocache' to other tools. + // It's internal to the current cluster. + case model.ActionAlterCacheTable, model.ActionAlterNoCacheTable: + return true } return false diff --git a/sessionctx/binloginfo/binloginfo_test.go b/sessionctx/binloginfo/binloginfo_test.go index 511db81cd8..141f3f2c2d 100644 --- a/sessionctx/binloginfo/binloginfo_test.go +++ b/sessionctx/binloginfo/binloginfo_test.go @@ -675,6 +675,14 @@ func TestAddSpecialComment(t *testing.T) { "alter table t force, auto_increment = 12;", "alter table t force, auto_increment = 12;", }, + { + "alter table t cache", + "alter table t cache", + }, + { + "alter table t nocache", + "alter table t nocache", + }, } for _, ca := range testCase { re := binloginfo.AddSpecialComment(ca.input) @@ -785,6 +793,32 @@ func TestTempTableBinlog(t *testing.T) { require.True(t, ok) } +func TestAlterTableCache(t *testing.T) { + s, clean := createBinlogSuite(t) + defer clean() + + // Don't write binlog for 'ALTER TABLE t CACHE|NOCACHE'. + // Cached table is regarded as normal table. + + tk := testkit.NewTestKit(t, s.store) + tk.MustExec("use test") + tk.Session().GetSessionVars().BinlogClient = s.client + tk.MustExec("drop table if exists t") + ddlQuery := "create table t (id int)" + tk.MustExec(ddlQuery) + + tk.MustExec(`alter table t cache`) + // The latest DDL is still the previous one. + getLatestDDLBinlog(t, s.pump, ddlQuery) + + tk.MustExec("insert into t values (?)", 666) + prewriteVal := getLatestBinlogPrewriteValue(t, s.pump) + require.Equal(t, 1, len(prewriteVal.Mutations)) + + tk.MustExec(`alter table t nocache`) + getLatestDDLBinlog(t, s.pump, ddlQuery) +} + func TestIssue28292(t *testing.T) { s, clean := createBinlogSuite(t) defer clean()