-
Notifications
You must be signed in to change notification settings - Fork 4
/
etcd_op.go
121 lines (100 loc) · 2.97 KB
/
etcd_op.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
package etcd_utils
import (
"context"
"encoding/binary"
"errors"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/api/v3/mvccpb"
)
const etcdTimeout = 2 * time.Second
func EtcdWatchEvents(c *clientv3.Client, start, end string, startRev int64) (clientv3.WatchChan, func()) {
watcher := clientv3.NewWatcher(c)
var rch clientv3.WatchChan
if len(end) > 0 {
rch = watcher.Watch(context.Background(), start,
clientv3.WithRange(end),
clientv3.WithPrevKV(),
clientv3.WithRev(startRev))
} else {
rch = watcher.Watch(context.Background(), start,
clientv3.WithPrevKV(),
clientv3.WithRev(startRev))
}
return rch, func() {
watcher.Close()
}
}
func EtcdSetKV(c *clientv3.Client, key string, val []byte, opts ...clientv3.OpOption) error {
kv := clientv3.NewKV(c)
ctx, cancel := context.WithTimeout(context.Background(), etcdTimeout)
defer cancel()
_, err := kv.Put(ctx, key, string(val), opts...)
return err
}
func EtcdSetKVS(c *clientv3.Client, cmps []clientv3.Cmp, ops []clientv3.Op) error {
ctx, cancel := context.WithTimeout(context.Background(), etcdTimeout)
defer cancel()
txn := clientv3.NewKV(c).Txn(ctx)
txn.If(cmps...)
res, err := txn.Then(ops...).Commit()
if err != nil {
return err
}
if !res.Succeeded {
return errors.New("set etcd kv failed, maybe mutex not match")
}
return nil
}
func EtcdGetKV(c *clientv3.Client, key string, opts ...clientv3.OpOption) ([]byte, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), etcdTimeout)
defer cancel()
resp, err := clientv3.NewKV(c).Get(ctx, key, opts...)
if err != nil {
return nil, 0, err
}
if resp == nil || len(resp.Kvs) == 0 {
return nil, 0, nil
}
return resp.Kvs[0].Value, resp.Header.Revision, nil
}
//EtcdRange return (KeyValue, Revision, error)
func EtcdRange(c *clientv3.Client, prefix string) ([]*mvccpb.KeyValue, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), etcdTimeout)
defer cancel()
opts := []clientv3.OpOption{
clientv3.WithPrefix(),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
clientv3.WithLimit(0),
}
kv := clientv3.NewKV(c)
resp, err := kv.Get(ctx, prefix, opts...)
if err != nil {
return nil, 0, err
}
return resp.Kvs, resp.Header.Revision, err
}
func EtcdAllocUniqID(c *clientv3.Client, idKey string, count uint64) (uint64, uint64, error) {
curValue, _, err := EtcdGetKV(c, idKey)
if err != nil {
return 0, 0, err
}
//build txn, compare and set ID
var cmp clientv3.Cmp
var curr uint64 = 1
if curValue == nil {
cmp = clientv3.Compare(clientv3.CreateRevision(idKey), "=", 0)
} else {
curr = binary.BigEndian.Uint64(curValue)
cmp = clientv3.Compare(clientv3.Value(idKey), "=", string(curValue))
}
var newValue [8]byte
binary.BigEndian.PutUint64(newValue[:], curr+count)
txn := clientv3.NewKV(c).Txn(context.Background())
t := txn.If(cmp)
_, err = t.Then(clientv3.OpPut(idKey, string(newValue[:]))).Commit()
if err != nil {
return 0, 0, err
}
return curr, curr + count, nil
}