forked from smallnest/libkv-etcdv3-store
/
etcdv3.go
411 lines (376 loc) · 9.39 KB
/
etcdv3.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
package etcdv3
import (
// "context"
"errors"
"strings"
"time"
"github.com/docker/libkv"
"github.com/docker/libkv/store"
// "go.etcd.io/etcd/clientv3"
// "go.etcd.io/etcd/clientv3/concurrency"
)
const (
// ETCDV3 backend
ETCDV3 store.Backend = "etcdv3"
)
// EtcdV3 is the receiver type for the Store interface
type EtcdV3 struct {
timeout time.Duration
// client *clientv3.Client
//leaseID clientv3.LeaseID
//cfg clientv3.Config
done chan struct{}
startKeepAlive chan struct{}
}
// Register registers etcd to libkv
func Register() {
libkv.AddStore(ETCDV3, New)
}
// New creates a new Etcd client given a list
// of endpoints and an optional tls config
func New(addrs []string, options *store.Config) (store.Store, error) {
s := &EtcdV3{
done: make(chan struct{}),
startKeepAlive: make(chan struct{}),
}
// cfg := clientv3.Config{
// Endpoints: addrs,
// }
//
// if options != nil {
// s.timeout = options.ConnectionTimeout
// cfg.DialTimeout = options.ConnectionTimeout
// cfg.DialKeepAliveTimeout = options.ConnectionTimeout
// cfg.TLS = options.TLS
// cfg.Username = options.Username
// cfg.Password = options.Password
//
// cfg.AutoSyncInterval = 5 * time.Minute
// }
// if s.timeout == 0 {
// s.timeout = 10 * time.Second
// }
// s.cfg = cfg
// err := s.init()
// if err != nil {
// return nil, err
// }
//
// go func() {
// <-s.startKeepAlive
//
// var ch <-chan *clientv3.LeaseKeepAliveResponse
// var kaerr error
// rekeepalive:
// cli := s.client
// for {
// if s.leaseID != 0 {
// ch, kaerr = cli.KeepAlive(context.Background(), s.leaseID)
// }
// if kaerr == nil {
// break
// }
// time.Sleep(time.Second)
// }
//
// for {
// select {
// case <-s.done:
// return
// case resp := <-ch:
// if resp == nil { // connection is closed
// cli.Close()
// for {
// select {
// case <-s.done:
// return
// default:
// err = s.init()
// if err != nil {
// time.Sleep(time.Second)
// continue
// }
// goto rekeepalive
// }
//
// }
//
// }
// }
// }
// }()
return s, nil
}
func (s *EtcdV3) init() error {
// cli, err := clientv3.New(s.cfg)
// if err != nil {
// return err
// }
//
// s.client = cli
return nil
}
func (s *EtcdV3) normalize(key string) string {
key = store.Normalize(key)
return strings.TrimPrefix(key, "/")
}
func (s *EtcdV3) grant(ttl int64) error {
// resp, err := s.client.Grant(context.Background(), ttl)
// if err == nil {
// s.leaseID = resp.ID
// }
// return err
return nil
}
// Put a value at the specified key
func (s *EtcdV3) Put(key string, value []byte, options *store.WriteOptions) error {
// ttl := int64(options.TTL.Seconds())
// if ttl == 0 {
// ttl = 30
// }
//
// // init leaseID
// if s.leaseID == 0 {
// err := s.grant(ttl)
// if err != nil {
// return err
// }
// close(s.startKeepAlive)
// }
//
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// _, err := s.client.Put(ctx, key, string(value), clientv3.WithLease(s.leaseID))
// cancel()
//
// // try
// if err != nil && strings.Contains(err.Error(), "requested lease not found") {
// err := s.grant(ttl)
// if err != nil {
// return err
// }
// }
//
// // reput with leaseID
// ctx, cancel = context.WithTimeout(context.Background(), s.timeout)
// _, err = s.client.Put(ctx, key, string(value), clientv3.WithLease(s.leaseID))
// cancel()
//
// return err
return nil
}
// Get a value given its key
func (s *EtcdV3) Get(key string) (*store.KVPair, error) {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// resp, err := s.client.Get(ctx, key)
// cancel()
// if err != nil {
// return nil, err
// }
// if len(resp.Kvs) == 0 {
// return nil, store.ErrKeyNotFound
// }
pair := &store.KVPair{
Key: key,
Value: nil,
LastIndex: 0,
}
return pair, nil
}
// Delete the value at the specified key
func (s *EtcdV3) Delete(key string) error {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// _, err := s.client.Delete(ctx, key)
// cancel()
//
// return err
return nil
}
// Exists verifies if a Key exists in the store
func (s *EtcdV3) Exists(key string) (bool, error) {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// resp, err := s.client.Get(ctx, key)
// cancel()
// if err != nil {
// return false, err
// }
//
// return len(resp.Kvs) != 0, nil
return true, nil
}
// Watch for changes on a key
func (s *EtcdV3) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
watchCh := make(chan *store.KVPair)
// go func() {
// defer close(watchCh)
//
// pair, err := s.Get(key)
// if err != nil {
// return
// }
// watchCh <- pair
//
// rch := s.client.Watch(context.Background(), key)
// for {
// select {
// case <-s.done:
// return
// case wresp := <-rch:
// for _, event := range wresp.Events {
// watchCh <- &store.KVPair{
// Key: string(event.Kv.Key),
// Value: event.Kv.Value,
// LastIndex: uint64(event.Kv.Version),
// }
// }
// }
// }
// }()
return watchCh, nil
}
// WatchTree watches for changes on child nodes under
// a given directory
func (s *EtcdV3) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
watchCh := make(chan []*store.KVPair)
// go func() {
// defer close(watchCh)
//
// list, err := s.List(directory)
// if err != nil {
// return
// }
//
// watchCh <- list
//
// rch := s.client.Watch(context.Background(), directory, clientv3.WithPrefix())
// for {
// select {
// case <-s.done:
// return
// case <-rch:
// list, err := s.List(directory)
// if err != nil {
// return
// }
// watchCh <- list
// }
// }
// }()
return watchCh, nil
}
type etcdLock struct {
// session *concurrency.Session
// mutex *concurrency.Mutex
}
// NewLock creates a lock for a given key.
// The returned Locker is not held and must be acquired
// with `.Lock`. The Value is optional.
func (s *EtcdV3) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
return nil, errors.New("not implemented")
}
// List the content of a given prefix
func (s *EtcdV3) List(directory string) ([]*store.KVPair, error) {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// defer cancel()
//
// resp, err := s.client.Get(ctx, directory, clientv3.WithPrefix())
// if err != nil {
// return nil, err
// }
//
// kvpairs := make([]*store.KVPair, 0, len(resp.Kvs))
//
// if len(resp.Kvs) == 0 {
// return kvpairs, nil
// }
// for _, kv := range resp.Kvs {
// pair := &store.KVPair{
// Key: string(kv.Key),
// Value: kv.Value,
// LastIndex: uint64(kv.Version),
// }
// kvpairs = append(kvpairs, pair)
// }
return nil, nil
}
// DeleteTree deletes a range of keys under a given directory
func (s *EtcdV3) DeleteTree(directory string) error {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// _, err := s.client.Delete(ctx, directory, clientv3.WithPrefix())
// cancel()
//
// return err
return nil
}
// AtomicPut CAS operation on a single value.
// Pass previous = nil to create a new key.
func (s *EtcdV3) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
//
// var revision int64
// var presp *clientv3.PutResponse
// var txresp *clientv3.TxnResponse
// var err error
// if previous == nil {
// presp, err = s.client.Put(ctx, key, string(value), clientv3.WithLease(s.leaseID))
// if presp != nil {
// revision = presp.Header.GetRevision()
// }
// } else {
//
// var cmps = []clientv3.Cmp{
// clientv3.Compare(clientv3.Value("key"), "=", string(previous.Value)),
// clientv3.Compare(clientv3.Version("key"), "=", int64(previous.LastIndex)),
// }
// txresp, err = s.client.Txn(ctx).If(cmps...).
// Then(clientv3.OpPut(key, string(value))).
// Commit()
// if txresp != nil {
// revision = txresp.Header.GetRevision()
// }
// }
// cancel()
//
// if err != nil {
// return false, nil, err
// }
pair := &store.KVPair{
Key: key,
Value: value,
LastIndex: 0,
}
return true, pair, nil
}
// AtomicDelete cas deletes a single value
func (s *EtcdV3) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
var deleted = false
// var err error
// var txresp *clientv3.TxnResponse
// ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
// if previous == nil {
// var dresp *clientv3.DeleteResponse
// dresp, err = s.client.Delete(ctx, key)
// deleted = len(dresp.PrevKvs) != 0
// } else {
// // dresp, err = s.client.Delete(ctx, key, clientv3.WithRev(int64(previous.LastIndex)))
//
// var cmps = []clientv3.Cmp{
// clientv3.Compare(clientv3.Value("key"), "=", string(previous.Value)),
// clientv3.Compare(clientv3.Version("key"), "=", int64(previous.LastIndex)),
// }
// txresp, err = s.client.Txn(ctx).If(cmps...).
// Then(clientv3.OpDelete(key)).
// Commit()
// deleted = txresp.Succeeded
// }
// cancel()
//
// if err != nil {
// return false, err
// }
return deleted, nil
}
// Close closes the client connection
func (s *EtcdV3) Close() {
// close(s.done)
// s.client.Close()
}