Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local_elector.go « nodes « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12308ab26547c913307cf509b3d5f087dc69e2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package nodes

import (
	"context"
	"sync"
	"time"

	"gitlab.com/gitlab-org/gitaly/internal/praefect/metrics"
)

type nodeCandidate struct {
	node     Node
	up       bool
	primary  bool
	statuses []bool
}

// localElector relies on an in-memory datastore to track the primary
// and secondaries. A single election strategy pertains to a single
// shard. It does NOT support multiple Praefect nodes or have any
// persistence. This is used mostly for testing and development.
type localElector struct {
	m               sync.RWMutex
	failoverEnabled bool
	shardName       string
	nodes           []*nodeCandidate
	primaryNode     *nodeCandidate
}

// healthcheckThreshold is the number of consecutive healthpb.HealthCheckResponse_SERVING necessary
// for deeming a node "healthy"
const healthcheckThreshold = 3

func (n *nodeCandidate) isHealthy() bool {
	if len(n.statuses) < healthcheckThreshold {
		return false
	}

	for _, status := range n.statuses[len(n.statuses)-healthcheckThreshold:] {
		if !status {
			return false
		}
	}

	return true
}

func newLocalElector(name string, failoverEnabled bool) *localElector {
	return &localElector{
		shardName:       name,
		failoverEnabled: failoverEnabled,
	}
}

// addNode registers a primary or secondary in the internal
// datastore.
func (s *localElector) addNode(node Node, primary bool) {
	localNode := nodeCandidate{
		node:     node,
		primary:  primary,
		statuses: make([]bool, 0),
		up:       false,
	}

	// If failover hasn't been activated, we assume all nodes are up
	// since health checks aren't run.
	if !s.failoverEnabled {
		localNode.up = true
	}

	s.m.Lock()
	defer s.m.Unlock()

	if primary {
		s.primaryNode = &localNode
	}

	s.nodes = append(s.nodes, &localNode)
}

// Start launches a Goroutine to check the state of the nodes and
// continuously monitor their health via gRPC health checks.
func (s *localElector) start(bootstrapInterval, monitorInterval time.Duration) error {
	s.bootstrap(bootstrapInterval)
	go s.monitor(monitorInterval)

	return nil
}

func (s *localElector) bootstrap(d time.Duration) {
	timer := time.NewTimer(d)
	defer timer.Stop()

	for i := 0; i < healthcheckThreshold; i++ {
		<-timer.C

		ctx := context.TODO()
		s.checkNodes(ctx)
		timer.Reset(d)
	}
}

func (s *localElector) monitor(d time.Duration) {
	ticker := time.NewTicker(d)
	defer ticker.Stop()

	for {
		ctx := context.Background()
		s.checkNodes(ctx)
	}
}

// checkNodes issues a gRPC health check for each node managed by the
// shard.
func (s *localElector) checkNodes(ctx context.Context) {
	defer s.updateMetrics()

	for _, n := range s.nodes {
		status, _ := n.node.check(ctx)
		n.statuses = append(n.statuses, status)

		if len(n.statuses) > healthcheckThreshold {
			n.statuses = n.statuses[1:]
		}

		up := n.isHealthy()
		n.up = up
	}

	if s.primaryNode != nil && s.primaryNode.isHealthy() {
		return
	}

	var newPrimary *nodeCandidate

	for _, node := range s.nodes {
		if !node.primary && node.isHealthy() {
			newPrimary = node
			break
		}
	}

	if newPrimary == nil {
		return
	}

	s.m.Lock()
	defer s.m.Unlock()

	s.primaryNode.primary = false
	s.primaryNode = newPrimary
	newPrimary.primary = true
}

// GetPrimary gets the primary of a shard. If no primary exists, it will
// be nil. If a primary has been elected but is down, err will be
// ErrPrimaryNotHealthy.
func (s *localElector) GetPrimary() (Node, error) {
	s.m.RLock()
	defer s.m.RUnlock()

	if s.primaryNode == nil {
		return nil, ErrPrimaryNotHealthy
	}

	if !s.primaryNode.up {
		return s.primaryNode.node, ErrPrimaryNotHealthy
	}

	return s.primaryNode.node, nil
}

// GetSecondaries gets the secondaries of a shard
func (s *localElector) GetSecondaries() ([]Node, error) {
	s.m.RLock()
	defer s.m.RUnlock()

	var secondaries []Node
	for _, n := range s.nodes {
		if !n.primary {
			secondaries = append(secondaries, n.node)
		}
	}

	return secondaries, nil
}

func (s *localElector) updateMetrics() {
	s.m.RLock()
	defer s.m.RUnlock()

	for _, node := range s.nodes {
		var val float64

		if node.primary {
			val = 1
		}

		metrics.PrimaryGauge.WithLabelValues(s.shardName, node.node.GetStorage()).Set(val)
	}
}