Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 6 additions & 3 deletions downstreamadapter/sink/mysql/causality/conflict_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ func New(
for i := 0; i < opt.Count; i++ {
ret.resolvedTxnCaches[i] = newTxnCache(opt)
}
log.Info("conflict detector initialized", zap.Int("cacheCount", opt.Count),
zap.Int("cacheSize", opt.Size), zap.String("BlockStrategy", string(opt.BlockStrategy)))
log.Info("conflict detector initialized",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Int("cacheCount", opt.Count),
zap.Int("cacheSize", opt.Size),
zap.String("blockStrategy", string(opt.BlockStrategy)))
return ret
}

Expand All @@ -89,7 +93,6 @@ func (d *ConflictDetector) Run(ctx context.Context) error {
if notifyCallback, ok := d.notifiedNodes.Get(); ok {
notifyCallback()
} else {
log.Info("notifiedNodes is closed, return")
return nil
}
}
Expand Down
2 changes: 0 additions & 2 deletions downstreamadapter/sink/mysql/causality/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/collate"
"go.uber.org/zap"
)

var conflictCollators map[string]collate.Collator
Expand Down Expand Up @@ -111,7 +110,6 @@ func genRowKeys(row commonEvent.RowChange, tableInfo *common.TableInfo, dispatch
if len(keys) == 0 {
// use dispatcherID as key if no key generated (no PK/UK),
// no concurrence for rows in the same dispatcher.
log.Debug("Use dispatcherID as the key", zap.Any("dispatcherID", dispatcherID))
tableKey := make([]byte, 8)
binary.BigEndian.PutUint64(tableKey, uint64(dispatcherID.GetLow()))
keys = [][]byte{tableKey}
Expand Down
24 changes: 14 additions & 10 deletions downstreamadapter/sink/mysql/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ func newMySQLSinkWithDBs(
if enableActiveActive && cfg.IsTiDB && cfg.ActiveActiveSyncStatsInterval > 0 {
supported, err := mysql.CheckActiveActiveSyncStatsSupported(ctx, dmlDB)
if err != nil {
log.Info("failed to check tidb_cdc_active_active_sync_stats support, disable metric collection",
log.Warn("check tidb_cdc_active_active_sync_stats support failed, disable metric collection",
zap.String("keyspace", changefeedID.Keyspace()),
zap.Stringer("changefeed", changefeedID),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(err))
} else if supported {
activeActiveSyncStatsCollector = mysql.NewActiveActiveSyncStatsCollector(changefeedID)
} else {
log.Info("downstream does not support tidb_cdc_active_active_sync_stats, disable metric collection",
zap.String("keyspace", changefeedID.Keyspace()),
zap.Stringer("changefeed", changefeedID))
zap.String("changefeedID", changefeedID.Name()))
}
}

Expand Down Expand Up @@ -438,7 +438,7 @@ func (s *Sink) WriteBlockEvent(event commonEvent.BlockEvent) error {
default:
log.Panic("mysql sink meet unknown event type",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeed", s.changefeedID.Name()),
zap.String("changefeedID", s.changefeedID.Name()),
zap.String("eventType", commonEvent.TypeToString(event.GetType())))
}
if err != nil {
Expand Down Expand Up @@ -480,8 +480,9 @@ func (s *Sink) runProgressTableWriter(ctx context.Context) {
if ctx.Err() != nil {
return
}
log.Warn("failed to update active active progress table",
zap.String("changefeed", s.changefeedID.DisplayName.String()),
log.Warn("update active active progress table failed",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeedID", s.changefeedID.Name()),
zap.Error(err))
}
}
Expand Down Expand Up @@ -574,8 +575,9 @@ func (s *Sink) Close() {

func (s *Sink) closeDBPool(role string, db *sql.DB) {
if err := db.Close(); err != nil {
log.Warn("failed to close mysql sink db pool",
zap.String("changefeed", s.changefeedID.String()),
log.Warn("close mysql sink db pool failed",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeedID", s.changefeedID.Name()),
zap.String("dbRole", role),
zap.Error(err))
}
Expand All @@ -601,8 +603,10 @@ func (s *Sink) CleanupRemovedChangefeed() error {
}
defer func() {
if closeErr := db.Close(); closeErr != nil {
log.Warn("close mysql cleanup db meet error",
zap.Any("changefeed", s.changefeedID.String()), zap.Error(closeErr))
log.Warn("close mysql cleanup db failed",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeedID", s.changefeedID.Name()),
zap.Error(closeErr))
}
}()

Expand Down
2 changes: 0 additions & 2 deletions downstreamadapter/sink/mysql/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,6 @@ func TestMysqlSinkBatchCreateTablesAcknowledgement(t *testing.T) {
})

for i, query := range queries {
mock.ExpectQuery("BEGIN; SET @ticdc_ts := TIDB_PARSE_TSO(@@tidb_current_ts); ROLLBACK; SELECT @ticdc_ts; SET @ticdc_ts=NULL;").
WillReturnRows(sqlmock.NewRows([]string{"@ticdc_ts"}))
mock.ExpectBegin()
mock.ExpectExec("USE `test`;").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("SET TIMESTAMP = DEFAULT").WillReturnResult(sqlmock.NewResult(0, 0))
Expand Down
2 changes: 0 additions & 2 deletions pkg/applier/redo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ func TestApplyReplicationKeyLossRecovery(t *testing.T) {
AddRow(tableID, 120, completed, false))
}
if !completed {
mock.ExpectQuery("BEGIN; SET @ticdc_ts := TIDB_PARSE_TSO(@@tidb_current_ts); ROLLBACK; SELECT @ticdc_ts; SET @ticdc_ts=NULL;").
WillReturnRows(sqlmock.NewRows([]string{"ts"}).AddRow("2026-01-01 00:00:00"))
mock.ExpectBegin()
mock.ExpectExec("USE `test`;").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("SET TIMESTAMP = DEFAULT").WillReturnResult(sqlmock.NewResult(0, 0))
Expand Down
79 changes: 58 additions & 21 deletions pkg/sink/mysql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ func (c *Config) Apply(
cfg *config.ChangefeedConfig,
) (err error) {
if sinkURI == nil {
log.Error("empty SinkURI")
return errors.ErrMySQLInvalidConfig.GenWithStack("fail to open MySQL sink, empty SinkURI")
}
c.sinkURI = sinkURI
Expand All @@ -309,19 +308,19 @@ func (c *Config) Apply(
}

query := sinkURI.Query()
if err = getWorkerCount(query, &c.WorkerCount, &c.workerCountSpecified); err != nil {
if err = getWorkerCount(changefeedID, query, &c.WorkerCount, &c.workerCountSpecified); err != nil {
return err
}
if err = getMaxTxnRow(query, &c.MaxTxnRow); err != nil {
if err = getMaxTxnRow(changefeedID, query, &c.MaxTxnRow); err != nil {
return err
}
if err = getMaxMultiUpdateRowCount(query, &c.MaxMultiUpdateRowCount); err != nil {
if err = getMaxMultiUpdateRowCount(changefeedID, query, &c.MaxMultiUpdateRowCount); err != nil {
return err
}
if err = getMaxMultiUpdateRowSize(query, &c.MaxMultiUpdateRowSize); err != nil {
if err = getMaxMultiUpdateRowSize(changefeedID, query, &c.MaxMultiUpdateRowSize); err != nil {
return err
}
if err = getTiDBTxnMode(query, &c.TidbTxnMode, &c.tidbTxnModeSpecified); err != nil {
if err = getTiDBTxnMode(changefeedID, query, &c.TidbTxnMode, &c.tidbTxnModeSpecified); err != nil {
return err
}
if err = c.getSSLCA(query, changefeedID, &c.TLS); err != nil {
Expand Down Expand Up @@ -371,12 +370,17 @@ func (c *Config) Apply(
// We do this is because it can cause the data to be inconsistent if the TiDBSourceID is 0
// in BDR Mode cluster.
if cfg.SinkConfig.TiDBSourceID == 0 {
log.Error("The TiDB source ID should never be set to 0. Please report it as a bug. The default value will be used: 1.",
log.Error("TiDB source ID should never be set to 0, please report it as a bug, using default value 1",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Uint64("tidbSourceID", cfg.SinkConfig.TiDBSourceID))
c.SourceID = config.DefaultTiDBSourceID
} else {
c.SourceID = cfg.SinkConfig.TiDBSourceID
log.Info("TiDB source ID is set", zap.Uint64("sourceID", c.SourceID))
log.Info("TiDB source ID is set",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Uint64("sourceID", c.SourceID))
}
return nil
}
Expand Down Expand Up @@ -405,7 +409,9 @@ func NewMysqlConfigAndDBs(
if err != nil {
if closeErr := dmlDB.Close(); closeErr != nil {
log.Warn("close mysql dml db after control db creation failed",
zap.String("changefeed", changefeedID.String()), zap.Error(closeErr))
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(closeErr))
}
return nil, nil, nil, nil, err
}
Expand Down Expand Up @@ -437,18 +443,25 @@ func closeDMLAndControlDBAfterFailure(
) {
if closeErr := dmlDB.Close(); closeErr != nil {
log.Warn("close mysql dml db after "+failureContext,
zap.String("changefeed", changefeedID.String()), zap.Error(closeErr))
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(closeErr))
}
if closeErr := controlDB.Close(); closeErr != nil {
log.Warn("close mysql control db after "+failureContext,
zap.String("changefeed", changefeedID.String()), zap.Error(closeErr))
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(closeErr))
}
}

func newMysqlConfigAndDB(
ctx context.Context, changefeedID common.ChangeFeedID, sinkURI *url.URL, config *config.ChangefeedConfig,
) (cfg *Config, db *sql.DB, dsnStr string, err error) {
log.Info("create db connection", zap.String("sinkURI", util.MaskSensitiveDataInURIForError(sinkURI.String())))
log.Info("create db connection",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.String("sinkURI", util.MaskSensitiveDataInURIForError(sinkURI.String())))
// create db connection
cfg = New()
err = cfg.Apply(sinkURI, changefeedID, config)
Expand All @@ -473,7 +486,9 @@ func newMysqlConfigAndDB(
}
if closeErr := db.Close(); closeErr != nil {
log.Warn("close mysql db after config creation failed",
zap.String("changefeed", changefeedID.String()), zap.Error(closeErr))
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(closeErr))
}
}()

Expand Down Expand Up @@ -513,7 +528,10 @@ func newMysqlConfigAndDB(
cfg.stmtCache, err = lru.NewWithEvict(prepStmtCacheSize, func(key, value interface{}) {
stmt := value.(*sql.Stmt)
if err := stmt.Close(); err != nil {
log.Warn("failed to close cached prepared statement", zap.Error(err))
log.Warn("close cached prepared statement failed",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(err))
}
})
if err != nil {
Expand All @@ -525,8 +543,9 @@ func newMysqlConfigAndDB(
cfg.SyncPointRetention = config.SyncPointRetention
cfg.MaxAllowedPacket, err = queryMaxAllowedPacket(ctx, db)
if err != nil {
log.Warn("failed to query max_allowed_packet, use default value",
zap.String("changefeed", changefeedID.String()),
log.Warn("query max_allowed_packet failed, use default value",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Error(err))
cfg.MaxAllowedPacket = int64(vardef.DefMaxAllowedPacket)
}
Expand Down Expand Up @@ -566,7 +585,9 @@ func configureControlDBConn(db *sql.DB) {
})
}

func getWorkerCount(values url.Values, workerCount *int, workerCountSpecified *bool) error {
func getWorkerCount(
changefeedID common.ChangeFeedID, values url.Values, workerCount *int, workerCountSpecified *bool,
) error {
s := values.Get("worker-count")
if len(s) == 0 {
return nil
Expand All @@ -582,6 +603,8 @@ func getWorkerCount(values url.Values, workerCount *int, workerCountSpecified *b
}
if c > maxWorkerCount {
log.Warn("worker-count too large",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Int("original", c), zap.Int("override", maxWorkerCount))
c = maxWorkerCount
}
Expand All @@ -591,7 +614,7 @@ func getWorkerCount(values url.Values, workerCount *int, workerCountSpecified *b
return nil
}

func getMaxTxnRow(values url.Values, maxTxnRow *int) error {
func getMaxTxnRow(changefeedID common.ChangeFeedID, values url.Values, maxTxnRow *int) error {
s := values.Get("max-txn-row")
if len(s) == 0 {
return nil
Expand All @@ -607,14 +630,18 @@ func getMaxTxnRow(values url.Values, maxTxnRow *int) error {
}
if c > maxMaxTxnRow {
log.Warn("max-txn-row too large",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Int("original", c), zap.Int("override", maxMaxTxnRow))
c = maxMaxTxnRow
}
*maxTxnRow = c
return nil
}

func getMaxMultiUpdateRowCount(values url.Values, maxMultiUpdateRow *int) error {
func getMaxMultiUpdateRowCount(
changefeedID common.ChangeFeedID, values url.Values, maxMultiUpdateRow *int,
) error {
s := values.Get("max-multi-update-row")
if len(s) == 0 {
return nil
Expand All @@ -630,14 +657,18 @@ func getMaxMultiUpdateRowCount(values url.Values, maxMultiUpdateRow *int) error
}
if c > maxMaxMultiUpdateRowCount {
log.Warn("max-multi-update-row too large",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Int("original", c), zap.Int("override", maxMaxMultiUpdateRowCount))
c = maxMaxMultiUpdateRowCount
}
*maxMultiUpdateRow = c
return nil
}

func getMaxMultiUpdateRowSize(values url.Values, maxMultiUpdateRowSize *int) error {
func getMaxMultiUpdateRowSize(
changefeedID common.ChangeFeedID, values url.Values, maxMultiUpdateRowSize *int,
) error {
s := values.Get("max-multi-update-row-size")
if len(s) == 0 {
return nil
Expand All @@ -654,14 +685,18 @@ func getMaxMultiUpdateRowSize(values url.Values, maxMultiUpdateRowSize *int) err
}
if c > maxMaxMultiUpdateRowSize {
log.Warn("max-multi-update-row-size too large",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.Int("original", c), zap.Int("override", maxMaxMultiUpdateRowSize))
c = maxMaxMultiUpdateRowSize
}
*maxMultiUpdateRowSize = c
return nil
}

func getTiDBTxnMode(values url.Values, mode *string, modeSpecified *bool) error {
func getTiDBTxnMode(
changefeedID common.ChangeFeedID, values url.Values, mode *string, modeSpecified *bool,
) error {
s := values.Get("tidb-txn-mode")
if len(s) == 0 {
return nil
Expand All @@ -671,6 +706,8 @@ func getTiDBTxnMode(values url.Values, mode *string, modeSpecified *bool) error
*mode = s
} else {
log.Warn("invalid tidb-txn-mode, should be pessimistic or optimistic",
zap.String("keyspace", changefeedID.Keyspace()),
zap.String("changefeedID", changefeedID.Name()),
zap.String("default", defaultTiDBTxnMode))
}
return nil
Expand Down
11 changes: 5 additions & 6 deletions pkg/sink/mysql/format_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
package mysql

import (
"github.com/pingcap/log"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/mysql"
"go.uber.org/zap"
)

type visiter struct{}
Expand All @@ -45,17 +44,17 @@ func (f *visiter) Leave(n ast.Node) (node ast.Node, ok bool) {
return n, true
}

func formatQuery(sql string) string {
func formatQuery(sql string) (string, error) {
p := parser.New()
stmt, err := p.ParseOneStmt(sql, "", "")
if err != nil {
log.Error("format query parse one stmt failed", zap.Error(err))
return "", errors.Trace(err)
}
stmt.Accept(&visiter{})

query, err := commonEvent.Restore(stmt)
if err != nil {
log.Error("format query restore failed", zap.Error(err))
return "", errors.Trace(err)
}
return query
return query, nil
}
Loading
Loading