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