github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdcv2/controller/controller.go (about)

     1  // Copyright 2023 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  //nolint:unused
    15  package controller
    16  
    17  import (
    18  	"context"
    19  	"sync/atomic"
    20  	"time"
    21  
    22  	"github.com/pingcap/log"
    23  	"github.com/pingcap/tiflow/cdc/controller"
    24  	"github.com/pingcap/tiflow/cdc/model"
    25  	"github.com/pingcap/tiflow/cdcv2/metadata"
    26  	cerror "github.com/pingcap/tiflow/pkg/errors"
    27  	"github.com/pingcap/tiflow/pkg/orchestrator"
    28  	"github.com/pingcap/tiflow/pkg/upstream"
    29  	"go.uber.org/zap"
    30  )
    31  
    32  var _ controller.Controller = &controllerImpl{}
    33  
    34  type controllerImpl struct {
    35  	captureInfo     *model.CaptureInfo
    36  	captures        map[model.CaptureID]*model.CaptureInfo
    37  	upstreamManager *upstream.Manager
    38  
    39  	lastTickTime time.Time
    40  	// bootstrapped specifies whether the controller has been initialized.
    41  	// This will only be done when the controller starts the first Tick.
    42  	// NOTICE: Do not use it in a method other than tick unexpectedly,
    43  	//         as it is not a thread-safe value.
    44  	bootstrapped bool
    45  	closed       int32
    46  
    47  	controllerObservation metadata.ControllerObservation
    48  	captureObservation    metadata.CaptureObservation
    49  }
    50  
    51  // NewController creates a new Controller
    52  func NewController(
    53  	upstreamManager *upstream.Manager,
    54  	captureInfo *model.CaptureInfo,
    55  	controllerObservation metadata.ControllerObservation,
    56  	captureObservation metadata.CaptureObservation,
    57  ) *controllerImpl {
    58  	return &controllerImpl{
    59  		upstreamManager:       upstreamManager,
    60  		captures:              map[model.CaptureID]*model.CaptureInfo{},
    61  		lastTickTime:          time.Now(),
    62  		captureInfo:           captureInfo,
    63  		controllerObservation: controllerObservation,
    64  		captureObservation:    captureObservation,
    65  	}
    66  }
    67  
    68  func (o *controllerImpl) Run(stdCtx context.Context) error {
    69  	tick := time.Tick(time.Second * 5)
    70  	for {
    71  		select {
    72  		case <-stdCtx.Done():
    73  			return stdCtx.Err()
    74  		case <-tick:
    75  			changefeeds, captures, err := o.controllerObservation.ScheduleSnapshot()
    76  			if err != nil {
    77  				log.Error("failed to get snapshot", zap.Error(err))
    78  			}
    79  			log.Info("controller snapshot",
    80  				zap.Int("changefeeds", len(changefeeds)),
    81  				zap.Int("captures", len(captures)))
    82  
    83  			// if closed, exit the etcd worker loop
    84  			if atomic.LoadInt32(&o.closed) != 0 {
    85  				return cerror.ErrReactorFinished.GenWithStackByArgs()
    86  			}
    87  		}
    88  	}
    89  }
    90  
    91  func (o *controllerImpl) Tick(ctx context.Context,
    92  	state orchestrator.ReactorState,
    93  ) (nextState orchestrator.ReactorState, err error) {
    94  	// TODO implement me
    95  	panic("implement me")
    96  }
    97  
    98  func (o *controllerImpl) AsyncStop() {
    99  	// TODO implement me
   100  	panic("implement me")
   101  }
   102  
   103  func (o *controllerImpl) GetChangefeedOwnerCaptureInfo(id model.ChangeFeedID) *model.CaptureInfo {
   104  	// TODO implement me
   105  	panic("implement me")
   106  }
   107  
   108  func (o *controllerImpl) GetAllChangeFeedInfo(ctx context.Context) (map[model.ChangeFeedID]*model.ChangeFeedInfo, error) {
   109  	// TODO implement me
   110  	panic("implement me")
   111  }
   112  
   113  func (o *controllerImpl) GetAllChangeFeedCheckpointTs(ctx context.Context) (map[model.ChangeFeedID]uint64, error) {
   114  	// TODO implement me
   115  	panic("implement me")
   116  }
   117  
   118  func (o *controllerImpl) GetCaptures(ctx context.Context) ([]*model.CaptureInfo, error) {
   119  	// TODO implement me
   120  	panic("implement me")
   121  }
   122  
   123  func (o *controllerImpl) GetProcessors(ctx context.Context) ([]*model.ProcInfoSnap, error) {
   124  	// TODO implement me
   125  	panic("implement me")
   126  }
   127  
   128  func (o *controllerImpl) IsChangefeedExists(ctx context.Context, id model.ChangeFeedID) (bool, error) {
   129  	// TODO implement me
   130  	panic("implement me")
   131  }
   132  
   133  func (o *controllerImpl) CreateChangefeed(
   134  	ctx context.Context,
   135  	upstreamInfo *model.UpstreamInfo,
   136  	changefeedInfo *model.ChangeFeedInfo,
   137  ) error {
   138  	// TODO implement me
   139  	panic("implement me")
   140  }