0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Master.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef Hyperspace_Master_h
23 #define Hyperspace_Master_h
24 
27 #include <Hyperspace/Protocol.h>
40 
41 #include <AsyncComm/Comm.h>
43 #include <AsyncComm/Event.h>
45 
46 #include <Common/Properties.h>
47 #include <Common/SockAddrMap.h>
48 #include <Common/Status.h>
49 #include <Common/StringExt.h>
50 #include <Common/Time.h>
51 
52 #include <chrono>
53 #include <memory>
54 #include <mutex>
55 #include <queue>
56 #include <unordered_map>
57 #include <vector>
58 
59 namespace Hyperspace {
60 
61  class Master {
62  public:
63 
64  enum { TIMER_INTERVAL_MS=1000 };
65 
66  /*
67  * Enumerate handle deletion states
68  * TODO: Add more states as part of Hyperspace state recovery. Hence don't use bool now
69  */
70  enum {
73  };
74 
76  ApplicationQueuePtr &app_queue_ptr);
77  ~Master();
78  bool is_master() {
79  if (m_bdb_fs)
80  return m_bdb_fs->is_master();
81  else
82  return false;
83  }
85  if (m_bdb_fs)
86  return m_bdb_fs->get_current_master();
87  else
88  return (String) "";
89  };
90  // Hyperspace command implementations
91  void mkdir(ResponseCallback *cb, uint64_t session_id, const char *name, const std::vector<Attribute>& init_attrs);
92  void mkdirs(ResponseCallback *cb, uint64_t session_id, const char *name, const std::vector<Attribute>& init_attrs);
93  void unlink(ResponseCallback *cb, uint64_t session_id, const char *name);
94  void open(ResponseCallbackOpen *cb, uint64_t session_id, const char *name,
95  uint32_t flags, uint32_t event_mask,
96  std::vector<Attribute> &init_attrs);
97  void close(ResponseCallback *cb, uint64_t session_id, uint64_t handle);
98  void attr_set(ResponseCallback *cb, uint64_t session_id, uint64_t handle,
99  const char *name, uint32_t oflags, const std::vector<Attribute> &attrs);
100  void attr_get(ResponseCallbackAttrGet *cb, uint64_t session_id,
101  uint64_t handle, const char *name, const std::vector<String> &attrs);
102  void attr_incr(ResponseCallbackAttrIncr *cb, uint64_t session_id,
103  uint64_t handle, const char *name, const char *attr);
104  void attr_del(ResponseCallback *cb, uint64_t session_id, uint64_t handle,
105  const char *name);
106  void attr_exists(ResponseCallbackAttrExists *cb, uint64_t session_id, uint64_t handle,
107  const char *name, const char *attr);
109  uint64_t session_id, uint64_t handle);
110  void exists(ResponseCallbackExists *cb, uint64_t session_id,
111  const char *name);
112  void readdir(ResponseCallbackReaddir *cb, uint64_t session_id,
113  uint64_t handle);
114  void readdir_attr(ResponseCallbackReaddirAttr *cb, uint64_t session_id,
115  uint64_t handle, const char *name, const char *attr,
116  bool include_sub_entries);
117  void readpath_attr(ResponseCallbackReadpathAttr *cb, uint64_t session_id,
118  uint64_t handle, const char *name, const char *attr);
119  void shutdown(ResponseCallback *cb, uint64_t session_id);
120  void status(ResponseCallbackStatus *cb);
121  void lock(ResponseCallbackLock *cb, uint64_t session_id, uint64_t handle,
122  uint32_t mode, bool try_lock);
123  void release(ResponseCallback *cb, uint64_t session_id, uint64_t handle);
124 
125  /*
126  * Creates a new session by allocating a new SessionData object, obtaining a
127  * new session ID and inserting the object into the Session map.
128  *
129  * @param addr Address from which the UDP client is sending
130  * @return the new session ID
131  */
132  uint64_t create_session(struct sockaddr_in &addr);
133 
134  /*
135  * Obtains the SessionData object for the given id from the session map.
136  *
137  * @param session_id Session ID to lookup
138  * @param session_data Reference to SessionData smart pointer
139  * @return true if found, false otherwise
140  */
141  bool get_session(uint64_t session_id, SessionDataPtr &session_data);
142 
143  void destroy_session(uint64_t session_id);
144  void initialize_session(uint64_t session_id, const String &name);
145 
146  /*
147  * Attempts to renew the session lease for session with the given ID. If
148  * the session cannot be found or if it is expired, the method returns
149  * Error::HYPERSPACE_EXPIRED_SESSION otherwise, it renews the session
150  * lease by invoking the RenewLease method of the SessionData object.
151  *
152  * @param session_id Session ID to renew
153  * @return Error::OK if successful
154  */
155  int renew_session_lease(uint64_t session_id);
156 
158  std::chrono::steady_clock::time_point now);
160 
161 
162  void get_datagram_send_address(struct sockaddr_in *addr) {
163  memcpy(addr, &m_local_addr, sizeof(m_local_addr));
164  }
165 
166  void tick() {
167  std::lock_guard<std::mutex> lock(m_last_tick_mutex);
168  m_last_tick = std::chrono::steady_clock::now();
169  }
170 
177  void handle_sleep();
178 
188  void handle_wakeup();
189 
190  void do_maintenance();
191 
192  private:
193 
194  struct EventContext {
198 
200  : persisted_notifications(false) {}
201 
203  : event(_event), persisted_notifications(false) {}
204 
205  void reset() {
206  event = 0;
207  notifications.clear();
208  persisted_notifications = false;
209  }
210  };
211 
212  struct CommandContext {
213  const char* friendly_name;
214  uint64_t session_id;
217  std::vector<EventContext> evts;
218  bool aborted;
219  int error;
221 
222  CommandContext(const char* _friendly_name, uint64_t _session_id)
223  : friendly_name(_friendly_name), session_id(_session_id), txn(0),
224  aborted(false), error(Error::OK) {}
225 
226  void set_error(int _error, const char *_error_msg, bool abort=true) {
227  error = _error;
228  error_msg = _error_msg;
229  aborted = abort;
230  }
231 
232  void set_error(int _error, const String &_error_msg, bool abort=true) {
233  error = _error;
234  error_msg = _error_msg;
235  aborted = abort;
236  }
237 
238  void reset_error() {
239  aborted = false;
240  error = Error::OK;
241  error_msg.clear();
242  }
243 
244  void reset(BDbTxn *_txn) {
245  session_data = 0;
246  txn = _txn;
247  evts.clear();
248  reset_error();
249  }
250  };
251 
252  void mkdir(CommandContext &ctx, const char *name);
253  void unlink(CommandContext &ctx, const char *name);
254  void open(CommandContext &ctx, const char *name,
255  uint32_t flags, uint32_t event_mask,
256  std::vector<Attribute> &init_attrs, uint64_t& handle,
257  bool& created, uint64_t& lock_generation);
258  void close(CommandContext &ctx, uint64_t handle);
259  void attr_set(CommandContext &ctx, uint64_t handle,
260  const char *name, const std::vector<Attribute> &attrs);
261  void attr_get(CommandContext &ctx, uint64_t handle,
262  const char *name, const char *attr, DynamicBuffer &dbuf);
263  void attr_get(CommandContext &ctx, uint64_t handle,
264  const char *name, const std::vector<String> &attrs,
265  std::vector<DynamicBufferPtr> &dbufs);
266  void attr_incr(CommandContext &ctx, uint64_t handle,
267  const char *name, const char *attr, uint64_t& attr_val);
268  void attr_del(CommandContext &ctx, uint64_t handle, const char *name);
269  void attr_exists(CommandContext& ctx, uint64_t handle,
270  const char *name, const char *attr, bool& exists);
271  void attr_list(CommandContext& ctx, uint64_t handle, std::vector<String>& attributes);
272  void exists(CommandContext& ctx, const char *name, bool& file_exists);
273  void readdir(CommandContext& ctx, uint64_t handle, std::vector<DirEntry>& listing);
274  void readdir_attr(CommandContext& ctx, uint64_t handle, const char *name, const char *attr,
275  bool include_sub_entries, std::vector<DirEntryAttr>& listing);
276  void readpath_attr(CommandContext& ctx, uint64_t handle, const char *name, const char *attr,
277  std::vector<DirEntryAttr>& listing);
278  bool get_handle_node(CommandContext &ctx, uint64_t handle, const char* attr, String &node);
279  bool get_named_node(CommandContext &ctx, const char *name, const char* attr, String &node, bool *is_dir=0);
280  void create_event(CommandContext &ctx, const String &node,
281  uint32_t event_mask, const String &name);
282  void deliver_event_notifications(CommandContext &ctx, bool wait_for_notify = true);
283  void deliver_event_notifications(EventContext &evt, bool wait_for_notify = true);
284 
285  void get_generation_number();
286 
287  void normalize_name(std::string name, std::string &normal);
289  NotificationMap &handles_to_sessions, bool wait_for_notify = true);
290  void persist_event_notifications(BDbTxn &txn, uint64_t event_id,
291  NotificationMap &handles_to_sessions);
292  void persist_event_notifications(BDbTxn &txn, uint64_t event_id, uint64_t handle);
293  bool validate_and_create_node_data(BDbTxn &txn, const String &node);
294  /*
295  * Locates the parent 'node' of the given pathname. It determines the name
296  * of the parent node by stripping off the characters incuding and after
297  * the last '/' character. It then looks up the name in the m_node_map and
298  * sets the pointer reference <code>parent_name</code> to it. As a side effect,
299  * it also saves the child name (e.g. characters after the last '/' character
300  * to the string reference child_name. NOTE: This method locks the
301  * m_node_map_mutex.
302  *
303  * @param normal_name Normalized (e.g. no trailing '/') name of path to
304  * find parent of
305  * @param parent_name reference to string to hold return parent name
306  * @param child_name reference to string to hold the child directory entry
307  * name
308  * @return true if parent node found, false otherwise
309  */
310  bool find_parent_node(const std::string &normal_name,
311  std::string &parent_name, std::string &child_name);
312  bool destroy_handle(uint64_t handle, int &error, String &errmsg,
313  bool wait_for_notify=true);
314  void release_lock(BDbTxn &txn, uint64_t handle, const String &node,
315  HyperspaceEventPtr &release_event, NotificationMap &release_notifications);
316  void lock_handle(BDbTxn &txn, uint64_t handle, uint32_t mode, String &node);
317  void lock_handle(BDbTxn &txn, uint64_t handle, uint32_t mode, const String &node);
318  void lock_handle_with_notification(uint64_t handle, uint32_t mode,
319  bool wait_for_notify=true);
320  void grant_pending_lock_reqs(BDbTxn &txn, const String &node,
321  HyperspaceEventPtr &lock_granted_event, NotificationMap &lock_granted_notifications,
322  HyperspaceEventPtr &lock_acquired_event, NotificationMap &lock_acquired_notifications);
323 
324  typedef std::vector<SessionDataPtr> SessionDataVec;
325  typedef std::unordered_map<uint64_t, SessionDataPtr> SessionMap;
326 
327  bool m_verbose;
331  std::string m_base_dir;
332  std::string m_lock_file;
334  uint32_t m_generation;
338  struct sockaddr_in m_local_addr;
339  SessionDataVec m_session_heap;
340  SessionMap m_session_map;
342 
347  std::chrono::steady_clock::time_point m_last_tick;
348 
350  std::chrono::steady_clock::time_point m_sleep_time;
351 
352  bool m_shutdown {};
353 
354  // BerkeleyDB state
356 
359 
360  };
361 
362  typedef std::shared_ptr<Master> MasterPtr;
363 
364 } // namespace Hyperspace
365 
366 #endif // Hyperspace_Master_h
bool validate_and_create_node_data(BDbTxn &txn, const String &node)
Definition: Master.cc:2526
Declarations for ResponseCallbackAttrExists.
bool next_expired_session(SessionDataPtr &, std::chrono::steady_clock::time_point now)
Definition: Master.cc:397
static std::mutex mutex
Definition: Logger.cc:43
void close(ResponseCallback *cb, uint64_t session_id, uint64_t handle)
Definition: Master.cc:686
std::chrono::steady_clock::time_point m_last_tick
Definition: Master.h:347
std::shared_ptr< MetricsHandler > MetricsHandlerPtr
Smart pointer to MetricsHandler.
void attr_set(ResponseCallback *cb, uint64_t session_id, uint64_t handle, const char *name, uint32_t oflags, const std::vector< Attribute > &attrs)
Definition: Master.cc:758
SessionMap m_session_map
Definition: Master.h:340
Holds Nagios-style program status information.
Definition: Status.h:42
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
uint64_t m_next_handle_number
Definition: Master.h:335
String get_current_master()
Returns hostname of currently elected master.
Declarations for Status.
std::unordered_map< uint64_t, SessionDataPtr > SessionMap
Definition: Master.h:325
void release_lock(BDbTxn &txn, uint64_t handle, const String &node, HyperspaceEventPtr &release_event, NotificationMap &release_notifications)
Definition: Master.cc:1509
void lock_handle(BDbTxn &txn, uint64_t handle, uint32_t mode, String &node)
Definition: Master.cc:1379
void status(ResponseCallbackStatus *cb)
Definition: Master.cc:1194
Declarations for Protocol.
Manages transaction state.
void unlink(ResponseCallback *cb, uint64_t session_id, const char *name)
Definition: Master.cc:600
struct sockaddr_in m_local_addr
Definition: Master.h:338
std::string m_base_dir
Definition: Master.h:331
bool m_maintenance_outstanding
Definition: Master.h:346
void open(ResponseCallbackOpen *cb, uint64_t session_id, const char *name, uint32_t flags, uint32_t event_mask, std::vector< Attribute > &init_attrs)
Definition: Master.cc:638
Program options handling.
bool get_session(uint64_t session_id, SessionDataPtr &session_data)
Definition: Master.cc:291
std::shared_ptr< SessionData > SessionDataPtr
Definition: SessionData.h:156
std::shared_ptr< Master > MasterPtr
Definition: Master.h:362
SessionDataVec m_session_heap
Definition: Master.h:339
std::vector< SessionDataPtr > SessionDataVec
Definition: Master.h:324
Declarations for BerkeleyDbFilesystem.
void initialize_session(uint64_t session_id, const String &name)
Definition: Master.cc:323
std::shared_ptr< Event > HyperspaceEventPtr
Definition: Event.h:145
std::vector< EventContext > evts
Definition: Master.h:217
void attr_incr(ResponseCallbackAttrIncr *cb, uint64_t session_id, uint64_t handle, const char *name, const char *attr)
Definition: Master.cc:883
Declarations for ResponseCallbackAttrGet.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
void attr_del(ResponseCallback *cb, uint64_t session_id, uint64_t handle, const char *name)
Definition: Master.cc:922
Declarations for Event.
void remove_expired_sessions()
Definition: Master.cc:423
std::mutex m_session_map_mutex
Definition: Master.h:343
Hyperspace definitions
void destroy_session(uint64_t session_id)
Definition: Master.cc:305
void grant_pending_lock_reqs(BDbTxn &txn, const String &node, HyperspaceEventPtr &lock_granted_event, NotificationMap &lock_granted_notifications, HyperspaceEventPtr &lock_acquired_event, NotificationMap &lock_acquired_notifications)
Definition: Master.cc:1554
void readdir(ResponseCallbackReaddir *cb, uint64_t session_id, uint64_t handle)
Definition: Master.cc:1054
bool is_master()
Definition: Master.h:78
void get_datagram_send_address(struct sockaddr_in *addr)
Definition: Master.h:162
void shutdown(ResponseCallback *cb, uint64_t session_id)
Definition: Master.cc:1160
void handle_wakeup()
Handle wakeup event (e.g.
Definition: Master.cc:1835
std::unordered_map< uint64_t, uint64_t > NotificationMap
Hash map from Node handle ID to Session ID.
Master(ConnectionManagerPtr &, PropertiesPtr &, ServerKeepaliveHandlerPtr &, ApplicationQueuePtr &app_queue_ptr)
Definition: Master.cc:133
std::string m_lock_file
Definition: Master.h:332
NotificationMap notifications
Definition: Master.h:196
Sends back result of an attr_exists request.
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
void readpath_attr(ResponseCallbackReadpathAttr *cb, uint64_t session_id, uint64_t handle, const char *name, const char *attr)
Definition: Master.cc:1129
void lock_handle_with_notification(uint64_t handle, uint32_t mode, bool wait_for_notify=true)
uint32_t m_maintenance_interval
Definition: Master.h:330
Status m_status
Program status tracker.
Definition: Master.h:358
void normalize_name(std::string name, std::string &normal)
std::shared_ptr< ServerKeepaliveHandler > ServerKeepaliveHandlerPtr
Sends back result of an attr_get request.
Time related declarations.
BerkeleyDbFilesystem * m_bdb_fs
Definition: Master.h:355
Declarations for SockAddrMap.
CommandContext(const char *_friendly_name, uint64_t _session_id)
Definition: Master.h:222
EventContext(HyperspaceEventPtr _event)
Definition: Master.h:202
Hyperspace filesystem implementation on top of BerkeleyDB.
This class is used to generate and deliver standard responses back to a client.
void get_generation_number()
Definition: Master.cc:2507
void do_maintenance()
Definition: Master.cc:1856
bool is_master()
Check if we're the current master.
std::chrono::steady_clock::time_point m_sleep_time
Suspension time recorded by handle_sleep()
Definition: Master.h:350
uint32_t m_generation
Definition: Master.h:334
uint64_t m_next_session_id
Definition: Master.h:336
void set_error(int _error, const String &_error_msg, bool abort=true)
Definition: Master.h:232
void attr_exists(ResponseCallbackAttrExists *cb, uint64_t session_id, uint64_t handle, const char *name, const char *attr)
Definition: Master.cc:956
void deliver_event_notifications(CommandContext &ctx, bool wait_for_notify=true)
Definition: Master.cc:2495
HyperspaceEventPtr event
Definition: Master.h:195
void mkdir(ResponseCallback *cb, uint64_t session_id, const char *name, const std::vector< Attribute > &init_attrs)
Definition: Master.cc:484
void attr_list(ResponseCallbackAttrList *cb, uint64_t session_id, uint64_t handle)
Definition: Master.cc:983
void attr_get(ResponseCallbackAttrGet *cb, uint64_t session_id, uint64_t handle, const char *name, const std::vector< String > &attrs)
Definition: Master.cc:831
Declarations for ConnectionManager.
void lock(ResponseCallbackLock *cb, uint64_t session_id, uint64_t handle, uint32_t mode, bool try_lock)
Definition: Master.cc:1203
std::mutex m_maintenance_mutex
Definition: Master.h:345
bool find_parent_node(const std::string &normal_name, std::string &parent_name, std::string &child_name)
Definition: Master.cc:1695
Declarations for Comm.
uint64_t create_session(struct sockaddr_in &addr)
Definition: Master.cc:263
void readdir_attr(ResponseCallbackReaddirAttr *cb, uint64_t session_id, uint64_t handle, const char *name, const char *attr, bool include_sub_entries)
Definition: Master.cc:1090
int renew_session_lease(uint64_t session_id)
Definition: Master.cc:354
bool get_named_node(CommandContext &ctx, const char *name, const char *attr, String &node, bool *is_dir=0)
Definition: Master.cc:2430
uint32_t m_lease_interval
Definition: Master.h:328
uint32_t m_keep_alive_interval
Definition: Master.h:329
std::mutex m_last_tick_mutex
Definition: Master.h:344
void reset(BDbTxn *_txn)
Definition: Master.h:244
void create_event(CommandContext &ctx, const String &node, uint32_t event_mask, const String &name)
Definition: Master.cc:2474
String get_current_master()
Definition: Master.h:84
void set_error(int _error, const char *_error_msg, bool abort=true)
Definition: Master.h:226
void release(ResponseCallback *cb, uint64_t session_id, uint64_t handle)
Definition: Master.cc:1416
ServerKeepaliveHandlerPtr m_keepalive_handler_ptr
Definition: Master.h:337
Declarations for MetricsHandler.
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
Declarations for ResponseCallback.
std::shared_ptr< ApplicationQueue > ApplicationQueuePtr
Shared smart pointer to ApplicationQueue object.
MetricsHandlerPtr m_metrics_handler
Definition: Master.h:341
String extensions and helpers: sets, maps, append operators etc.
void exists(ResponseCallbackExists *cb, uint64_t session_id, const char *name)
Definition: Master.cc:1017
bool get_handle_node(CommandContext &ctx, uint64_t handle, const char *attr, String &node)
Definition: Master.cc:2391
void handle_sleep()
Handle sleep event (e.g.
Definition: Master.cc:1829
bool destroy_handle(uint64_t handle, int &error, String &errmsg, bool wait_for_notify=true)
Definition: Master.cc:1734
void mkdirs(ResponseCallback *cb, uint64_t session_id, const char *name, const std::vector< Attribute > &init_attrs)
Definition: Master.cc:524
void persist_event_notifications(BDbTxn &txn, uint64_t event_id, NotificationMap &handles_to_sessions)
Definition: Master.cc:1621