github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/framework/canal/kafka_docker_env.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  	"io/ioutil"
    19  	"net/http"
    20  
    21  	"github.com/integralist/go-findroot/find"
    22  	"github.com/pingcap/errors"
    23  	"github.com/pingcap/log"
    24  	"github.com/pingcap/ticdc/integration/framework"
    25  	"go.uber.org/zap"
    26  )
    27  
    28  const (
    29  	dockerComposeFilePath   = "/docker-compose-canal.yml"
    30  	controllerContainerName = "ticdc_controller_1"
    31  )
    32  
    33  // KafkaDockerEnv represents the docker-compose service defined in docker-compose-canal.yml
    34  type KafkaDockerEnv struct {
    35  	framework.DockerEnv
    36  }
    37  
    38  // NewKafkaDockerEnv creates a new KafkaDockerEnv
    39  func NewKafkaDockerEnv(dockerComposeFile string) *KafkaDockerEnv {
    40  	healthChecker := func() error {
    41  		if err := checkCanalAdapterState(); err != nil {
    42  			return err
    43  		}
    44  		if err := checkDbConn(framework.UpstreamDSN); err != nil {
    45  			return err
    46  		}
    47  		return checkDbConn(framework.DownstreamDSN)
    48  	}
    49  	var file string
    50  	if dockerComposeFile == "" {
    51  		st, err := find.Repo()
    52  		if err != nil {
    53  			log.Fatal("Could not find git repo root", zap.Error(err))
    54  		}
    55  		file = st.Path + dockerComposeFilePath
    56  	} else {
    57  		file = dockerComposeFile
    58  	}
    59  
    60  	return &KafkaDockerEnv{DockerEnv: framework.DockerEnv{
    61  		DockerComposeOperator: framework.DockerComposeOperator{
    62  			FileName:      file,
    63  			Controller:    controllerContainerName,
    64  			HealthChecker: healthChecker,
    65  		},
    66  	}}
    67  }
    68  
    69  func checkDbConn(dsn string) error {
    70  	db, err := sql.Open("mysql", dsn)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	if db == nil {
    75  		return errors.New("Can not connect to " + dsn)
    76  	}
    77  	defer db.Close()
    78  	err = db.Ping()
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return nil
    83  }
    84  
    85  func checkCanalAdapterState() error {
    86  	resp, err := http.Get(
    87  		"http://127.0.0.1:8081/syncSwitch/" + testDbName)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if resp.Body == nil {
    93  		return errors.New("Canal Adapter Rest API returned empty body, there is no subscript topic")
    94  	}
    95  	defer resp.Body.Close()
    96  
    97  	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
    98  		str, err := ioutil.ReadAll(resp.Body)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		log.Warn(
   103  			"Canal Adapter Rest API returned",
   104  			zap.Int("status", resp.StatusCode),
   105  			zap.ByteString("body", str))
   106  		return errors.Errorf("Kafka Connect Rest API returned status code %d", resp.StatusCode)
   107  	}
   108  	return nil
   109  }