github.com/DerekStrickland/consul@v1.4.5/agent/consul/autopilot/structs.go (about)

     1  package autopilot
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashicorp/serf/serf"
     7  )
     8  
     9  // Config holds the Autopilot configuration for a cluster.
    10  type Config struct {
    11  	// CleanupDeadServers controls whether to remove dead servers when a new
    12  	// server is added to the Raft peers.
    13  	CleanupDeadServers bool
    14  
    15  	// LastContactThreshold is the limit on the amount of time a server can go
    16  	// without leader contact before being considered unhealthy.
    17  	LastContactThreshold time.Duration
    18  
    19  	// MaxTrailingLogs is the amount of entries in the Raft Log that a server can
    20  	// be behind before being considered unhealthy.
    21  	MaxTrailingLogs uint64
    22  
    23  	// ServerStabilizationTime is the minimum amount of time a server must be
    24  	// in a stable, healthy state before it can be added to the cluster. Only
    25  	// applicable with Raft protocol version 3 or higher.
    26  	ServerStabilizationTime time.Duration
    27  
    28  	// (Enterprise-only) RedundancyZoneTag is the node tag to use for separating
    29  	// servers into zones for redundancy. If left blank, this feature will be disabled.
    30  	RedundancyZoneTag string
    31  
    32  	// (Enterprise-only) DisableUpgradeMigration will disable Autopilot's upgrade migration
    33  	// strategy of waiting until enough newer-versioned servers have been added to the
    34  	// cluster before promoting them to voters.
    35  	DisableUpgradeMigration bool
    36  
    37  	// (Enterprise-only) UpgradeVersionTag is the node tag to use for version info when
    38  	// performing upgrade migrations. If left blank, the Consul version will be used.
    39  	UpgradeVersionTag string
    40  
    41  	// CreateIndex/ModifyIndex store the create/modify indexes of this configuration.
    42  	CreateIndex uint64
    43  	ModifyIndex uint64
    44  }
    45  
    46  // ServerHealth is the health (from the leader's point of view) of a server.
    47  type ServerHealth struct {
    48  	// ID is the raft ID of the server.
    49  	ID string
    50  
    51  	// Name is the node name of the server.
    52  	Name string
    53  
    54  	// Address is the address of the server.
    55  	Address string
    56  
    57  	// The status of the SerfHealth check for the server.
    58  	SerfStatus serf.MemberStatus
    59  
    60  	// Version is the version of the server.
    61  	Version string
    62  
    63  	// Leader is whether this server is currently the leader.
    64  	Leader bool
    65  
    66  	// LastContact is the time since this node's last contact with the leader.
    67  	LastContact time.Duration
    68  
    69  	// LastTerm is the highest leader term this server has a record of in its Raft log.
    70  	LastTerm uint64
    71  
    72  	// LastIndex is the last log index this server has a record of in its Raft log.
    73  	LastIndex uint64
    74  
    75  	// Healthy is whether or not the server is healthy according to the current
    76  	// Autopilot config.
    77  	Healthy bool
    78  
    79  	// Voter is whether this is a voting server.
    80  	Voter bool
    81  
    82  	// StableSince is the last time this server's Healthy value changed.
    83  	StableSince time.Time
    84  }
    85  
    86  // IsHealthy determines whether this ServerHealth is considered healthy
    87  // based on the given Autopilot config
    88  func (h *ServerHealth) IsHealthy(lastTerm uint64, leaderLastIndex uint64, autopilotConf *Config) bool {
    89  	if h.SerfStatus != serf.StatusAlive {
    90  		return false
    91  	}
    92  
    93  	if h.LastContact > autopilotConf.LastContactThreshold || h.LastContact < 0 {
    94  		return false
    95  	}
    96  
    97  	if h.LastTerm != lastTerm {
    98  		return false
    99  	}
   100  
   101  	if leaderLastIndex > autopilotConf.MaxTrailingLogs && h.LastIndex < leaderLastIndex-autopilotConf.MaxTrailingLogs {
   102  		return false
   103  	}
   104  
   105  	return true
   106  }
   107  
   108  // IsStable returns true if the ServerHealth shows a stable, passing state
   109  // according to the given AutopilotConfig
   110  func (h *ServerHealth) IsStable(now time.Time, conf *Config) bool {
   111  	if h == nil {
   112  		return false
   113  	}
   114  
   115  	if !h.Healthy {
   116  		return false
   117  	}
   118  
   119  	if now.Sub(h.StableSince) < conf.ServerStabilizationTime {
   120  		return false
   121  	}
   122  
   123  	return true
   124  }
   125  
   126  // ServerStats holds miscellaneous Raft metrics for a server
   127  type ServerStats struct {
   128  	// LastContact is the time since this node's last contact with the leader.
   129  	LastContact string
   130  
   131  	// LastTerm is the highest leader term this server has a record of in its Raft log.
   132  	LastTerm uint64
   133  
   134  	// LastIndex is the last log index this server has a record of in its Raft log.
   135  	LastIndex uint64
   136  }
   137  
   138  // OperatorHealthReply is a representation of the overall health of the cluster
   139  type OperatorHealthReply struct {
   140  	// Healthy is true if all the servers in the cluster are healthy.
   141  	Healthy bool
   142  
   143  	// FailureTolerance is the number of healthy servers that could be lost without
   144  	// an outage occurring.
   145  	FailureTolerance int
   146  
   147  	// Servers holds the health of each server.
   148  	Servers []ServerHealth
   149  }
   150  
   151  func (o *OperatorHealthReply) ServerHealth(id string) *ServerHealth {
   152  	for _, health := range o.Servers {
   153  		if health.ID == id {
   154  			return &health
   155  		}
   156  	}
   157  	return nil
   158  }