-
Notifications
You must be signed in to change notification settings - Fork 568
/
etcd_collection.go
697 lines (634 loc) · 20.6 KB
/
etcd_collection.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package collection
import (
"context"
"fmt"
"path"
"reflect"
"strings"
"sync/atomic"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/errutil"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
"github.com/pachyderm/pachyderm/v2/src/internal/tracing"
"github.com/pachyderm/pachyderm/v2/src/internal/watch"
"go.uber.org/zap"
"github.com/gogo/protobuf/proto"
"go.etcd.io/etcd/api/v3/mvccpb"
etcd "go.etcd.io/etcd/client/v3"
)
// defaultLimit was experimentally determined to be the highest value that could work
// (It gets scaled down for specific collections if they trip the max-message size.)
const (
defaultLimit int64 = 262144
DefaultPrefix string = "pachyderm/1.7.0"
indexIdentifier string = "__index_"
)
var (
// ErrNotClaimed is an error used to indicate that a different requester beat
// the current requester to a key claim.
ErrNotClaimed = errors.New("NOT_CLAIMED")
DefaultTTL = int64(30)
)
type etcdCollection struct {
etcdClient *etcd.Client
prefix string
indexes []*Index
// The limit used when listing the collection. This gets automatically
// tuned when requests fail so it's stored per collection.
limit int64
// We need this to figure out the concrete type of the objects
// that this collection is storing. Not sure what else we can
// do since types in Go are not first-class objects.
// To be clear, this is only necessary because of `Delete`, where we
// need to know the type in order to properly remove secondary indexes.
template proto.Message
// keyCheck is a function that checks if a key is valid. Invalid keys
// cannot be created.
keyCheck func(string) error
// valCheck is a function that checks if a value is valid.
valCheck func(proto.Message) error
}
// NewEtcdCollection creates a new collection backed by etcd.
func NewEtcdCollection(etcdClient *etcd.Client, prefix string, indexes []*Index, template proto.Message, keyCheck func(string) error, valCheck func(proto.Message) error) EtcdCollection {
// We want to ensure that the prefix always ends with a trailing
// slash. Otherwise, when you list the items under a collection
// such as `foo`, you might end up listing items under `foobar`
// as well.
if len(prefix) > 0 && prefix[len(prefix)-1] != '/' {
prefix = prefix + "/"
}
return &etcdCollection{
prefix: prefix,
etcdClient: etcdClient,
indexes: indexes,
limit: defaultLimit,
template: template,
keyCheck: keyCheck,
valCheck: valCheck,
}
}
func (c *etcdCollection) ReadWrite(stm STM) EtcdReadWriteCollection {
return &etcdReadWriteCollection{
etcdCollection: c,
stm: stm,
}
}
func (c *etcdCollection) ReadOnly(ctx context.Context) EtcdReadOnlyCollection {
return &etcdReadOnlyCollection{
etcdCollection: c,
ctx: ctx,
}
}
func (c *etcdCollection) Claim(ctx context.Context, key string, val proto.Message, cb func(context.Context) error) error {
var claimed bool
if _, err := NewSTM(ctx, c.etcdClient, func(stm STM) error {
readWriteC := c.ReadWrite(stm)
if err := readWriteC.Get(key, val); err != nil {
if !IsErrNotFound(err) {
return errors.EnsureStack(err)
}
claimed = true
return errors.EnsureStack(readWriteC.PutTTL(key, val, DefaultTTL))
}
claimed = false
return nil
}); err != nil {
return err
}
if !claimed {
return ErrNotClaimed
}
return c.WithRenewer(ctx, func(ctx context.Context, renewer *Renewer) error {
if err := renewer.Put(ctx, key, val); err != nil {
return err
}
return cb(ctx)
})
}
type Renewer struct {
client *etcd.Client
collection *etcdCollection
lease etcd.LeaseID
}
func newRenewer(client *etcd.Client, collection *etcdCollection, lease etcd.LeaseID) *Renewer {
return &Renewer{
client: client,
collection: collection,
lease: lease,
}
}
func (r *Renewer) Put(ctx context.Context, key string, val proto.Message) error {
_, err := NewSTM(ctx, r.client, func(stm STM) error {
// TODO: This is a bit messy, but I don't want put lease to be a part of the exported interface.
col := &etcdReadWriteCollection{
etcdCollection: r.collection,
stm: stm,
}
return col.putLease(key, val, r.lease)
})
return err
}
func (c *etcdCollection) WithRenewer(ctx context.Context, cb func(context.Context, *Renewer) error) error {
resp, err := c.etcdClient.Grant(ctx, DefaultTTL)
if err != nil {
return errors.EnsureStack(err)
}
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(pctx.Child(ctx, "WithRenewer"))
defer cancel()
keepAliveChan, err := c.etcdClient.KeepAlive(ctx, resp.ID)
if err != nil {
return errors.EnsureStack(err)
}
go func() {
for {
_, more := <-keepAliveChan
if !more {
if ctx.Err() == nil {
log.Error(ctx, "failed to renew etcd lease", zap.Stack("stacktrace"))
cancel()
}
return
}
}
}()
renewer := newRenewer(c.etcdClient, c, resp.ID)
return cb(ctx, renewer)
}
// path returns the full path of a key in the etcd namespace
func (c *etcdCollection) path(key string) string {
return path.Join(c.prefix, key)
}
func (c *etcdCollection) indexRoot(index *Index) string {
// remove trailing slash from c.prefix
return fmt.Sprintf("%s%s%s/",
strings.TrimRight(c.prefix, "/"), indexIdentifier, index.Name)
}
// See the documentation for `Index` for details.
func (c *etcdCollection) indexDir(index *Index, indexVal string) string {
return path.Join(c.indexRoot(index), indexVal)
}
// See the documentation for `Index` for details.
func (c *etcdCollection) indexPath(index *Index, indexVal string, key string) string {
return path.Join(c.indexDir(index, indexVal), key)
}
type etcdReadWriteCollection struct {
*etcdCollection
stm STM
}
func (c *etcdReadWriteCollection) Get(maybeKey interface{}, val proto.Message) (retErr error) {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
span, _ := tracing.AddSpanToAnyExisting(c.stm.Context(), "/etcd.RW/Get",
"col", c.prefix, "key", strings.TrimPrefix(key, c.prefix))
defer func() {
tracing.TagAnySpan(span, "err", retErr)
tracing.FinishAnySpan(span)
}()
if err := watch.CheckType(c.template, val); err != nil {
return err
}
valStr, err := c.stm.Get(c.path(key))
if err != nil {
if IsErrNotFound(err) {
return ErrNotFound{Type: c.prefix, Key: key}
}
return errors.EnsureStack(err)
}
c.stm.SetSafePutCheck(c.path(key), reflect.ValueOf(val).Pointer())
return errors.EnsureStack(proto.Unmarshal([]byte(valStr), val))
}
func cloneProtoMsg(original proto.Message) proto.Message {
val := reflect.ValueOf(original)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
return reflect.New(val.Type()).Interface().(proto.Message)
}
// Giving a value, an index, and the key of the item, return the path
// under which the new index item should be stored.
func (c *etcdReadWriteCollection) getIndexPath(val proto.Message, index *Index, key string) string {
return c.indexPath(index, index.Extract(val), key)
}
func (c *etcdReadWriteCollection) Put(maybeKey interface{}, val proto.Message) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
return c.PutTTL(key, val, 0)
}
func (c *etcdReadWriteCollection) TTL(key string) (int64, error) {
ttl, err := c.stm.TTL(c.path(key))
if IsErrNotFound(err) {
return ttl, ErrNotFound{Type: c.prefix, Key: key}
}
return ttl, errors.EnsureStack(err)
}
func (c *etcdReadWriteCollection) PutTTL(key string, val proto.Message, ttl int64) error {
return c.put(key, val, func(key string, val string, ptr uintptr) error {
return errors.EnsureStack(c.stm.Put(key, val, ttl, ptr))
})
}
func (c *etcdReadWriteCollection) put(key string, val proto.Message, putFunc func(string, string, uintptr) error) error {
if strings.Contains(key, indexIdentifier) {
return errors.Errorf("cannot put key %q which contains reserved string %q", key, indexIdentifier)
}
if err := watch.CheckType(c.template, val); err != nil {
return err
}
if c.keyCheck != nil {
if err := c.keyCheck(key); err != nil {
return err
}
}
if c.valCheck != nil {
if err := c.valCheck(val); err != nil {
return err
}
}
ptr := reflect.ValueOf(val).Pointer()
if !c.stm.IsSafePut(c.path(key), ptr) {
return errors.Errorf("unsafe put for key %v (passed ptr did not receive updated value)", key)
}
if c.indexes != nil {
clone := cloneProtoMsg(val)
// Put the appropriate record in any of c's secondary indexes
for _, index := range c.indexes {
indexPath := c.getIndexPath(val, index, key)
// If we can get the original value, we remove the original indexes
if err := c.Get(key, clone); err == nil {
originalIndexPath := c.getIndexPath(clone, index, key)
if originalIndexPath != indexPath {
c.stm.Del(originalIndexPath)
}
}
// Put the index even if it already exists, so that watchers may be
// notified that the value has been updated.
if err := putFunc(indexPath, key, 0); err != nil {
return err
}
}
}
bytes, err := proto.Marshal(val)
if err != nil {
return errors.EnsureStack(err)
}
return putFunc(c.path(key), string(bytes), ptr)
}
func (c *etcdReadWriteCollection) putLease(key string, val proto.Message, lease etcd.LeaseID) error {
return c.put(key, val, func(key string, val string, ptr uintptr) error {
return errors.EnsureStack(c.stm.PutLease(key, val, lease, ptr))
})
}
func (c *etcdReadWriteCollection) putIgnoreLease(key string, val proto.Message) error {
return c.put(key, val, func(key string, val string, ptr uintptr) error {
return errors.EnsureStack(c.stm.PutIgnoreLease(key, val, ptr))
})
}
// Update reads the current value associated with 'key', calls 'f' to update
// the value, and writes the new value back to the collection. 'key' must be
// present in the collection, or a 'Not Found' error is returned
func (c *etcdReadWriteCollection) Update(maybeKey interface{}, val proto.Message, f func() error) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
if err := watch.CheckType(c.template, val); err != nil {
return err
}
if err := c.Get(key, val); err != nil {
if IsErrNotFound(err) {
return ErrNotFound{Type: c.prefix, Key: key}
}
return err
}
if err := f(); err != nil {
return err
}
return c.putIgnoreLease(key, val)
}
// Upsert is like Update but 'key' is not required to be present
func (c *etcdReadWriteCollection) Upsert(maybeKey interface{}, val proto.Message, f func() error) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
if err := watch.CheckType(c.template, val); err != nil {
return err
}
if err := c.Get(key, val); err != nil && !IsErrNotFound(err) {
return err
}
if err := f(); err != nil {
return err
}
return c.Put(key, val)
}
func (c *etcdReadWriteCollection) Create(maybeKey interface{}, val proto.Message) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
if err := watch.CheckType(c.template, val); err != nil {
return err
}
fullKey := c.path(key)
_, err := c.stm.Get(fullKey)
if err != nil && !IsErrNotFound(err) {
return errors.EnsureStack(err)
}
if err == nil {
return ErrExists{Type: c.prefix, Key: key}
}
return c.Put(key, val)
}
func (c *etcdReadWriteCollection) Delete(maybeKey interface{}) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
fullKey := c.path(key)
if _, err := c.stm.Get(fullKey); err != nil {
return errors.EnsureStack(err)
}
if c.indexes != nil && c.template != nil {
val := proto.Clone(c.template)
for _, index := range c.indexes {
if err := c.Get(key, val); err == nil {
indexPath := c.getIndexPath(val, index, key)
c.stm.Del(indexPath)
}
}
}
c.stm.Del(fullKey)
return nil
}
func (c *etcdReadWriteCollection) DeleteAll() error {
// Delete indexes
for _, index := range c.indexes {
c.stm.DelAll(c.indexRoot(index))
}
c.stm.DelAll(c.prefix)
return nil
}
func (c *etcdReadWriteCollection) DeleteAllPrefix(prefix string) error {
c.stm.DelAll(path.Join(c.prefix, prefix) + "/")
return nil
}
type etcdReadOnlyCollection struct {
*etcdCollection
ctx context.Context
}
// get is an internal wrapper around etcdClient.Get that wraps the call in a
// trace
func (c *etcdReadOnlyCollection) get(key string, opts ...etcd.OpOption) (resp *etcd.GetResponse, retErr error) {
span, ctx := tracing.AddSpanToAnyExisting(c.ctx, "/etcd.RO/Get",
"col", c.prefix, "key", strings.TrimPrefix(key, c.prefix))
defer func() {
tracing.TagAnySpan(span, "err", retErr)
tracing.FinishAnySpan(span)
}()
resp, err := c.etcdClient.Get(ctx, key, opts...)
return resp, errors.EnsureStack(err)
}
func (c *etcdReadOnlyCollection) Get(maybeKey interface{}, val proto.Message) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
if err := watch.CheckType(c.template, val); err != nil {
return err
}
resp, err := c.get(c.path(key))
if err != nil {
return err
}
if len(resp.Kvs) == 0 {
return ErrNotFound{Type: c.prefix, Key: key}
}
return errors.EnsureStack(proto.Unmarshal(resp.Kvs[0].Value, val))
}
func (c *etcdReadOnlyCollection) GetByIndex(index *Index, indexVal string, val proto.Message, opts *Options, f func(key string) error) error {
span, _ := tracing.AddSpanToAnyExisting(c.ctx, "/etcd.RO/GetByIndex", "col", c.prefix, "index", index, "indexVal", indexVal)
defer tracing.FinishAnySpan(span)
if atomic.LoadInt64(&index.limit) == 0 {
atomic.CompareAndSwapInt64(&index.limit, 0, defaultLimit)
}
return c.list(c.indexDir(index, indexVal), &index.limit, opts, func(kv *mvccpb.KeyValue) error {
key := path.Base(string(kv.Key))
if err := c.Get(key, val); err != nil {
if IsErrNotFound(err) {
// In cases where we changed how certain objects are
// indexed, we could end up in a situation where the
// object is deleted but the old indexes still exist.
return nil
}
return err
}
return f(key)
})
}
func (c *etcdReadOnlyCollection) TTL(key string) (int64, error) {
resp, err := c.get(c.path(key))
if err != nil {
return 0, err
}
if len(resp.Kvs) == 0 {
return 0, ErrNotFound{Type: c.prefix, Key: key}
}
leaseID := etcd.LeaseID(resp.Kvs[0].Lease)
span, ctx := tracing.AddSpanToAnyExisting(c.ctx, "/etcd.RO/TimeToLive")
defer tracing.FinishAnySpan(span)
leaseTTLResp, err := c.etcdClient.TimeToLive(ctx, leaseID)
if err != nil {
return 0, errors.Wrapf(err, "could not fetch lease TTL")
}
return leaseTTLResp.TTL, nil
}
// List returns objects sorted based on the options passed in. f will be called
// for each item, val will contain the corresponding value. Val is not an
// argument to f because that would require f to perform a cast before it could
// be used.
// You can break out of iteration by returning errutil.ErrBreak.
func (c *etcdReadOnlyCollection) List(val proto.Message, opts *Options, f func(key string) error) error {
span, _ := tracing.AddSpanToAnyExisting(c.ctx, "/etcd.RO/List", "col", c.prefix)
defer tracing.FinishAnySpan(span)
if err := watch.CheckType(c.template, val); err != nil {
return err
}
return c.list(c.prefix, &c.limit, opts, func(kv *mvccpb.KeyValue) error {
if err := proto.Unmarshal(kv.Value, val); err != nil {
return errors.EnsureStack(err)
}
return f(strings.TrimPrefix(string(kv.Key), c.prefix))
})
}
// ListRev returns objects sorted based on the options passed in. f will be called
// with each key and the create-revision of the key, val will contain the
// corresponding value. Val is not an argument to f because that would require
// f to perform a cast before it could be used. You can break out of iteration
// by returning errutil.ErrBreak.
func (c *etcdReadOnlyCollection) ListRev(val proto.Message, opts *Options, f func(key string, createRev int64) error) error {
span, _ := tracing.AddSpanToAnyExisting(c.ctx, "/etcd.RO/List", "col", c.prefix)
defer tracing.FinishAnySpan(span)
if err := watch.CheckType(c.template, val); err != nil {
return err
}
return c.list(c.prefix, &c.limit, opts, func(kv *mvccpb.KeyValue) error {
if err := proto.Unmarshal(kv.Value, val); err != nil {
return errors.EnsureStack(err)
}
return f(strings.TrimPrefix(string(kv.Key), c.prefix), kv.CreateRevision)
})
}
func (c *etcdReadOnlyCollection) list(prefix string, limitPtr *int64, opts *Options, f func(*mvccpb.KeyValue) error) error {
return listRevision(c, prefix, limitPtr, opts, f)
}
var (
countOpts = []etcd.OpOption{etcd.WithPrefix(), etcd.WithCountOnly()}
)
func (c *etcdReadOnlyCollection) Count() (int64, error) {
resp, err := c.get(c.prefix, countOpts...)
if err != nil {
return 0, err
}
return resp.Count, err
}
func (c *etcdReadOnlyCollection) CountRev(rev int64) (int64, int64, error) {
resp, err := c.get(c.prefix, append(countOpts, etcd.WithRev(rev))...)
if err != nil {
return 0, 0, err
}
return resp.Count, resp.Header.Revision, err
}
// Watch a collection, returning the current content of the collection as
// well as any future additions.
func (c *etcdReadOnlyCollection) Watch(opts ...watch.Option) (watch.Watcher, error) {
return watch.NewEtcdWatcher(c.ctx, c.etcdClient, c.prefix, c.prefix, c.template, opts...)
}
// WatchF watches a collection and executes a callback function each time an event occurs.
func (c *etcdReadOnlyCollection) WatchF(f func(e *watch.Event) error, opts ...watch.Option) error {
watcher, err := c.Watch(opts...)
if err != nil {
return err
}
defer watcher.Close()
return watchF(c.ctx, watcher, f)
}
func watchF(ctx context.Context, watcher watch.Watcher, f func(e *watch.Event) error) error {
for {
select {
case e, ok := <-watcher.Watch():
if !ok {
return nil
}
if e.Type == watch.EventError {
return e.Err
}
if err := f(e); err != nil {
if errors.Is(err, errutil.ErrBreak) {
return nil
}
return err
}
case <-ctx.Done():
return errors.EnsureStack(ctx.Err())
}
}
}
// WatchByIndex watches items in a collection that match a particular index
func (c *etcdReadOnlyCollection) WatchByIndex(index *Index, val string, opts ...watch.Option) (watch.Watcher, error) {
eventCh := make(chan *watch.Event)
done := make(chan struct{})
watcher, err := watch.NewEtcdWatcher(c.ctx, c.etcdClient, c.prefix, c.indexDir(index, val), c.template, opts...)
if err != nil {
return nil, err
}
go func() {
watchForEvents := func() error {
for {
var ev *watch.Event
var ok bool
select {
case ev, ok = <-watcher.Watch():
case <-done:
watcher.Close()
return nil
}
if !ok {
watcher.Close()
return nil
}
var directEv *watch.Event
switch ev.Type {
case watch.EventError:
// pass along the error
return ev.Err
case watch.EventPut:
resp, err := c.get(c.path(path.Base(string(ev.Key))))
if err != nil {
return err
}
if len(resp.Kvs) == 0 {
// this happens only if the item was deleted shortly after
// we receive this event.
continue
}
directEv = &watch.Event{
Key: []byte(path.Base(string(ev.Key))),
Value: resp.Kvs[0].Value,
Type: ev.Type,
Template: c.template,
}
case watch.EventDelete:
directEv = &watch.Event{
Key: []byte(path.Base(string(ev.Key))),
Type: ev.Type,
Template: c.template,
}
}
eventCh <- directEv
}
}
defer close(eventCh)
if err := watchForEvents(); err != nil {
eventCh <- &watch.Event{Type: watch.EventError, Err: err}
watcher.Close()
}
}()
return watch.MakeEtcdWatcher(eventCh, done), nil
}
func (c *etcdReadOnlyCollection) WatchByIndexF(index *Index, indexVal string, f func(e *watch.Event) error, opts ...watch.Option) error {
watcher, err := c.WatchByIndex(index, indexVal, opts...)
if err != nil {
return err
}
defer watcher.Close()
return watchF(c.ctx, watcher, f)
}
// WatchOne watches a given item. The first value returned from the watch
// will be the current value of the item.
func (c *etcdReadOnlyCollection) WatchOne(maybeKey interface{}, opts ...watch.Option) (watch.Watcher, error) {
key, ok := maybeKey.(string)
if !ok {
return nil, errors.New("key must be a string")
}
return watch.NewEtcdWatcher(c.ctx, c.etcdClient, c.prefix, c.path(key), c.template, opts...)
}
// WatchOneF watches a given item and executes a callback function each time an event occurs.
// The first value returned from the watch will be the current value of the item.
func (c *etcdReadOnlyCollection) WatchOneF(maybeKey interface{}, f func(e *watch.Event) error, opts ...watch.Option) error {
key, ok := maybeKey.(string)
if !ok {
return errors.New("key must be a string")
}
watcher, err := watch.NewEtcdWatcher(c.ctx, c.etcdClient, c.prefix, c.path(key), c.template, opts...)
if err != nil {
return err
}
defer watcher.Close()
return watchF(c.ctx, watcher, f)
}