-
Notifications
You must be signed in to change notification settings - Fork 123
/
SyncClient.hpp
944 lines (855 loc) · 33.5 KB
/
SyncClient.hpp
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
#ifndef __ETCD_SYNC_CLIENT_HPP__
#define __ETCD_SYNC_CLIENT_HPP__
#include <chrono>
#include <map>
#include <memory>
#include <mutex>
#include <ratio>
#include <string>
#include "etcd/Response.hpp"
#include "etcd/v3/action_constants.hpp"
namespace etcdv3 {
struct ActionParameters;
class AsyncCompareAndDeleteAction;
class AsyncCompareAndSwapAction;
class AsyncDeleteAction;
class AsyncCampaignAction;
class AsyncProclaimAction;
class AsyncLeaderAction;
class AsyncObserveAction;
class AsyncResignAction;
class AsyncHeadAction;
class AsyncLeaseGrantAction;
class AsyncLeaseRevokeAction;
class AsyncLeaseKeepAliveAction;
class AsyncLeaseTimeToLiveAction;
class AsyncLeaseLeasesAction;
class AsyncLockAction;
class AsyncAddMemberAction;
class AsyncListMemberAction;
class AsyncRemoveMemberAction;
class AsyncUnlockAction;
class AsyncPutAction;
class AsyncRangeAction;
class AsyncSetAction;
class AsyncTxnAction;
class AsyncUpdateAction;
class AsyncWatchAction;
enum class AtomicityType;
class Transaction;
namespace detail {
std::string string_plus_one(std::string const& value);
std::string resolve_etcd_endpoints(std::string const& default_endpoints);
} // namespace detail
} // namespace etcdv3
#if defined(WITH_GRPC_CHANNEL_CLASS)
namespace grpc {
class Channel;
class ChannelArguments;
} // namespace grpc
#else
namespace grpc_impl {
class Channel;
class ChannelArguments;
} // namespace grpc_impl
#endif
namespace etcd {
using etcdv3::ERROR_ACTION_CANCELLED;
using etcdv3::ERROR_COMPARE_FAILED;
using etcdv3::ERROR_KEY_ALREADY_EXISTS;
using etcdv3::ERROR_KEY_NOT_FOUND;
class KeepAlive;
class Watcher;
class Client;
/**
* Client is responsible for maintaining a connection towards an etcd server.
* Etcd operations can be reached via the methods of the client.
*/
/**
* SyncClient is a wrapper around etcd::Client and provides a simplified sync
* interface with blocking operations.
*
* In case of any exceptions occur in the backend the Response value's
* error_message will contain the text of the exception and the error code will
* be 600
*
* Use this class only if performance does not matter.
*/
class SyncClient {
private:
class TokenAuthenticator;
class TokenAuthenticatorDeleter {
public:
void operator()(TokenAuthenticator* authenticator);
};
public:
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
*/
SyncClient(std::string const& etcd_url,
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param arguments user provided grpc channel arguments.
*/
SyncClient(std::string const& etcd_url,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments
#else
grpc_impl::ChannelArguments const& arguments
#endif
);
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
*/
static SyncClient* WithUrl(std::string const& etcd_url,
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param arguments user provided grpc channel arguments.
*/
static SyncClient* WithUrl(std::string const& etcd_url,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments
#else
grpc_impl::ChannelArguments const& arguments
#endif
);
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param username username of etcd auth
* @param password password of etcd auth
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
* @param auth_token_ttl TTL seconds for auth token, see also
* `--auth-token-ttl` flags of etcd.
*/
SyncClient(std::string const& etcd_url, std::string const& username,
std::string const& password, int const auth_token_ttl = 300,
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param username username of etcd auth
* @param password password of etcd auth
* @param arguments user provided grpc channel arguments.
* @param auth_token_ttl TTL seconds for auth token, see also
* `--auth-token-ttl` flags of etcd. Default value should be 300.
*/
SyncClient(std::string const& etcd_url, std::string const& username,
std::string const& password, int const auth_token_ttl,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments
#else
grpc_impl::ChannelArguments const& arguments
#endif
);
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param username username of etcd auth
* @param password password of etcd auth
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
* @param auth_token_ttl TTL seconds for auth token, see also
* `--auth-token-ttl` flags of etcd.
*/
static SyncClient* WithUser(std::string const& etcd_url,
std::string const& username,
std::string const& password,
int const auth_token_ttl = 300,
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param username username of etcd auth
* @param password password of etcd auth
* @param arguments user provided grpc channel arguments.
* @param auth_token_ttl TTL seconds for auth token, see also
* `--auth-token-ttl` flags of etcd. Default value should be 300.
*/
static SyncClient* WithUser(std::string const& etcd_url,
std::string const& username,
std::string const& password,
int const auth_token_ttl,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments
#else
grpc_impl::ChannelArguments const& arguments
#endif
);
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param ca root CA file for SSL/TLS connection.
* @param cert cert chain file for SSL/TLS authentication, could be empty
* string.
* @param privkey private key file for SSL/TLS authentication, could be empty
* string.
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
*/
SyncClient(std::string const& etcd_url, std::string const& ca,
std::string const& cert, std::string const& privkey,
std::string const& target_name_override,
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param ca root CA file for SSL/TLS connection.
* @param cert cert chain file for SSL/TLS authentication, could be empty
* string.
* @param privkey private key file for SSL/TLS authentication, could be empty
* string.
* @param arguments user provided grpc channel arguments.
*/
SyncClient(std::string const& etcd_url, std::string const& ca,
std::string const& cert, std::string const& privkey,
std::string const& target_name_override,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments
#else
grpc_impl::ChannelArguments const& arguments
#endif
);
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param ca root CA file for SSL/TLS connection.
* @param cert cert chain file for SSL/TLS authentication, could be empty
* string.
* @param privkey private key file for SSL/TLS authentication, could be empty
* string.
* @param target_name_override Override the target host name if you want to
* pass multiple address for load balancing with SSL, and there's no DNS. The
* @target_name_override@ must exist in the SANS of your SSL certificate.
* @param load_balancer is the load balance strategy, can be one of
* round_robin/pick_first/grpclb/xds.
*/
static SyncClient* WithSSL(std::string const& etcd_url, std::string const& ca,
std::string const& cert = "",
std::string const& privkey = "",
std::string const& target_name_override = "",
std::string const& load_balancer = "round_robin");
/**
* Constructs an etcd client object.
*
* @param etcd_url is the url of the etcd server to connect to, like
* "http://127.0.0.1:2379", or multiple url, separated by ',' or ';'.
* @param ca root CA file for SSL/TLS connection.
* @param cert cert chain file for SSL/TLS authentication, could be empty
* string.
* @param privkey private key file for SSL/TLS authentication, could be empty
* string.
* @param target_name_override Override the target host name if you want to
* pass multiple address for load balancing with SSL, and there's no DNS. The
* @target_name_override@ must exist in the SANS of your SSL certificate.
* @param arguments user provided grpc channel arguments.
*/
static SyncClient* WithSSL(std::string const& etcd_url,
#if defined(WITH_GRPC_CHANNEL_CLASS)
grpc::ChannelArguments const& arguments,
#else
grpc_impl::ChannelArguments const& arguments,
#endif
std::string const& ca,
std::string const& cert = "",
std::string const& privkey = "",
std::string const& target_name_override = "");
~SyncClient();
/**
* Get the HEAD revision of the connected etcd server.
*/
Response head();
/**
* Get the value of specified key from the etcd server
* @param key is the key to be read
*/
Response get(std::string const& key);
/**
* Get the value of specified key of specified revision from the etcd server
* @param key is the key to be read
* @param revision is the revision of the key to be read
*/
Response get(std::string const& key, int64_t revision);
/**
* Sets the value of a key. The key will be modified if already exists or
* created if it does not exists.
* @param key is the key to be created or modified
* @param value is the new value to be set
* @param leaseId is the lease attached to the key
*/
Response set(std::string const& key, std::string const& value,
const int64_t leaseId = 0);
/**
* Creates a new key and sets it's value. Fails if the key already exists.
* @param key is the key to be created
* @param value is the value to be set
* @param leaseId is the lease attached to the key
*/
Response add(std::string const& key, std::string const& value,
const int64_t leaseId = 0);
/**
* Put a new key-value pair.
* @param key is the key to be put
* @param value is the value to be put
*/
Response put(std::string const& key, std::string const& value);
/**
* Put a new key-value pair.
* @param key is the key to be put
* @param value is the value to be put
* @param leaseId is the lease id to be associated with the key
*/
Response put(std::string const& key, std::string const& value,
const int64_t leaseId);
/**
* Modifies an existing key. Fails if the key does not exists.
* @param key is the key to be modified
* @param value is the new value to be set
* @param leaseId is the lease attached to the key
*/
Response modify(std::string const& key, std::string const& value,
const int64_t leaseId = 0);
/**
* Modifies an existing key only if it has a specific value. Fails if the key
* does not exists or the original value differs from the expected one.
* @param key is the key to be modified
* @param value is the new value to be set
* @param old_value is the value to be replaced
* @param leaseId is the lease attached to the key
*/
Response modify_if(std::string const& key, std::string const& value,
std::string const& old_value, const int64_t leaseId = 0);
/**
* Modifies an existing key only if it has a specific modification index
* value. Fails if the key does not exists or the modification index of the
* previous value differs from the expected one.
* @param key is the key to be modified
* @param value is the new value to be set
* @param old_index is the expected index of the original value
* @param leaseId is the lease attached to the key
*/
Response modify_if(std::string const& key, std::string const& value,
int64_t old_index, const int64_t leaseId = 0);
/**
* Removes a single key. The key has to point to a plain, non directory entry.
* @param key is the key to be deleted
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the key does not exist.
*/
Response rm(std::string const& key);
/**
* Removes a single key but only if it has a specific value. Fails if the key
* does not exists or the its value differs from the expected one.
* @param key is the key to be deleted
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the key does not exist.
*/
Response rm_if(std::string const& key, std::string const& old_value);
/**
* Removes an existing key only if it has a specific modification index value.
* Fails if the key does not exists or the modification index of it differs
* from the expected one.
* @param key is the key to be deleted
* @param old_index is the expected index of the existing value
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the key does not exist.
*/
Response rm_if(std::string const& key, int64_t old_index);
/**
* Removes a directory node. Fails if the parent directory dos not exists or
* not a directory.
* @param key is the directory to be created to be listed
* @param recursive if true then delete a whole subtree, otherwise deletes
* only an empty directory.
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the no key been deleted.
*/
Response rmdir(std::string const& key, bool recursive = false);
/**
* Removes multiple keys between [key, range_end).
*
* This overload for `const char *` is to avoid const char * to bool implicit
* casting.
*
* @param key is the directory to be created to be listed
* @param range_end is the end of key range to be removed.
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the no key been deleted.
*/
Response rmdir(std::string const& key, const char* range_end);
/**
* Removes multiple keys between [key, range_end).
*
* @param key is the directory to be created to be listed
* @param range_end is the end of key range to be removed.
*
* @return Returns etcdv3::ERROR_KEY_NOT_FOUND if the no key been deleted.
*/
Response rmdir(std::string const& key, std::string const& range_end);
/**
* Gets a directory listing of the directory prefixed by the key.
*
* @param key is the key to be listed
*/
Response ls(std::string const& key);
/**
* Gets a directory listing of the directory prefixed by the key.
*
* @param key is the key to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility. 0 means no
* limit.
*/
Response ls(std::string const& key, size_t const limit);
/**
* Gets a directory listing of the directory prefixed by the key with given
* revision.
*
* @param key is the key to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility. 0 means no
* limit.
* @param revision is the revision to be listed
*/
Response ls(std::string const& key, size_t const limit, int64_t revision);
/**
* Gets a directory listing of the directory identified by the key and
* range_end, i.e., get all keys in the range [key, range_end).
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
*/
Response ls(std::string const& key, std::string const& range_end);
/**
* Gets a directory listing of the directory identified by the key and
* range_end, i.e., get all keys in the range [key, range_end), and respects
* the given limit.
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility. 0 means no
* limit.
*/
Response ls(std::string const& key, std::string const& range_end,
size_t const limit);
/**
* Gets a directory listing of the directory identified by the key and
* range_end, i.e., get all keys in the range [key, range_end), and respects
* the given limit and revision.
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility. 0 means no
* limit.
* @param revision is the revision to be listed
*/
Response ls(std::string const& key, std::string const& range_end,
size_t const limit, int64_t revision);
/**
* Gets a directory listing of the directory prefixed by the key.
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
*/
Response keys(std::string const& key);
/**
* Gets a directory listing of the directory prefixed by the key.
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility.
*/
Response keys(std::string const& key, size_t const limit);
/**
* Gets a directory listing of the directory prefixed by the key with
* specified revision.
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility.
* @param revision is the revision to be listed
*/
Response keys(std::string const& key, size_t const limit, int64_t revision);
/**
* List keys identified by the key and range_end, i.e., get all keys in the
* range [key, range_end).
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
*/
Response keys(std::string const& key, std::string const& range_end);
/**
* List keys identified by the key and range_end, i.e., get all keys in the
* range [key, range_end), and respects the given limit.
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility.
*/
Response keys(std::string const& key, std::string const& range_end,
size_t const limit);
/**
* List keys identified by the key and range_end, i.e., get all keys in the
* range [key, range_end), and respects the given limit and revision.
*
* Note that only keys are included in the response.
*
* @param key is the key to be listed
* @param range_end is the end of key range to be listed
* @param limit is the size limit of results to be listed, we don't use
* default parameters to ensure backwards binary compatibility.
* @param revision is the revision to be listed
*/
Response keys(std::string const& key, std::string const& range_end,
size_t const limit, int64_t revision);
/**
* Watches for changes of a key or a subtree. Please note that if you watch
* e.g. "/testdir" and a new key is created, like "/testdir/newkey" then no
* change happened in the value of
* "/testdir" so your watch will not detect this. If you want to detect
* addition and deletion of directory entries then you have to do a recursive
* watch.
* @param key is the value or directory to be watched
* @param recursive if true watch a whole subtree
*/
Response watch(std::string const& key, bool recursive = false);
/**
* Watches for changes of a key or a subtree from a specific index. The index
* value can be in the "past".
* @param key is the value or directory to be watched
* @param fromIndex the first index we are interested in
* @param recursive if true watch a whole subtree
*/
Response watch(std::string const& key, int64_t fromIndex,
bool recursive = false);
/**
* Watches for changes of a range of keys inside [key, range_end).
*
* This overload for `const char *` is to avoid const char * to bool implicit
* casting.
*
* @param key is the value or directory to be watched
* @param range_end is the end of key range to be removed.
*/
Response watch(std::string const& key, const char* range_end);
/**
* Watches for changes of a range of keys inside [key, range_end).
*
* @param key is the value or directory to be watched
* @param range_end is the end of key range to be removed.
*/
Response watch(std::string const& key, std::string const& range_end);
/**
* Watches for changes of a range of keys inside [key, range_end) from a
* specific index. The index value can be in the "past".
*
* Watches for changes of a key or a subtree from a specific index. The index
* value can be in the "past".
* @param key is the value or directory to be watched
* @param range_end is the end of key range to be removed.
* @param fromIndex the first index we are interested in
*/
Response watch(std::string const& key, std::string const& range_end,
int64_t fromIndex);
/**
* Grants a lease.
* @param ttl is the time to live of the lease
*/
Response leasegrant(int ttl);
/**
* Grants a lease.
* @param ttl is the time to live of the lease
*/
std::shared_ptr<KeepAlive> leasekeepalive(int ttl);
/**
* Revoke a lease.
* @param lease_id is the id the lease
*/
Response leaserevoke(int64_t lease_id);
/**
* Get time-to-live of a lease.
* @param lease_id is the id the lease
*/
Response leasetimetolive(int64_t lease_id);
/**
* List all alive leases, equivalent to `etcdctl lease list`.
*/
Response leases();
/**
* Add an etcd member to the etcd cluster, equivalent to `etcdctl member add`.
* @param peer_urls is comma separated list of URLs for the new member.
* @param is_learner is true if the added member is a learner.
*/
Response add_member(std::string const& peer_urls, bool is_learner);
/**
* List all members, equivalent to `etcdctl member list`.
*/
Response list_member();
/**
* Remove a member of an etcd cluster, equivalent to `etcdctl member remove`.
* @param member_id is the id of the member to be removed.
*/
Response remove_member(uint64_t member_id);
/**
* Gains a lock at a key, using a default created lease, using the default
* lease (10 seconds), with keeping alive has already been taken care of by
* the library.
* @param key is the key to be used to request the lock.
*/
Response lock(std::string const& key);
/**
* Gains a lock at a key, using a default created lease, using the specified
* lease TTL (in seconds), with keeping alive has already been taken care of
* by the library.
* @param key is the key to be used to request the lock.
* @param lease_ttl is the TTL used to create a lease for the key.
*/
Response lock(std::string const& key, int lease_ttl);
/**
* Gains a lock at a key, using a user-provided lease, the lifetime of the
* lease won't be taken care of by the library.
* @param key is the key to be used to request the lock.
*/
Response lock_with_lease(std::string const& key, int64_t lease_id);
/**
* Releases a lock at a key.
* @param key is the lock key to release.
*/
Response unlock(std::string const& lock_key);
/**
* Execute a etcd transaction.
* @param txn is the transaction object to be executed.
*/
Response txn(etcdv3::Transaction const& txn);
/**
* Campaign for the election @name@.
*
* @param name is the name of election that will campaign for.
* @param lease_id is a user-managed (usually with a `KeepAlive`) lease id.
* @param value is the value for campaign.
*
* @returns a leader key if succeed, consist of
*
* - name: the name of the election
* - key: a generated election key
* - created rev: the revision of the generated key
* - lease: the lease id of the election leader
*/
Response campaign(std::string const& name, int64_t lease_id,
std::string const& value);
/**
* Updates the value of election with a new value, with leader key returns by
* @campaign@.
*
* @param name is the name of election
* @param lease_id is the user-provided lease id for the proclamation
* @param key is the generated associated key returned by @campaign@
* @param revision is the created revision of key-value returned by @campaign@
* @param value is the new value to set.
*/
Response proclaim(std::string const& name, int64_t lease_id,
std::string const& key, int64_t revision,
std::string const& value);
/**
* Get the current leader proclamation.
*
* @param name is the names of election.
*
* @returns current election key and value.
*/
Response leader(std::string const& name);
/**
* An observer that will cancel the associated election::observe request
* when being destruct.
*/
class Observer {
public:
~Observer();
// wait at least *one* response from the observer.
Response WaitOnce();
private:
std::shared_ptr<etcdv3::AsyncObserveAction> action = nullptr;
friend class SyncClient;
};
/**
* Observe the leader change.
*
* @param name is the names of election to watch.
*
* @returns an observer that holds that action and will cancel the request
* when being destructed.
*/
std::unique_ptr<Observer> observe(std::string const& name);
/**
* Updates the value of election with a new value, with leader key returns by
* @campaign@.
*
* @param name is the name of election
* @param lease_id is the user-provided lease id for the proclamation
* @param key is the generated associated key returned by @campaign@
* @param revision is the created revision of key-value returned by @campaign@
*/
Response resign(std::string const& name, int64_t lease_id,
std::string const& key, int64_t revision);
private:
// TODO: use std::unique_ptr<>
std::shared_ptr<etcdv3::AsyncHeadAction> head_internal();
std::shared_ptr<etcdv3::AsyncRangeAction> get_internal(
std::string const& key, const int64_t revision = 0);
std::shared_ptr<etcdv3::AsyncSetAction> add_internal(
std::string const& key, std::string const& value,
const int64_t leaseId = 0);
std::shared_ptr<etcdv3::AsyncPutAction> put_internal(
std::string const& key, std::string const& value,
const int64_t leaseId = 0);
std::shared_ptr<etcdv3::AsyncUpdateAction> modify_internal(
std::string const& key, std::string const& value,
const int64_t leaseId = 0);
std::shared_ptr<etcdv3::AsyncCompareAndSwapAction> modify_if_internal(
std::string const& key, std::string const& value, int64_t old_index,
std::string const& old_value, etcdv3::AtomicityType const& atomicity_type,
const int64_t leaseId = 0);
std::shared_ptr<etcdv3::AsyncDeleteAction> rm_internal(
std::string const& key);
std::shared_ptr<etcdv3::AsyncCompareAndDeleteAction> rm_if_internal(
std::string const& key, int64_t old_index, const std::string& old_value,
etcdv3::AtomicityType const& atomicity_type);
std::shared_ptr<etcdv3::AsyncDeleteAction> rmdir_internal(
std::string const& key, bool recursive = false);
std::shared_ptr<etcdv3::AsyncDeleteAction> rmdir_internal(
std::string const& key, std::string const& range_end);
std::shared_ptr<etcdv3::AsyncRangeAction> ls_internal(
std::string const& key, size_t const limit, bool const keys_only = false,
int64_t revision = 0);
std::shared_ptr<etcdv3::AsyncRangeAction> ls_internal(
std::string const& key, std::string const& range_end, size_t const limit,
bool const keys_only = false, int64_t revision = 0);
std::shared_ptr<etcdv3::AsyncWatchAction> watch_internal(
std::string const& key, int64_t fromIndex, bool recursive = false);
std::shared_ptr<etcdv3::AsyncWatchAction> watch_internal(
std::string const& key, std::string const& range_end, int64_t fromIndex);
std::shared_ptr<etcdv3::AsyncLeaseRevokeAction> leaserevoke_internal(
int64_t lease_id);
std::shared_ptr<etcdv3::AsyncLeaseTimeToLiveAction> leasetimetolive_internal(
int64_t lease_id);
std::shared_ptr<etcdv3::AsyncLeaseLeasesAction> leases_internal();
std::shared_ptr<etcdv3::AsyncAddMemberAction> add_member_internal(
std::string const& peer_urls, bool is_learner);
std::shared_ptr<etcdv3::AsyncListMemberAction> list_member_internal();
std::shared_ptr<etcdv3::AsyncRemoveMemberAction> remove_member_internal(
const uint64_t member_id);
Response lock_internal(std::string const& key,
std::shared_ptr<etcd::KeepAlive> const& keepalive);
std::shared_ptr<etcdv3::AsyncLockAction> lock_with_lease_internal(
std::string const& key, int64_t lease_id);
std::shared_ptr<etcdv3::AsyncUnlockAction> unlock_internal(
std::string const& lock_key);
std::shared_ptr<etcdv3::AsyncTxnAction> txn_internal(
etcdv3::Transaction const& txn);
std::shared_ptr<etcdv3::AsyncCampaignAction> campaign_internal(
std::string const& name, int64_t lease_id, std::string const& value);
std::shared_ptr<etcdv3::AsyncProclaimAction> proclaim_internal(
std::string const& name, int64_t lease_id, std::string const& key,
int64_t revision, std::string const& value);
std::shared_ptr<etcdv3::AsyncLeaderAction> leader_internal(
std::string const& name);
std::shared_ptr<etcdv3::AsyncResignAction> resign_internal(
std::string const& name, int64_t lease_id, std::string const& key,
int64_t revision);
public:
/**
* Return current auth token.
*/
const std::string& current_auth_token() const;
/**
* Obtain the underlying gRPC channel.
*/
#if defined(WITH_GRPC_CHANNEL_CLASS)
std::shared_ptr<grpc::Channel> grpc_channel() const;
#else
std::shared_ptr<grpc_impl::Channel> grpc_channel() const;
#endif
/**
* Set a timeout value for grpc operations.
*/
template <typename Period = std::micro>
void set_grpc_timeout(std::chrono::duration<std::chrono::microseconds::rep,
Period> const& timeout) {
grpc_timeout =
std::chrono::duration_cast<std::chrono::milliseconds>(timeout);
}
/**
* Get the current timeout value for grpc operations.
*/
std::chrono::microseconds get_grpc_timeout() const {
return this->grpc_timeout;
}
private:
#if defined(WITH_GRPC_CHANNEL_CLASS)
std::shared_ptr<grpc::Channel> channel;
#else
std::shared_ptr<grpc_impl::Channel> channel;
#endif
mutable std::unique_ptr<TokenAuthenticator, TokenAuthenticatorDeleter>
token_authenticator;
mutable std::chrono::microseconds grpc_timeout =
std::chrono::microseconds::zero();
struct EtcdServerStubs;
struct EtcdServerStubsDeleter {
void operator()(EtcdServerStubs* stubs);
};
std::unique_ptr<EtcdServerStubs, EtcdServerStubsDeleter> stubs;
std::mutex mutex_for_keepalives;
std::map<std::string, int64_t> leases_for_locks;
std::map<int64_t, std::shared_ptr<KeepAlive>> keep_alive_for_locks;
friend class KeepAlive;
friend class Watcher;
friend class Client;
};
} // namespace etcd
#endif