forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_etcd.go
565 lines (458 loc) · 13.4 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package model
import (
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/fagongzi/log"
"github.com/fagongzi/util/task"
"golang.org/x/net/context"
)
var (
// ErrHasBind error has bind into, can not delete
ErrHasBind = errors.New("Has bind info, can not delete")
)
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
)
// slowLogTxn wraps etcd transaction and log slow one.
type slowLogTxn struct {
clientv3.Txn
cancel context.CancelFunc
}
func newSlowLogTxn(client *clientv3.Client) clientv3.Txn {
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
return &slowLogTxn{
Txn: client.Txn(ctx),
cancel: cancel,
}
}
func (t *slowLogTxn) If(cs ...clientv3.Cmp) clientv3.Txn {
return &slowLogTxn{
Txn: t.Txn.If(cs...),
cancel: t.cancel,
}
}
func (t *slowLogTxn) Then(ops ...clientv3.Op) clientv3.Txn {
return &slowLogTxn{
Txn: t.Txn.Then(ops...),
cancel: t.cancel,
}
}
// Commit implements Txn Commit interface.
func (t *slowLogTxn) Commit() (*clientv3.TxnResponse, error) {
start := time.Now()
resp, err := t.Txn.Commit()
t.cancel()
cost := time.Now().Sub(start)
if cost > DefaultSlowRequestTime {
log.Warn("slow: txn runs too slow, resp=<%v> cost=<%s> errors:\n %+v",
resp,
cost,
err)
}
return resp, err
}
// EtcdStore etcd store impl
type EtcdStore struct {
prefix string
clustersDir string
serversDir string
bindsDir string
apisDir string
proxiesDir string
routingsDir string
cli *clientv3.Client
evtCh chan *Evt
watchMethodMapping map[EvtSrc]func(EvtType, *mvccpb.KeyValue) *Evt
taskRunner *task.Runner
}
// NewEtcdStore create a etcd store
func NewEtcdStore(etcdAddrs []string, prefix string, taskRunner *task.Runner) (Store, error) {
store := &EtcdStore{
prefix: prefix,
clustersDir: fmt.Sprintf("%s/clusters", prefix),
serversDir: fmt.Sprintf("%s/servers", prefix),
bindsDir: fmt.Sprintf("%s/binds", prefix),
apisDir: fmt.Sprintf("%s/apis", prefix),
proxiesDir: fmt.Sprintf("%s/proxy", prefix),
routingsDir: fmt.Sprintf("%s/routings", prefix),
watchMethodMapping: make(map[EvtSrc]func(EvtType, *mvccpb.KeyValue) *Evt),
taskRunner: taskRunner,
}
cli, err := clientv3.New(clientv3.Config{
Endpoints: etcdAddrs,
DialTimeout: DefaultTimeout,
})
if err != nil {
return nil, err
}
store.cli = cli
store.init()
return store, nil
}
func (e *EtcdStore) txn() clientv3.Txn {
return newSlowLogTxn(e.cli)
}
// SaveAPI save a api in store
func (e *EtcdStore) SaveAPI(api *API) error {
return e.UpdateAPI(api)
}
// UpdateAPI update a api in store
func (e *EtcdStore) UpdateAPI(api *API) error {
key := fmt.Sprintf("%s/%s", e.apisDir, getAPIKey(api.URL, api.Method))
return e.put(key, string(api.Marshal()))
}
// DeleteAPI delete a api from store
func (e *EtcdStore) DeleteAPI(apiURL, method string) error {
key := fmt.Sprintf("%s/%s", e.apisDir, getAPIKey(apiURL, method))
return e.delete(key)
}
// GetAPIs return api list from store
func (e *EtcdStore) GetAPIs() ([]*API, error) {
var values []*API
err := e.getList(e.apisDir, func(item *mvccpb.KeyValue) {
values = append(values, UnMarshalAPI(item.Value))
})
return values, err
}
// GetAPI return api by url from store
func (e *EtcdStore) GetAPI(apiURL, method string) (*API, error) {
key := fmt.Sprintf("%s/%s", e.apisDir, getAPIKey(apiURL, method))
var value *API
err := e.getList(key, func(item *mvccpb.KeyValue) {
value = UnMarshalAPI(item.Value)
})
return value, err
}
// SaveServer save a server to store
func (e *EtcdStore) SaveServer(svr *Server) error {
return e.doUpdateServer(svr)
}
// UpdateServer update a server to store
func (e *EtcdStore) UpdateServer(svr *Server) error {
old, err := e.GetServer(svr.Addr)
if nil != err {
return err
}
old.updateFrom(svr)
return e.doUpdateServer(old)
}
func (e *EtcdStore) doUpdateServer(svr *Server) error {
key := fmt.Sprintf("%s/%s", e.serversDir, svr.Addr)
return e.put(key, string(svr.Marshal()))
}
// DeleteServer delete a server from store
func (e *EtcdStore) DeleteServer(addr string) error {
svr, err := e.GetServer(addr)
if err != nil {
return err
}
if svr.HasBind() {
return ErrHasBind
}
key := fmt.Sprintf("%s/%s", e.serversDir, svr.Addr)
return e.delete(key)
}
// GetServers return server from store
func (e *EtcdStore) GetServers() ([]*Server, error) {
var values []*Server
err := e.getList(e.serversDir, func(item *mvccpb.KeyValue) {
values = append(values, UnMarshalServer(item.Value))
})
return values, err
}
// GetServer return spec server
func (e *EtcdStore) GetServer(serverAddr string) (*Server, error) {
key := fmt.Sprintf("%s/%s", e.serversDir, serverAddr)
var value *Server
err := e.getList(key, func(item *mvccpb.KeyValue) {
value = UnMarshalServer(item.Value)
})
return value, err
}
// SaveCluster save a cluster to store
func (e *EtcdStore) SaveCluster(cluster *Cluster) error {
return e.doUpdateCluster(cluster)
}
// UpdateCluster update a cluster to store
func (e EtcdStore) UpdateCluster(cluster *Cluster) error {
old, err := e.GetCluster(cluster.Name)
if nil != err {
return err
}
old.updateFrom(cluster)
return e.doUpdateCluster(old)
}
func (e *EtcdStore) doUpdateCluster(cluster *Cluster) error {
key := fmt.Sprintf("%s/%s", e.clustersDir, cluster.Name)
return e.put(key, string(cluster.Marshal()))
}
// DeleteCluster delete a cluster from store
func (e *EtcdStore) DeleteCluster(name string) error {
c, err := e.GetCluster(name)
if err != nil {
return err
}
if c.HasBind() {
return ErrHasBind
}
key := fmt.Sprintf("%s/%s", e.clustersDir, name)
return e.delete(key)
}
// GetClusters return clusters in store
func (e *EtcdStore) GetClusters() ([]*Cluster, error) {
var values []*Cluster
err := e.getList(e.clustersDir, func(item *mvccpb.KeyValue) {
values = append(values, UnMarshalCluster(item.Value))
})
return values, err
}
// GetCluster return cluster info
func (e *EtcdStore) GetCluster(clusterName string) (*Cluster, error) {
key := fmt.Sprintf("%s/%s", e.clustersDir, clusterName)
var value *Cluster
err := e.getList(key, func(item *mvccpb.KeyValue) {
value = UnMarshalCluster(item.Value)
})
return value, err
}
// SaveBind save bind to store
func (e *EtcdStore) SaveBind(bind *Bind) error {
svr, err := e.GetServer(bind.ServerAddr)
if err != nil {
return err
}
svr.AddBind(bind)
cluster, err := e.GetCluster(bind.ClusterName)
if err != nil {
return err
}
cluster.AddBind(bind)
bindKey := fmt.Sprintf("%s/%s", e.bindsDir, bind.ToString())
svrKey := fmt.Sprintf("%s/%s", e.serversDir, svr.Addr)
clusterKey := fmt.Sprintf("%s/%s", e.clustersDir, cluster.Name)
opBind := clientv3.OpPut(bindKey, string(bind.Marshal()))
opSvr := clientv3.OpPut(svrKey, string(svr.Marshal()))
opCluster := clientv3.OpPut(clusterKey, string(cluster.Marshal()))
_, err = e.txn().Then(opBind, opSvr, opCluster).Commit()
return err
}
// UnBind delete bind from store
func (e *EtcdStore) UnBind(bind *Bind) error {
svr, err := e.GetServer(bind.ServerAddr)
if err != nil {
return err
}
svr.RemoveBind(bind.ClusterName)
c, err := e.GetCluster(bind.ClusterName)
if err != nil {
return err
}
c.RemoveBind(bind.ServerAddr)
bindKey := fmt.Sprintf("%s/%s", e.bindsDir, bind.ToString())
svrKey := fmt.Sprintf("%s/%s", e.serversDir, svr.Addr)
clusterKey := fmt.Sprintf("%s/%s", e.clustersDir, c.Name)
opBind := clientv3.OpDelete(bindKey)
opSvr := clientv3.OpPut(svrKey, string(svr.Marshal()))
opCluster := clientv3.OpPut(clusterKey, string(c.Marshal()))
_, err = e.txn().Then(opBind, opSvr, opCluster).Commit()
return err
}
// GetBinds return binds info
func (e *EtcdStore) GetBinds() ([]*Bind, error) {
var values []*Bind
err := e.getList(e.bindsDir, func(item *mvccpb.KeyValue) {
values = append(values, UnMarshalBind(item.Value))
})
return values, err
}
// SaveRouting save route to store
func (e *EtcdStore) SaveRouting(routing *Routing) error {
key := fmt.Sprintf("%s/%s", e.routingsDir, routing.ID)
return e.putTTL(key, string(routing.Marshal()), int64(routing.deadline))
}
// GetRoutings return routes in store
func (e *EtcdStore) GetRoutings() ([]*Routing, error) {
var values []*Routing
err := e.getList(e.routingsDir, func(item *mvccpb.KeyValue) {
values = append(values, UnMarshalRouting(item.Value))
})
return values, err
}
// Clean clean data in store
func (e *EtcdStore) Clean() error {
_, err := e.txn().Then(clientv3.OpDelete(e.prefix, clientv3.WithPrefix())).Commit()
return err
}
// Watch watch event from etcd
func (e *EtcdStore) Watch(evtCh chan *Evt, stopCh chan bool) error {
e.evtCh = evtCh
log.Infof("meta: etcd watch at: <%s>",
e.prefix)
e.doWatch()
return nil
}
func (e EtcdStore) doWatch() {
watcher := clientv3.NewWatcher(e.cli)
defer watcher.Close()
ctx := e.cli.Ctx()
for {
rch := watcher.Watch(ctx, e.prefix, clientv3.WithPrefix())
for wresp := range rch {
if wresp.Canceled {
return
}
for _, ev := range wresp.Events {
var evtSrc EvtSrc
var evtType EvtType
switch ev.Type {
case mvccpb.DELETE:
evtType = EventTypeDelete
case mvccpb.PUT:
if ev.IsCreate() {
evtType = EventTypeNew
} else if ev.IsModify() {
evtType = EventTypeUpdate
}
}
key := string(ev.Kv.Key)
if strings.HasPrefix(key, e.clustersDir) {
evtSrc = EventSrcCluster
} else if strings.HasPrefix(key, e.serversDir) {
evtSrc = EventSrcServer
} else if strings.HasPrefix(key, e.bindsDir) {
evtSrc = EventSrcBind
} else if strings.HasPrefix(key, e.apisDir) {
evtSrc = EventSrcAPI
} else if strings.HasPrefix(key, e.routingsDir) {
evtSrc = EventSrcRouting
} else {
continue
}
log.Infof("meta: etcd changed: <%s, %v>",
key,
evtType)
e.evtCh <- e.watchMethodMapping[evtSrc](evtType, ev.Kv)
}
}
select {
case <-ctx.Done():
// server closed, return
return
default:
}
}
}
func (e *EtcdStore) doWatchWithCluster(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
cluster := UnMarshalCluster([]byte(kv.Value))
return &Evt{
Src: EventSrcCluster,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.clustersDir), "", 1),
Value: cluster,
}
}
func (e *EtcdStore) doWatchWithServer(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
server := UnMarshalServer([]byte(kv.Value))
return &Evt{
Src: EventSrcServer,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.serversDir), "", 1),
Value: server,
}
}
func (e *EtcdStore) doWatchWithBind(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
key := strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.bindsDir), "", 1)
infos := strings.SplitN(key, "-", 2)
return &Evt{
Src: EventSrcBind,
Type: evtType,
Key: string(kv.Key),
Value: &Bind{
ServerAddr: infos[0],
ClusterName: infos[1],
},
}
}
func (e *EtcdStore) doWatchWithAPI(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
api := UnMarshalAPI([]byte(kv.Value))
value := strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.apisDir), "", 1)
return &Evt{
Src: EventSrcAPI,
Type: evtType,
Key: value,
Value: api,
}
}
func (e *EtcdStore) doWatchWithRouting(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
routing := UnMarshalRouting([]byte(kv.Value))
return &Evt{
Src: EventSrcRouting,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.routingsDir), "", 1),
Value: routing,
}
}
func (e *EtcdStore) init() {
e.watchMethodMapping[EventSrcBind] = e.doWatchWithBind
e.watchMethodMapping[EventSrcServer] = e.doWatchWithServer
e.watchMethodMapping[EventSrcCluster] = e.doWatchWithCluster
e.watchMethodMapping[EventSrcAPI] = e.doWatchWithAPI
e.watchMethodMapping[EventSrcRouting] = e.doWatchWithRouting
}
func (e *EtcdStore) put(key, value string) error {
_, err := e.txn().Then(clientv3.OpPut(key, value)).Commit()
return err
}
func (e *EtcdStore) putTTL(key, value string, ttl int64) error {
lessor := clientv3.NewLease(e.cli)
defer lessor.Close()
ctx, cancel := context.WithTimeout(e.cli.Ctx(), DefaultRequestTimeout)
leaseResp, err := lessor.Grant(ctx, ttl)
cancel()
if err != nil {
return err
}
_, err = e.txn().Then(clientv3.OpPut(key, value, clientv3.WithLease(leaseResp.ID))).Commit()
return err
}
func (e *EtcdStore) delete(key string) error {
_, err := e.txn().Then(clientv3.OpDelete(key)).Commit()
return err
}
func (e *EtcdStore) getList(key string, fn func(*mvccpb.KeyValue)) error {
ctx, cancel := context.WithTimeout(e.cli.Ctx(), DefaultRequestTimeout)
defer cancel()
resp, err := clientv3.NewKV(e.cli).Get(ctx, key, clientv3.WithPrefix())
if nil != err {
return err
}
for _, item := range resp.Kvs {
fn(item)
}
return nil
}
func getAPIKey(apiURL, method string) string {
key := fmt.Sprintf("%s-%s", apiURL, method)
return base64.RawURLEncoding.EncodeToString([]byte(key))
}
func parseAPIKey(key string) (url string, method string) {
raw := decodeAPIKey(key)
splits := strings.SplitN(raw, "-", 2)
url = splits[0]
method = splits[1]
return url, method
}
func decodeAPIKey(key string) string {
raw, _ := base64.RawURLEncoding.DecodeString(key)
return string(raw)
}