github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/consistent.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 config
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/tidb/br/pkg/storage"
    20  	"github.com/pingcap/tiflow/pkg/compression"
    21  	cerror "github.com/pingcap/tiflow/pkg/errors"
    22  	"github.com/pingcap/tiflow/pkg/redo"
    23  	"github.com/pingcap/tiflow/pkg/util"
    24  )
    25  
    26  // ConsistentConfig represents replication consistency config for a changefeed.
    27  // It is used by redo log functionality.
    28  type ConsistentConfig struct {
    29  	// Level is the consistency level, it can be `none` or `eventual`.
    30  	// `eventual` means enable redo log.
    31  	// Default is `none`.
    32  	Level string `toml:"level" json:"level"`
    33  	// MaxLogSize is the max size(MiB) of a log file written by redo log.
    34  	// Default is 64MiB.
    35  	MaxLogSize int64 `toml:"max-log-size" json:"max-log-size"`
    36  	// FlushIntervalInMs is the flush interval(ms) of redo log to flush log to storage.
    37  	// Default is 2000ms.
    38  	FlushIntervalInMs int64 `toml:"flush-interval" json:"flush-interval"`
    39  	// MetaFlushIntervalInMs is the flush interval(ms) of redo log to
    40  	// flush meta(resolvedTs and checkpointTs) to storage.
    41  	// Default is 200ms.
    42  	MetaFlushIntervalInMs int64 `toml:"meta-flush-interval" json:"meta-flush-interval"`
    43  	// EncodingWorkerNum is the number of workers to encode `RowChangeEvent`` to redo log.
    44  	// Default is 16.
    45  	EncodingWorkerNum int `toml:"encoding-worker-num" json:"encoding-worker-num"`
    46  	// FlushWorkerNum is the number of workers to flush redo log to storage.
    47  	// Default is 8.
    48  	FlushWorkerNum int `toml:"flush-worker-num" json:"flush-worker-num"`
    49  	// Storage is the storage path(uri) to store redo log.
    50  	Storage string `toml:"storage" json:"storage"`
    51  	// UseFileBackend is a flag to enable file backend for redo log.
    52  	// file backend means before flush redo log to storage, it will be written to local file.
    53  	// Default is false.
    54  	UseFileBackend bool `toml:"use-file-backend" json:"use-file-backend"`
    55  	// Compression is the compression algorithm used for redo log.
    56  	// Default is "", it means no compression, equals to `none`.
    57  	// Supported compression algorithms are `none` and `lz4`.
    58  	Compression string `toml:"compression" json:"compression"`
    59  	// FlushConcurrency is the concurrency of flushing a single log file.
    60  	// Default is 1. It means a single log file will be flushed by only one worker.
    61  	// The singe file concurrent flushing feature supports only `s3` storage.
    62  	FlushConcurrency int `toml:"flush-concurrency" json:"flush-concurrency,omitempty"`
    63  	// MemoryUsage represents the percentage of ReplicaConfig.MemoryQuota
    64  	// that can be utilized by the redo log module.
    65  	MemoryUsage *ConsistentMemoryUsage `toml:"memory-usage" json:"memory-usage"`
    66  }
    67  
    68  // ConsistentMemoryUsage represents memory usage of Consistent module.
    69  type ConsistentMemoryUsage struct {
    70  	// ReplicaConfig.MemoryQuota * MemoryQuotaPercentage / 100 will be used for redo events.
    71  	MemoryQuotaPercentage uint64 `toml:"memory-quota-percentage" json:"memory-quota-percentage"`
    72  }
    73  
    74  // ValidateAndAdjust validates the consistency config and adjusts it if necessary.
    75  func (c *ConsistentConfig) ValidateAndAdjust() error {
    76  	if !redo.IsConsistentEnabled(c.Level) {
    77  		return nil
    78  	}
    79  
    80  	if c.MaxLogSize == 0 {
    81  		c.MaxLogSize = redo.DefaultMaxLogSize
    82  	}
    83  
    84  	if c.FlushIntervalInMs == 0 {
    85  		c.FlushIntervalInMs = redo.DefaultFlushIntervalInMs
    86  	}
    87  	if c.FlushIntervalInMs < redo.MinFlushIntervalInMs {
    88  		return cerror.ErrInvalidReplicaConfig.FastGenByArgs(
    89  			fmt.Sprintf("The consistent.flush-interval:%d must be equal or greater than %d",
    90  				c.FlushIntervalInMs, redo.MinFlushIntervalInMs))
    91  	}
    92  
    93  	if c.MetaFlushIntervalInMs == 0 {
    94  		c.MetaFlushIntervalInMs = redo.DefaultMetaFlushIntervalInMs
    95  	}
    96  	if c.MetaFlushIntervalInMs < redo.MinFlushIntervalInMs {
    97  		return cerror.ErrInvalidReplicaConfig.FastGenByArgs(
    98  			fmt.Sprintf("The consistent.meta-flush-interval:%d must be equal or greater than %d",
    99  				c.MetaFlushIntervalInMs, redo.MinFlushIntervalInMs))
   100  	}
   101  	if len(c.Compression) > 0 &&
   102  		c.Compression != compression.None && c.Compression != compression.LZ4 {
   103  		return cerror.ErrInvalidReplicaConfig.FastGenByArgs(
   104  			fmt.Sprintf("The consistent.compression:%s must be 'none' or 'lz4'", c.Compression))
   105  	}
   106  
   107  	if c.EncodingWorkerNum == 0 {
   108  		c.EncodingWorkerNum = redo.DefaultEncodingWorkerNum
   109  	}
   110  	if c.FlushWorkerNum == 0 {
   111  		c.FlushWorkerNum = redo.DefaultFlushWorkerNum
   112  	}
   113  
   114  	uri, err := storage.ParseRawURL(c.Storage)
   115  	if err != nil {
   116  		return cerror.ErrInvalidReplicaConfig.GenWithStackByArgs(
   117  			fmt.Sprintf("invalid storage uri: %s", c.Storage))
   118  	}
   119  	return redo.ValidateStorage(uri)
   120  }
   121  
   122  // MaskSensitiveData masks sensitive data in ConsistentConfig
   123  func (c *ConsistentConfig) MaskSensitiveData() {
   124  	c.Storage = util.MaskSensitiveDataInURI(c.Storage)
   125  }