7 #include <sandesh/sandesh.h>
8 #include <boost/algorithm/string.hpp>
9 #include <boost/algorithm/string/join.hpp>
10 #include <boost/foreach.hpp>
11 #include <boost/unordered_map.hpp>
12 #include <boost/system/error_code.hpp>
13 #include <eql_types.h>
15 #include "schema/vnc_cfg_types.h"
32 EtcdIf::EtcdIf(
const std::vector<std::string> &etcd_hosts,
33 const int port,
bool useSsl)
37 BOOST_FOREACH(
const std::string &etcd_host, etcd_hosts) {
38 hosts_.push_back(etcd_host);
39 boost::system::error_code ec;
40 boost::asio::ip::address etcd_addr;
43 EQL_DEBUG(EtcdClientDebug,
"Invalid IP address");
58 BOOST_FOREACH(
const std::string &etcd_host,
hosts_) {
59 url << etcd_host <<
":" <<
port_;
61 shared_ptr<Channel> chan;
64 auto channel_creds = grpc::SslCredentials(
65 grpc::SslCredentialsOptions());
66 chan = grpc::CreateChannel(url.str(), channel_creds);
68 chan = grpc::CreateChannel(
70 grpc::InsecureChannelCredentials());
84 string const& range_end,
88 EQL_DEBUG(EtcdClientDebug, os <<
"Get Request - key: " << key
89 <<
" range_end: " << range_end
90 <<
" limit: " << limit);
111 (
static_cast<void *
>(&
get_call_->gtag_)));
128 while (
cq_.Next(&got_tag, &ok)) {
140 EQL_TRACE(EtcdClientErrorTrace,
"Get Response: Error",
145 if (got_tag == (
static_cast<void *
>(&
gtag_))) {
154 "Get Response: Prefix Not Found",
159 multimap<string, string> kvs;
161 kvs.insert(pair<string, string>
170 EQL_DEBUG(EtcdClientDebug, os <<
"Get Response: Success"
173 <<
" KEY-VALUE LIST: ");
178 EQL_DEBUG(EtcdClientDebug, os <<
" Index: " << i
196 EQL_DEBUG(EtcdClientDebug, os <<
"Set Request - Key: "
223 while (
set_call_->cq_.Next(&got_tag, &ok)) {
227 "Set Response: Error",
232 if (got_tag == (
void *)
this) {
234 EQL_DEBUG(EtcdClientDebug, os <<
"Set Response: Success"
236 << (
set_call_->set_resp_.prev_kv()).key()
238 << (
set_call_->set_resp_.prev_kv()).value());
249 EQL_DEBUG(EtcdClientDebug, os <<
"Delete Request - Key: "
280 "Delete Response: Error",
284 if (got_tag == (
void *)
this) {
287 EQL_DEBUG(EtcdClientDebug, os <<
"Delete Response: Success"
288 <<
" # Keys Deleted: "
291 for (
int i = 0; i <
delete_call_->delete_resp_.deleted(); i++) {
294 EQL_DEBUG(EtcdClientDebug, os <<
" Index: " << i
312 EQL_DEBUG(EtcdClientDebug, os <<
"Watch Request - Key: " << key);
324 string range_end(key);
325 range_end.back() = ((int)range_end[range_end.length()-1])+1;
339 if (got_tag == (
void *)
this) {
340 watch_call_->watch_reader_->Write(req, (
void *)
"write");
363 while(cq_.Next(&got_tag, &ok)) {
374 if (!ok || !watch_active_) {
379 "Watch Response: Error",
385 if (got_tag == (
void*)&wtag) {
390 if (watch_resp_.events_size()) {
397 for (
int i = 0; i < watch_resp_.events_size(); i++) {
398 auto event = watch_resp_.events(i);
400 switch (event.type()) {
402 if (event.kv().version() == 0) {
416 resp.
set_key(event.kv().key());
417 resp.
set_val(event.kv().value());
419 if (event.has_prev_kv()) {
424 EQL_DEBUG(EtcdClientDebug, os <<
"Watch Response: "
449 watch_reader_->Read(&watch_resp_, (
void*)&wtag);
457 watch_call_->watch_reader_->WritesDone((
void *)
"Stop Watch");
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
std::unique_ptr< EtcdAsyncGetCall > get_call_
virtual void Delete(const std::string &key, const std::string &range_end)
std::unique_ptr< KV::Stub > kv_stub_
std::vector< std::string > hosts_
boost::function< void(const EtcdResponse &Resp)> WatchCb
virtual void Set(const std::string &key, const std::string &value)
virtual void Watch(const std::string &key, WatchCb cb)
std::unique_ptr< EtcdAsyncDeleteCall > delete_call_
std::vector< Endpoint > endpoints_
std::unique_ptr< EtcdAsyncWatchCall > watch_call_
virtual EtcdResponse Get(const std::string &key, const std::string &range_end, int limit)
std::unique_ptr< Watch::Stub > watch_stub_
std::unique_ptr< EtcdAsyncSetCall > set_call_
void set_kv_map(kv_map kvs)
void set_prev_key(std::string prev_key)
WatchAction action() const
void set_val(std::string val)
void set_err_code(int code)
void set_key(std::string key)
const std::string & err_msg() const
const std::string & key() const
void set_err_msg(std::string msg)
void set_prev_val(std::string prev_val)
void set_revision(int revision)
const std::string & prev_value() const
const std::string & value() const
void set_action(WatchAction action)
const std::string & prev_key() const
void set_range_end(const ::std::string &value)
void set_key(const ::std::string &value)
void set_prev_kv(bool value)
void set_prev_kv(bool value)
void set_value(const ::std::string &value)
void set_key(const ::std::string &value)
void set_key(const ::std::string &value)
void set_sort_target(::etcdserverpb::RangeRequest_SortTarget value)
void set_sort_order(::etcdserverpb::RangeRequest_SortOrder value)
void set_range_end(const ::std::string &value)
void set_limit(::google::protobuf::int64 value)
const ::mvccpb::KeyValue & kvs(int index) const
const ::etcdserverpb::ResponseHeader & header() const
void set_range_end(const ::std::string &value)
void set_prev_kv(bool value)
void CopyFrom(const ::google::protobuf::Message &from) PROTOBUF_FINAL
void set_key(const ::std::string &value)
::etcdserverpb::WatchCreateRequest * mutable_create_request()
const ::std::string & key() const
const ::std::string & value() const
SandeshTraceBufferPtr EqlTraceBuf(SandeshTraceBufferCreate(EQL_TRACE_BUF, 10000))
#define EQL_TRACE(obj,...)
#define EQL_DEBUG(obj, arg)
boost::asio::ip::tcp::endpoint Endpoint
@ RangeRequest_SortTarget_KEY
@ RangeRequest_SortOrder_ASCEND
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
EtcdResponse ParseGetResponse()
void WaitForWatchResponse(WatchCb cb)