OpenSDN source code
config_etcd_client.h
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 #ifndef config_etcd_client_h
7 #define config_etcd_client_h
8 
9 #include <boost/ptr_container/ptr_map.hpp>
10 #include <boost/shared_ptr.hpp>
11 
12 #include "database/etcd/eql_if.h"
13 
14 #include <atomic>
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "base/queue_task.h"
23 #include "base/timer.h"
24 
25 #include "config_db_client.h"
27 #include "json_adapter_data.h"
28 
29 using namespace std;
32 using contrail_rapidjson::Document;
33 using contrail_rapidjson::Value;
34 
35 class EventManager;
37 struct ConfigDBConnInfo;
38 class TaskTrigger;
39 class ConfigEtcdClient;
40 class ConfigDBUUIDCacheEntry;
41 
43  public:
44  ConfigEtcdPartition(ConfigEtcdClient *client, size_t idx);
45  virtual ~ConfigEtcdPartition();
46 
47  typedef boost::shared_ptr<WorkQueue<ObjectProcessReq *> >
49 
51  public:
53  const string &value_str,
54  uint64_t last_read_tstamp)
55  : ObjectCacheEntry(last_read_tstamp),
56  retry_count_(0),
57  retry_timer_(NULL),
58  json_str_(value_str),
59  parent_(parent) {
60  }
61 
62  ~UUIDCacheEntry();
63 
64  void EnableEtcdReadRetry(const string uuid,
65  const string value);
66  void DisableEtcdReadRetry(const string uuid);
67 
68  const string &GetJsonString() const { return json_str_; }
69  void SetJsonString(const string &value_str) {
70  json_str_ = value_str;
71  }
72 
73  void SetListOrMapPropEmpty(const string &prop, bool empty) {
74  list_map_set_.insert(make_pair(prop.c_str(), empty));
75  }
76  bool ListOrMapPropEmpty(const string &prop) const;
77 
78  uint32_t GetRetryCount() const {
79  return retry_count_;
80  }
81  bool IsRetryTimerCreated() const {
82  return (retry_timer_ != NULL);
83  }
84  bool IsRetryTimerRunning() const;
85  Timer *GetRetryTimer() { return retry_timer_; }
86 
87  private:
88  friend class ConfigEtcdPartitionTest;
89  bool EtcdReadRetryTimerExpired(const string uuid,
90  const string value);
91  void EtcdReadRetryTimerErrorHandler();
92  typedef map<string, bool> ListMapSet;
94  uint32_t retry_count_;
96  string json_str_;
98  };
99 
100  static const uint32_t kMaxUUIDRetryTimePowOfTwo = 20;
101  static const uint32_t kMinUUIDRetryTimeMSec = 100;
102 
103  typedef boost::ptr_map<string, UUIDCacheEntry> UUIDCacheMap;
104 
105  UUIDCacheEntry *GetUUIDCacheEntry(const string &uuid);
106  const UUIDCacheEntry *GetUUIDCacheEntry(const string &uuid) const;
107  UUIDCacheEntry *GetUUIDCacheEntry(const string &uuid,
108  const string &value_str,
109  bool &is_new);
110  const UUIDCacheEntry *GetUUIDCacheEntry(const string &uuid,
111  const string &value_str,
112  bool &is_new) const;
113  void DeleteCacheMap(const string &uuid) {
114  uuid_cache_map_.erase(uuid);
115  }
116  virtual int UUIDRetryTimeInMSec(const UUIDCacheEntry *obj) const;
117 
118  void FillUUIDToObjCacheInfo(const string &uuid,
119  UUIDCacheMap::const_iterator uuid_iter,
120  ConfigDBUUIDCacheEntry *entry) const;
121  bool UUIDToObjCacheShow(
122  const string &search_string, const string &last_uuid,
123  uint32_t num_entries,
124  vector<ConfigDBUUIDCacheEntry> *entries) const;
125 
126  int GetInstanceId() const { return worker_id_; }
127 
129  return obj_process_queue_;
130  }
131 
132  void Enqueue(ObjectProcessReq *req);
133  bool IsListOrMapPropEmpty(const string &uuid_key,
134  const string &lookup_key);
135  virtual bool IsTaskTriggered() const;
136 
137 protected:
139  return config_client_;
140  }
141 
142 private:
143  friend class ConfigEtcdClient;
144 
146  UUIDProcessRequestType(const string &in_oper,
147  const string &in_uuid,
148  const string &in_value)
149  : oper(in_oper), uuid(in_uuid), value(in_value) {
150  }
151  string oper;
152  string uuid;
153  string value;
154  };
155 
156  typedef map<string, UUIDProcessRequestType *> UUIDProcessSet;
157 
158  bool RequestHandler(ObjectProcessReq *req);
159  void AddUUIDToProcessList(const string &oper,
160  const string &uuid_key,
161  const string &value_str);
162  bool ConfigReader();
163  void ProcessUUIDUpdate(const string &uuid_key,
164  const string &value_str);
165  void ProcessUUIDDelete(const string &uuid_key);
166  virtual bool GenerateAndPushJson(
167  const string &uuid_key,
168  Document &doc,
169  bool add_change,
170  UUIDCacheEntry *cache);
171  void RemoveObjReqEntry(string &uuid);
172 
176  boost::shared_ptr<TaskTrigger> config_reader_;
179 };
180 
181 /*
182  * This class has the functionality to interact with the cassandra servers that
183  * store the user configuration.
184  */
186  public:
187  typedef vector<ConfigEtcdPartition *> PartitionList;
188 
191  int num_workers);
192  virtual ~ConfigEtcdClient();
193 
194  virtual void InitDatabase();
195  void BulkSyncDone();
196  void EnqueueUUIDRequest(string oper, string obj_type,
197  string uuid_str);
198 
199  ConfigEtcdPartition *GetPartition(const string &uuid);
200  const ConfigEtcdPartition *GetPartition(const string &uuid) const;
201  const ConfigEtcdPartition *GetPartition(int worker_id) const;
202 
203  // Start ETCD watch for config updates
204  void StartWatcher();
205 
206  // UUID Cache
207  virtual bool UUIDToObjCacheShow(
208  const string &search_string, int inst_num,
209  const string &last_uuid, uint32_t num_entries,
210  vector<ConfigDBUUIDCacheEntry> *entries) const;
211 
212  virtual bool IsListOrMapPropEmpty(const string &uuid_key,
213  const string &lookup_key);
214 
215  bool IsTaskTriggered() const;
216  virtual void ProcessResponse(EtcdResponse resp);
217 
218  // For testing
219  static void set_watch_disable(bool disable) {
220  disable_watch_ = disable;
221  }
222 protected:
223  typedef pair<string, string> UUIDValueType;
224  typedef list<UUIDValueType> UUIDValueList;
225 
226  virtual bool BulkDataSync();
227  void EnqueueDBSyncRequest(const UUIDValueList &uuid_list);
228 
229  virtual int HashUUID(const std::string &uuid_str) const;
230  int num_workers() const { return num_workers_; }
231  PartitionList &partitions() { return partitions_; }
232  virtual void PostShutdown();
233 
234 private:
235  friend class ConfigEtcdPartition;
236 
237  // A Job for watching changes to config stored in etcd
238  class EtcdWatcher;
239 
240  bool InitRetry();
241  bool UUIDReader();
242  void HandleEtcdConnectionStatus(bool success,
243  bool force_update = false);
244 
245  // For testing
246  static bool disable_watch_;
247 
248  boost::scoped_ptr<EtcdIf> eqlif_;
251  boost::scoped_ptr<TaskTrigger> uuid_reader_;
252  std::atomic<long> bulk_sync_status_;
253 };
254 
255 #endif // config_etcd_client_h
PartitionList partitions_
pair< string, string > UUIDValueType
vector< ConfigEtcdPartition * > PartitionList
std::atomic< long > bulk_sync_status_
static void set_watch_disable(bool disable)
boost::scoped_ptr< EtcdIf > eqlif_
static bool disable_watch_
PartitionList & partitions()
list< UUIDValueType > UUIDValueList
boost::scoped_ptr< TaskTrigger > uuid_reader_
UUIDCacheEntry(ConfigEtcdPartition *parent, const string &value_str, uint64_t last_read_tstamp)
void SetJsonString(const string &value_str)
void SetListOrMapPropEmpty(const string &prop, bool empty)
boost::ptr_map< string, UUIDCacheEntry > UUIDCacheMap
void DeleteCacheMap(const string &uuid)
boost::shared_ptr< TaskTrigger > config_reader_
ConfigEtcdClient * config_client_
boost::shared_ptr< WorkQueue< ObjectProcessReq * > > UUIDProcessWorkQType
const UUIDCacheEntry * GetUUIDCacheEntry(const string &uuid, const string &value_str, bool &is_new) const
UUIDProcessWorkQType obj_process_queue_
ConfigEtcdClient * client()
UUIDProcessWorkQType obj_process_queue()
UUIDCacheMap uuid_cache_map_
map< string, UUIDProcessRequestType * > UUIDProcessSet
const UUIDCacheEntry * GetUUIDCacheEntry(const string &uuid) const
UUIDProcessSet uuid_process_set_
Definition: timer.h:58
static EventManager evm
UUIDProcessRequestType(const string &in_oper, const string &in_uuid, const string &in_value)
boost::uuids::uuid uuid