Skip to content

Commit 24fbb5b

Browse files
morgolance6716
andauthored
Add DisableFlushBinlogWhileWaiting config and almost every query event triggers OnPosSynced (#900)
* Add ANALYZE TABLE to canal DDL list * Change to using flush-less WaitUntilPos * Add a better test * Include reviewer feedback * Add greater check --------- Co-authored-by: lance6716 <[email protected]>
1 parent ee48e78 commit 24fbb5b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

canal/canal_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ func (s *canalTestSuite) TestCanal() {
138138
require.NoError(s.T(), err)
139139
}
140140

141+
func (s *canalTestSuite) TestAnalyzeAdvancesSyncedPos() {
142+
<-s.c.WaitDumpDone()
143+
144+
// We should not need to use FLUSH BINARY LOGS
145+
// An ANALYZE TABLE statement should advance the saved position.
146+
// There are still cases that don't advance, such as
147+
// statements that won't parse like [CREATE|DROP] TRIGGER.
148+
s.c.cfg.DisableFlushBinlogWhileWaiting = true
149+
defer func() {
150+
s.c.cfg.DisableFlushBinlogWhileWaiting = false
151+
}()
152+
153+
startingPos, err := s.c.GetMasterPos()
154+
require.NoError(s.T(), err)
155+
156+
s.execute("ANALYZE TABLE test.canal_test")
157+
err = s.c.CatchMasterPos(10 * time.Second)
158+
require.NoError(s.T(), err)
159+
160+
// Ensure the ending pos is greater than the starting pos
161+
// but the filename is the same. This ensures that
162+
// FLUSH BINARY LOGS was not used.
163+
endingPos, err := s.c.GetMasterPos()
164+
require.NoError(s.T(), err)
165+
require.Equal(s.T(), startingPos.Name, endingPos.Name)
166+
require.Greater(s.T(), endingPos.Pos, startingPos.Pos)
167+
}
168+
141169
func (s *canalTestSuite) TestCanalFilter() {
142170
// included
143171
sch, err := s.c.GetTable("test", "canal_test")

canal/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ type Config struct {
9191
// whether disable re-sync for broken connection
9292
DisableRetrySync bool `toml:"disable_retry_sync"`
9393

94+
// whether the function WaitUntilPos() can use FLUSH BINARY LOGS
95+
// to ensure we advance past a position. This should not strictly be required,
96+
// and requires additional privileges.
97+
DisableFlushBinlogWhileWaiting bool `toml:"disable_flush_binlog_while_waiting"`
98+
9499
// Set TLS config
95100
TLSConfig *tls.Config
96101

canal/sync.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ func (c *Canal) handleEvent(ev *replication.BinlogEvent) error {
141141
case *replication.QueryEvent:
142142
stmts, _, err := c.parser.Parse(string(e.Query), "", "")
143143
if err != nil {
144+
// The parser does not understand all syntax.
145+
// For example, it won't parse [CREATE|DROP] TRIGGER statements.
144146
c.cfg.Logger.Errorf("parse query(%s) err %v, will skip this event", e.Query, err)
145147
return nil
146148
}
149+
if len(stmts) > 0 {
150+
savePos = true
151+
}
147152
for _, stmt := range stmts {
148153
nodes := parseStmt(stmt)
149154
for _, node := range nodes {
@@ -155,7 +160,6 @@ func (c *Canal) handleEvent(ev *replication.BinlogEvent) error {
155160
}
156161
}
157162
if len(nodes) > 0 {
158-
savePos = true
159163
force = true
160164
// Now we only handle Table Changed DDL, maybe we will support more later.
161165
if err = c.eventHandler.OnDDL(ev.Header, pos, e); err != nil {
@@ -300,9 +304,11 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error {
300304
case <-timer.C:
301305
return errors.Errorf("wait position %v too long > %s", pos, timeout)
302306
default:
303-
err := c.FlushBinlog()
304-
if err != nil {
305-
return errors.Trace(err)
307+
if !c.cfg.DisableFlushBinlogWhileWaiting {
308+
err := c.FlushBinlog()
309+
if err != nil {
310+
return errors.Trace(err)
311+
}
306312
}
307313
curPos := c.master.Position()
308314
if curPos.Compare(pos) >= 0 {

0 commit comments

Comments
 (0)