@@ -25,6 +25,7 @@ import (
2525 commonEvent "github.com/pingcap/ticdc/pkg/common/event"
2626 "github.com/pingcap/ticdc/pkg/errors"
2727 "github.com/pingcap/ticdc/pkg/retry"
28+ "github.com/pingcap/tidb/dumpling/export"
2829 timodel "github.com/pingcap/tidb/pkg/meta/model"
2930 "go.uber.org/zap"
3031)
@@ -189,23 +190,16 @@ func (w *Writer) waitAsyncDDLDone(event *commonEvent.DDLEvent) {
189190 return
190191 }
191192
192- var relatedTableIDs []int64
193193 switch event .GetBlockedTables ().InfluenceType {
194- case commonEvent .InfluenceTypeNormal :
195- relatedTableIDs = event .GetBlockedTables ().TableIDs
196194 // db-class, all-class ddl with not affect by async ddl, just return
197195 case commonEvent .InfluenceTypeDB , commonEvent .InfluenceTypeAll :
198196 return
199197 }
200198
201- for _ , tableID := range relatedTableIDs {
202- // tableID 0 means table trigger, which can't do async ddl
203- if tableID == 0 {
204- continue
205- }
199+ for _ , blockedTable := range event .GetBlockedTableNames () {
206200 // query the downstream,
207201 // if the ddl is still running, we should wait for it.
208- err := w .checkAndWaitAsyncDDLDoneDownstream (tableID )
202+ err := w .checkAndWaitAsyncDDLDoneDownstream (blockedTable . SchemaName , blockedTable . TableName )
209203 if err != nil {
210204 log .Error ("check previous asynchronous ddl failed" ,
211205 zap .String ("keyspace" , w .ChangefeedID .Keyspace ()),
@@ -216,51 +210,45 @@ func (w *Writer) waitAsyncDDLDone(event *commonEvent.DDLEvent) {
216210}
217211
218212// true means the async ddl is still running, false means the async ddl is done.
219- func (w * Writer ) doQueryAsyncDDL (tableID int64 , query string ) (bool , error ) {
213+ func (w * Writer ) doQueryAsyncDDL (query string ) (bool , error ) {
220214 start := time .Now ()
221215 rows , err := w .db .QueryContext (w .ctx , query )
222216 log .Debug ("query duration" , zap .Any ("duration" , time .Since (start )), zap .Any ("query" , query ))
223217 if err != nil {
224218 return false , errors .WrapError (errors .ErrMySQLTxnError , errors .WithMessage (err , fmt .Sprintf ("failed to query ddl jobs table; Query is %s" , query )))
225219 }
226-
227- defer rows .Close ()
228- var jobID int64
229- var jobType string
230- var schemaState string
231- var state string
232-
233- noRows := true
234- for rows .Next () {
235- noRows = false
236- err := rows .Scan (& jobID , & jobType , & schemaState , & state )
237- if err != nil {
238- return false , errors .WrapError (errors .ErrMySQLTxnError , errors .WithMessage (err , fmt .Sprintf ("failed to query ddl jobs table; Query is %s" , query )))
239- }
240-
241- log .Info ("async ddl is still running" ,
220+ rets , err := export .GetSpecifiedColumnValuesAndClose (rows , "JOB_ID" , "JOB_TYPE" , "SCHEMA_STATE" , "STATE" , "QUERY" )
221+ if err != nil {
222+ log .Error ("check previous asynchronous ddl failed" ,
242223 zap .String ("changefeed" , w .ChangefeedID .String ()),
243- zap .Duration ("checkDuration" , time .Since (start )),
244- zap .Any ("tableID" , tableID ),
245- zap .Any ("jobID" , jobID ),
246- zap .String ("jobType" , jobType ),
247- zap .String ("schemaState" , schemaState ),
248- zap .String ("state" , state ))
249- break
224+ zap .Error (err ))
225+ return false , errors .Trace (err )
250226 }
251227
252- if noRows {
228+ if len ( rets ) == 0 {
253229 return false , nil
254230 }
231+ ret := rets [0 ]
232+ jobID , jobType , schemaState , state , runningDDL := ret [0 ], ret [1 ], ret [2 ], ret [3 ], ret [4 ]
233+ log .Info ("async ddl is still running" ,
234+ zap .String ("changefeed" , w .ChangefeedID .String ()),
235+ zap .Duration ("checkDuration" , time .Since (start )),
236+ zap .String ("runningDDL" , runningDDL ),
237+ zap .String ("query" , query ),
238+ zap .Any ("jobID" , jobID ),
239+ zap .String ("jobType" , jobType ),
240+ zap .String ("schemaState" , schemaState ),
241+ zap .String ("state" , state ))
255242
256243 return true , nil
257244}
258245
259246// query the ddl jobs to find the state of the async ddl
260247// if the ddl is still running, we should wait for it.
261- func (w * Writer ) checkAndWaitAsyncDDLDoneDownstream (tableID int64 ) error {
262- query := fmt .Sprintf (checkRunningAddIndexSQL , tableID )
263- running , err := w .doQueryAsyncDDL (tableID , query )
248+ func (w * Writer ) checkAndWaitAsyncDDLDoneDownstream (schemaName , tableName string ) error {
249+ checkSQL := getCheckRunningAddIndexSQL (w .cfg )
250+ query := fmt .Sprintf (checkSQL , schemaName , tableName )
251+ running , err := w .doQueryAsyncDDL (query )
264252 if err != nil {
265253 return err
266254 }
@@ -276,7 +264,7 @@ func (w *Writer) checkAndWaitAsyncDDLDoneDownstream(tableID int64) error {
276264 case <- w .ctx .Done ():
277265 return nil
278266 case <- ticker .C :
279- running , err = w .doQueryAsyncDDL (tableID , query )
267+ running , err = w .doQueryAsyncDDL (query )
280268 if err != nil {
281269 return err
282270 }
0 commit comments