github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/framework/canal/kafka_single_table.go (about)

     1  // Copyright 2020 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 canal
    15  
    16  import (
    17  	"database/sql"
    18  	"time"
    19  
    20  	"github.com/pingcap/log"
    21  	"github.com/pingcap/ticdc/integration/framework"
    22  )
    23  
    24  const (
    25  	testDbName = "testdb"
    26  )
    27  
    28  // SingleTableTask provides a basic implementation for an Avro test case
    29  type SingleTableTask struct {
    30  	TableName string
    31  	UseJSON   bool
    32  }
    33  
    34  // Name implements Task
    35  func (c *SingleTableTask) Name() string {
    36  	log.Warn("SingleTableTask should be embedded in another Task")
    37  	return "SingleTableTask-" + c.TableName
    38  }
    39  
    40  // GetCDCProfile implements Task
    41  func (c *SingleTableTask) GetCDCProfile() *framework.CDCProfile {
    42  	var protocol string
    43  	if c.UseJSON {
    44  		protocol = "canal-json"
    45  	} else {
    46  		protocol = "canal"
    47  	}
    48  	return &framework.CDCProfile{
    49  		PDUri:      "http://upstream-pd:2379",
    50  		SinkURI:    "kafka://kafka:9092/" + testDbName + "?kafka-version=2.6.0&protocol=" + protocol,
    51  		Opts:       map[string]string{"force-handle-key-pkey": "true", "support-txn": "true"},
    52  		ConfigFile: "/config/canal-test-config.toml",
    53  	}
    54  }
    55  
    56  // Prepare implements Task
    57  func (c *SingleTableTask) Prepare(taskContext *framework.TaskContext) error {
    58  	err := taskContext.CreateDB(testDbName)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	_ = taskContext.Upstream.Close()
    64  	taskContext.Upstream, err = sql.Open("mysql", framework.UpstreamDSN+testDbName)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	_ = taskContext.Downstream.Close()
    70  	taskContext.Downstream, err = sql.Open("mysql", framework.DownstreamDSN+testDbName)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	taskContext.Downstream.SetConnMaxLifetime(5 * time.Second)
    75  
    76  	if taskContext.WaitForReady != nil {
    77  		log.Info("Waiting for env to be ready")
    78  		return taskContext.WaitForReady()
    79  	}
    80  	return nil
    81  }
    82  
    83  // Run implements Task
    84  func (c *SingleTableTask) Run(taskContext *framework.TaskContext) error {
    85  	log.Warn("SingleTableTask has been run")
    86  	return nil
    87  }