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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
package nodes
import (
"context"
"database/sql"
"errors"
"fmt"
"math/rand"
"sync"
"time"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/sirupsen/logrus"
gitalyauth "gitlab.com/gitlab-org/gitaly/v14/auth"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/client"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/commonerr"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/grpc-proxy/proxy"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/metrics"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/middleware"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/nodes/tracker"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/protoregistry"
prommetrics "gitlab.com/gitlab-org/gitaly/v14/internal/prometheus/metrics"
"google.golang.org/grpc"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
// Shard is a primary with a set of secondaries
type Shard struct {
Primary Node
Secondaries []Node
}
func (s Shard) GetNode(storage string) (Node, error) {
if storage == s.Primary.GetStorage() {
return s.Primary, nil
}
for _, node := range s.Secondaries {
if storage == node.GetStorage() {
return node, nil
}
}
return nil, fmt.Errorf("node with storage %q does not exist", storage)
}
// GetHealthySecondaries returns all secondaries of the shard whose which are
// currently known to be healthy.
func (s Shard) GetHealthySecondaries() []Node {
healthySecondaries := make([]Node, 0, len(s.Secondaries))
for _, secondary := range s.Secondaries {
if !secondary.IsHealthy() {
continue
}
healthySecondaries = append(healthySecondaries, secondary)
}
return healthySecondaries
}
// Manager is responsible for returning shards for virtual storages
type Manager interface {
GetShard(ctx context.Context, virtualStorageName string) (Shard, error)
// GetSyncedNode returns a random storage node based on the state of the replication.
// It returns primary in case there are no up to date secondaries or error occurs.
GetSyncedNode(ctx context.Context, virtualStorageName, repoPath string) (Node, error)
// HealthyNodes returns healthy storages by virtual storage.
HealthyNodes() map[string][]string
// Nodes returns nodes by their virtual storages.
Nodes() map[string][]Node
}
const (
// healthcheckTimeout is the max duration allowed for checking of node health status.
// If check takes more time it considered as failed.
healthcheckTimeout = 1 * time.Second
// healthcheckThreshold is the number of consecutive healthpb.HealthCheckResponse_SERVING necessary
// for deeming a node "healthy"
healthcheckThreshold = 3
)
// Node represents some metadata of a node as well as a connection
type Node interface {
GetStorage() string
GetAddress() string
GetToken() string
GetConnection() *grpc.ClientConn
// IsHealthy reports if node is healthy and can handle requests.
// Node considered healthy if last 'healthcheckThreshold' checks were positive.
IsHealthy() bool
// CheckHealth executes health check for the node and tracks last 'healthcheckThreshold' checks for it.
CheckHealth(context.Context) (bool, error)
}
// Mgr is a concrete type that adheres to the Manager interface
type Mgr struct {
// strategies is a map of strategies keyed on virtual storage name
strategies map[string]leaderElectionStrategy
db *sql.DB
// nodes contains nodes by their virtual storages
nodes map[string][]Node
csg datastore.ConsistentStoragesGetter
}
// leaderElectionStrategy defines the interface by which primary and
// secondaries are managed.
type leaderElectionStrategy interface {
start(bootstrapInterval, monitorInterval time.Duration)
checkNodes(context.Context) error
GetShard(ctx context.Context) (Shard, error)
}
// ErrPrimaryNotHealthy indicates the primary of a shard is not in a healthy state and hence
// should not be used for a new request
var ErrPrimaryNotHealthy = errors.New("primary is not healthy")
const dialTimeout = 10 * time.Second
// Dial dials a node with the necessary interceptors configured.
func Dial(ctx context.Context, node *config.Node, registry *protoregistry.Registry, errorTracker tracker.ErrorTracker, handshaker client.Handshaker) (*grpc.ClientConn, error) {
streamInterceptors := []grpc.StreamClientInterceptor{
grpc_prometheus.StreamClientInterceptor,
}
if errorTracker != nil {
streamInterceptors = append(streamInterceptors, middleware.StreamErrorHandler(registry, errorTracker, node.Storage))
}
dialOpts := []grpc.DialOption{
grpc.WithDefaultCallOptions(grpc.ForceCodec(proxy.NewCodec())),
grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(node.Token)),
grpc.WithChainStreamInterceptor(streamInterceptors...),
grpc.WithChainUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
}
return client.Dial(ctx, node.Address, dialOpts, handshaker)
}
// NewManager creates a new NodeMgr based on virtual storage configs
func NewManager(
log *logrus.Entry,
c config.Config,
db *sql.DB,
csg datastore.ConsistentStoragesGetter,
latencyHistogram prommetrics.HistogramVec,
registry *protoregistry.Registry,
errorTracker tracker.ErrorTracker,
handshaker client.Handshaker,
) (*Mgr, error) {
if !c.Failover.Enabled {
errorTracker = nil
}
ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
defer cancel()
nodes := make(map[string][]Node, len(c.VirtualStorages))
strategies := make(map[string]leaderElectionStrategy, len(c.VirtualStorages))
for _, virtualStorage := range c.VirtualStorages {
log = log.WithField("virtual_storage", virtualStorage.Name)
ns := make([]*nodeStatus, 0, len(virtualStorage.Nodes))
for _, node := range virtualStorage.Nodes {
conn, err := Dial(ctx, node, registry, errorTracker, handshaker)
if err != nil {
return nil, err
}
cs := newConnectionStatus(*node, conn, log, latencyHistogram, errorTracker)
ns = append(ns, cs)
}
for _, node := range ns {
nodes[virtualStorage.Name] = append(nodes[virtualStorage.Name], node)
}
if c.Failover.Enabled {
if c.Failover.ElectionStrategy == config.ElectionStrategySQL {
strategies[virtualStorage.Name] = newSQLElector(virtualStorage.Name, c, db, log, ns)
} else {
strategies[virtualStorage.Name] = newLocalElector(virtualStorage.Name, log, ns)
}
} else {
strategies[virtualStorage.Name] = newDisabledElector(virtualStorage.Name, ns)
}
}
return &Mgr{
db: db,
strategies: strategies,
nodes: nodes,
csg: csg,
}, nil
}
// Start will bootstrap the node manager by calling healthcheck on the nodes as well as kicking off
// the monitoring process. Start must be called before NodeMgr can be used.
func (n *Mgr) Start(bootstrapInterval, monitorInterval time.Duration) {
for _, strategy := range n.strategies {
strategy.start(bootstrapInterval, monitorInterval)
}
}
// checkShards performs health checks on all the available shards. The
// election strategy is responsible for determining the criteria for
// when to elect a new primary and when a node is down.
func (n *Mgr) checkShards() {
for _, strategy := range n.strategies {
ctx := context.Background()
strategy.checkNodes(ctx)
}
}
// ErrVirtualStorageNotExist indicates the node manager is not aware of the virtual storage for which a shard is being requested
var ErrVirtualStorageNotExist = errors.New("virtual storage does not exist")
// GetShard retrieves a shard for a virtual storage name
func (n *Mgr) GetShard(ctx context.Context, virtualStorageName string) (Shard, error) {
strategy, ok := n.strategies[virtualStorageName]
if !ok {
return Shard{}, fmt.Errorf("virtual storage %q: %w", virtualStorageName, ErrVirtualStorageNotExist)
}
return strategy.GetShard(ctx)
}
// GetPrimary returns the current primary of a repository. This is an adapter so NodeManager can be used
// as a praefect.PrimaryGetter in newer code which written to support repository specific primaries.
func (n *Mgr) GetPrimary(ctx context.Context, virtualStorage, _ string) (string, error) {
shard, err := n.GetShard(ctx, virtualStorage)
if err != nil {
return "", err
}
return shard.Primary.GetStorage(), nil
}
func (n *Mgr) GetSyncedNode(ctx context.Context, virtualStorageName, repoPath string) (Node, error) {
upToDateStorages, err := n.csg.GetConsistentStorages(ctx, virtualStorageName, repoPath)
if err != nil && !errors.As(err, new(commonerr.RepositoryNotFoundError)) {
return nil, err
}
if len(upToDateStorages) == 0 {
// this possible when there is no data yet in the database for the repository
shard, err := n.GetShard(ctx, virtualStorageName)
if err != nil {
return nil, fmt.Errorf("get shard for %q: %w", virtualStorageName, err)
}
upToDateStorages = map[string]struct{}{shard.Primary.GetStorage(): {}}
}
healthyStorages := make([]Node, 0, len(upToDateStorages))
for _, node := range n.Nodes()[virtualStorageName] {
if !node.IsHealthy() {
continue
}
if _, ok := upToDateStorages[node.GetStorage()]; !ok {
continue
}
healthyStorages = append(healthyStorages, node)
}
if len(healthyStorages) == 0 {
return nil, fmt.Errorf("no healthy nodes: %w", ErrPrimaryNotHealthy)
}
return healthyStorages[rand.Intn(len(healthyStorages))], nil
}
func (n *Mgr) HealthyNodes() map[string][]string {
healthy := make(map[string][]string, len(n.nodes))
for vs, nodes := range n.nodes {
storages := make([]string, 0, len(nodes))
for _, node := range nodes {
if node.IsHealthy() {
storages = append(storages, node.GetStorage())
}
}
healthy[vs] = storages
}
return healthy
}
func (n *Mgr) Nodes() map[string][]Node { return n.nodes }
func newConnectionStatus(node config.Node, cc *grpc.ClientConn, l logrus.FieldLogger, latencyHist prommetrics.HistogramVec, errorTracker tracker.ErrorTracker) *nodeStatus {
return &nodeStatus{
node: node,
clientConn: cc,
log: l,
latencyHist: latencyHist,
errTracker: errorTracker,
}
}
type nodeStatus struct {
node config.Node
clientConn *grpc.ClientConn
log logrus.FieldLogger
latencyHist prommetrics.HistogramVec
mtx sync.RWMutex
statuses []bool
errTracker tracker.ErrorTracker
}
// GetStorage gets the storage name of a node
func (n *nodeStatus) GetStorage() string {
return n.node.Storage
}
// GetAddress gets the address of a node
func (n *nodeStatus) GetAddress() string {
return n.node.Address
}
// GetToken gets the token of a node
func (n *nodeStatus) GetToken() string {
return n.node.Token
}
// GetConnection gets the client connection of a node
func (n *nodeStatus) GetConnection() *grpc.ClientConn {
return n.clientConn
}
func (n *nodeStatus) IsHealthy() bool {
n.mtx.RLock()
healthy := n.isHealthy()
n.mtx.RUnlock()
return healthy
}
func (n *nodeStatus) isHealthy() bool {
if len(n.statuses) < healthcheckThreshold {
return false
}
for _, ok := range n.statuses[len(n.statuses)-healthcheckThreshold:] {
if !ok {
return false
}
}
return true
}
func (n *nodeStatus) updateStatus(status bool) {
n.mtx.Lock()
n.statuses = append(n.statuses, status)
if len(n.statuses) > healthcheckThreshold {
n.statuses = n.statuses[1:]
}
n.mtx.Unlock()
}
func (n *nodeStatus) CheckHealth(ctx context.Context) (bool, error) {
health := healthpb.NewHealthClient(n.clientConn)
if n.errTracker != nil {
health = tracker.NewHealthClient(health, n.GetStorage(), n.errTracker)
}
ctx, cancel := context.WithTimeout(ctx, healthcheckTimeout)
defer cancel()
start := time.Now()
resp, err := health.Check(ctx, &healthpb.HealthCheckRequest{})
n.latencyHist.WithLabelValues(n.node.Storage).Observe(time.Since(start).Seconds())
if err != nil {
n.log.WithError(err).WithFields(logrus.Fields{
"storage": n.node.Storage,
"address": n.node.Address,
}).Warn("error when pinging healthcheck")
}
status := resp.GetStatus() == healthpb.HealthCheckResponse_SERVING
metrics.NodeLastHealthcheckGauge.WithLabelValues(n.GetStorage()).Set(metrics.BoolAsFloat(status))
n.updateStatus(status)
return status, err
}
|