/
agentops_crud.go
1062 lines (901 loc) · 26.4 KB
/
agentops_crud.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gocbcore
import (
"encoding/binary"
"regexp"
"sync"
"github.com/opentracing/opentracing-go"
)
var vbTakeoverRegex = regexp.MustCompile(`dcp-vbtakeover (\d+$|\d+ \S+)`)
// GetOptions encapsulates the parameters for a GetEx operation.
type GetOptions struct {
Key []byte
TraceContext opentracing.SpanContext
}
// GetResult encapsulates the result of a GetEx operation.
type GetResult struct {
Value []byte
Flags uint32
Datatype uint8
Cas Cas
}
// GetExCallback is invoked upon completion of a GetEx operation.
type GetExCallback func(*GetResult, error)
// GetEx retrieves a document.
func (agent *Agent) GetEx(opts GetOptions, cb GetExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetEx", opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Extras) != 4 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
res := GetResult{}
res.Value = resp.Value
res.Flags = binary.BigEndian.Uint32(resp.Extras[0:])
res.Cas = Cas(resp.Cas)
res.Datatype = resp.Datatype
tracer.Finish()
cb(&res, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdGet,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// GetAndTouchOptions encapsulates the parameters for a GetAndTouchEx operation.
type GetAndTouchOptions struct {
Key []byte
Expiry uint32
TraceContext opentracing.SpanContext
}
// GetAndTouchResult encapsulates the result of a GetAndTouchEx operation.
type GetAndTouchResult struct {
Value []byte
Flags uint32
Datatype uint8
Cas Cas
}
// GetAndTouchExCallback is invoked upon completion of a GetAndTouchEx operation.
type GetAndTouchExCallback func(*GetAndTouchResult, error)
// GetAndTouchEx retrieves a document and updates its expiry.
func (agent *Agent) GetAndTouchEx(opts GetAndTouchOptions, cb GetAndTouchExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetAndTouchEx", opts.TraceContext)
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Extras) != 4 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
flags := binary.BigEndian.Uint32(resp.Extras[0:])
tracer.Finish()
cb(&GetAndTouchResult{
Value: resp.Value,
Flags: flags,
Cas: Cas(resp.Cas),
Datatype: resp.Datatype,
}, nil)
}
extraBuf := make([]byte, 4)
binary.BigEndian.PutUint32(extraBuf[0:], opts.Expiry)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdGAT,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: nil,
},
Callback: handler,
}
return agent.dispatchOp(req)
}
// GetAndLockOptions encapsulates the parameters for a GetAndLockEx operation.
type GetAndLockOptions struct {
Key []byte
LockTime uint32
TraceContext opentracing.SpanContext
}
// GetAndLockResult encapsulates the result of a GetAndLockEx operation.
type GetAndLockResult struct {
Value []byte
Flags uint32
Datatype uint8
Cas Cas
}
// GetAndLockExCallback is invoked upon completion of a GetAndLockEx operation.
type GetAndLockExCallback func(*GetAndLockResult, error)
// GetAndLockEx retrieves a document and locks it.
func (agent *Agent) GetAndLockEx(opts GetAndLockOptions, cb GetAndLockExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetAndLockEx", opts.TraceContext)
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Extras) != 4 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
flags := binary.BigEndian.Uint32(resp.Extras[0:])
tracer.Finish()
cb(&GetAndLockResult{
Value: resp.Value,
Flags: flags,
Cas: Cas(resp.Cas),
Datatype: resp.Datatype,
}, nil)
}
extraBuf := make([]byte, 4)
binary.BigEndian.PutUint32(extraBuf[0:], opts.LockTime)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdGetLocked,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// GetReplicaOptions encapsulates the parameters for a GetReplicaEx operation.
type GetReplicaOptions struct {
Key []byte
ReplicaIdx int
TraceContext opentracing.SpanContext
}
// GetReplicaResult encapsulates the result of a GetReplicaEx operation.
type GetReplicaResult struct {
Value []byte
Flags uint32
Datatype uint8
Cas Cas
}
// GetReplicaExCallback is invoked upon completion of a GetReplicaEx operation.
type GetReplicaExCallback func(*GetReplicaResult, error)
func (agent *Agent) getOneReplica(tracer *opTracer, opts GetReplicaOptions, cb GetReplicaExCallback) (PendingOp, error) {
if opts.ReplicaIdx <= 0 {
return nil, ErrInvalidReplica
}
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
cb(nil, err)
return
}
if len(resp.Extras) != 4 {
cb(nil, ErrProtocol)
return
}
flags := binary.BigEndian.Uint32(resp.Extras[0:])
cb(&GetReplicaResult{
Value: resp.Value,
Flags: flags,
Cas: Cas(resp.Cas),
Datatype: resp.Datatype,
}, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdGetReplica,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: opts.Key,
Value: nil,
},
Callback: handler,
ReplicaIdx: opts.ReplicaIdx,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// GetReplicaEx retrieves a document from a replica server.
func (agent *Agent) GetReplicaEx(opts GetReplicaOptions, cb GetReplicaExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetReplicaEx", opts.TraceContext)
if opts.ReplicaIdx > 0 {
return agent.getOneReplica(tracer, opts, func(resp *GetReplicaResult, err error) {
tracer.Finish()
cb(resp, err)
})
}
if opts.ReplicaIdx < 0 {
tracer.Finish()
return nil, ErrInvalidReplica
}
numReplicas := agent.NumReplicas()
if numReplicas == 0 {
tracer.Finish()
return nil, ErrInvalidReplica
}
var resultLock sync.Mutex
var firstResult *GetReplicaResult
op := new(multiPendingOp)
expected := uint32(numReplicas)
opHandledLocked := func() {
completed := op.IncrementCompletedOps()
if expected-completed == 0 {
if firstResult == nil {
tracer.Finish()
cb(nil, ErrNoReplicas)
return
}
tracer.Finish()
cb(firstResult, nil)
}
}
handler := func(resp *GetReplicaResult, err error) {
resultLock.Lock()
if err != nil {
opHandledLocked()
return
}
if firstResult == nil {
newReplica := *resp
firstResult = &newReplica
}
// Mark this op as completed
opHandledLocked()
// Try to cancel every other operation so we can
// return as soon as possible to the user (and close
// any open tracing spans)
for _, op := range op.ops {
if op.Cancel() {
opHandledLocked()
}
}
resultLock.Unlock()
}
// Dispatch a getReplica for each replica server
for repIdx := 1; repIdx <= numReplicas; repIdx++ {
subOp, err := agent.getOneReplica(tracer, GetReplicaOptions{
Key: opts.Key,
ReplicaIdx: repIdx,
}, handler)
resultLock.Lock()
if err != nil {
opHandledLocked()
resultLock.Unlock()
continue
}
op.ops = append(op.ops, subOp)
resultLock.Unlock()
}
return op, nil
}
// TouchOptions encapsulates the parameters for a TouchEx operation.
type TouchOptions struct {
Key []byte
Cas Cas
Expiry uint32
TraceContext opentracing.SpanContext
}
// TouchResult encapsulates the result of a TouchEx operation.
type TouchResult struct {
Cas Cas
MutationToken MutationToken
}
// TouchExCallback is invoked upon completion of a TouchEx operation.
type TouchExCallback func(*TouchResult, error)
// TouchEx updates the expiry for a document.
func (agent *Agent) TouchEx(opts TouchOptions, cb TouchExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("TouchEx", opts.TraceContext)
if opts.Cas != 0 {
tracer.Finish()
return nil, ErrNonZeroCas
}
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&TouchResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
extraBuf := make([]byte, 4)
binary.BigEndian.PutUint32(extraBuf[0:], opts.Expiry)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdTouch,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// UnlockOptions encapsulates the parameters for a UnlockEx operation.
type UnlockOptions struct {
Key []byte
Cas Cas
TraceContext opentracing.SpanContext
}
// UnlockResult encapsulates the result of a UnlockEx operation.
type UnlockResult struct {
Cas Cas
MutationToken MutationToken
}
// UnlockExCallback is invoked upon completion of a UnlockEx operation.
type UnlockExCallback func(*UnlockResult, error)
// UnlockEx unlocks a locked document.
func (agent *Agent) UnlockEx(opts UnlockOptions, cb UnlockExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("UnlockEx", opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&UnlockResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdUnlockKey,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: nil,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// DeleteOptions encapsulates the parameters for a DeleteEx operation.
type DeleteOptions struct {
Key []byte
Cas Cas
TraceContext opentracing.SpanContext
}
// DeleteResult encapsulates the result of a DeleteEx operation.
type DeleteResult struct {
Cas Cas
MutationToken MutationToken
}
// DeleteExCallback is invoked upon completion of a DeleteEx operation.
type DeleteExCallback func(*DeleteResult, error)
// DeleteEx removes a document.
func (agent *Agent) DeleteEx(opts DeleteOptions, cb DeleteExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("DeleteEx", opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&DeleteResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdDelete,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: nil,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
type storeOptions struct {
Key []byte
Value []byte
Flags uint32
Datatype uint8
Cas Cas
Expiry uint32
TraceContext opentracing.SpanContext
}
// StoreResult encapsulates the result of a AddEx, SetEx or ReplaceEx operation.
type StoreResult struct {
Cas Cas
MutationToken MutationToken
}
// StoreExCallback is invoked upon completion of a AddEx, SetEx or ReplaceEx operation.
type StoreExCallback func(*StoreResult, error)
func (agent *Agent) storeEx(opName string, opcode commandCode, opts storeOptions, cb StoreExCallback) (PendingOp, error) {
tracer := agent.createOpTrace(opName, opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&StoreResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
extraBuf := make([]byte, 8)
binary.BigEndian.PutUint32(extraBuf[0:], opts.Flags)
binary.BigEndian.PutUint32(extraBuf[4:], opts.Expiry)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: opcode,
Datatype: opts.Datatype,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: opts.Value,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// AddOptions encapsulates the parameters for a AddEx operation.
type AddOptions struct {
Key []byte
Value []byte
Flags uint32
Datatype uint8
Expiry uint32
TraceContext opentracing.SpanContext
}
// AddEx stores a document as long as it does not already exist.
func (agent *Agent) AddEx(opts AddOptions, cb StoreExCallback) (PendingOp, error) {
return agent.storeEx("AddEx", cmdAdd, storeOptions{
Key: opts.Key,
Value: opts.Value,
Flags: opts.Flags,
Datatype: opts.Datatype,
Cas: 0,
Expiry: opts.Expiry,
TraceContext: opts.TraceContext,
}, cb)
}
// SetOptions encapsulates the parameters for a SetEx operation.
type SetOptions struct {
Key []byte
Value []byte
Flags uint32
Datatype uint8
Expiry uint32
TraceContext opentracing.SpanContext
}
// SetEx stores a document.
func (agent *Agent) SetEx(opts SetOptions, cb StoreExCallback) (PendingOp, error) {
return agent.storeEx("SetEx", cmdSet, storeOptions{
Key: opts.Key,
Value: opts.Value,
Flags: opts.Flags,
Datatype: opts.Datatype,
Cas: 0,
Expiry: opts.Expiry,
TraceContext: opts.TraceContext,
}, cb)
}
// ReplaceOptions encapsulates the parameters for a ReplaceEx operation.
type ReplaceOptions struct {
Key []byte
Value []byte
Flags uint32
Datatype uint8
Cas Cas
Expiry uint32
TraceContext opentracing.SpanContext
}
// ReplaceEx replaces the value of a Couchbase document with another value.
func (agent *Agent) ReplaceEx(opts ReplaceOptions, cb StoreExCallback) (PendingOp, error) {
return agent.storeEx("ReplaceEx", cmdSet, storeOptions{
Key: opts.Key,
Value: opts.Value,
Flags: opts.Flags,
Datatype: opts.Datatype,
Cas: opts.Cas,
Expiry: opts.Expiry,
TraceContext: opts.TraceContext,
}, cb)
}
// AdjoinOptions encapsulates the parameters for a AppendEx or PrependEx operation.
type AdjoinOptions struct {
Key []byte
Value []byte
TraceContext opentracing.SpanContext
}
// AdjoinResult encapsulates the result of a AppendEx or PrependEx operation.
type AdjoinResult struct {
Cas Cas
MutationToken MutationToken
}
// AdjoinExCallback is invoked upon completion of a AppendEx or PrependEx operation.
type AdjoinExCallback func(*AdjoinResult, error)
func (agent *Agent) adjoinEx(opName string, opcode commandCode, opts AdjoinOptions, cb AdjoinExCallback) (PendingOp, error) {
tracer := agent.createOpTrace(opName, opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&AdjoinResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: opcode,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: opts.Key,
Value: opts.Value,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// AppendEx appends some bytes to a document.
func (agent *Agent) AppendEx(opts AdjoinOptions, cb AdjoinExCallback) (PendingOp, error) {
return agent.adjoinEx("AppendEx", cmdAppend, opts, cb)
}
// PrependOptions encapsulates the parameters for a ReplaceEx operation.
type PrependOptions struct {
Key []byte
Value []byte
TraceContext opentracing.SpanContext
}
// PrependEx prepends some bytes to a document.
func (agent *Agent) PrependEx(opts AdjoinOptions, cb AdjoinExCallback) (PendingOp, error) {
return agent.adjoinEx("PrependEx", cmdPrepend, opts, cb)
}
// CounterOptions encapsulates the parameters for a IncrementEx or DecrementEx operation.
type CounterOptions struct {
Key []byte
Delta uint64
Initial uint64
Expiry uint32
TraceContext opentracing.SpanContext
}
// CounterResult encapsulates the result of a IncrementEx or DecrementEx operation.
type CounterResult struct {
Value uint64
Cas Cas
MutationToken MutationToken
}
// CounterExCallback is invoked upon completion of a IncrementEx or DecrementEx operation.
type CounterExCallback func(*CounterResult, error)
func (agent *Agent) counterEx(opName string, opcode commandCode, opts CounterOptions, cb CounterExCallback) (PendingOp, error) {
tracer := agent.createOpTrace(opName, opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Value) != 8 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
intVal := binary.BigEndian.Uint64(resp.Value)
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&CounterResult{
Value: intVal,
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
// You cannot have an expiry when you do not want to create the document.
if opts.Initial == uint64(0xFFFFFFFFFFFFFFFF) && opts.Expiry != 0 {
return nil, ErrInvalidArgs
}
extraBuf := make([]byte, 20)
binary.BigEndian.PutUint64(extraBuf[0:], opts.Delta)
if opts.Initial != uint64(0xFFFFFFFFFFFFFFFF) {
binary.BigEndian.PutUint64(extraBuf[8:], opts.Initial)
binary.BigEndian.PutUint32(extraBuf[16:], opts.Expiry)
} else {
binary.BigEndian.PutUint64(extraBuf[8:], 0x0000000000000000)
binary.BigEndian.PutUint32(extraBuf[16:], 0xFFFFFFFF)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: opcode,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// IncrementEx increments the unsigned integer value in a document.
func (agent *Agent) IncrementEx(opts CounterOptions, cb CounterExCallback) (PendingOp, error) {
return agent.counterEx("IncrementEx", cmdIncrement, opts, cb)
}
// DecrementEx decrements the unsigned integer value in a document.
func (agent *Agent) DecrementEx(opts CounterOptions, cb CounterExCallback) (PendingOp, error) {
return agent.counterEx("DecrementEx", cmdDecrement, opts, cb)
}
// GetRandomOptions encapsulates the parameters for a GetRandomEx operation.
type GetRandomOptions struct {
TraceContext opentracing.SpanContext
}
// GetRandomResult encapsulates the result of a GetRandomEx operation.
type GetRandomResult struct {
Key []byte
Value []byte
Flags uint32
Datatype uint8
Cas Cas
}
// GetRandomExCallback is invoked upon completion of a GetRandomEx operation.
type GetRandomExCallback func(*GetRandomResult, error)
// GetRandomEx retrieves the key and value of a random document stored within Couchbase Server.
func (agent *Agent) GetRandomEx(opts GetRandomOptions, cb GetRandomExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetRandomEx", opts.TraceContext)
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Extras) != 4 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
flags := binary.BigEndian.Uint32(resp.Extras[0:])
tracer.Finish()
cb(&GetRandomResult{
Key: resp.Key,
Value: resp.Value,
Flags: flags,
Cas: Cas(resp.Cas),
Datatype: resp.Datatype,
}, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdGetRandom,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: nil,
Value: nil,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// SingleServerStats represents the stats returned from a single server.
type SingleServerStats struct {
Stats map[string]string
Error error
}
// StatsTarget is used for providing a specific target to the StatsEx operation.
type StatsTarget interface {
}
// VBucketIdStatsTarget indicates that a specific vbucket should be targeted by the StatsEx operation.
type VBucketIdStatsTarget struct {
Vbid uint16
}
// StatsOptions encapsulates the parameters for a StatsEx operation.
type StatsOptions struct {
Key string
// Target indicates that something specific should be targeted by the operation. If left nil
// then the stats command will be sent to all servers.
Target StatsTarget
TraceContext opentracing.SpanContext
}
// StatsResult encapsulates the result of a StatsEx operation.
type StatsResult struct {
Servers map[string]SingleServerStats
}
// StatsExCallback is invoked upon completion of a StatsEx operation.
type StatsExCallback func(*StatsResult, error)
// StatsEx retrieves statistics information from the server. Note that as this
// function is an aggregator across numerous servers, there are no guarantees
// about the consistency of the results. Occasionally, some nodes may not be
// represented in the results, or there may be conflicting information between
// multiple nodes (a vbucket active on two separate nodes at once).
func (agent *Agent) StatsEx(opts StatsOptions, cb StatsExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("StatsEx", opts.TraceContext)
config := agent.routingInfo.Get()
if config == nil {
tracer.Finish()
return nil, ErrShutdown
}
stats := make(map[string]SingleServerStats)
var statsLock sync.Mutex
op := new(multiPendingOp)
var expected uint32
pipelines := make([]*memdPipeline, 0)
switch target := opts.Target.(type) {
case nil:
expected = uint32(config.clientMux.NumPipelines())
for i := 0; i < config.clientMux.NumPipelines(); i++ {
pipelines = append(pipelines, config.clientMux.GetPipeline(i))
}
case VBucketIdStatsTarget:
expected = 1
srvIdx, err := config.vbMap.NodeByVbucket(target.Vbid, 0)
if err != nil {
return nil, err
}
pipelines = append(pipelines, config.clientMux.GetPipeline(srvIdx))
default:
return nil, ErrUnsupportedStatsTarget
}
opHandledLocked := func() {
completed := op.IncrementCompletedOps()
if expected-completed == 0 {
tracer.Finish()
cb(&StatsResult{
Servers: stats,
}, nil)
}
}
for _, pipeline := range pipelines {
serverAddress := pipeline.Address()
// This is created on a per-pipeline basis as we need to capture
// the serverAddress into the lambda for mapping the result when
// there might not be a valid memdQResponse available.
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
statsLock.Lock()
defer statsLock.Unlock()
// Fetch the specific stats key for this server. Creating a new entry
// for the server if we did not previously have one.
curStats, ok := stats[serverAddress]
if !ok {
stats[serverAddress] = SingleServerStats{
Stats: make(map[string]string),
}
curStats = stats[serverAddress]
}
if err != nil {
// Store the first (and hopefully only) error into the Error field of this
// server's stats entry.
if curStats.Error == nil {
curStats.Error = err