OpenSDN source code
eql_if.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3 // Copyright Contributors to the OpenSDN project
4 //
5 
6 #include <iostream>
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>
14 #include "proto/kv.pb.h"
15 #include "schema/vnc_cfg_types.h"
16 #include "eql_if.h"
17 #include "eql_log.h"
18 #include "base/address_util.h"
19 
20 using namespace std;
21 using namespace etcd::etcdql;
22 
23 using grpc::Channel;
28 
30  EQL_TRACE_BUF, 10000));
31 
32 EtcdIf::EtcdIf(const std::vector<std::string> &etcd_hosts,
33  const int port, bool useSsl)
34  : port_(port),
35  useSsl_(useSsl) {
36 
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;
41  etcd_addr = AddressFromString(etcd_host, &ec);
42  if (ec) {
43  EQL_DEBUG(EtcdClientDebug, "Invalid IP address");
44  }
45  Endpoint endpoint(etcd_addr, port);
46  endpoints_.push_back(endpoint);
47  }
48 
50 }
51 
53 }
54 
56  ostringstream url;
57 
58  BOOST_FOREACH(const std::string &etcd_host, hosts_) {
59  url << etcd_host << ":" << port_;
60 
61  shared_ptr<Channel> chan;
62 
63  if (useSsl_) {
64  auto channel_creds = grpc::SslCredentials(
65  grpc::SslCredentialsOptions());
66  chan = grpc::CreateChannel(url.str(), channel_creds);
67  } else {
68  chan = grpc::CreateChannel(
69  url.str(),
70  grpc::InsecureChannelCredentials());
71  }
72 
73  //if (chan->GetState(false) != GRPC_CHANNEL_READY) continue;
74 
75  kv_stub_ = KV::NewStub(chan);
76  watch_stub_ = Watch::NewStub(chan);
77  return true;
78  }
79 
80  return false;
81 }
82 
83 EtcdResponse EtcdIf::Get(string const& key,
84  string const& range_end,
85  int limit) {
86  ostringstream os;
87 
88  EQL_DEBUG(EtcdClientDebug, os << "Get Request - key: " << key
89  << " range_end: " << range_end
90  << " limit: " << limit);
91 
95  RangeRequest req;
96  req.set_key(key);
97  req.set_range_end(range_end);
100  req.set_limit(limit);
101 
105  get_call_.reset(new EtcdAsyncGetCall);
106  get_call_->get_reader_ = kv_stub_->AsyncRange(&get_call_->ctx_,
107  req,
108  &get_call_->cq_);
109  get_call_->get_reader_->Finish(&get_call_->get_resp_,
110  &get_call_->status_,
111  (static_cast<void *>(&get_call_->gtag_)));
112 
116  EtcdResponse resp = get_call_->ParseGetResponse();
117 
118  return resp;
119 }
120 
122  EtcdResponse resp;
123  void* got_tag;
124  bool ok = false;
125  ostringstream os;
126 
127  // Block until the next result is available in the completion queue "cq".
128  while (cq_.Next(&got_tag, &ok)) {
129 
130  /*
131  * The tag is the memory location of the call's tag object
132  */
137  if (!status_.ok()) {
138  resp.set_err_code(status_.error_code());
139  resp.set_err_msg(status_.error_message());
140  EQL_TRACE(EtcdClientErrorTrace, "Get Response: Error",
141  resp.err_code(), resp.err_msg());
142  break;
143  }
144 
145  if (got_tag == (static_cast<void *>(&gtag_))) {
146  /*
147  * Read the Get response.
148  */
149  resp.set_revision((get_resp_.header()).revision());
150  if(get_resp_.kvs_size() == 0) {
151  resp.set_err_code(100);
152  resp.set_err_msg("Prefix/Key not found");
153  EQL_TRACE(EtcdClientErrorTrace,
154  "Get Response: Prefix Not Found",
155  resp.err_code(),
156  resp.err_msg());
157  break;
158  } else {
159  multimap<string, string> kvs;
160  for(int i = 0; i < get_resp_.kvs_size(); i++) {
161  kvs.insert(pair<string, string>
162  (get_resp_.kvs(i).key(),
163  get_resp_.kvs(i).value()));
164  }
165  resp.set_kv_map(kvs);
166  }
167  }
168 
169  os.str("");
170  EQL_DEBUG(EtcdClientDebug, os << "Get Response: Success"
171  << " revision: "
172  << resp.revision()
173  << " KEY-VALUE LIST: ");
174 
175  for(int i = 0; i < get_resp_.kvs_size(); i++) {
176 
177  os.str("");
178  EQL_DEBUG(EtcdClientDebug, os << " Index: " << i
179  << " Key: "
180  << get_resp_.kvs(i).key()
181  << " Value: "
182  << get_resp_.kvs(i).value());
183  }
184 
185  break;
186  }
187 
188  return (resp);
189 }
190 
191 void EtcdIf::Set (const string& key, const string& value) {
192  void *got_tag;
193  bool ok = false;
194  ostringstream os;
195 
196  EQL_DEBUG(EtcdClientDebug, os << "Set Request - Key: "
197  << key
198  << " Value: "
199  << value);
200 
204  PutRequest req;
205  req.set_key(key);
206  req.set_value(value);
207  req.set_prev_kv(true);
208 
212  set_call_.reset(new EtcdAsyncSetCall);
213  set_call_->set_reader_ = kv_stub_->AsyncPut(&set_call_->ctx_,
214  req,
215  &set_call_->cq_);
216  set_call_->set_reader_->Finish(&set_call_->set_resp_,
217  &set_call_->status_,
218  (void*)this);
219 
223  while (set_call_->cq_.Next(&got_tag, &ok)) {
224 
225  if (!set_call_->status_.ok()) {
226  EQL_TRACE(EtcdClientErrorTrace,
227  "Set Response: Error",
228  set_call_->status_.error_code(),
229  set_call_->status_.error_message());
230  }
231 
232  if (got_tag == (void *)this) {
233  os.str("");
234  EQL_DEBUG(EtcdClientDebug, os << "Set Response: Success"
235  << " PrevKey: "
236  << (set_call_->set_resp_.prev_kv()).key()
237  << " PrevValue: "
238  << (set_call_->set_resp_.prev_kv()).value());
239  }
240  break;
241  }
242 }
243 
244 void EtcdIf::Delete (const string& key, string const& range_end) {
245  void *got_tag;
246  bool ok = false;
247  ostringstream os;
248 
249  EQL_DEBUG(EtcdClientDebug, os << "Delete Request - Key: "
250  << key
251  << " Range End: "
252  << range_end);
253 
257  DeleteRangeRequest req;
258  req.set_key(key);
259  req.set_range_end(range_end);
260  req.set_prev_kv(true);
261 
266  delete_call_->delete_reader_ = kv_stub_->AsyncDeleteRange(
267  &delete_call_->ctx_,
268  req,
269  &delete_call_->cq_);
270  delete_call_->delete_reader_->Finish(&delete_call_->delete_resp_,
271  &delete_call_->status_,
272  (void*)this);
273 
277  while (delete_call_->cq_.Next(&got_tag, &ok)) {
278  if (!delete_call_->status_.ok()) {
279  EQL_TRACE(EtcdClientErrorTrace,
280  "Delete Response: Error",
281  delete_call_->status_.error_code(),
282  delete_call_->status_.error_message());
283  }
284  if (got_tag == (void *)this) {
285 
286  os.str("");
287  EQL_DEBUG(EtcdClientDebug, os << "Delete Response: Success"
288  << " # Keys Deleted: "
289  << delete_call_->delete_resp_.deleted());
290 
291  for (int i = 0; i < delete_call_->delete_resp_.deleted(); i++) {
292 
293  os.str("");
294  EQL_DEBUG(EtcdClientDebug, os << " Index: " << i
295  << " PrevKey: "
296  << (delete_call_->delete_resp_.prev_kvs(i)).key()
297  << " PrevVal: "
298  << (delete_call_->delete_resp_.prev_kvs(i)).value());
299  }
300  }
301  break;
302  }
303 }
304 
305 void EtcdIf::Watch (const string& key, WatchCb cb) {
306  WatchRequest req;
307  WatchCreateRequest create_req;
308  void* got_tag;
309  bool ok = false;
310  ostringstream os;
311 
312  EQL_DEBUG(EtcdClientDebug, os << "Watch Request - Key: " << key);
313 
317  watch_call_->watch_reader_ = watch_stub_->AsyncWatch(&watch_call_->ctx_,
318  &watch_call_->cq_,
319  (void *)this);
320 
321  create_req.set_key(key);
322  create_req.set_prev_kv(true);
323 
324  string range_end(key);
325  range_end.back() = ((int)range_end[range_end.length()-1])+1;
326 
327  create_req.set_range_end(range_end);
328 
329  req.mutable_create_request()->CopyFrom(create_req);
330 
338  while (watch_call_->cq_.Next(&got_tag, &ok)) {
339  if (got_tag == (void *)this) {
340  watch_call_->watch_reader_->Write(req, (void *)"write");
341  watch_call_->watch_reader_->Read(&watch_call_->watch_resp_,
342  (void *)(&watch_call_->wtag));
343  break;
344  }
345  }
346 
347  /* Set bool indicating that watch has started */
348  watch_call_->watch_active_ = true;
349 
350  /* Wait for changes to happen and process them */
351  watch_call_->WaitForWatchResponse(cb);
352 }
353 
355  void* got_tag;
356  bool ok = false;
357  EtcdResponse resp;
358  ostringstream os;
359 
363  while(cq_.Next(&got_tag, &ok)) {
364 
374  if (!ok || !watch_active_) {
375  resp.set_err_code(10);
376  resp.set_err_msg("Watch RPC failed");
377 
378  EQL_TRACE(EtcdClientErrorTrace,
379  "Watch Response: Error",
380  resp.err_code(),
381  resp.err_msg());
382  break;
383  }
384 
385  if (got_tag == (void*)&wtag) {
390  if (watch_resp_.events_size()) {
391 
396  resp.set_revision(watch_resp_.header().revision());
397  for (int i = 0; i < watch_resp_.events_size(); i++) {
398  auto event = watch_resp_.events(i);
399 
400  switch (event.type()) {
402  if (event.kv().version() == 0) {
403  resp.set_action(CREATE);
404  } else {
405  resp.set_action(UPDATE);
406  }
407  break;
408  }
410  resp.set_action(DELETE);
411  break;
412  }
413  default:
414  break;
415  }
416  resp.set_key(event.kv().key());
417  resp.set_val(event.kv().value());
418 
419  if (event.has_prev_kv()) {
420  resp.set_prev_key(event.prev_kv().key());
421  resp.set_prev_val(event.prev_kv().value());
422  }
423 
424  EQL_DEBUG(EtcdClientDebug, os << "Watch Response: "
425  << "Success"
426  << " revision: "
427  << resp.revision()
428  << " action: "
429  << resp.action()
430  << " Key: "
431  << resp.key()
432  << " Value: "
433  << resp.value()
434  << " PrevKey: "
435  << resp.prev_key()
436  << " PrevValue: "
437  << resp.prev_value());
438 
442  cb(resp);
443  }
444  }
445 
449  watch_reader_->Read(&watch_resp_, (void*)&wtag);
450  }
451  } // while
452 }
453 
455  if (watch_call_->watch_active_) {
456  watch_call_->watch_active_ = false;
457  watch_call_->watch_reader_->WritesDone((void *)"Stop Watch");
458  }
459 }
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
std::unique_ptr< EtcdAsyncGetCall > get_call_
Definition: eql_if.h:140
virtual void Delete(const std::string &key, const std::string &range_end)
Definition: eql_if.cc:244
std::unique_ptr< KV::Stub > kv_stub_
Definition: eql_if.h:119
virtual bool Connect()
Definition: eql_if.cc:55
std::vector< std::string > hosts_
Definition: eql_if.h:116
virtual ~EtcdIf()
Definition: eql_if.cc:52
boost::function< void(const EtcdResponse &Resp)> WatchCb
Definition: eql_if.h:45
virtual void Set(const std::string &key, const std::string &value)
Definition: eql_if.cc:191
virtual void Watch(const std::string &key, WatchCb cb)
Definition: eql_if.cc:305
std::unique_ptr< EtcdAsyncDeleteCall > delete_call_
Definition: eql_if.h:160
std::vector< Endpoint > endpoints_
Definition: eql_if.h:115
int port() const
Definition: eql_if.h:107
std::unique_ptr< EtcdAsyncWatchCall > watch_call_
Definition: eql_if.h:172
virtual void StopWatch()
Definition: eql_if.cc:454
virtual EtcdResponse Get(const std::string &key, const std::string &range_end, int limit)
Definition: eql_if.cc:83
std::unique_ptr< Watch::Stub > watch_stub_
Definition: eql_if.h:120
std::unique_ptr< EtcdAsyncSetCall > set_call_
Definition: eql_if.h:150
void set_kv_map(kv_map kvs)
Definition: eql_if.h:234
void set_prev_key(std::string prev_key)
Definition: eql_if.h:224
int revision() const
Definition: eql_if.h:203
WatchAction action() const
Definition: eql_if.h:208
void set_val(std::string val)
Definition: eql_if.h:219
void set_err_code(int code)
Definition: eql_if.h:194
void set_key(std::string key)
Definition: eql_if.h:214
const std::string & err_msg() const
Definition: eql_if.h:198
const std::string & key() const
Definition: eql_if.h:213
void set_err_msg(std::string msg)
Definition: eql_if.h:199
void set_prev_val(std::string prev_val)
Definition: eql_if.h:229
void set_revision(int revision)
Definition: eql_if.h:204
const std::string & prev_value() const
Definition: eql_if.h:228
const std::string & value() const
Definition: eql_if.h:218
int err_code() const
Definition: eql_if.h:193
void set_action(WatchAction action)
Definition: eql_if.h:209
const std::string & prev_key() const
Definition: eql_if.h:223
void set_range_end(const ::std::string &value)
Definition: rpc.pb.h:10255
void set_key(const ::std::string &value)
Definition: rpc.pb.h:10202
void set_prev_kv(bool value)
Definition: rpc.pb.h:10308
void set_prev_kv(bool value)
Definition: rpc.pb.h:10086
void set_value(const ::std::string &value)
Definition: rpc.pb.h:10019
void set_key(const ::std::string &value)
Definition: rpc.pb.h:9966
void set_key(const ::std::string &value)
Definition: rpc.pb.h:9649
void set_sort_target(::etcdserverpb::RangeRequest_SortTarget value)
Definition: rpc.pb.h:9797
void set_sort_order(::etcdserverpb::RangeRequest_SortOrder value)
Definition: rpc.pb.h:9783
void set_range_end(const ::std::string &value)
Definition: rpc.pb.h:9702
void set_limit(::google::protobuf::int64 value)
Definition: rpc.pb.h:9755
const ::mvccpb::KeyValue & kvs(int index) const
Definition: rpc.pb.h:9903
const ::etcdserverpb::ResponseHeader & header() const
Definition: rpc.pb.h:9859
void set_range_end(const ::std::string &value)
Definition: rpc.pb.h:11584
void set_prev_kv(bool value)
Definition: rpc.pb.h:11695
void CopyFrom(const ::google::protobuf::Message &from) PROTOBUF_FINAL
Definition: rpc.pb.cc:10178
void set_key(const ::std::string &value)
Definition: rpc.pb.h:11531
::etcdserverpb::WatchCreateRequest * mutable_create_request()
Definition: rpc.pb.h:11460
const ::std::string & key() const
Definition: kv.pb.h:396
const ::std::string & value() const
Definition: kv.pb.h:491
SandeshTraceBufferPtr EqlTraceBuf(SandeshTraceBufferCreate(EQL_TRACE_BUF, 10000))
#define EQL_TRACE_BUF
Definition: eql_log.h:13
#define EQL_TRACE(obj,...)
Definition: eql_log.h:36
#define EQL_DEBUG(obj, arg)
Definition: eql_log.h:26
boost::asio::ip::tcp::endpoint Endpoint
Definition: eql_if.h:28
@ RangeRequest_SortTarget_KEY
Definition: rpc.pb.h:534
@ RangeRequest_SortOrder_ASCEND
Definition: rpc.pb.h:513
@ Event_EventType_PUT
Definition: kv.pb.h:66
@ Event_EventType_DELETE
Definition: kv.pb.h:67
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:47