/
etcd.go
335 lines (276 loc) · 8.24 KB
/
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
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"context"
"crypto/tls"
"fmt"
"path"
"strings"
"time"
"github.com/pingcap/errors"
clientv3 "go.etcd.io/etcd/client/v3"
)
// Node organizes the ectd query result as a Trie tree
type Node struct {
Value []byte
Childs map[string]*Node
}
// OpType is operation's type in etcd
type OpType string
var (
// CreateOp is create operation type
CreateOp OpType = "create"
// UpdateOp is update operation type
UpdateOp OpType = "update"
// DeleteOp is delete operation type
DeleteOp OpType = "delete"
)
// Operation represents an operation in etcd, include create, update and delete.
type Operation struct {
Tp OpType
Key string
Value string
TTL int64
WithPrefix bool
Opts []clientv3.OpOption
}
// String implements Stringer interface.
func (o *Operation) String() string {
return fmt.Sprintf("{Tp: %s, Key: %s, Value: %s, TTL: %d, WithPrefix: %v, Opts: %v}", o.Tp, o.Key, o.Value, o.TTL, o.WithPrefix, o.Opts)
}
// Client is a wrapped etcd client that support some simple method
type Client struct {
client *clientv3.Client
rootPath string
}
// NewClient returns a wrapped etcd client
func NewClient(cli *clientv3.Client, root string) *Client {
return &Client{
client: cli,
rootPath: root,
}
}
// NewClientFromCfg returns a wrapped etcd client
func NewClientFromCfg(endpoints []string, dialTimeout time.Duration, root string, security *tls.Config) (*Client, error) {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
TLS: security,
})
if err != nil {
return nil, errors.Trace(err)
}
return &Client{
client: cli,
rootPath: root,
}, nil
}
// Close shutdowns the connection to etcd
func (e *Client) Close() error {
if err := e.client.Close(); err != nil {
return errors.Trace(err)
}
return nil
}
// GetClient returns client
func (e *Client) GetClient() *clientv3.Client {
return e.client
}
// Create guarantees to set a key = value with some options(like ttl)
func (e *Client) Create(ctx context.Context, key string, val string, opts []clientv3.OpOption) (int64, error) {
key = keyWithPrefix(e.rootPath, key)
txnResp, err := e.client.KV.Txn(ctx).If(
clientv3.Compare(clientv3.ModRevision(key), "=", 0),
).Then(
clientv3.OpPut(key, val, opts...),
).Commit()
if err != nil {
return 0, errors.Trace(err)
}
if !txnResp.Succeeded {
return 0, errors.AlreadyExistsf("key %s in etcd", key)
}
if txnResp.Header != nil {
return txnResp.Header.Revision, nil
}
// impossible to happen
return 0, errors.New("revision is unknown")
}
// Get returns a key/value matchs the given key
func (e *Client) Get(ctx context.Context, key string) (value []byte, revision int64, err error) {
key = keyWithPrefix(e.rootPath, key)
resp, err := e.client.KV.Get(ctx, key)
if err != nil {
return nil, -1, errors.Trace(err)
}
if len(resp.Kvs) == 0 {
return nil, -1, errors.NotFoundf("key %s in etcd", key)
}
return resp.Kvs[0].Value, resp.Header.Revision, nil
}
// Update updates a key/value.
// set ttl 0 to disable the Lease ttl feature
func (e *Client) Update(ctx context.Context, key string, val string, ttl int64) error {
key = keyWithPrefix(e.rootPath, key)
var opts []clientv3.OpOption
if ttl > 0 {
lcr, err := e.client.Lease.Grant(ctx, ttl)
if err != nil {
return errors.Trace(err)
}
opts = []clientv3.OpOption{clientv3.WithLease(lcr.ID)}
}
txnResp, err := e.client.KV.Txn(ctx).If(
clientv3.Compare(clientv3.ModRevision(key), ">", 0),
).Then(
clientv3.OpPut(key, val, opts...),
).Commit()
if err != nil {
return errors.Trace(err)
}
if !txnResp.Succeeded {
return errors.NotFoundf("key %s in etcd", key)
}
return nil
}
// UpdateOrCreate updates a key/value, if the key does not exist then create, or update
func (e *Client) UpdateOrCreate(ctx context.Context, key string, val string, ttl int64) error {
key = keyWithPrefix(e.rootPath, key)
var opts []clientv3.OpOption
if ttl > 0 {
lcr, err := e.client.Lease.Grant(ctx, ttl)
if err != nil {
return errors.Trace(err)
}
opts = []clientv3.OpOption{clientv3.WithLease(lcr.ID)}
}
_, err := e.client.KV.Do(ctx, clientv3.OpPut(key, val, opts...))
if err != nil {
return errors.Trace(err)
}
return nil
}
// List returns the trie struct that constructed by the key/value with same prefix
func (e *Client) List(ctx context.Context, key string) (node *Node, revision int64, err error) {
key = keyWithPrefix(e.rootPath, key)
if !strings.HasSuffix(key, "/") {
key += "/"
}
resp, err := e.client.KV.Get(ctx, key, clientv3.WithPrefix())
if err != nil {
return nil, -1, errors.Trace(err)
}
root := new(Node)
length := len(key)
for _, kv := range resp.Kvs {
key := string(kv.Key)
if len(key) <= length {
continue
}
keyTail := key[length:]
tailNode := parseToDirTree(root, keyTail)
tailNode.Value = kv.Value
}
return root, resp.Header.Revision, nil
}
// Delete deletes the key/values with matching prefix or key
func (e *Client) Delete(ctx context.Context, key string, withPrefix bool) error {
key = keyWithPrefix(e.rootPath, key)
var opts []clientv3.OpOption
if withPrefix {
opts = []clientv3.OpOption{clientv3.WithPrefix()}
}
_, err := e.client.KV.Delete(ctx, key, opts...)
if err != nil {
return errors.Trace(err)
}
return nil
}
// Watch watchs the events of key with prefix.
func (e *Client) Watch(ctx context.Context, prefix string, revision int64) clientv3.WatchChan {
if revision > 0 {
return e.client.Watch(ctx, prefix, clientv3.WithPrefix(), clientv3.WithRev(revision))
}
return e.client.Watch(ctx, prefix, clientv3.WithPrefix())
}
// DoTxn does some operation in one transaction.
// Note: should only have one opereration for one key, otherwise will get duplicate key error.
func (e *Client) DoTxn(ctx context.Context, operations []*Operation) (int64, error) {
cmps := make([]clientv3.Cmp, 0, len(operations))
ops := make([]clientv3.Op, 0, len(operations))
for _, operation := range operations {
operation.Key = keyWithPrefix(e.rootPath, operation.Key)
if operation.TTL > 0 {
if operation.Tp == DeleteOp {
return 0, errors.Errorf("unexpected TTL in delete operation")
}
lcr, err := e.client.Lease.Grant(ctx, operation.TTL)
if err != nil {
return 0, errors.Trace(err)
}
operation.Opts = append(operation.Opts, clientv3.WithLease(lcr.ID))
}
if operation.WithPrefix {
operation.Opts = append(operation.Opts, clientv3.WithPrefix())
}
switch operation.Tp {
case CreateOp:
cmps = append(cmps, clientv3.Compare(clientv3.ModRevision(operation.Key), "=", 0))
ops = append(ops, clientv3.OpPut(operation.Key, operation.Value, operation.Opts...))
case UpdateOp:
cmps = append(cmps, clientv3.Compare(clientv3.ModRevision(operation.Key), ">", 0))
ops = append(ops, clientv3.OpPut(operation.Key, operation.Value, operation.Opts...))
case DeleteOp:
ops = append(ops, clientv3.OpDelete(operation.Key, operation.Opts...))
default:
return 0, errors.Errorf("unknown operation type %s", operation.Tp)
}
}
txnResp, err := e.client.KV.Txn(ctx).If(
cmps...,
).Then(
ops...,
).Commit()
if err != nil {
return 0, errors.Trace(err)
}
if !txnResp.Succeeded {
return 0, errors.Errorf("do transaction failed, operations: %+v", operations)
}
return txnResp.Header.Revision, nil
}
func parseToDirTree(root *Node, path string) *Node {
pathDirs := strings.Split(path, "/")
current := root
var next *Node
var ok bool
for _, dir := range pathDirs {
if current.Childs == nil {
current.Childs = make(map[string]*Node)
}
next, ok = current.Childs[dir]
if !ok {
current.Childs[dir] = new(Node)
next = current.Childs[dir]
}
current = next
}
return current
}
func keyWithPrefix(prefix, key string) string {
if strings.HasPrefix(key, prefix) {
return key
}
return path.Join(prefix, key)
}