-
Notifications
You must be signed in to change notification settings - Fork 0
/
leader.go
287 lines (215 loc) · 7.24 KB
/
leader.go
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
package natty
import (
"context"
"fmt"
"strings"
"time"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/relistan/go-director"
)
const (
DefaultAsLeaderBucketTTL = time.Second * 10
DefaultAsLeaderElectionLooperInterval = time.Second
DefaultAsLeaderReplicaCount = 1
)
var (
ErrBucketTTLMismatch = errors.New("bucket ttl mismatch")
)
type AsLeaderConfig struct {
// Looper is the loop construct that will be used to execute Func (required)
Looper director.Looper
// Bucket specifies what K/V bucket will be used for leader election (required)
Bucket string
// Key specifies the keyname that the leader election will occur on (required)
Key string
// NodeName is the name used for this node; should be unique in cluster (required)
NodeName string
// Description will set the bucket description (optional)
Description string
// ElectionLooper allows you to override the used election looper (optional)
ElectionLooper director.Looper
// BucketTTL specifies the TTL policy the bucket should use (optional)
BucketTTL time.Duration
// ReplicaCount specifies the number of replicas the bucket should use (optional, default 1)
ReplicaCount int
}
func (n *Natty) GetLeader(ctx context.Context, bucketName, keyName string) (string, error) {
currentLeader, err := n.Get(ctx, bucketName, keyName)
if err != nil {
if err == nats.ErrKeyNotFound {
return "", err
}
n.log.Errorf("unable to determine leader for '%s:%s': %s", bucketName, keyName, err)
return "", fmt.Errorf("unable to determine leader for '%s:%s': %s", bucketName, keyName, err)
}
return string(currentLeader), nil
}
func (n *Natty) HaveLeader(ctx context.Context, nodeName, bucketName, keyName string) bool {
currentLeader, err := n.GetLeader(ctx, bucketName, keyName)
if err != nil {
return false
}
return currentLeader == nodeName
}
func (n *Natty) AsLeader(ctx context.Context, cfg AsLeaderConfig, f func() error) error {
if err := validateAsLeaderArgs(&cfg, f); err != nil {
return errors.Wrap(err, "unable to validate AsLeader args")
}
// Attempt to create bucket; if bucket exists, verify that TTL matches
if err := n.CreateBucket(ctx, cfg.Bucket, cfg.BucketTTL, cfg.ReplicaCount, cfg.Description); err != nil {
if strings.Contains(err.Error(), "stream name already in use") {
n.log.Debug("bucket exists, checking if ttl matches")
kv, err := n.js.KeyValue(cfg.Bucket)
if err != nil {
return errors.Wrap(err, "unable to fetch existing bucket")
}
s, err := kv.Status()
if err != nil {
return errors.Wrap(err, "unable to fetch existing bucket status")
}
n.log.Debugf("ttl on bucket: %v desired ttl: %v", s.TTL(), cfg.BucketTTL)
if s.TTL().Seconds() != cfg.BucketTTL.Seconds() {
n.log.Error("bucket ttls do not match")
return ErrBucketTTLMismatch
}
} else {
return errors.Wrap(err, "unable to pre-create leader bucket")
}
}
errCh := make(chan error, 1)
n.log.Debugf("%s: starting leader election goroutine", cfg.NodeName)
// Launch leader election in goroutine (leader election goroutine should quit when AsLeader is cancelled or exits)
go func() {
err := n.runLeaderElection(ctx, &cfg)
if err != nil {
n.log.Errorf("%s: unable to run leader election: %v", cfg.NodeName, err)
errCh <- err
return
}
n.log.Debugf("%s: leader election goroutine exited", cfg.NodeName)
}()
n.log.Debugf("%s: waiting for goroutine to not error", cfg.NodeName)
select {
case err := <-errCh:
return errors.Wrap(err, "ran into error during leader election startup")
case <-ctx.Done():
return errors.New("context cancelled - leader election cancelled")
// Leader election did not error after 2 seconds, all is well
case <-time.After(time.Second * 1):
break
}
// Leader election goroutine started; run main loop
n.log.Debugf("%s: leader election goroutine started; running main loop", cfg.NodeName)
var quit bool
cfg.Looper.Loop(func() error {
if quit {
// Give looper time to catch up
time.Sleep(100 * time.Millisecond)
return nil
}
select {
case <-ctx.Done():
n.log.Debugf("%s: AsLeader cancelled - exiting loop func", cfg.NodeName)
quit = true
cfg.Looper.Quit()
return nil
default:
// Continue
}
if !n.HaveLeader(ctx, cfg.NodeName, cfg.Bucket, cfg.Key) {
n.log.Debugf("%s: AsLeader: not leader", cfg.NodeName)
return nil
}
n.log.Debugf("%s: AsLeader: running func", cfg.NodeName)
// Have leader, exec func
if err := f(); err != nil {
n.log.Errorf("%s: error during func execution: %v", cfg.NodeName, err)
return nil
}
return nil
})
n.log.Debugf("%s: AsLeader func loop exited for '%s'", cfg.NodeName, cfg.Bucket, cfg.Key)
return nil
}
func (n *Natty) runLeaderElection(ctx context.Context, cfg *AsLeaderConfig) error {
var quit bool
var haveLeader bool
cfg.ElectionLooper.Loop(func() error {
// We are supposed to quit - give looper time to react to quit
if quit {
time.Sleep(25 * time.Millisecond)
return nil
}
// NATS K/V client does not support ctx yet so we do it here instead
select {
case <-ctx.Done():
n.log.Debugf("%s: context cancelled, exiting leader election", cfg.NodeName)
quit = true
cfg.ElectionLooper.Quit()
return nil
default:
// Continue
}
// Have leader - attempt to update key to increase TTL
if n.HaveLeader(ctx, cfg.NodeName, cfg.Bucket, cfg.Key) {
if err := n.Put(ctx, cfg.Bucket, cfg.Key, []byte(cfg.NodeName), cfg.BucketTTL); err != nil {
// Something happened, try again next iteration - maybe we're
// still the leader.
n.log.Errorf("%s: unable to update leader key '%s:%s': %v", cfg.NodeName, cfg.Bucket, cfg.Key, err)
return nil
}
haveLeader = true
n.log.Debugf("%s: updated leader key '%s:%s'", cfg.NodeName, cfg.Bucket, cfg.Key)
return nil
} else {
if haveLeader {
n.log.Infof("%s: lost leader '%s:%s'", cfg.NodeName, cfg.Bucket, cfg.Key)
}
haveLeader = false
}
if err := n.Create(ctx, cfg.Bucket, cfg.Key, []byte(cfg.NodeName), cfg.BucketTTL); err != nil {
if strings.Contains(err.Error(), "wrong last sequence") {
n.log.Debugf("%s: leader key already exists, ignoring", cfg.NodeName)
return nil
}
n.log.Errorf("%s: unable to create leader key '%s:%s': %v", cfg.NodeName, cfg.Bucket, cfg.Key, err)
return nil
}
n.log.Infof("%s: became leader '%s:%s'", cfg.NodeName, cfg.Bucket, cfg.Key)
haveLeader = true
return nil
})
n.log.Debugf("%s: leader election goroutine exiting", cfg.NodeName)
return nil
}
func validateAsLeaderArgs(cfg *AsLeaderConfig, f func() error) error {
if cfg == nil {
return errors.New("AsLeaderConfig is required")
}
if cfg.Looper == nil {
return errors.New("Looper is required")
}
if cfg.Bucket == "" {
return errors.New("Bucket is required")
}
if cfg.Key == "" {
return errors.New("Key is required")
}
if cfg.NodeName == "" {
return errors.New("NodeName is required")
}
if cfg.ReplicaCount == 0 {
cfg.ReplicaCount = DefaultAsLeaderReplicaCount
}
if f == nil {
return errors.New("Function cannot be nil")
}
if cfg.ElectionLooper == nil {
cfg.ElectionLooper = director.NewTimedLooper(director.FOREVER, DefaultAsLeaderElectionLooperInterval, make(chan error, 1))
}
if cfg.BucketTTL == 0 {
cfg.BucketTTL = DefaultAsLeaderBucketTTL
}
return nil
}