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