-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcdseq.go
332 lines (286 loc) · 6.51 KB
/
etcdseq.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
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
package etcdseq
import (
"context"
"errors"
"fmt"
"sync"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
)
const TimeToLive = 10
const indexInvalid = -1
const countInvalid = 0
var DefaultInfo = Info{
Index: indexInvalid,
Count: countInvalid,
}
type EtcdSeqOption func(client *EtcdSeq)
type Info struct {
Index int
Count int
revision int64
}
func (i Info) String() string {
if i.Invalid() {
return "Invalid"
}
return fmt.Sprintf("%d/%d rev:%d", i.Index, i.Count, i.revision)
}
func (i *Info) Reset() {
i.Index = indexInvalid
i.Count = countInvalid
}
func (i Info) Invalid() bool {
return i.Index == indexInvalid || i.Count == countInvalid
}
func (i Info) Equal(other Info) bool {
if i.Invalid() {
return other.Invalid()
}
return i.Index == other.Index && i.Count == other.Count
}
type Handler interface {
OnChange(info Info)
}
type EtcdSeq struct {
client *clientv3.Client
key string
keyPrefix string
value string
handler Handler
quit *DoneChan
quitWatch *DoneChan
logger Logger
fullKey string
info Info
lease clientv3.LeaseID
wg sync.WaitGroup
wgWatch sync.WaitGroup
}
func NewEtcdSeq(client *clientv3.Client, key string, handler Handler, opts ...EtcdSeqOption) *EtcdSeq {
l := &EtcdSeq{
client: client,
key: key,
value: key,
keyPrefix: makeKeyPrefix(key),
handler: handler,
quit: NewDoneChan(),
quitWatch: NewDoneChan(),
logger: stdLogger{},
}
l.reset()
for _, opt := range opts {
opt(l)
}
return l
}
func (l *EtcdSeq) Start() error {
err := l.doRegister()
if err != nil {
return fmt.Errorf("doRegister: %w", err)
}
err = l.keepAliveAsync()
if err != nil {
return fmt.Errorf("keepAliveAsync: %w", err)
}
l.watchAsync()
return nil
}
func (l *EtcdSeq) Stop() {
l.quitWatch.Close()
l.wgWatch.Wait()
l.quit.Close()
l.wg.Wait()
}
func (l *EtcdSeq) reset() {
l.info.Reset()
l.lease = clientv3.NoLease
}
func (l *EtcdSeq) doKeepAlive() error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-l.quit.Done():
l.logger.Infof("doKeepAlive quit")
return nil
case <-ticker.C:
err := l.doRegister()
if err != nil {
l.logger.Errorf("doRegister: %+v", err)
break
}
if err := l.keepAliveAsync(); err != nil {
l.logger.Errorf("keepAliveAsync: %+v", err)
break
}
return nil
}
}
}
func (l *EtcdSeq) keepAliveAsync() error {
ch, err := l.client.KeepAlive(l.client.Ctx(), l.lease)
if err != nil {
return fmt.Errorf("etcd KeepAlive: %w", err)
}
l.wg.Add(1)
go l.runSafe(func() {
defer l.wg.Done()
for {
select {
case _, ok := <-ch:
if !ok {
l.doRevoke()
if err := l.doKeepAlive(); err != nil {
l.logger.Errorf("doKeepAlive: %+v", err)
}
return
}
case <-l.quit.Done():
l.doRevoke()
l.logger.Infof("keepAliveAsync quit")
return
}
}
})
return nil
}
func (l *EtcdSeq) watchAsync() {
l.wgWatch.Add(1)
go l.runSafe(func() {
defer l.wgWatch.Done()
if err := l.watch(); err != nil {
l.logger.Errorf("etcd publisher watch: %+v", err)
}
})
}
func (l *EtcdSeq) doRegister() (err error) {
lease, err := l.register()
if err != nil {
return fmt.Errorf("etcd register: %w", err)
}
l.lease = lease
l.logger.Debugf("registerred: %s", l.fullKey)
return l.handleInfoChange()
}
func (l *EtcdSeq) register() (clientv3.LeaseID, error) {
resp, err := l.client.Grant(l.client.Ctx(), TimeToLive)
if err != nil {
return clientv3.NoLease, fmt.Errorf("etcd grant: %w", err)
}
lease := resp.ID
l.fullKey = makeEtcdKey(l.key, int64(lease))
_, err = l.client.Put(l.client.Ctx(), l.fullKey, l.value, clientv3.WithLease(lease))
if err != nil {
return clientv3.NoLease, fmt.Errorf("etcd put: %w", err)
}
return lease, err
}
func (l *EtcdSeq) doRevoke() {
err := l.revoke()
if err != nil {
l.logger.Errorf("revoke: %+v", err)
return
}
l.reset()
l.handler.OnChange(l.info)
}
func (l *EtcdSeq) revoke() error {
_, err := l.client.Revoke(l.client.Ctx(), l.lease)
if err != nil {
l.logger.Errorf("etcd Revoke: %+v", err)
}
return err
}
func (l *EtcdSeq) load() (info Info, err error) {
resp, err := l.client.Get(l.client.Ctx(), l.key, clientv3.WithPrefix())
if err != nil {
return
}
index := indexInvalid
for i, kv := range resp.Kvs {
if kv.Lease == int64(l.lease) {
index = i
break
}
}
if index == indexInvalid {
return info, fmt.Errorf("lease not found")
}
info.Index = index
info.Count = len(resp.Kvs)
info.revision = resp.Header.Revision
return
}
func makeEtcdKey(key string, id int64) string {
return fmt.Sprintf("%s/seat/%d", key, id)
}
func makeKeyPrefix(key string) string {
return key + "/seat/"
}
func makeLockKey(key string, index int) string {
return fmt.Sprintf("%s/lk/%d", key, index)
}
func (l *EtcdSeq) watch() error {
var rch clientv3.WatchChan
rev := l.info.revision
if rev == 0 {
rch = l.client.Watch(clientv3.WithRequireLeader(l.client.Ctx()), l.keyPrefix, clientv3.WithPrefix())
} else {
rch = l.client.Watch(clientv3.WithRequireLeader(l.client.Ctx()), l.keyPrefix, clientv3.WithPrefix(), clientv3.WithRev(rev+1))
}
for {
select {
case <-l.quitWatch.Done():
l.logger.Infof("watch quit")
return nil
case resp, ok := <-rch:
if !ok {
return errors.New("etcd watch chan has been closed")
}
if resp.Canceled {
return fmt.Errorf("etcd watch chan has been canceled, error: %w", resp.Err())
}
if resp.Err() != nil {
return fmt.Errorf("etcd watch chan has error, error: %w", resp.Err())
}
l.logger.Debugf("watch event")
err := l.handleInfoChange()
if err != nil {
return fmt.Errorf("etcd handleInfoChange: %w", err)
}
}
}
}
func (l *EtcdSeq) handleInfoChange() error {
info, err := l.load()
if err != nil {
return fmt.Errorf("load: %w", err)
}
l.logger.Debugf("loaded: %+v", info)
if l.info.Index != info.Index || l.info.Count != info.Count {
l.logger.Debugf("info changed from: %+v to %+v", l.info, info)
l.handler.OnChange(info)
l.info = info
}
return nil
}
type Unlocker func(ctx context.Context) error
func (l *EtcdSeq) Lock(ctx context.Context, info Info) (Unlocker, error) {
s, err := concurrency.NewSession(l.client)
if err != nil {
return nil, fmt.Errorf("etcd NewSession: %w", err)
}
m := concurrency.NewMutex(s, makeLockKey(l.key, info.Index))
err = m.Lock(ctx)
if err != nil {
return nil, fmt.Errorf("etcd Lock: %w", err)
}
return m.Unlock, nil
}
func WithLogger(logger Logger) EtcdSeqOption {
return func(client *EtcdSeq) {
client.logger = logger
}
}