0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Session.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_Session_h
23 #define Hyperspace_Session_h
24 
26 #include <Hyperspace/DirEntry.h>
31 #include <Hyperspace/Protocol.h>
32 
33 #include <AsyncComm/Comm.h>
34 #include <AsyncComm/CommBuf.h>
37 
38 #include <Common/DynamicBuffer.h>
39 #include <Common/Properties.h>
40 #include <Common/Status.h>
41 #include <Common/String.h>
42 #include <Common/Timer.h>
43 
44 #include <chrono>
45 #include <condition_variable>
46 #include <memory>
47 #include <mutex>
48 #include <unordered_map>
49 #include <vector>
50 
51 namespace Hypertable {
52  class SleepWakeNotifier;
53 }
54 
56 namespace Hyperspace {
57 
62  using namespace std;
69  enum {
71  OPEN_FLAG_READ = 0x00001,
73  OPEN_FLAG_WRITE = 0x00002,
75  OPEN_FLAG_LOCK = 0x00004,
77  OPEN_FLAG_CREATE = 0x00008,
79  OPEN_FLAG_EXCL = 0x00010,
81  OPEN_FLAG_TEMP = 0x00020,
86  };
87 
95  public:
96  SessionCallback() : m_id(0){}
97  virtual ~SessionCallback() { return; }
98  virtual void safe() = 0;
99  virtual void expired() = 0;
100  virtual void jeopardy() = 0;
101  virtual void disconnected() = 0;
102  virtual void reconnected() = 0;
103  void set_id(uint32_t id) {m_id = id;}
104  uint32_t get_id() {return m_id;}
105  private:
106  uint32_t m_id;
107  };
108 
148  class Session {
149 
150  public:
151 
155  enum State {
163  STATE_DISCONNECTED
164  };
165 
166  enum Locate {
169  };
170 
181  Session(Comm *comm, PropertiesPtr &props);
182 
183  virtual ~Session();
184 
188  void add_callback(SessionCallback *cb);
189 
193  bool remove_callback(SessionCallback *cb);
194 
207  uint64_t open(const std::string &name, uint32_t flags,
208  HandleCallbackPtr &callback, Timer *timer = 0);
209 
219  uint64_t open(const std::string &name, uint32_t flags, Timer *timer = 0);
220 
237  uint64_t create(const std::string &name, uint32_t flags,
238  HandleCallbackPtr &callback,
239  const std::vector<Attribute> &init_attrs, Timer *timer=0);
240 
241  /*
242  int cancel(uint64_t handle);
243  int poison(uint64_t handle);
244  */
245 
251  void close(uint64_t handle, Timer *timer=0);
252 
258  void close_nowait(uint64_t handle);
259 
269  void mkdir(const std::string &name, Timer *timer=0);
270 
282  void mkdir(const std::string &name, const std::vector<Attribute> &init_attrs, Timer *timer=0);
283 
291  void mkdirs(const std::string &name, Timer *timer=0);
292 
302  void mkdirs(const std::string &name, const std::vector<Attribute> &init_attrs, Timer *timer=0);
303 
312  void attr_set(uint64_t handle, const std::string &attr,
313  const void *value, size_t value_len, Timer *timer=0);
314 
321  void attr_set(uint64_t handle, const std::vector<Attribute> &attrs,
322  Timer *timer=0);
323 
332  void attr_set(const std::string &name, const std::string &attr,
333  const void *value, size_t value_len, Timer *timer=0);
334 
344  void attr_set(const std::string &name, uint32_t oflags, const std::string &attr,
345  const void *value, size_t value_len, Timer *timer=0);
346 
354  void attr_set(const std::string &name, uint32_t oflags,
355  const std::vector<Attribute> &attrs, Timer *timer=0);
356 
364  uint64_t attr_incr(uint64_t handle, const std::string &attr, Timer *timer=0);
365 
373  uint64_t attr_incr(const std::string &name, const std::string &attr, Timer *timer=0);
374 
381  void attr_list(uint64_t handle, vector<String> &anames, Timer *timer=0);
382 
383  bool attr_exists(uint64_t handle, const std::string& attr, Timer *timer=0);
384  bool attr_exists(const std::string &name, const std::string& attr, Timer *timer=0);
385 
396  void attr_get(uint64_t handle, const std::string &attr,
397  DynamicBuffer &value, Timer *timer=0);
398 
409  void attr_get(const std::string &name, const std::string &attr,
410  DynamicBuffer &value, Timer *timer=0);
411 
423  void attr_get(const std::string &name, const std::string &attr,
424  bool& attr_exists, DynamicBuffer &value, Timer *timer=0);
425 
436  void attrs_get(uint64_t handle, const std::vector<std::string> &attrs,
437  std::vector<DynamicBufferPtr> &values, Timer *timer=0);
438 
449  void attrs_get(const std::string &name, const std::vector<std::string> &attrs,
450  std::vector<DynamicBufferPtr> &values, Timer *timer=0);
451 
458  void attr_del(uint64_t handle, const std::string &name, Timer *timer=0);
459 
466  bool exists(const std::string &name, Timer *timer=0);
467 
474  void unlink(const std::string &name, Timer *timer=0);
475 
484  void readdir(uint64_t handle, std::vector<DirEntry> &listing,
485  Timer *timer=0);
486 
498  void readdir_attr(uint64_t handle, const std::string &attr, bool include_sub_entries,
499  std::vector<DirEntryAttr> &listing, Timer *timer=0);
500 
512  void readdir_attr(const std::string &name, const std::string &attr, bool include_sub_entries,
513  std::vector<DirEntryAttr> &listing, Timer *timer=0);
514 
526  void readpath_attr(uint64_t handle, const std::string &attr,
527  std::vector<DirEntryAttr> &listing, Timer *timer=0);
528 
540  void readpath_attr(const std::string &name, const std::string &attr,
541  std::vector<DirEntryAttr> &listing, Timer *timer=0);
542 
559  void lock(uint64_t handle, LockMode mode,
560  LockSequencer *sequencerp, Timer *timer=0);
561 
582  void try_lock(uint64_t handle, LockMode mode, LockStatus *statusp,
583  LockSequencer *sequencerp, Timer *timer=0);
584 
590  void release(uint64_t handle, Timer *timer=0);
591 
598  void get_sequencer(uint64_t handle, LockSequencer *sequencerp,
599  Timer *timer=0);
600 
609  void check_sequencer(LockSequencer &sequencer, Timer *timer=0);
610 
611 
615  String locate(int type);
616 
623  int status(Status &status, Timer *timer=0);
624 
630  bool wait_for_connection(uint32_t max_wait_ms);
631 
637  bool wait_for_connection(Timer &timer);
638 
643  void set_verbose_flag(bool verbose) { m_verbose = verbose; }
644 
650  int state_transition(int state);
651 
656  int get_state();
657 
663  bool expired();
664 
670  HsCommandInterpreterPtr create_hs_interpreter();
671 
672  void advance_expire_time(std::chrono::steady_clock::time_point now) {
673  std::lock_guard<std::mutex> lock(m_mutex);
674  m_expire_time = now + std::chrono::milliseconds(m_lease_interval);
675  }
676 
677  void update_master_addr(const String &host);
678 
682  void handle_sleep();
683 
688  void handle_wakeup();
689 
694  void shutdown(Timer *timer=0);
695 
696  private:
697 
698  typedef std::unordered_map<uint64_t, SessionCallback *> CallbackMap;
699 
700  void mkdir(const std::string &name, bool create_intermediate, const std::vector<Attribute> *init_attrs, Timer *timer);
701  void decode_listing(Hypertable::EventPtr& event_ptr, std::vector<DirEntryAttr> &listing);
702  void decode_value(Hypertable::EventPtr& event_ptr, DynamicBuffer &value);
703  void decode_values(Hypertable::EventPtr& event_ptr, std::vector<DynamicBufferPtr> &values);
704  bool wait_for_safe();
705  int send_message(CommBufPtr &, DispatchHandler *, Timer *timer);
706  void normalize_name(const std::string &name, std::string &normal);
707  uint64_t open(ClientHandleStatePtr &, CommBufPtr &, Timer *timer);
708 
710  std::condition_variable m_cond;
713  bool m_verbose;
714  bool m_silent;
717  int m_state;
718  uint32_t m_grace_period;
720  uint32_t m_timeout_ms;
721  std::chrono::steady_clock::time_point m_expire_time;
724  CallbackMap m_callbacks;
727  vector<String> m_hyperspace_replicas;
729 
732  };
733 
734  typedef std::shared_ptr<Session> SessionPtr;
735 
736  void close_handle(SessionPtr hyperspace, uint64_t handle);
737  void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep);
738 
741 } // namespace Hyperspace
742 
743 #endif // Hyperspace_Session_h
uint16_t m_hyperspace_port
Definition: Session.h:716
static std::mutex mutex
Definition: Logger.cc:43
std::shared_ptr< HsCommandInterpreter > HsCommandInterpreterPtr
A callback object derived from this class gets passed into the constructor of Hyperspace.
Definition: Session.h:94
Holds Nagios-style program status information.
Definition: Status.h:42
Delivers sleep and wakeup notifications.
std::shared_ptr< ClientKeepaliveHandler > ClientKeepaliveHandlerPtr
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Declarations for Status.
void set_verbose_flag(bool verbose)
Sets verbose flag.
Definition: Session.h:643
Declarations for Protocol.
Abstract base class for application dispatch handlers registered with AsyncComm.
Error if create and file exists.
Definition: Session.h:79
void advance_expire_time(std::chrono::steady_clock::time_point now)
Definition: Session.h:672
Program options handling.
SleepWakeNotifier * m_sleep_wake_notifier
Delivers suspend/resume notifications (e.g. laptop close/open).
Definition: Session.h:731
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
LockStatus
Lock status.
Definition: LockSequencer.h:56
STL namespace.
std::mutex m_mutex
Definition: Session.h:709
std::unordered_map< uint64_t, SessionCallback * > CallbackMap
Definition: Session.h:698
session has expired
Definition: Session.h:157
CallbackMap m_callbacks
Definition: Session.h:724
Atomically open and lock file shared, fail if can't.
Definition: Session.h:83
InetAddr m_master_addr
Definition: Session.h:722
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
uint64_t m_last_callback_id
Definition: Session.h:725
Hyperspace definitions
State
Session state values
Definition: Session.h:155
uint32_t m_lease_interval
Definition: Session.h:719
Open file for locking.
Definition: Session.h:75
vector< String > m_hyperspace_replicas
Definition: Session.h:727
Declarations for DispatchHandler.
A dynamic, resizable memory buffer.
Open file for writing.
Definition: Session.h:73
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
void set_id(uint32_t id)
Definition: Session.h:103
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
Definition: Utility.cc:408
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
uint32_t m_timeout_ms
Definition: Session.h:720
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
A timer class to keep timeout states across AsyncComm related calls.
void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep)
Definition: Session.cc:1400
std::chrono::steady_clock::time_point m_expire_time
Definition: Session.h:721
PropertiesPtr m_cfg
Definition: Session.h:712
void close_handle(SessionPtr hyperspace, uint64_t handle)
Definition: Session.cc:1395
ClientKeepaliveHandlerPtr m_keepalive_handler_ptr
Definition: Session.h:723
std::shared_ptr< HandleCallback > HandleCallbackPtr
Hyperspace session.
Definition: Session.h:148
std::mutex m_callback_mutex
Definition: Session.h:726
Hypertable definitions
Entry point to AsyncComm service.
Definition: Comm.h:61
LockMode
Lock mode.
Definition: LockSequencer.h:47
String m_hyperspace_master
Definition: Session.h:728
Declarations for CommBuf.
Declarations for ConnectionManager.
Declarations for Comm.
atomically open and lock file exclusive, fail if can't
Definition: Session.h:85
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
A String class based on std::string.
Used in conjunction with CREATE to create an ephemeral file.
Definition: Session.h:81
session is in jeopardy
Definition: Session.h:159
std::shared_ptr< ClientHandleState > ClientHandleStatePtr
Create file if it does not exist.
Definition: Session.h:77
virtual ~SessionCallback()
Definition: Session.h:97
Open file for reading.
Definition: Session.h:71
std::condition_variable m_cond
Definition: Session.h:710
uint32_t m_grace_period
Definition: Session.h:718