github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/scheduler/internal/scheduler.go (about)

     1  // Copyright 2021 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 internal
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/tiflow/cdc/model"
    20  	"github.com/pingcap/tiflow/cdc/scheduler/schedulepb"
    21  )
    22  
    23  const (
    24  	// CheckpointCannotProceed is a placeholder indicating that the
    25  	// Owner should not advance the global checkpoint TS just yet.
    26  	CheckpointCannotProceed = model.Ts(0)
    27  )
    28  
    29  // Scheduler is an interface for scheduling tables.
    30  // Since in our design, we do not record checkpoints per table,
    31  // how we calculate the global watermarks (checkpoint-ts and resolved-ts)
    32  // is heavily coupled with how tables are scheduled.
    33  // That is why we have a scheduler interface that also reports the global watermarks.
    34  type Scheduler interface {
    35  	// Tick is called periodically from the owner, and returns
    36  	// updated global watermarks.
    37  	// It is not thread-safe.
    38  	Tick(
    39  		ctx context.Context,
    40  		// Latest global checkpoint of the changefeed
    41  		checkpointTs model.Ts,
    42  		// All tables that SHOULD be replicated (or started) at the current checkpoint.
    43  		currentTables []model.TableID,
    44  		// All captures that are alive according to the latest Etcd states.
    45  		aliveCaptures map[model.CaptureID]*model.CaptureInfo,
    46  		// barrier contains the barrierTs of those tables that have
    47  		// ddl jobs that need to be replicated. The Scheduler will
    48  		// broadcast the barrierTs to all captures through the Heartbeat.
    49  		barrier *schedulepb.BarrierWithMinTs,
    50  	) (watermark schedulepb.Watermark, err error)
    51  
    52  	// MoveTable requests that a table be moved to target.
    53  	// It is thread-safe.
    54  	MoveTable(tableID model.TableID, target model.CaptureID)
    55  
    56  	// Rebalance triggers a rebalance operation.
    57  	// It is thread-safe
    58  	Rebalance()
    59  
    60  	// DrainCapture is used to drop all tables situated at the target capture
    61  	// It is thread-safe.
    62  	DrainCapture(target model.CaptureID) (int, error)
    63  
    64  	// Close scheduler and release resource.
    65  	// It is not thread-safe.
    66  	Close(ctx context.Context)
    67  }
    68  
    69  // Query is for scheduler related owner job.
    70  // at the moment, only for `DrainCapture`, we can use this to handle all manual schedule task.
    71  // TODO: refactor `MoveTable` use Query to access the scheduler
    72  type Query struct {
    73  	CaptureID model.CaptureID
    74  
    75  	Resp interface{}
    76  }