File size: 5,613 Bytes
95d599c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package bootstrap

import (
	"context"
	"fmt"
	"math/rand"
	"time"

	"github.com/getsentry/sentry-go"
	"github.com/sirupsen/logrus"
	cmd "github.com/weaviate/weaviate/cluster/proto/api"
	"github.com/weaviate/weaviate/cluster/resolver"
	entSentry "github.com/weaviate/weaviate/entities/sentry"
)

// PeerJoiner is the interface we expect to be able to talk to the other peers to either Join or Notify them
type PeerJoiner interface {
	Join(_ context.Context, leaderAddr string, _ *cmd.JoinPeerRequest) (*cmd.JoinPeerResponse, error)
	Notify(_ context.Context, leaderAddr string, _ *cmd.NotifyPeerRequest) (*cmd.NotifyPeerResponse, error)
}

// Bootstrapper is used to bootstrap this node by attempting to join it to a RAFT cluster.
type Bootstrapper struct {
	peerJoiner   PeerJoiner
	addrResolver resolver.ClusterStateReader
	isStoreReady func() bool

	localRaftAddr string
	localNodeID   string
	voter         bool

	retryPeriod time.Duration
	jitter      time.Duration
}

// NewBootstrapper constructs a new bootsrapper
func NewBootstrapper(peerJoiner PeerJoiner, raftID string, raftAddr string, voter bool, r resolver.ClusterStateReader, isStoreReady func() bool) *Bootstrapper {
	return &Bootstrapper{
		peerJoiner:    peerJoiner,
		addrResolver:  r,
		retryPeriod:   time.Second,
		jitter:        time.Second,
		localNodeID:   raftID,
		localRaftAddr: raftAddr,
		isStoreReady:  isStoreReady,
		voter:         voter,
	}
}

// Do iterates over a list of servers in an attempt to join this node to a cluster.
func (b *Bootstrapper) Do(ctx context.Context, serverPortMap map[string]int, lg *logrus.Logger, stop chan struct{}) error {
	if entSentry.Enabled() {
		transaction := sentry.StartTransaction(ctx, "raft.bootstrap",
			sentry.WithOpName("init"),
			sentry.WithDescription("Attempt to bootstrap a raft cluster"),
		)
		ctx = transaction.Context()
		defer transaction.Finish()
	}
	ticker := time.NewTicker(jitter(b.retryPeriod, b.jitter))
	defer ticker.Stop()
	for {
		select {
		case <-stop:
			return nil
		case <-ctx.Done():
			return ctx.Err()
		case <-ticker.C:
			if b.isStoreReady() {
				lg.WithField("action", "bootstrap").Info("node reporting ready, exiting bootstrap process")
				return nil
			}

			remoteNodes := ResolveRemoteNodes(b.addrResolver, serverPortMap)
			// We were not able to resolve any nodes to an address
			if len(remoteNodes) == 0 {
				lg.WithField("action", "bootstrap").WithField("join_list", serverPortMap).Warn("unable to resolve any node address to join")
				continue
			}

			// Always try to join an existing cluster first
			joiner := NewJoiner(b.peerJoiner, b.localNodeID, b.localRaftAddr, b.voter)
			if leader, err := joiner.Do(ctx, lg, remoteNodes); err != nil {
				lg.WithFields(logrus.Fields{
					"action":  "bootstrap",
					"servers": remoteNodes,
					"voter":   b.voter,
				}).WithError(err).Warning("failed to join cluster")
			} else {
				lg.WithFields(logrus.Fields{
					"action": "bootstrap",
					"leader": leader,
				}).Info("successfully joined cluster")
				return nil
			}

			// We are a voter, we resolve other peers but we're unable to join them. We're in the situation where we are
			// bootstrapping a new cluster and now we want to notify the other nodes.
			// Each node on notify will build a list of notified node. Once bootstrap expect is reached the nodes will
			// bootstrap together.
			if b.voter {
				// notify other servers about readiness of this node to be joined
				if err := b.notify(ctx, remoteNodes); err != nil {
					lg.WithFields(logrus.Fields{
						"action":  "bootstrap",
						"servers": remoteNodes,
					}).WithError(err).Error("failed to notify peers")
					continue
				}
				lg.WithFields(logrus.Fields{
					"action":  "bootstrap",
					"servers": remoteNodes,
				}).Info("notified peers this node is ready to join as voter")
			}
		}
	}
}

// notify attempts to notify all nodes in remoteNodes that this server is ready to bootstrap
func (b *Bootstrapper) notify(ctx context.Context, remoteNodes map[string]string) (err error) {
	if entSentry.Enabled() {
		span := sentry.StartSpan(ctx, "raft.bootstrap.notify",
			sentry.WithOpName("notify"),
			sentry.WithDescription("Attempt to notify existing node(s) to join a cluster"),
		)
		ctx = span.Context()
		span.SetData("servers", remoteNodes)
		defer span.Finish()
	}
	for _, addr := range remoteNodes {
		req := &cmd.NotifyPeerRequest{Id: b.localNodeID, Address: b.localRaftAddr}
		_, err = b.peerJoiner.Notify(ctx, addr, req)
		if err != nil {
			return err
		}
	}
	return
}

// ResolveRemoteNodes returns a list of remoteNodes addresses resolved using addrResolver. The nodes id used are
// taken from serverPortMap keys and ports from the values
func ResolveRemoteNodes(addrResolver resolver.ClusterStateReader, serverPortMap map[string]int) map[string]string {
	candidates := make(map[string]string, len(serverPortMap))
	for name, raftPort := range serverPortMap {
		if addr := addrResolver.NodeAddress(name); addr != "" {
			candidates[name] = fmt.Sprintf("%s:%d", addr, raftPort)
		}
	}
	return candidates
}

// jitter introduce some jitter to a given duration d + [0, 1) * jit -> [d, d+jit]
func jitter(d time.Duration, jit time.Duration) time.Duration {
	return d + time.Duration(float64(jit)*rand.Float64())
}