github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/checker/onlineddl.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package checker
    15  
    16  import (
    17  	"context"
    18  	"database/sql"
    19  
    20  	"github.com/pingcap/tidb/pkg/util/dbutil"
    21  	"github.com/pingcap/tidb/pkg/util/filter"
    22  	onlineddl "github.com/pingcap/tiflow/dm/syncer/online-ddl-tools"
    23  )
    24  
    25  type OnlineDDLChecker struct {
    26  	db           *sql.DB
    27  	onlineDDL    onlineddl.OnlinePlugin
    28  	bwlist       *filter.Filter
    29  	checkSchemas map[string]struct{}
    30  }
    31  
    32  func NewOnlineDDLChecker(db *sql.DB, checkSchemas map[string]struct{}, onlineDDL onlineddl.OnlinePlugin, bwlist *filter.Filter) RealChecker {
    33  	return &OnlineDDLChecker{
    34  		db:           db,
    35  		checkSchemas: checkSchemas,
    36  		onlineDDL:    onlineDDL,
    37  		bwlist:       bwlist,
    38  	}
    39  }
    40  
    41  func (c *OnlineDDLChecker) Check(ctx context.Context) *Result {
    42  	r := &Result{
    43  		Name:  c.Name(),
    44  		Desc:  "check if in online ddl phase",
    45  		State: StateSuccess,
    46  		Extra: "online ddl",
    47  	}
    48  
    49  	for schema := range c.checkSchemas {
    50  		tableList, err := dbutil.GetTables(ctx, c.db, schema)
    51  		if err != nil {
    52  			markCheckError(r, err)
    53  			return r
    54  		}
    55  		realTables := []*filter.Table{}
    56  		for _, table := range tableList {
    57  			if c.onlineDDL.TableType(table) == onlineddl.GhostTable {
    58  				realTable := c.onlineDDL.RealName(table)
    59  				realTables = append(realTables, &filter.Table{
    60  					Schema: schema,
    61  					Name:   realTable,
    62  				})
    63  			}
    64  		}
    65  		tables := c.bwlist.Apply(realTables)
    66  		if len(tables) != 0 {
    67  			r.State = StateFailure
    68  			r.Extra = "please wait the online-ddl over"
    69  			r.Errors = append(r.Errors, NewError("your ddl is in pt/ghost online-ddl"))
    70  			return r
    71  		}
    72  	}
    73  
    74  	return r
    75  }
    76  
    77  func (c *OnlineDDLChecker) Name() string {
    78  	return "online ddl checker"
    79  }