github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/integrity/integrity.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 integrity
    15  
    16  import (
    17  	"github.com/pingcap/log"
    18  	cerror "github.com/pingcap/tiflow/pkg/errors"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  // Config represents integrity check config for a changefeed.
    23  type Config struct {
    24  	IntegrityCheckLevel   string `toml:"integrity-check-level" json:"integrity-check-level"`
    25  	CorruptionHandleLevel string `toml:"corruption-handle-level" json:"corruption-handle-level"`
    26  }
    27  
    28  const (
    29  	// CheckLevelNone means no integrity check, the default value.
    30  	CheckLevelNone string = "none"
    31  	// CheckLevelCorrectness means check each row data correctness.
    32  	CheckLevelCorrectness string = "correctness"
    33  )
    34  
    35  const (
    36  	// CorruptionHandleLevelWarn is the default value,
    37  	// log the corrupted event, and mark it as corrupted and send it to the downstream.
    38  	CorruptionHandleLevelWarn string = "warn"
    39  	// CorruptionHandleLevelError means log the corrupted event, and then stopped the changefeed.
    40  	CorruptionHandleLevelError string = "error"
    41  )
    42  
    43  // Validate the integrity config.
    44  func (c *Config) Validate() error {
    45  	if c.IntegrityCheckLevel != CheckLevelNone &&
    46  		c.IntegrityCheckLevel != CheckLevelCorrectness {
    47  		return cerror.ErrInvalidReplicaConfig.GenWithStackByArgs()
    48  	}
    49  	if c.CorruptionHandleLevel != CorruptionHandleLevelWarn &&
    50  		c.CorruptionHandleLevel != CorruptionHandleLevelError {
    51  		return cerror.ErrInvalidReplicaConfig.GenWithStackByArgs()
    52  	}
    53  
    54  	if c.Enabled() {
    55  		log.Info("integrity check is enabled",
    56  			zap.Any("integrityCheckLevel", c.IntegrityCheckLevel),
    57  			zap.Any("corruptionHandleLevel", c.CorruptionHandleLevel))
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // Enabled returns true if the integrity check is enabled.
    64  func (c *Config) Enabled() bool {
    65  	return c.IntegrityCheckLevel == CheckLevelCorrectness
    66  }
    67  
    68  // ErrorHandle returns true if the corruption handle level is error.
    69  func (c *Config) ErrorHandle() bool {
    70  	return c.CorruptionHandleLevel == CorruptionHandleLevelError
    71  }