github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/scheduler_config.go (about)

     1  // Copyright 2022 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 config
    15  
    16  import (
    17  	"errors"
    18  	"time"
    19  
    20  	cerror "github.com/pingcap/tiflow/pkg/errors"
    21  )
    22  
    23  // ChangefeedSchedulerConfig is per changefeed scheduler settings.
    24  type ChangefeedSchedulerConfig struct {
    25  	// EnableTableAcrossNodes set true to split one table to multiple spans and
    26  	// distribute to multiple TiCDC nodes.
    27  	EnableTableAcrossNodes bool `toml:"enable-table-across-nodes" json:"enable-table-across-nodes"`
    28  	// RegionThreshold is the region count threshold of splitting a table.
    29  	RegionThreshold int `toml:"region-threshold" json:"region-threshold"`
    30  	// WriteKeyThreshold is the written keys threshold of splitting a table.
    31  	WriteKeyThreshold int `toml:"write-key-threshold" json:"write-key-threshold"`
    32  	// Deprecated.
    33  	RegionPerSpan int `toml:"region-per-span" json:"region-per-span"`
    34  }
    35  
    36  // Validate validates the config.
    37  func (c *ChangefeedSchedulerConfig) Validate() error {
    38  	if !c.EnableTableAcrossNodes {
    39  		return nil
    40  	}
    41  	if c.RegionThreshold < 0 {
    42  		return errors.New("region-threshold must be larger than 0")
    43  	}
    44  	if c.WriteKeyThreshold < 0 {
    45  		return errors.New("write-key-threshold must be larger than 0")
    46  	}
    47  	return nil
    48  }
    49  
    50  // SchedulerConfig configs TiCDC scheduler.
    51  type SchedulerConfig struct {
    52  	// HeartbeatTick is the number of owner tick to initial a heartbeat to captures.
    53  	HeartbeatTick int `toml:"heartbeat-tick" json:"heartbeat-tick"`
    54  	// CollectStatsTick is the number of owner tick to collect stats.
    55  	CollectStatsTick int `toml:"collect-stats-tick" json:"collect-stats-tick"`
    56  	// MaxTaskConcurrency the maximum of concurrent running schedule tasks.
    57  	MaxTaskConcurrency int `toml:"max-task-concurrency" json:"max-task-concurrency"`
    58  	// CheckBalanceInterval the interval of balance tables between each capture.
    59  	CheckBalanceInterval TomlDuration `toml:"check-balance-interval" json:"check-balance-interval"`
    60  	// AddTableBatchSize is the batch size of adding tables on each tick,
    61  	// used by the `BasicScheduler`.
    62  	// When the new owner in power, other captures may not online yet, there might have hundreds of
    63  	// tables need to be dispatched, add tables in a batch way to prevent suddenly resource usage
    64  	// spikes, also wait for other captures join the cluster
    65  	// When there are only 2 captures, and a large number of tables, this can be helpful to prevent
    66  	// oom caused by all tables dispatched to only one capture.
    67  	AddTableBatchSize int `toml:"add-table-batch-size" json:"add-table-batch-size"`
    68  
    69  	// ChangefeedSettings is setting by changefeed.
    70  	ChangefeedSettings *ChangefeedSchedulerConfig `toml:"-" json:"-"`
    71  }
    72  
    73  // NewDefaultSchedulerConfig return the default scheduler configuration.
    74  func NewDefaultSchedulerConfig() *SchedulerConfig {
    75  	return &SchedulerConfig{
    76  		HeartbeatTick: 2,
    77  		// By default, owner ticks every 50ms, we want to low the frequency of
    78  		// collecting stats to reduce memory allocation and CPU usage.
    79  		CollectStatsTick:   200, // 200 * 50ms = 10s.
    80  		MaxTaskConcurrency: 10,
    81  		// TODO: no need to check balance each minute, relax the interval.
    82  		CheckBalanceInterval: TomlDuration(time.Minute),
    83  		AddTableBatchSize:    50,
    84  	}
    85  }
    86  
    87  // ValidateAndAdjust verifies that each parameter is valid.
    88  func (c *SchedulerConfig) ValidateAndAdjust() error {
    89  	if c.HeartbeatTick <= 0 {
    90  		return cerror.ErrInvalidServerOption.GenWithStackByArgs(
    91  			"heartbeat-tick must be larger than 0")
    92  	}
    93  	if c.CollectStatsTick <= 0 {
    94  		return cerror.ErrInvalidServerOption.GenWithStackByArgs(
    95  			"collect-stats-tick must be larger than 0")
    96  	}
    97  	if c.MaxTaskConcurrency <= 0 {
    98  		return cerror.ErrInvalidServerOption.GenWithStackByArgs(
    99  			"max-task-concurrency must be larger than 0")
   100  	}
   101  	if time.Duration(c.CheckBalanceInterval) <= time.Second {
   102  		return cerror.ErrInvalidServerOption.GenWithStackByArgs(
   103  			"check-balance-interval must be larger than 1s")
   104  	}
   105  	if c.AddTableBatchSize <= 0 {
   106  		return cerror.ErrInvalidServerOption.GenWithStackByArgs(
   107  			"add-table-batch-size must be large than 0")
   108  	}
   109  	return nil
   110  }