-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_etcd.go
475 lines (382 loc) · 9.44 KB
/
store_etcd.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package prophet
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/fagongzi/goetty"
"github.com/fagongzi/util/format"
"go.etcd.io/etcd/clientv3"
)
const (
// DefaultTimeout default timeout
DefaultTimeout = time.Second * 3
// DefaultRequestTimeout default request timeout
DefaultRequestTimeout = 10 * time.Second
// DefaultSlowRequestTime default slow request time
DefaultSlowRequestTime = time.Second * 1
idBatch = 1000
)
var (
errMaybeNotLeader = errors.New("may be not leader")
errSchedulerExisted = errors.New("scheduler is existed")
errSchedulerNotFound = errors.New("scheduler is not found")
)
var (
endID = uint64(math.MaxUint64)
)
func getCurrentClusterMembers(client *clientv3.Client) (*clientv3.MemberListResponse, error) {
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
members, err := client.MemberList(ctx)
cancel()
return members, err
}
type etcdStore struct {
sync.Mutex
adapter Adapter
client *clientv3.Client
idPath string
resourcePath string
containerPath string
clusterPath string
store Store
base uint64
end uint64
signature string
elector Elector
}
func newEtcdStore(client *clientv3.Client, adapter Adapter, signature string, elector Elector) Store {
return &etcdStore{
adapter: adapter,
client: client,
signature: signature,
elector: elector,
idPath: "/meta/id",
resourcePath: "/meta/resources",
containerPath: "/meta/containers",
clusterPath: "/cluster",
}
}
// PutContainer returns nil if container is add or update succ
func (s *etcdStore) PutContainer(meta Container) error {
key := s.getKey(meta.ID(), s.containerPath)
data, err := meta.Marshal()
if err != nil {
return err
}
return s.save(key, string(data))
}
func (s *etcdStore) GetResource(id uint64) (Resource, error) {
key := s.getKey(id, s.resourcePath)
data, _, err := s.getValue(key)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
res := s.adapter.NewResource()
err = res.Unmarshal(data)
if err != nil {
return nil, err
}
return res, nil
}
// GetContainer returns the spec container
func (s *etcdStore) GetContainer(id uint64) (Container, error) {
key := s.getKey(id, s.containerPath)
data, _, err := s.getValue(key)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
c := s.adapter.NewContainer()
err = c.Unmarshal(data)
if err != nil {
return nil, err
}
return c, nil
}
// PutResource returns nil if resource is add or update succ
func (s *etcdStore) PutResource(meta Resource) error {
key := s.getKey(meta.ID(), s.resourcePath)
data, err := meta.Marshal()
if err != nil {
return err
}
return s.save(key, string(data))
}
func (s *etcdStore) LoadResources(limit int64, do func(Resource)) error {
startID := uint64(0)
endKey := s.getKey(endID, s.resourcePath)
withRange := clientv3.WithRange(endKey)
withLimit := clientv3.WithLimit(limit)
for {
startKey := s.getKey(startID, s.resourcePath)
resp, err := s.get(startKey, withRange, withLimit)
if err != nil {
return err
}
for _, item := range resp.Kvs {
v := s.adapter.NewResource()
err := v.Unmarshal(item.Value)
if err != nil {
return err
}
startID = v.ID() + 1
do(v)
}
// read complete
if len(resp.Kvs) < int(limit) {
break
}
}
return nil
}
func (s *etcdStore) LoadContainers(limit int64, do func(Container)) error {
startID := uint64(0)
endKey := s.getKey(endID, s.containerPath)
withRange := clientv3.WithRange(endKey)
withLimit := clientv3.WithLimit(limit)
for {
startKey := s.getKey(startID, s.containerPath)
resp, err := s.get(startKey, withRange, withLimit)
if err != nil {
return err
}
for _, item := range resp.Kvs {
v := s.adapter.NewContainer()
err := v.Unmarshal(item.Value)
if err != nil {
return err
}
startID = v.ID() + 1
do(v)
}
// read complete
if len(resp.Kvs) < int(limit) {
break
}
}
return nil
}
func (s *etcdStore) AllocID() (uint64, error) {
s.Lock()
defer s.Unlock()
if s.base == s.end {
end, err := s.generate()
if err != nil {
return 0, err
}
s.end = end
s.base = s.end - idBatch
}
s.base++
return s.base, nil
}
func (s *etcdStore) generate() (uint64, error) {
value, err := s.getID()
if err != nil {
return 0, err
}
max := value + idBatch
// create id
if value == 0 {
max := value + idBatch
err := s.createID(s.signature, max)
if err != nil {
return 0, err
}
return max, nil
}
err = s.updateID(s.signature, value, max)
if err != nil {
return 0, err
}
return max, nil
}
func (s *etcdStore) AlreadyBootstrapped() (bool, error) {
resp, err := s.get(s.clusterPath, clientv3.WithCountOnly())
if err != nil {
return false, nil
}
return resp.Count > 0, nil
}
// PutBootstrapped put cluster is bootstrapped
func (s *etcdStore) PutBootstrapped(container Container, resources ...Resource) (bool, error) {
clusterID, err := s.AllocID()
if err != nil {
return false, err
}
ctx, cancel := context.WithTimeout(s.client.Ctx(), DefaultTimeout)
defer cancel()
// build operations
var ops []clientv3.Op
ops = append(ops, clientv3.OpPut(s.clusterPath, string(goetty.Uint64ToBytes(clusterID))))
meta, err := container.Marshal()
if err != nil {
return false, err
}
ops = append(ops, clientv3.OpPut(s.getKey(container.ID(), s.containerPath), string(meta)))
resp, err := s.client.Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(s.clusterPath), "=", 0)).
Then(ops...).
Commit()
if err != nil {
return false, err
}
// already bootstrapped
if !resp.Succeeded {
return false, nil
}
ops = ops[:0]
for idx, res := range resources {
meta, err = res.Marshal()
if err != nil {
return false, err
}
ops = append(ops, clientv3.OpPut(s.getKey(res.ID(), s.resourcePath), string(meta)))
if idx > 0 && idx%16 == 0 {
resp, err = s.client.Txn(ctx).
Then(ops...).
Commit()
if err != nil {
return false, err
}
ops = ops[:0]
}
}
if len(ops) > 0 {
resp, err = s.client.Txn(ctx).
Then(ops...).
Commit()
if err != nil {
return false, err
}
}
return true, nil
}
func (s *etcdStore) PutIfNotExists(path string, value []byte) (bool, []byte, error) {
resp, err := s.txn().
If(clientv3.Compare(clientv3.CreateRevision(path), "=", 0)).
Then(clientv3.OpPut(path, string(value))).
Commit()
if err != nil {
return false, nil, err
}
if !resp.Succeeded {
data, _, err := s.getValue(path)
if err != nil {
return false, nil, err
}
return false, data, nil
}
return true, nil, nil
}
func (s *etcdStore) RemoveIfValueMatched(path string, expect []byte) (bool, error) {
resp, err := s.txn().
If(clientv3.Compare(clientv3.Value(path), "=", string(expect))).
Then(clientv3.OpDelete(path)).
Commit()
if err != nil {
return false, err
}
if !resp.Succeeded {
return false, nil
}
return true, nil
}
func (s *etcdStore) getID() (uint64, error) {
resp, _, err := s.getValue(s.idPath)
if err != nil {
return 0, err
}
if len(resp) == 0 {
return 0, nil
}
return format.BytesToUint64(resp)
}
func (s *etcdStore) createID(leaderSignature string, value uint64) error {
cmp := clientv3.Compare(clientv3.CreateRevision(s.idPath), "=", 0)
op := clientv3.OpPut(s.idPath, string(format.Uint64ToBytes(value)))
ok, err := s.elector.DoIfLeader(math.MaxUint64, leaderSignature, []clientv3.Cmp{cmp}, op)
if err != nil {
return err
}
if !ok {
return errMaybeNotLeader
}
return nil
}
func (s *etcdStore) updateID(leaderSignature string, old, value uint64) error {
cmp := clientv3.Compare(clientv3.Value(s.idPath), "=", string(format.Uint64ToBytes(old)))
op := clientv3.OpPut(s.idPath, string(format.Uint64ToBytes(value)))
ok, err := s.elector.DoIfLeader(math.MaxUint64, leaderSignature, []clientv3.Cmp{cmp}, op)
if err != nil {
return err
}
if !ok {
return errMaybeNotLeader
}
return nil
}
func (s *etcdStore) getKey(id uint64, base string) string {
return fmt.Sprintf("%s/%020d", base, id)
}
func (s *etcdStore) txn() clientv3.Txn {
return newSlowLogTxn(s.client)
}
func (s *etcdStore) getValue(key string, opts ...clientv3.OpOption) ([]byte, int64, error) {
resp, err := s.get(key, opts...)
if err != nil {
return nil, 0, err
}
if n := len(resp.Kvs); n == 0 {
return nil, 0, nil
} else if n > 1 {
return nil, 0, fmt.Errorf("invalid get value resp %v, must only one", resp.Kvs)
}
return resp.Kvs[0].Value, resp.Kvs[0].ModRevision, nil
}
func (s *etcdStore) get(key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(s.client.Ctx(), DefaultRequestTimeout)
defer cancel()
start := time.Now()
resp, err := clientv3.NewKV(s.client).Get(ctx, key, opts...)
if err != nil {
log.Errorf("read option failure, key=<%s>, errors:\n %+v",
key,
err)
return resp, err
}
if cost := time.Since(start); cost > DefaultSlowRequestTime {
log.Warningf("read option is too slow, key=<%s>, cost=<%d>",
key,
cost)
}
return resp, nil
}
func (s *etcdStore) save(key, value string) error {
ok, err := s.elector.DoIfLeader(math.MaxUint64, s.signature, nil, clientv3.OpPut(key, value))
if err != nil {
return err
}
if !ok {
return errMaybeNotLeader
}
return nil
}
func (s *etcdStore) create(key, value string) error {
resp, err := s.txn().If(clientv3.Compare(clientv3.CreateRevision(key), "=", 0)).Then(clientv3.OpPut(key, value)).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return errMaybeNotLeader
}
return nil
}