OpenSDN source code
config_client_manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
6 
7 #include <boost/assign/list_of.hpp>
8 #include <sandesh/request_pipeline.h>
9 #include <sstream>
10 #include <string>
11 
12 #include "base/connection_info.h"
13 #include "base/task.h"
14 #include "base/task_trigger.h"
15 #include "config_amqp_client.h"
16 #include "config_db_client.h"
18 #include "config_client_log.h"
19 #include "config_client_log_types.h"
20 #include "config_client_show_types.h"
21 #include "config_factory.h"
22 #include "io/event_manager.h"
23 #include "schema/bgp_schema_types.h"
24 #include "schema/vnc_cfg_types.h"
25 
26 using namespace boost::assign;
27 using namespace std;
28 
29 const set<string> ConfigClientManager::skip_properties =
30  list_of("perms2")("draft_mode_state");
32 
34  static bool init_ = false;
35  static int num_config_readers = 0;
36 
37  if (!init_) {
38  // XXX To be used for testing purposes only.
39  char *count_str = getenv("CONFIG_NUM_WORKERS");
40  if (count_str) {
41  num_config_readers = strtol(count_str, NULL, 0);
42  } else {
43  num_config_readers = kNumConfigReaderTasks;
44  }
45  init_ = true;
46  }
47  return num_config_readers;
48 }
49 
51  static bool config_policy_set;
52  if (config_policy_set)
53  return;
54  config_policy_set = true;
55 
57  // Policy for config_client::Reader Task.
58  TaskPolicy cassadra_reader_policy = boost::assign::list_of
59  (TaskExclusion(scheduler->GetTaskId("config_client::Init")))
60  (TaskExclusion(scheduler->GetTaskId("config_client::DBReader")));
61  for (int idx = 0; idx < ConfigClientManager::GetNumConfigReader(); ++idx) {
62  cassadra_reader_policy.push_back(
63  TaskExclusion(scheduler->GetTaskId("config_client::ObjectProcessor"), idx));
64  }
65  scheduler->SetPolicy(scheduler->GetTaskId("config_client::Reader"),
66  cassadra_reader_policy);
67 
68  // Policy for config_client::ObjectProcessor Task.
69  TaskPolicy cassadra_obj_process_policy = boost::assign::list_of
70  (TaskExclusion(scheduler->GetTaskId("config_client::Init")));
71  for (int idx = 0; idx < ConfigClientManager::GetNumConfigReader(); ++idx) {
72  cassadra_obj_process_policy.push_back(
73  TaskExclusion(scheduler->GetTaskId("config_client::Reader"), idx));
74  }
75  scheduler->SetPolicy(scheduler->GetTaskId("config_client::ObjectProcessor"),
76  cassadra_obj_process_policy);
77 
78  // Policy for config_client::DBReader Task.
79  TaskPolicy fq_name_reader_policy = boost::assign::list_of
80  (TaskExclusion(scheduler->GetTaskId("config_client::Init")))
81  (TaskExclusion(scheduler->GetTaskId("config_client::Reader")));
82  scheduler->SetPolicy(scheduler->GetTaskId("config_client::DBReader"),
83  fq_name_reader_policy);
84 
85  // Policy for config_client::Init process
86  TaskPolicy cassandra_init_policy = boost::assign::list_of
87  (TaskExclusion(scheduler->GetTaskId("amqp::RabbitMQReader")))
88  (TaskExclusion(scheduler->GetTaskId("config_client::ObjectProcessor")))
89  (TaskExclusion(scheduler->GetTaskId("config_client::DBReader")))
90  (TaskExclusion(scheduler->GetTaskId("config_client::Reader")));
91  scheduler->SetPolicy(scheduler->GetTaskId("config_client::Init"),
92  cassandra_init_policy);
93 
94  // Policy for amqp::RabbitMQReader process
95  TaskPolicy rabbitmq_reader_policy = boost::assign::list_of
96  (TaskExclusion(scheduler->GetTaskId("config_client::Init")));
97  scheduler->SetPolicy(scheduler->GetTaskId("amqp::RabbitMQReader"),
98  rabbitmq_reader_policy);
99 
100  // Policy for etcd::EtcdWatcher process
101  TaskPolicy etcd_watcher_policy = boost::assign::list_of
102  (TaskExclusion(scheduler->GetTaskId("config_client::Init")));
103  scheduler->SetPolicy(scheduler->GetTaskId("etcd::EtcdWatcher"),
104  etcd_watcher_policy);
105 
106 }
107 
109  config_json_parser_.reset(cfg_json_base);
110  config_json_parser_->Init(this);
111  thread_count_ = GetNumConfigReader();
112  end_of_rib_computed_at_ = UTCTimestampUsec();
113  if (!config_options_.config_db_use_etcd) {
114  config_db_client_.reset(
115  ConfigStaticObjectFactory::CreateRef<ConfigCassandraClient>(
116  this,
117  evm_,
118  config_options_,
119  thread_count_));
120  config_amqp_client_.reset(new ConfigAmqpClient(this, hostname_,
121  module_name_, config_options_));
122  }
123  SetDefaultSchedulingPolicy();
124 
125  int task_id;
126  task_id = TaskScheduler::GetInstance()->GetTaskId("config_client::Init");
127 
128  init_trigger_.reset(new
130  task_id, 0));
131 
132  reinit_triggered_ = false;
133 }
134 
136  ConfigJsonParserBase *cfg_json_base,
137  std::string hostname,
138  std::string module_name,
139  const ConfigClientOptions& config_options)
140  : evm_(evm),
141  generation_number_(0),
142  hostname_(hostname), module_name_(module_name),
143  config_options_(config_options) {
144  end_of_rib_computed_ = false;
145  SetUp(cfg_json_base);
146 }
147 
149 }
150 
152  if (init_trigger_.get() != nullptr) {
153  init_trigger_->Set();
154  }
155 }
156 
158  return config_db_client_.get();
159 }
160 
162  return config_amqp_client_.get();
163 }
164 
166  std::scoped_lock lock(end_of_rib_sync_mutex_);
167  return end_of_rib_computed_;
168 }
169 
171  std::scoped_lock lock(end_of_rib_sync_mutex_);
173 }
174 
175 void ConfigClientManager::EnqueueUUIDRequest(string oper, string obj_type,
176  string uuid_str) {
177  config_db_client_->EnqueueUUIDRequest(oper, obj_type, uuid_str);
178 }
179 
181  {
182  // Notify waiting caller with the result
183  std::scoped_lock lock(end_of_rib_sync_mutex_);
184  assert(!end_of_rib_computed_);
185  end_of_rib_computed_ = true;
186  cond_var_.notify_all();
188  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug,
189  "Config Client Mgr SM: End of RIB computed and notification sent");
190  }
191 
192  // Once we have finished reading the complete cassandra DB, we should verify
193  // whether all DBEntries(node/link) are as per the new generation number.
194  // The stale entry cleanup task ensure this.
195  // There is no need to run stale clean up during first time startup
196  if (GetGenerationNumber())
198 
200 }
201 
202 // This function waits forever for bulk sync of cassandra config to finish
203 // The condition variable is triggered even in case of "reinit". In such a case
204 // wait is terminated and function returns.
205 // AMQP reader task starts consuming messages only after bulk sync.
206 // During reinit, the tight loop is broken by triggering the condition variable
208  std::unique_lock<std::mutex> lock(end_of_rib_sync_mutex_);
209  // Wait for End of config
210  while (!end_of_rib_computed_) {
211  cond_var_.wait(lock);
212  if (is_reinit_triggered()) break;
213  }
214  string message;
215  message = "Config Client Mgr SM: End of RIB notification received, "
216  "re init triggered" + is_reinit_triggered()?"TRUE":"FALSE";
217  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug, message);
218  return;
219 }
220 
222  ConfigClientManagerInfo &info) const {
223  std::scoped_lock lock(end_of_rib_sync_mutex_);
224  info.end_of_rib_computed = end_of_rib_computed_;
225  info.end_of_rib_computed_at = end_of_rib_computed_at_;
226  info.end_of_rib_computed_at = UTCUsecToString(end_of_rib_computed_at_);
227 }
228 
230  config_db_client_->PostShutdown();
231  reinit_triggered_ = false;
232  end_of_rib_computed_ = false;
233 
234  // All set to read next version of the config. Increment the generation
236 
237  // scoped ptr reset deletes the previous config db object
238  // Create new config db client and amqp client
239  // Delete of config db client object guarantees the flusing of
240  // object uuid cache and uuid read request list.
243  CreateRef<ConfigCassandraClient>(
244  this,
245  evm_,
247  thread_count_));
250  }
251  stringstream ss;
252  ss << GetGenerationNumber();
253  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug,
254  "Config Client Mgr SM: Post shutdown, next version of config: "
255  + ss.str());
256 }
257 
259  if (is_reinit_triggered()) {
260  // "config_client::Init" task is mutually exclusive to
261  // 1. FQName reader task
262  // 2. Object UUID Table reader task
263  // 3. AMQP reader task
264  // 4. Object processing Work queue task
265  // Due to this task policy, if the reinit task is running, it ensured
266  // that above mutually exclusive tasks have finished/aborted
267  // Perform PostShutdown to prepare for new connection
268  // However, it is possible that these tasks have been scheduled
269  // but yet to begin execution. For Task and WorkQueue events, their
270  // destructor takes core of this but for TaskTrigger events, the
271  // destructor will crash (See Bug #1786154) as it expects the task
272  // to not be scheduled. Taking care of that case here by checking
273  // if TaskTrigger events are scheduled (but not executing) and
274  // return if they are so that this will be retried.
275  if (config_db_client_->IsTaskTriggered()) {
276  return false;
277  }
278  PostShutdown();
279  }
280  // Common code path for both init/reinit
282  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug,
283  "Config Client Mgr SM: Start RabbitMqReader and init Database");
284  config_amqp_client_->StartRabbitMQReader();
285  }
286  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug,
287  "Config Client Mgr SM: Init Database");
288  config_db_client_->InitDatabase();
289  if (is_reinit_triggered()) return false;
290  return true;
291 }
292 
294  const ConfigClientOptions &config) {
295  config_options_ = config;
297 }
298 
300  {
301  // Wake up the amqp task waiting for EOR for config reading
302  std::scoped_lock lock(end_of_rib_sync_mutex_);
303  cond_var_.notify_all();
304  }
305  reinit_triggered_ = true;
306  if (init_trigger_.get() != nullptr) {
307  init_trigger_->Set();
308  }
309  CONFIG_CLIENT_DEBUG(ConfigClientMgrDebug,
310  "Config Client Mgr SM: Re init triggered!");
311 }
void EnqueueUUIDRequest(std::string oper, std::string obj_type, std::string uuid_str)
static const std::set< std::string > skip_properties
ConfigDbClient * config_db_client() const
ConfigClientOptions config_options_
boost::scoped_ptr< TaskTrigger > init_trigger_
uint64_t GetGenerationNumber() const
boost::scoped_ptr< ConfigAmqpClient > config_amqp_client_
uint64_t GetEndOfRibComputedAt() const
ConfigJsonParserBase * config_json_parser()
uint64_t IncrementGenerationNumber()
ConfigAmqpClient * config_amqp_client() const
void GetClientManagerInfo(ConfigClientManagerInfo &info) const
ConfigClientManager(EventManager *evm, ConfigJsonParserBase *cfg_json_base, std::string hostname, std::string module_name, const ConfigClientOptions &config_options)
boost::scoped_ptr< ConfigDbClient > config_db_client_
void SetUp(ConfigJsonParserBase *)
std::atomic< bool > reinit_triggered_
std::condition_variable cond_var_
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
void SetPolicy(int task_id, TaskPolicy &policy)
Sets the task exclusion policy. Adds policy entries for the task Examples:
Definition: task.cc:637
static TaskScheduler * GetInstance()
Definition: task.cc:574
static ConnectionState * GetInstance()
#define CONFIG_CLIENT_DEBUG(obj,...)
static EventManager evm
The class is used to specify a Task label for formulating a task exclusion list (an execution policy)...
Definition: task.h:249
std::vector< TaskExclusion > TaskPolicy
Defines a type to store an execution policy (a list of task exclusions).
Definition: task.h:273
static std::string UTCUsecToString(uint64_t tstamp)
Definition: time_util.h:55
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14