forked from suryanathan/etcdcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd.hpp
1569 lines (1346 loc) · 44.6 KB
/
etcd.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
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
/*
The MIT License (MIT)
Copyright (C) 2015 Suryanathan Padmanabhan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @file etcd.hpp
* @brief c++ language binding for etcd
* @author Suryanathan Padmanabhan
* @version v0.1
* @date 2015-10-25
*
* C++ binding to access etcd API.
*
* Dependencies: libcurl, C++11
*
* The following abstractions are available to access the etcd API:
*
* a) etcd::Client implements the main client interface.
* b) etcd::Watch implements a callback based watchdog to watch etcd key and
* directory changes.
*
* Response from etcd is JSON. This implementation is agnostic to any specific
* json library. If you already have a json library in your project, just
* implement a wrapper simalar to one in "rapid_reply.hpp". If you would like
* to pick another JSON implementation, here:
* https://github.com/miloyip/nativejson-benchmark would be a good place to start.
*
*/
#ifndef __ETCD_HPP_INCLUDED__
#define __ETCD_HPP_INCLUDED__
#include <curl/curl.h>
#include <map>
#include <memory>
#include <mutex>
#include <sstream>
#include <functional>
#include <string>
#ifndef ETCD_SERVER
// Enable by default or use build flags
#define ETCD_SERVER 1
#endif // ETCD_SERVER
// Enable if you want to use the server api
#ifdef ETCD_SERVER
#include <vector>
#include <set>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#endif // ETCD_SERVER
//#define DEBUG 1
//#define CRAZY_VERBOSE 1
//#define CRAZY_VERBOSE_STREAM stderr
#ifndef MAX_FAILURES
#define MAX_FAILURES 5
#endif
namespace etcd {
//----------------------------- EXCEPTIONS ----------------------------------
/**
* @brief Exception generated by etcd client
*/
struct ClientException : public std::runtime_error {
ClientException(const std::string& error)
:std::runtime_error("etcd unknown exception"),
error(error)
{}
virtual const char* what() const throw() override {
return error.c_str();
}
std::string error;
};
#ifdef ETCD_SERVER
/**
* @brief Exception generated by etcd client
*/
struct ServerException : public std::runtime_error {
ServerException(const std::string& error)
:std::runtime_error("etcd server unknown exception"),
error(error)
{}
virtual const char* what() const throw() override {
return error.c_str();
}
std::string error;
};
#endif // ETCD_SERVER
/**
* @brief Exception generated by etcd. The json wrapper throws an exception
* based on whether etcd returned an error
*/
struct ReplyException : public std::runtime_error {
ReplyException(int error_code,
const std::string& msg,
const std::string& cause)
:std::runtime_error("etcd exception"),
cause(cause),
error_code(error_code)
{
std::ostringstream estr;
estr << msg << "["<< error_code << "]: " << cause;
msg_ = estr.str();
}
virtual const char* what() const throw() override {
return msg_.c_str();
}
std::string cause;
int error_code;
std::string msg_;
};
// ---------------------------- TYPES ---------------------------------------
namespace internal {
class Curl;
}
enum class Action {
SET,
GET,
DELETE,
UPDATE,
CREATE,
COMPARE_AND_SWAP,
COMPARE_AND_DELETE,
EXPIRE,
UNKNOWN
};
struct ResponseActionMap : public std::map<std::string, Action> {
ResponseActionMap() {
this->operator[]("set") = Action::SET;
this->operator[]("get") = Action::GET;
this->operator[]("delete") = Action::DELETE;
this->operator[]("update") = Action::UPDATE;
this->operator[]("create") = Action::CREATE;
this->operator[]("compareAndSwap") = Action::COMPARE_AND_SWAP;
this->operator[]("compareAndDelete") = Action::COMPARE_AND_DELETE;
this->operator[]("expire") = Action::EXPIRE;
}
~ResponseActionMap(){}
};
typedef uint16_t Port;
typedef uint64_t Index;
typedef uint64_t TtlValue;
/**
* @brief c++ language binding for an etcd curl client
*
* @tparam Reply see rapid_reply.hpp for an example Reply template. It should
* be constructable using a std::string(json response).
*/
template <typename Reply>
class Client {
public:
// LIFECYCLE
Client(const std::string& server, const Port& port);
// OPERATIONS
/**
* @brief Set a key-value pair
*
* @param key full prefix of the key
* @param value the value
*
* @return see etcd::Client @tparam
*/
Reply Set(
const std::string& key,
const std::string& value);
/**
* @brief Set a key-value pair that expires after a certain number of
* seconds.
*
* @param key full prefix of the key
* @param value the value
* @param ttl the time to live in seconds. key-value will expire immediately
* if ttl is set to zero.
*
* @return see etcd::Client @tparam
*/
Reply Set(
const std::string& key,
const std::string& value,
const TtlValue& ttl);
/**
* @brief Url encode a given value
*
* @param value string to escape
*
* @return escaped string
*/
std::string UrlEncode(const std::string& value);
/**
* @brief Url decode a given value
*
* @param value string to unescape
*
* @return unescaped string
*/
std::string UrlDecode(const std::string& value);
/**
* @brief Clear the ttl on a key.
*
* @param key full prefix of the key
* @param value the value
*
* @return see etcd::Client @tparam
*/
Reply ClearTtl(
const std::string& key,
const std::string& value);
/**
* @brief Create an in-order key. etcd will create a sequential key inside
* directory "dir" and associate it with value
*
* @param dir full prefix of the directory
* @param value the value
*
* @return see etcd::Client @tparam
*/
Reply SetOrdered(
const std::string& dir,
const std::string& value);
/**
* @brief Get the value of a key
*
* @param key full prefix of the key
*
* @return see etcd::Client @tparam
*/
Reply Get(const std::string& key);
/**
* @brief Recursively get all the keys and directory rooted @ key
*
* @param key the key or directory to fetch
*
* @return see etcd::Client @tparam
*/
Reply GetAll(const std::string& key);
/**
* @brief enumerate the in-order keys as a sorted list
*
* @param dir the directory which holds the in-order key
*
* @return see etcd::Client @tparam
*/
Reply GetOrdered(const std::string& dir);
/**
* @brief Delete a key-value pair
*
* @param key the key or empty directory to delete
*
* @return see etcd::Client @tparam
*/
Reply Delete(const std::string& key);
/**
* @brief Add a directory
*
* @param dir full prefix of the directory
*
* @return see etcd::Client @tparam
*/
Reply AddDirectory(const std::string& dir);
/**
* @brief Add a direcotry that expires after ttl seconds
*
* @param dir full prefix name of directory to add
* @param ttl expiry in seconds
*
* @return see etcd::Client @tparam
*/
Reply AddDirectory(const std::string& dir, const TtlValue& ttl);
/**
* @brief Update the ttl of a directory. The directory can only be
* updated with a specific ttl. Use clear_ttl to clear the ttl. A ttl
* value of zero will expire the directory immediately
*
* @param dir full prefix name of the directory to delete
* @param ttl expiry in seconds
*
* @return see etcd::Client @tparam
*/
Reply UpdateDirectoryTtl(const std::string& dir, const TtlValue& ttl);
/**
* @brief Delete a directory and optionally its contents. If recursive is
* false and the directory is not empty, it will throw an exception
*
* @param dir full prefix name of the directory
* @param recursive flag to indicate whether we should delete child nodes.
*
* @return see etcd::Client @tparam
*/
Reply DeleteDirectory(const std::string& dir, bool recursive = false);
/**
* @brief Atomic compare and swap if the previous value of key matches
* a specified key
*
* @param key full prefix of the key to update
* @param value new value of the key
* @param prevValue existing value to check
*
* @return see etcd::Client @tparam
*/
Reply CompareAndSwapIf(
const std::string& key,
const std::string& value,
const std::string& prevValue);
/**
* @brief Atomically compare and swap a key if the specified previousIndex
* matches the current modified index of the key
*
* @param key full prefix of the key to update
* @param value new value of the key
* @param prevIndex index to match with the modifiedIndex
*
* @return see etcd::Client @tparam
*/
Reply CompareAndSwapIf(
const std::string& key,
const std::string& value,
const Index& prevIndex);
/**
* @brief Atomically compare and swap a key based on whether it alreayd
* exists or not
*
* @param key full prefix of the key to update
* @param value new value
* @param prevExist should the key already exist or not?
*
* @return see etcd::Client @tparam
*/
Reply CompareAndSwapIf(
const std::string& key,
const std::string& value,
bool prevExist);
/**
* @brief Atomically compare and delete a key
*
* @param key full prefix of the key to delete
* @param prevValue only delete the key if the value matches this field
*
* @return see etcd::Client @tparam
*/
Reply CompareAndDeleteIf(
const std::string& key,
const std::string& prevValue);
/**
* @brief Atomically compare and delete a key if the current modified
* index is equal to the passed index
*
* @param key full prefix of the key to delete
* @param prevIndex only delete if the modifiedIndex is equal to this field
*
* @return see etcd::Client @tparam
*/
Reply CompareAndDeleteIf(
const std::string& key,
const Index& prevIndex);
private:
// CONSTANTS
const char *kPutRequest = "PUT";
const char *kPostRequest = "POST";
const char *kDeleteRequest = "DELETE";
const char *kValue = "value";
const char *kTttl = "ttl";
const char *kDir = "dir";
const char *kPrevExist = "prevExist";
const char *kPrevIndex = "prevIndex";
const char *kPrevValue = "prevValue";
const char *kSortedSuffix = "?recursive=true&sorted=true";
// DATA
bool enable_header_;
std::string url_;
std::string url_prefix_;
std::unique_ptr<internal::Curl> handle_;
// OPERATIONS
Reply _GetReply(const std::string& json);
};
/**
* @brief A watch abstraction for monitoring a key or directory
*
* @tparam Reply json reply wrapper
*/
template <typename Reply>
class Watch {
public:
// TYPES
typedef std::function <void (const Reply& r)> Callback;
// LIFECYCLE
/**
* @brief Create a etcd::Watch object without authentication
*
* @param server etcd client URL without the port
* @param port etcd client port
*/
Watch(const std::string& server, const Port& port);
/**
* @brief Start the watch on a specific key or directory
*
* @param key key or directory to watch
* @param callback call back when there is a change
* @param prevIndex index value to start a watch from
*
* This function assumes you already know the current state of the key
*
* It handles index out of date by performing a GET and using X-Etcd-Index
* filed from the header to start a new watch. The callback is also invoked
* with the response from GET.
*
* It handles empty reply (generated when etcd server is going down or
* cluster is getting reinitialized?) and tries to restart a watch upto
* MAX_FAILURE failures in a row.
*/
void Run(const std::string& key,
Callback callback,
const Index& prevIndex = 0);
/**
* @brief Start the watch on a specific key or directory. This will return
* immmediately after a first change. It is the user's responsibility to
* reschedule a watch. modifiedIndex will be stored by the API
*
* @param key key or directory to watch
* @param callback call back when there is a change
* @param prevIndex index value to start a watch from
*
* This function assumes you already know the current state of the key
*
* It handles index out of date by performing a GET and using X-Etcd-Index
* filed from the header to start a new watch. The callback is also invoked
* with the response from GET.
*
* It handles empty reply (generated when etcd server is going down and
* throws etcd::ClientException
*/
void RunOnce(const std::string& key,
Callback callback,
const Index& prevIndex = 0);
private:
// DATA MEMBERS
std::unique_ptr<internal::Curl> handle_;
Index prev_index_;
std::string url_prefix_;
};
#ifdef ETCD_SERVER
/**
* @brief A class to construct the server arguments
*/
struct Server {
// CONSTANTS
const bool STATE_NEW = true;
const bool STATE_EXISTING = false;
const std::string HTTP_PREFIX = "http://";
// TYPES
typedef std::set<std::string> UrlList;
typedef std::vector<std::string> ArgList;
struct Peer {
std::string name;
std::string url;
uint16_t port;
};
typedef std::vector<Peer> PeerList;
// Member flags
uint16_t peer_port = 0; // global peer port
uint16_t client_port = 0; // global client port
std::string name;
std::string client_url;
std::string peer_url;
std::string data_dir;
std::string wal_dir;
uint64_t snapshot_count = 0;
uint64_t heartbeat_interval_ms = 0;
uint64_t election_timeout_ms = 0;
UrlList listen_peer_urls;
UrlList listen_client_urls;
uint64_t max_snapshots = 0;
uint64_t max_wals = 0;
UrlList cors;
// Clustering flags
UrlList initial_advertise_peer_urls;
PeerList initial_cluster;
bool initial_cluster_state = STATE_NEW;
std::string initial_cluster_token;
UrlList advertise_client_urls;
std::string discovery;
std::string discovery_srv;
std::string discovery_fallback;
std::string discovery_proxy;
// Proxy flags
bool proxy = false;
uint64_t proxy_failure_wait = 0;
uint64_t proxy_refresh_interval = 0;
uint64_t proxy_dial_timeout = 0;
uint64_t proxy_write_timeout = 0;
uint64_t proxy_read_timeout = 0;
pid_t pid_;
template <typename List>
std::string _ListJoin(
const List& list, const std::string& prefix, const std::string& suffix) {
int i = 0;
std::string ret;
for (auto const& elem : list) {
if (i++) ret += ",";
ret += prefix + elem + suffix;
}
return ret;
}
std::string _GetUrlList(const UrlList& urls, uint16_t port) {
const std::string _SUFFIX = (port ? ":" + std::to_string(port) : "");
return _ListJoin(urls, HTTP_PREFIX, _SUFFIX);
}
void _SetArg(
const std::string& name,
const std::string& value,
ArgList& args) {
if (! value.empty()) {
args.push_back(name);
args.push_back(value);
}
}
void _SetArg(
const std::string& name,
uint64_t value,
ArgList& args) {
if (value) {
args.push_back(name);
args.push_back(std::to_string(value));
}
}
void _SetArg(
const std::string& name,
const UrlList& urls,
uint16_t port,
ArgList& args) {
if (urls.size()) {
args.push_back(name);
args.push_back(_GetUrlList(urls, port));
}
}
void _GetMemberArgs(ArgList& args) {
if (name.empty()) {
throw ServerException("name not specified");
}
_SetArg("-name", name, args);
_SetArg("-data-dir", data_dir, args);
_SetArg("-wal-dir", wal_dir, args);
_SetArg("-snapshot-count", snapshot_count, args);
_SetArg("-heartbeat-interval", heartbeat_interval_ms, args);
_SetArg("-election-timeout", election_timeout_ms, args);
_SetArg("-listen-peer-urls", listen_peer_urls, peer_port, args);
_SetArg("-listen-client-urls", listen_client_urls, client_port, args);
_SetArg("-max-snapshots", max_snapshots, args);
_SetArg("-max-wals", max_wals, args);
_SetArg("-cors", _ListJoin(cors, "", ""), args);
}
void _GetClusterArgs(ArgList& args) {
_SetArg("-initial-advertise-peer-urls", initial_advertise_peer_urls,
peer_port, args);
std::string peer_list;
int i(0);
for (auto const& peer: initial_cluster) {
if (i++) peer_list += ",";
peer_list += peer.name + "=" + HTTP_PREFIX + peer.url;
if(peer.port) peer_list += ":" + std::to_string(peer.port);
else if (peer_port) peer_list += ":" + std::to_string (peer_port);
}
_SetArg("-initial-cluster", peer_list, args);
_SetArg("-initial-cluster-state",
initial_cluster_state ? "new" : "existing", args);
_SetArg("-initial-cluster-token", initial_cluster_token, args);
_SetArg("-advertise-client-urls", advertise_client_urls,
client_port, args);
_SetArg("-discovery", discovery, args);
_SetArg("-discovery-srv", discovery_srv, args);
_SetArg("-discovery-fallback", discovery_fallback, args);
_SetArg("-discovery-proxy", discovery_proxy, args);
}
void _GetProxyArgs(ArgList& args) {
_SetArg("-proxy", (proxy ? "on": ""), args);
_SetArg("-proxy-failure-wait", proxy_failure_wait, args);
_SetArg("-proxy-refresh-interval", proxy_refresh_interval, args);
_SetArg("-proxy-dial-timeout", proxy_dial_timeout, args);
_SetArg("-proxy-write-timeout", proxy_write_timeout, args);
_SetArg("-proxy-read-timeout", proxy_read_timeout, args);
}
void _GetArgs(ArgList& args) {
_GetMemberArgs(args);
_GetClusterArgs(args);
_GetProxyArgs(args);
}
void _GetMemberEnvArgs(ArgList& args) {
if (name.empty()) {
throw ServerException("name not specified");
}
_SetArg("ETCD_NAME", name, args);
_SetArg("ETCD_DATA_DIR", data_dir, args);
_SetArg("ETCD_WAL_DIR", wal_dir, args);
_SetArg("ETCD_SNAPSHOT_COUNT", snapshot_count, args);
_SetArg("ETCD_HEARTBEAT_INTERVAL", heartbeat_interval_ms, args);
_SetArg("ETCD_ELECTION_TIMEOUT", election_timeout_ms, args);
_SetArg("ETCD_LISTEN_PEER_URLS", listen_peer_urls, peer_port, args);
_SetArg("ETCD_LISTEN_CLIENT_URLS", listen_client_urls, client_port, args);
_SetArg("ETCD_MAX_SNAPSHOTS", max_snapshots, args);
_SetArg("ETCD_MAX_WALS", max_wals, args);
_SetArg("ETCD_CORS", _ListJoin(cors, "", ""), args);
}
void _GetClusterEnvArgs(ArgList& args) {
_SetArg("ETCD_INITIAL_ADVERTISE_PEER_URLS", initial_advertise_peer_urls,
peer_port, args);
std::string peer_list;
int i(0);
for (auto const& peer: initial_cluster) {
if (i++) peer_list += ",";
peer_list += peer.name + "=" + HTTP_PREFIX + peer.url;
if(peer.port) peer_list += ":" + std::to_string(peer.port);
else if (peer_port) peer_list += ":" + std::to_string (peer_port);
}
_SetArg("ETCD_INITIAL_CLUSTER", peer_list, args);
_SetArg("ETCD_INITIAL_CLUSTER_STATE",
initial_cluster_state ? "new" : "existing", args);
_SetArg("ETCD_INITIAL_CLUSTER_TOKEN", initial_cluster_token, args);
_SetArg("ETCD_ADVERTISE_CLIENT_URLS", advertise_client_urls,
client_port, args);
_SetArg("ETCD_DISCOVERY", discovery, args);
_SetArg("ETCD_DISCOVERY_SRV", discovery_srv, args);
_SetArg("ETCD_DISCOVERY_FALLBACK", discovery_fallback, args);
_SetArg("ETCD_DISCOVERY_PROXY", discovery_proxy, args);
}
void _GetProxyEnvArgs(ArgList& args) {
_SetArg("ETCD_PROXY", (proxy ? "on": ""), args);
_SetArg("ETCD_PROXY_FAILURE_WAIT", proxy_failure_wait, args);
_SetArg("ETCD_PROXY_REFRESH_INTERVAL", proxy_refresh_interval, args);
_SetArg("ETCD_PROXY_DIAL_TIMEOUT", proxy_dial_timeout, args);
_SetArg("ETCD_PROXY_WRITE_TIMEOUT", proxy_write_timeout, args);
_SetArg("ETCD_PROXY_READ_TIMEOUT", proxy_read_timeout, args);
}
void _GetEnvArgs(ArgList& args) {
_GetMemberEnvArgs(args);
_GetClusterEnvArgs(args);
_GetProxyEnvArgs(args);
}
// ToDo Add TLS args
/**
* @brief Get Arguments for passing to etcd executable
*
* @param args output argument list
*/
void GetArgs(ArgList& args) {
_GetArgs(args);
}
/**
* @brief Get environment variables that can be set before starting
* etcd
*
* @param args output environment variable, value list
*/
void GetEnvArgs(ArgList& args) {
_GetEnvArgs(args);
}
};
#endif // ETCD_SERVER
//------------------------------ INTERNAL TYPES -----------------------------
namespace internal {
struct CurlUnknownException : public std::runtime_error {
CurlUnknownException(const std::string& error)
:std::runtime_error("curl unknown exception"),
error(error)
{}
virtual const char* what() const throw() override {
return error.c_str();
}
std::string error;
};
struct CurlException : public std::runtime_error {
CurlException(CURLcode errorCode,
const std::string& msg)
:std::runtime_error("curl exception"),
error_code(errorCode),
msg_(msg)
{
std::ostringstream estr;
estr << msg_ << " [code: " << error_code << "] ";
estr << curl_easy_strerror(error_code);
msg_ = estr.str();
}
virtual const char* what() const throw() override {
return msg_.c_str();
}
CURLcode error_code;
std::string msg_;
};
typedef std::map<std::string, std::string> CurlOptions;
class Curl {
public:
// LIFECYCLE
Curl();
~Curl();
// OPERATIONS
std::string Get(const std::string& url);
std::string Set(const std::string& url,
const std::string& type,
const CurlOptions& options);
std::string UrlEncode(const std::string& value);
std::string UrlDecode(const std::string& value);
void EnableHeader(bool onOff);
std::string GetHeader();
// callback from 'C' functions
size_t WriteCb(void* buffer_p, size_t size, size_t nmemb) throw();
size_t HeaderCb(void* buffer_p, size_t size, size_t nmemb) throw();
private:
// DATA MEMBERS
CURL *handle_;
std::ostringstream write_stream_;
std::ostringstream header_stream_;
bool enable_header_;
// libcurl is not thread-safe
// https://curl.haxx.se/libcurl/c/threadsafe.html
// but we are calling etcd client via ClusterManager from multiple threads
// so adding mutex here
std::mutex mutex_;
// LIFECYCLE
Curl(const Curl& rhs);
void operator=(const Curl&& rhs);
// OPERATIONS
void _CheckError(CURLcode err, const std::string& msg);
void _ResetHandle();
void _SetCommonOptions(const std::string& url);
void _SetGetOptions(const std::string& url);
void _SetPostOptions(const std::string& url,
const std::string& type,
const CurlOptions& options);
};
}
//------------------------------- LIFECYCLE ----------------------------------
template <typename Reply> Client<Reply>::
Client(const std::string& server, const Port& port)
try:
handle_(new internal::Curl()) {
std::ostringstream ostr;
ostr << "http://" << server << ":" << port;
url_ = ostr.str();
ostr << "/v2/keys";
url_prefix_ = ostr.str();
} catch (const std::exception& e) {
throw ClientException(e.what());
}
//------------------------------- OPERATIONS ---------------------------------
template <typename Reply> Reply Client<Reply>::
Set(const std::string& key, const std::string& value) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + key, kPutRequest, {{kValue, value}});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
Set(const std::string& key,
const std::string& value,
const TtlValue& ttl) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + key, kPutRequest,
{
{kValue, value},
{kTttl, std::to_string(ttl)},
});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
ClearTtl(const std::string& key, const std::string& value) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + key, kPutRequest,
{
{kValue, value},
{kTttl, ""},
{kPrevExist, "true"}
});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> std::string Client<Reply>::
UrlEncode(const std::string& value) {
return handle_->UrlEncode(value);
}
template <typename Reply> std::string Client<Reply>::
UrlDecode(const std::string& value) {
return handle_->UrlDecode(value);
}
template <typename Reply> Reply Client<Reply>::
SetOrdered(const std::string& dir, const std::string& value) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + dir,
kPostRequest, {{kValue, value}});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
Get(const std::string& key) {
std::string ret;
try {
ret = handle_->Get(url_prefix_ + key);
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
GetAll(const std::string& key) {
std::string ret;
try {
ret = handle_->Get(url_prefix_ + key + "?recursive=true");;
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
GetOrdered(const std::string& dir) {
std::string ret;
try {
ret = handle_->Get(url_prefix_ + dir + std::string(kSortedSuffix));
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
Delete(const std::string& key) {
std::string ret;
try {
ret = handle_->Set(
url_prefix_ + key, kDeleteRequest, {});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
AddDirectory(const std::string& dir) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + dir, kPutRequest, {{kDir, "true"}});
} catch (const std::exception& e) {
throw ClientException(e.what());
}
return _GetReply(ret);
}
template <typename Reply> Reply Client<Reply>::
AddDirectory(const std::string& dir, const TtlValue& ttl) {
std::string ret;
try {
ret = handle_->Set(url_prefix_ + dir, kPutRequest,
{