github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/config/checker_config.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  	"encoding/json"
    18  	"time"
    19  )
    20  
    21  // Backoff related constants.
    22  var (
    23  	DefaultCheckInterval           = 5 * time.Second
    24  	DefaultBackoffRollback         = 5 * time.Minute
    25  	DefaultBackoffMin              = 1 * time.Second
    26  	DefaultBackoffMax              = 5 * time.Minute
    27  	DefaultBackoffJitter           = true
    28  	DefaultBackoffFactor   float64 = 2
    29  )
    30  
    31  // Duration is used to hold a time.Duration field.
    32  type Duration struct {
    33  	time.Duration
    34  }
    35  
    36  // MarshalText hacks to satisfy the encoding.TextMarshaler interface
    37  // For MarshalText, we should use (d Duration) which can be used by both pointer and instance.
    38  func (d Duration) MarshalText() ([]byte, error) {
    39  	return []byte(d.Duration.String()), nil
    40  }
    41  
    42  // UnmarshalText hacks to satisfy the encoding.TextUnmarshaler interface
    43  // For UnmarshalText, we should use (d *Duration) to change the value of this instance instead of the copy.
    44  func (d *Duration) UnmarshalText(text []byte) error {
    45  	var err error
    46  	d.Duration, err = time.ParseDuration(string(text))
    47  	return err
    48  }
    49  
    50  // MarshalJSON hacks to satisfy the json.Marshaler interface.
    51  func (d *Duration) MarshalJSON() ([]byte, error) {
    52  	return json.Marshal(&struct {
    53  		Duration string `json:"Duration"`
    54  	}{
    55  		d.Duration.String(),
    56  	})
    57  }
    58  
    59  // CheckerConfig is configuration used for TaskStatusChecker.
    60  type CheckerConfig struct {
    61  	CheckEnable     bool     `yaml:"check-enable" toml:"check-enable" json:"check-enable"`
    62  	BackoffRollback Duration `yaml:"backoff-rollback" toml:"backoff-rollback" json:"backoff-rollback"`
    63  	BackoffMax      Duration `yaml:"backoff-max" toml:"backoff-max" json:"backoff-max"`
    64  	// unexpose config
    65  	CheckInterval Duration `yaml:"check-interval" toml:"check-interval" json:"-"`
    66  	BackoffMin    Duration `yaml:"backoff-min" toml:"backoff-min" json:"-"`
    67  	BackoffJitter bool     `yaml:"backoff-jitter" toml:"backoff-jitter" json:"-"`
    68  	BackoffFactor float64  `yaml:"backoff-factor" toml:"backoff-factor" json:"-"`
    69  }
    70  
    71  // Adjust sets default value for field: CheckInterval/BackoffMin/BackoffJitter/BackoffFactor.
    72  func (cc *CheckerConfig) Adjust() {
    73  	cc.CheckInterval = Duration{Duration: DefaultCheckInterval}
    74  	cc.BackoffMin = Duration{Duration: DefaultBackoffMin}
    75  	cc.BackoffJitter = DefaultBackoffJitter
    76  	cc.BackoffFactor = DefaultBackoffFactor
    77  }