github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/testing_utils/cdc_state_checker/reactor.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  
    19  	"github.com/pingcap/errors"
    20  	"github.com/pingcap/log"
    21  	"github.com/pingcap/ticdc/cdc/model"
    22  	"github.com/pingcap/ticdc/pkg/orchestrator"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  type cdcMonitReactor struct {
    27  	state *cdcReactorState
    28  }
    29  
    30  func (r *cdcMonitReactor) Tick(_ context.Context, state orchestrator.ReactorState) (orchestrator.ReactorState, error) {
    31  	r.state = state.(*cdcReactorState)
    32  
    33  	err := r.verifyTs()
    34  	if err != nil {
    35  		log.Error("Verifying Ts failed", zap.Error(err))
    36  		return r.state, err
    37  	}
    38  
    39  	err = r.verifyStartTs()
    40  	if err != nil {
    41  		log.Error("Verifying startTs failed", zap.Error(err))
    42  		return r.state, err
    43  	}
    44  
    45  	return r.state, nil
    46  }
    47  
    48  func (r *cdcMonitReactor) verifyTs() error {
    49  	for changfeedID, positions := range r.state.TaskPositions {
    50  		status, ok := r.state.ChangefeedStatuses[changfeedID]
    51  		if !ok {
    52  			log.Warn("changefeed status not found", zap.String("cfid", changfeedID))
    53  			return nil
    54  		}
    55  
    56  		actualCheckpointTs := status.CheckpointTs
    57  
    58  		for captureID, position := range positions {
    59  			if _, ok := r.state.Captures[captureID]; !ok {
    60  				// ignore positions whose capture is no longer present
    61  				continue
    62  			}
    63  
    64  			if position.CheckPointTs < actualCheckpointTs {
    65  				return errors.Errorf("checkpointTs too large, globalCkpt = %d, localCkpt = %d, capture = %s, cfid = %s",
    66  					actualCheckpointTs, position.CheckPointTs, captureID, changfeedID)
    67  			}
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (r *cdcMonitReactor) verifyStartTs() error {
    75  	for changfeedID, statuses := range r.state.TaskStatuses {
    76  		cStatus, ok := r.state.ChangefeedStatuses[changfeedID]
    77  		if !ok {
    78  			log.Warn("changefeed status not found", zap.String("cfid", changfeedID))
    79  			return nil
    80  		}
    81  
    82  		actualCheckpointTs := cStatus.CheckpointTs
    83  
    84  		for captureID, status := range statuses {
    85  			for tableID, operation := range status.Operation {
    86  				if operation.Status != model.OperFinished && !operation.Delete {
    87  					startTs := status.Tables[tableID].StartTs
    88  					if startTs < actualCheckpointTs {
    89  						return errors.Errorf("startTs too small, globalCkpt = %d, startTs = %d, table = %d, capture = %s, cfid = %s",
    90  							actualCheckpointTs, startTs, tableID, captureID, changfeedID)
    91  					}
    92  				}
    93  			}
    94  		}
    95  	}
    96  
    97  	return nil
    98  }