OpenSDN source code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
instance_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef __AGENT_OPER_INSTANCE_MANAGER_H__
6 #define __AGENT_OPER_INSTANCE_MANAGER_H__
7 
8 #include <boost/uuid/uuid.hpp>
9 #include "cmn/agent_signal.h"
10 #include "db/db_table.h"
11 #include "oper/service_instance.h"
13 
14 class Agent;
15 class DB;
16 class InstanceState;
17 class InstanceTask;
18 class InstanceTaskQueue;
19 
21 #define INSTANCE_MANAGER_TRACE(obj, ...) \
22 do { \
23  InstanceManager##obj::TraceMsg(InstanceManagerTraceBuf, __FILE__, __LINE__, __VA_ARGS__);\
24 } while (false)
25 
26 /*
27  * Starts and stops network namespaces corresponding to service-instances.
28  *
29  * In order to prevent concurrency issues between the signal hanlder leveraged
30  * by this class and the db::task context specific methods are protected by
31  * a mutex.
32  */
34  public:
35  enum CmdType {
36  Start = 1,
38  };
39 
44  };
45 
48  : type(0)
49  , task(NULL)
50  , errors()
51  , error_val(0)
52  , task_queue(NULL)
53  {}
54 
55  int type;
56 
57  /*
58  * OnError variables
59  */
61  std::string errors;
62  int error_val;
63 
64  /*
65  * OnTimeout
66  */
68  };
69 
70  static const int kTimeoutDefault = 30;
71  static const int kWorkersDefault = 1;
72  static const int kReattemptsDefault = 2;
73 
76 
77  void Initialize(DB *database, const std::string &netns_cmd,
78  const std::string &docker_cmd, const int netns_workers,
79  const int netns_timeout);
80  void Terminate();
82 
84  bool StaleTimeout();
85  void SetStaleTimerInterval(int minutes);
87  void SetNamespaceStorePath(std::string path);
88  void SetNetNSCmd(const std::string &netns_cmd);
89 
90  private:
91  friend class InstanceManagerTest;
93 
94  void RegisterSigHandler();
95  void InitSigHandler(AgentSignal *signal);
96 
98  void StartServiceInstance(ServiceInstance *svc_instance,
99  InstanceState *state, bool update);
100  void StopServiceInstance(ServiceInstance *svc_instance,
101  InstanceState *state);
102 
104 
105  void OnError(InstanceTask *task, const std::string errors);
106  void OnExit(InstanceTask *task, const boost::system::error_code &ec);
108  ServiceInstance *svc_instance);
109  void UnregisterSvcInstance(ServiceInstance *svc_instance);
112 
113  InstanceTaskQueue *GetTaskQueue(const std::string &str);
115  void ScheduleNextTask(InstanceTaskQueue *task_queue);
116  bool StartTask(InstanceTaskQueue *task_queue, InstanceTask *task);
117 
119  void SetState(ServiceInstance *svc_instance, InstanceState *state);
120  void ClearState(ServiceInstance *svc_instance);
121  bool DeleteState(ServiceInstance *svc_instance);
123 
124  void SetLastCmdType(ServiceInstance *svc_instance, int last_cmd_type);
125  int GetLastCmdType(ServiceInstance *svc_instance) const;
126  void ClearLastCmdType(ServiceInstance *svc_instance);
127 
128  void OnTaskTimeout(InstanceTaskQueue *task_queue);
129 
133 
134  /*
135  * Clear all the state entries. Used only at process shutdown.
136  */
137  void StateClear();
138 
139  /*
140  * Event observer for changes in the "db.service-instance.0" table.
141  */
142  void EventObserver(DBTablePartBase *db_part, DBEntryBase *entry);
143 
145  std::string netns_cmd_;
149 
150  std::vector<InstanceTaskQueue *> task_queues_;
151  typedef std::map<InstanceTask *, ServiceInstance *> TaskSvcMap;
153  std::map<std::string, int> last_cmd_types_;
158  std::unique_ptr<NamespaceStaleCleaner> stale_cleaner_;
160 
161  std::vector<InstanceManagerAdapter *> adapters_;
162 
164 };
165 
166 class InstanceState : public DBState {
167 
168  public:
169  enum StatusType {
170  Starting = 1,
177  };
178 
179  InstanceState();
180 
181  void set_pid(const pid_t &pid) {
182  pid_ = pid;
183  }
184  pid_t pid() const {
185  return pid_;
186  }
187 
188  void set_status(const int status) {
189  status_ = status;
190  }
191  pid_t status() const {
192  return status_;
193  }
194 
195  void set_errors(const std::string &errors) {
196  errors_.append(errors);
197  }
198 
199  void reset_errors() {
200  errors_.clear();
201  }
202 
203  std::string errors() const {
204  return errors_;
205  }
206 
207  void set_cmd(const std::string &cmd) {
208  cmd_ = cmd;
209  }
210  std::string cmd() const {
211  return cmd_;
212  }
213 
216  }
218  return properties_;
219  }
220 
221  void set_status_type(const int status) {
223  }
224  int status_type() const {
225  return status_type_;
226  }
227 
228  int tasks_running() const {
229  return tasks_running_;
230  }
231 
233  return ++tasks_running_;
234  }
235 
237  tasks_running_--;
238  assert(!(tasks_running_ < 0));
239  return tasks_running_;
240  }
241 
242 
243  void Clear();
244 
245  private:
246  pid_t pid_;
247  int status_;
248  std::string errors_;
249  std::string cmd_;
252 
254 
255  boost::system::error_code ec_;
256 };
257 
258 #endif
void InitSigHandler(AgentSignal *signal)
void UpdateStateStatusType(InstanceManagerChildEvent event)
SandeshTraceBufferPtr InstanceManagerTraceBuf
std::map< std::string, int > last_cmd_types_
void SetNamespaceStorePath(std::string path)
void Enqueue(InstanceTask *task, const boost::uuids::uuid &uuid)
DISALLOW_COPY_AND_ASSIGN(InstanceManager)
void StopServiceInstance(ServiceInstance *svc_instance, InstanceState *state)
std::vector< InstanceTaskQueue * > task_queues_
TaskSvcMap task_svc_instances_
std::map< InstanceTask *, ServiceInstance * > TaskSvcMap
DBTableBase::ListenerId si_listener_
int ListenerId
Definition: db_table.h:62
void RegisterSvcInstance(InstanceTask *task, ServiceInstance *svc_instance)
void SetStaleTimerInterval(int minutes)
void StopStaleNetNS(ServiceInstance::Properties &props)
boost::uuids::uuid uuid
void UnregisterSvcInstance(ServiceInstance *svc_instance)
Definition: task_int.h:10
void set_cmd(const std::string &cmd)
void OnErrorEventHandler(InstanceManagerChildEvent event)
std::vector< InstanceManagerAdapter * > adapters_
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
void OnTaskTimeout(InstanceTaskQueue *task_queue)
void set_pid(const pid_t &pid)
std::string cmd() const
static const int kReattemptsDefault
std::string loadbalancer_config_path_
ServiceInstance::Properties properties_
void RegisterSigHandler()
Definition: db.h:24
InstanceTaskQueue * GetTaskQueue(const std::string &str)
InstanceState * GetState(ServiceInstance *) const
Definition: agent.h:358
WorkQueue< InstanceManagerChildEvent > work_queue_
pid_t pid() const
void StartServiceInstance(ServiceInstance *svc_instance, InstanceState *state, bool update)
ServiceInstance * GetSvcInstance(InstanceTask *task) const
void set_properties(const ServiceInstance::Properties &properties)
int GetLastCmdType(ServiceInstance *svc_instance) const
std::string cmd_
void OnExit(InstanceTask *task, const boost::system::error_code &ec)
std::string errors() const
void Initialize(DB *database, const std::string &netns_cmd, const std::string &docker_cmd, const int netns_workers, const int netns_timeout)
void ScheduleNextTask(InstanceTaskQueue *task_queue)
const ServiceInstance::Properties & properties() const
boost::system::error_code ec_
void set_status(const int status)
std::unique_ptr< NamespaceStaleCleaner > stale_cleaner_
bool StartTask(InstanceTaskQueue *task_queue, InstanceTask *task)
void OnTaskTimeoutEventHandler(InstanceManagerChildEvent event)
static const int kWorkersDefault
static const int kTimeoutDefault
void OnExitEventHandler(InstanceManagerChildEvent event)
int tasks_running() const
InstanceManagerAdapter * FindApplicableAdapter(const ServiceInstance::Properties &props)
std::string netns_cmd_
void OnError(InstanceTask *task, const std::string errors)
void EventObserver(DBTablePartBase *db_part, DBEntryBase *entry)
std::string namespace_store_path_
std::string errors_
void SetState(ServiceInstance *svc_instance, InstanceState *state)
Definition: timer.h:54
void ClearState(ServiceInstance *svc_instance)
void set_errors(const std::string &errors)
bool DequeueEvent(InstanceManagerChildEvent event)
friend class InstanceManagerTest
pid_t status() const
void SetNetNSCmd(const std::string &netns_cmd)
void SetLastCmdType(ServiceInstance *svc_instance, int last_cmd_type)
bool DeleteState(ServiceInstance *svc_instance)
int status_type() const
void ClearLastCmdType(ServiceInstance *svc_instance)
void set_status_type(const int status)