github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/binlog/event/helper.go (about)

     1  // Copyright 2019 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 event
    15  
    16  import (
    17  	gmysql "github.com/go-mysql-org/go-mysql/mysql"
    18  	"github.com/go-mysql-org/go-mysql/replication"
    19  	"github.com/pingcap/tiflow/dm/pkg/gtid"
    20  	"github.com/pingcap/tiflow/dm/pkg/terror"
    21  )
    22  
    23  // GTIDsFromPreviousGTIDsEvent get GTID set from a PreviousGTIDsEvent.
    24  func GTIDsFromPreviousGTIDsEvent(e *replication.BinlogEvent) (gmysql.GTIDSet, error) {
    25  	var gSetStr string
    26  	switch ev := e.Event.(type) {
    27  	case *replication.PreviousGTIDsEvent:
    28  		gSetStr = ev.GTIDSets
    29  	default:
    30  		return nil, terror.ErrBinlogPrevGTIDEvNotValid.Generate(e.Event)
    31  	}
    32  
    33  	return gtid.ParserGTID(gmysql.MySQLFlavor, gSetStr)
    34  }
    35  
    36  // GTIDsFromMariaDBGTIDListEvent get GTID set from a MariaDBGTIDListEvent.
    37  func GTIDsFromMariaDBGTIDListEvent(e *replication.BinlogEvent) (gmysql.GTIDSet, error) {
    38  	var gtidListEv *replication.MariadbGTIDListEvent
    39  	switch ev := e.Event.(type) {
    40  	case *replication.MariadbGTIDListEvent:
    41  		gtidListEv = ev
    42  	default:
    43  		return nil, terror.ErrBinlogNeedMariaDBGTIDSet.Generate(e.Event)
    44  	}
    45  
    46  	ggSet, err := gmysql.ParseMariadbGTIDSet("")
    47  	if err != nil {
    48  		return nil, terror.ErrBinlogParseMariaDBGTIDSet.Delegate(err)
    49  	}
    50  	mGSet := ggSet.(*gmysql.MariadbGTIDSet)
    51  	for _, mGTID := range gtidListEv.GTIDs {
    52  		mgClone := mGTID // use another variable so we can get different pointer (&mgClone below) when iterating
    53  		err = mGSet.AddSet(&mgClone)
    54  		if err != nil {
    55  			return nil, terror.ErrBinlogMariaDBAddGTIDSet.Delegate(err, mGTID)
    56  		}
    57  	}
    58  
    59  	return ggSet, nil
    60  }