github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/cruisectl/config.go (about)

     1  package cruisectl
     2  
     3  import (
     4  	"time"
     5  
     6  	"go.uber.org/atomic"
     7  )
     8  
     9  // DefaultConfig returns the default config for the BlockTimeController.
    10  func DefaultConfig() *Config {
    11  	return &Config{
    12  		TimingConfig{
    13  			FallbackProposalDelay: atomic.NewDuration(250 * time.Millisecond),
    14  			MinViewDuration:       atomic.NewDuration(125 * time.Millisecond),
    15  			MaxViewDuration:       atomic.NewDuration(910 * time.Millisecond),
    16  			Enabled:               atomic.NewBool(true),
    17  		},
    18  		ControllerParams{
    19  			N_ewma: 5,
    20  			N_itg:  50,
    21  			KP:     2.0,
    22  			KI:     0.6,
    23  			KD:     3.0,
    24  		},
    25  	}
    26  }
    27  
    28  // Config defines configuration for the BlockTimeController.
    29  type Config struct {
    30  	TimingConfig
    31  	ControllerParams
    32  }
    33  
    34  // TimingConfig specifies the BlockTimeController's limits of authority.
    35  type TimingConfig struct {
    36  	// FallbackProposalDelay is the minimal block construction delay. When used, it behaves like the
    37  	// old command line flag `block-rate-delay`. Specifically, the primary measures the duration from
    38  	// starting to construct its proposal to the proposal being ready to be published. If this
    39  	// duration is _less_ than FallbackProposalDelay, the primary delays broadcasting its proposal
    40  	// by the remainder needed to reach `FallbackProposalDelay`
    41  	// It is used:
    42  	//  - when Enabled is false
    43  	//  - when epoch fallback has been triggered
    44  	FallbackProposalDelay *atomic.Duration
    45  
    46  	// MaxViewDuration is a hard maximum on the total view time targeted by ProposalTiming.
    47  	// If the BlockTimeController computes a larger desired ProposalTiming value
    48  	// based on the observed error and tuning, this value will be used instead.
    49  	MaxViewDuration *atomic.Duration
    50  
    51  	// MinViewDuration  is a hard maximum on the total view time targeted by ProposalTiming.
    52  	// If the BlockTimeController computes a smaller desired ProposalTiming value
    53  	// based on the observed error and tuning, this value will be used instead.
    54  	MinViewDuration *atomic.Duration
    55  
    56  	// Enabled defines whether responsive control of the GetProposalTiming is enabled.
    57  	// When disabled, the FallbackProposalDelay is used.
    58  	Enabled *atomic.Bool
    59  }
    60  
    61  // ControllerParams specifies the BlockTimeController's internal parameters.
    62  type ControllerParams struct {
    63  	// N_ewma defines how historical measurements are incorporated into the EWMA for the proportional error term.
    64  	// Intuition: Suppose the input changes from x to y instantaneously:
    65  	//  - N_ewma is the number of samples required to move the EWMA output about 2/3 of the way from x to y
    66  	// Per convention, this must be a _positive_ integer.
    67  	N_ewma uint
    68  
    69  	// N_itg defines how historical measurements are incorporated into the integral error term.
    70  	// Intuition: For a constant error x:
    71  	//  - the integrator value will saturate at `x•N_itg`
    72  	//  - an integrator initialized at 0 reaches 2/3 of the saturation value after N_itg samples
    73  	// Per convention, this must be a _positive_ integer.
    74  	N_itg uint
    75  
    76  	// KP, KI, KD, are the coefficients to the PID controller and define its response.
    77  	// KP adjusts the proportional term (responds to the magnitude of error).
    78  	// KI adjusts the integral term (responds to the error sum over a recent time interval).
    79  	// KD adjusts the derivative term (responds to the rate of change, i.e. time derivative, of the error).
    80  	KP, KI, KD float64
    81  }
    82  
    83  // alpha returns α, the inclusion parameter for the error EWMA. See N_ewma for details.
    84  func (c *ControllerParams) alpha() float64 {
    85  	return 1.0 / float64(c.N_ewma)
    86  }
    87  
    88  // beta returns ß, the memory parameter of the leaky error integrator. See N_itg for details.
    89  func (c *ControllerParams) beta() float64 {
    90  	return 1.0 / float64(c.N_itg)
    91  }
    92  
    93  // GetFallbackProposalDuration returns the proposal duration used when Cruise Control is not active.
    94  func (ctl TimingConfig) GetFallbackProposalDuration() time.Duration {
    95  	return ctl.FallbackProposalDelay.Load()
    96  }
    97  
    98  // GetMaxViewDuration returns the max view duration returned by the controller.
    99  func (ctl TimingConfig) GetMaxViewDuration() time.Duration {
   100  	return ctl.MaxViewDuration.Load()
   101  }
   102  
   103  // GetMinViewDuration returns the min view duration returned by the controller.
   104  func (ctl TimingConfig) GetMinViewDuration() time.Duration {
   105  	return ctl.MinViewDuration.Load()
   106  }
   107  
   108  // GetEnabled returns whether the controller is enabled.
   109  func (ctl TimingConfig) GetEnabled() bool {
   110  	return ctl.Enabled.Load()
   111  }
   112  
   113  // SetFallbackProposalDuration sets the proposal duration used when Cruise Control is not active.
   114  func (ctl TimingConfig) SetFallbackProposalDuration(dur time.Duration) error {
   115  	ctl.FallbackProposalDelay.Store(dur)
   116  	return nil
   117  }
   118  
   119  // SetMaxViewDuration sets the max view duration returned by the controller.
   120  func (ctl TimingConfig) SetMaxViewDuration(dur time.Duration) error {
   121  	ctl.MaxViewDuration.Store(dur)
   122  	return nil
   123  }
   124  
   125  // SetMinViewDuration sets the min view duration returned by the controller.
   126  func (ctl TimingConfig) SetMinViewDuration(dur time.Duration) error {
   127  	ctl.MinViewDuration.Store(dur)
   128  	return nil
   129  }
   130  
   131  // SetEnabled sets whether the controller is enabled.
   132  func (ctl TimingConfig) SetEnabled(enabled bool) error {
   133  	ctl.Enabled.Store(enabled)
   134  	return nil
   135  }