github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/testing_utils/cdc_state_checker/cdc_monitor.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 main
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/pingcap/ticdc/pkg/security"
    21  
    22  	"github.com/pingcap/log"
    23  
    24  	"github.com/pingcap/errors"
    25  	"github.com/pingcap/ticdc/cdc/kv"
    26  	"github.com/pingcap/ticdc/pkg/etcd"
    27  	"github.com/pingcap/ticdc/pkg/orchestrator"
    28  	"github.com/prometheus/client_golang/prometheus"
    29  	"go.etcd.io/etcd/clientv3"
    30  	"go.etcd.io/etcd/pkg/logutil"
    31  	"go.uber.org/zap"
    32  	"go.uber.org/zap/zapcore"
    33  	"google.golang.org/grpc"
    34  	"google.golang.org/grpc/backoff"
    35  )
    36  
    37  type cdcMonitor struct {
    38  	etcdCli    *etcd.Client
    39  	etcdWorker *orchestrator.EtcdWorker
    40  	reactor    *cdcMonitReactor
    41  }
    42  
    43  func newCDCMonitor(ctx context.Context, pd string, credential *security.Credential) (*cdcMonitor, error) {
    44  	logConfig := logutil.DefaultZapLoggerConfig
    45  	logConfig.Level = zap.NewAtomicLevelAt(zapcore.ErrorLevel)
    46  
    47  	grpcCredential, err := credential.ToGRPCDialOption()
    48  	if err != nil {
    49  		return nil, errors.Trace(err)
    50  	}
    51  
    52  	etcdCli, err := clientv3.New(clientv3.Config{
    53  		Endpoints:   []string{pd},
    54  		TLS:         nil,
    55  		Context:     ctx,
    56  		LogConfig:   &logConfig,
    57  		DialTimeout: 5 * time.Second,
    58  		DialOptions: []grpc.DialOption{
    59  			grpcCredential,
    60  			grpc.WithBlock(),
    61  			grpc.WithConnectParams(grpc.ConnectParams{
    62  				Backoff: backoff.Config{
    63  					BaseDelay:  time.Second,
    64  					Multiplier: 1.1,
    65  					Jitter:     0.1,
    66  					MaxDelay:   3 * time.Second,
    67  				},
    68  				MinConnectTimeout: 3 * time.Second,
    69  			}),
    70  		},
    71  	})
    72  	if err != nil {
    73  		return nil, errors.Trace(err)
    74  	}
    75  
    76  	wrappedCli := etcd.Wrap(etcdCli, map[string]prometheus.Counter{})
    77  	reactor := &cdcMonitReactor{}
    78  	initState := newCDCReactorState()
    79  	etcdWorker, err := orchestrator.NewEtcdWorker(wrappedCli, kv.EtcdKeyBase, reactor, initState)
    80  	if err != nil {
    81  		return nil, errors.Trace(err)
    82  	}
    83  
    84  	ret := &cdcMonitor{
    85  		etcdCli:    wrappedCli,
    86  		etcdWorker: etcdWorker,
    87  		reactor:    reactor,
    88  	}
    89  
    90  	return ret, nil
    91  }
    92  
    93  func (m *cdcMonitor) run(ctx context.Context) error {
    94  	log.Debug("start running cdcMonitor")
    95  	err := m.etcdWorker.Run(ctx, nil, 200*time.Millisecond)
    96  	log.Error("etcdWorker exited: test-case-failed", zap.Error(err))
    97  	log.Info("CDC state", zap.Reflect("state", m.reactor.state))
    98  	return err
    99  }