0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Operation.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; version 3 of the
9  * 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 
27 
28 #ifndef Hypertable_Master_Operation_h
29 #define Hypertable_Master_Operation_h
30 
31 #include "Context.h"
32 #include "MetaLogEntityTypes.h"
33 
35 
36 #include <AsyncComm/Clock.h>
37 #include <AsyncComm/Event.h>
38 
39 #include <Common/ScopeGuard.h>
40 
41 #include <ctime>
42 #include <set>
43 
44 namespace Hypertable {
45 
48 
50  namespace OperationState {
51 
53  enum {
54  INITIAL = 0,
55  COMPLETE = 1,
56  UNUSED = 2,
57  STARTED = 3,
58  ASSIGN_ID = 4,
71  FINALIZE = 17,
74  PREPARE = 20,
75  COMMIT = 21,
86  };
90  const char *get_text(int state);
91  }
92 
94  namespace Dependency {
95  extern const char *INIT;
96  extern const char *SERVERS;
97  extern const char *ROOT;
98  extern const char *METADATA;
99  extern const char *SYSTEM;
100  extern const char *USER;
101  extern const char *RECOVER_SERVER;
102  extern const char *RECOVERY_BLOCKER;
103  extern const char *RECOVERY;
104  }
105 
107  typedef std::set<String> DependencySet;
108 
124  class Operation : public MetaLog::Entity {
125  public:
126 
135  Operation(ContextPtr &context, int32_t type);
136 
148  Operation(ContextPtr &context, EventPtr &event, int32_t type);
149 
162  Operation(ContextPtr &context, const MetaLog::EntityHeader &header_);
163 
165  virtual ~Operation() { }
166 
180  virtual void execute() = 0;
181 
191  const String name() override = 0;
192 
198  virtual const String label() = 0;
199 
209  virtual const String graphviz_label() { return label(); }
210 
222  virtual bool exclusive() { return false; }
223 
228  virtual size_t encoded_result_length() const;
229 
246  virtual void encode_result(uint8_t **bufp) const;
247 
254  virtual void decode_result(const uint8_t **bufp, size_t *remainp);
255 
260  virtual uint8_t encoding_version_state() const = 0;
261 
265  virtual size_t encoded_length_state() const = 0;
266 
274  virtual void encode_state(uint8_t **bufp) const = 0;
275 
289  virtual void decode_state(uint8_t version, const uint8_t **bufp,
290  size_t *remainp) = 0;
291 
292  virtual void decode_state_old(uint8_t version, const uint8_t **bufp,
293  size_t *remainp) = 0;
294 
300  virtual void display_state(std::ostream &os) = 0;
301 
312  void decode(const uint8_t **bufp, size_t *remainp,
313  uint16_t definition_version) override;
314 
318  void display(std::ostream &os) override;
319 
326  int64_t id() { return header.id; }
327 
340  ClockT::time_point expiration_time() { std::lock_guard<std::mutex> lock(m_mutex); return m_expiration_time; }
341 
345  void set_remove_approval_mask(uint16_t mask) {
346  std::lock_guard<std::mutex> lock(m_mutex);
347  m_remove_approval_mask = mask;
348  }
349 
354  std::lock_guard<std::mutex> lock(m_mutex);
355  return m_remove_approval_mask;
356  }
357 
367  bool remove_approval_add(uint16_t approval) {
368  std::lock_guard<std::mutex> lock(m_mutex);
369  m_remove_approvals |= approval;
371  }
372 
379  bool removal_approved();
380 
396  void record_state(std::vector<MetaLog::EntityPtr> &additional);
397 
401  void record_state() {
402  std::vector<MetaLog::EntityPtr> additional;
403  record_state(additional);
404  }
405 
419  void complete_error(int error, const String &msg, std::vector<MetaLog::EntityPtr> &additional);
420 
428  void complete_error(int error, const String &msg, MetaLog::EntityPtr additional=MetaLog::EntityPtr());
429 
437  void complete_error(Exception &e, std::vector<MetaLog::EntityPtr> &additional) {
438  complete_error(e.code(), e.what(), additional);
439  }
440 
449  complete_error(e.code(), e.what(), additional);
450  }
451 
452  void complete_ok(std::vector<MetaLog::EntityPtr> &additional);
454 
455  virtual int64_t hash_code() const { return m_hash_code; }
456 
457  virtual void exclusivities(DependencySet &exclusivities);
458  virtual void dependencies(DependencySet &dependencies);
459  virtual void obstructions(DependencySet &obstructions);
460 
461  void add_exclusivity(const String &exclusivity) { m_exclusivities.insert(exclusivity); }
462  void add_dependency(const String &dependency) { m_dependencies.insert(dependency); }
463  void add_obstruction(const String &obstruction) { m_obstructions.insert(obstruction); }
464  void add_obstruction_permanent(const String &obstruction) {
465  m_obstructions_permanent.insert(obstruction);
466  }
467 
468  void fetch_sub_operations(std::vector<std::shared_ptr<Operation> > &sub_ops);
469 
470  void pre_run();
471  void post_run();
472  int32_t get_state() { std::lock_guard<std::mutex> lock(m_mutex); return m_state; }
473  void set_state(int32_t state) { std::lock_guard<std::mutex> lock(m_mutex); m_state = state; }
474  virtual bool is_perpetual() { return false; }
475  bool block();
476  bool unblock();
477  bool is_blocked() { std::lock_guard<std::mutex> lock(m_mutex); return m_blocked; }
478  bool is_complete() { std::lock_guard<std::mutex> lock(m_mutex); return m_state == OperationState::COMPLETE; }
479 
480  int32_t get_original_type() { return m_original_type; }
481  void set_original_type(int32_t original_type) { m_original_type = original_type; }
482 
484  void set_ephemeral() {
485  std::lock_guard<std::mutex> lock(m_mutex);
486  m_ephemeral = true;
487  }
488 
490  bool ephemeral() {
491  std::lock_guard<std::mutex> lock(m_mutex);
492  return m_ephemeral;
493  }
494 
497  int32_t get_error() {
498  std::lock_guard<std::mutex> lock(m_mutex);
500  return m_error;
501  }
502 
506  std::lock_guard<std::mutex> lock(m_mutex);
508  return m_error_msg;
509  }
510 
511  protected:
512 
513  uint8_t encoding_version() const override;
514 
515  size_t encoded_length_internal() const override;
516 
517  void encode_internal(uint8_t **bufp) const override;
518 
519  void decode_internal(uint8_t version, const uint8_t **bufp,
520  size_t *remainp) override;
521 
522  void decode_old(const uint8_t **bufp, size_t *remainp,
523  uint16_t definition_version);
524 
536  bool validate_subops();
537 
550  void stage_subop(std::shared_ptr<Operation> operation);
551 
554 
557 
559  int32_t m_state {OperationState::INITIAL};
560 
562  uint16_t m_remove_approvals {};
563 
566 
568  int32_t m_original_type {};
569 
571  int32_t m_error {};
572 
575 
578 
580  bool m_blocked {};
581 
583  bool m_ephemeral {};
584 
587 
589  int64_t m_hash_code;
590 
592  DependencySet m_exclusivities;
593 
595  DependencySet m_dependencies;
596 
598  DependencySet m_obstructions;
599 
602 
604  std::vector<int64_t> m_sub_ops;
605 
606  };
607 
609  typedef std::shared_ptr<Operation> OperationPtr;
610 
612 
613 } // namespace Hypertable
614 
615 #endif // Hypertable_Master_Operation_h
virtual void encode_result(uint8_t **bufp) const
Encode operation result.
Definition: Operation.cc:350
int64_t id()
Operation identifier.
Definition: Operation.h:326
virtual void display_state(std::ostream &os)=0
Write human readable operation state to output stream.
virtual bool exclusive()
Indicates if operation is exclusive.
Definition: Operation.h:222
Master MetaLog entity type constants.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
uint16_t m_remove_approvals
Remove approvals received.
Definition: Operation.h:562
String m_error_msg
Result error message.
Definition: Operation.h:574
virtual size_t encoded_length_state() const =0
Encoded length of operation state.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
std::shared_ptr< Entity > EntityPtr
Smart pointer to Entity.
virtual void execute()=0
Executes (carries out) the operation.
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
const char * SERVERS
Definition: Operation.cc:46
bool validate_subops()
Handles the results of sub operations.
Definition: Operation.cc:512
int64_t id
Unique ID of entity.
void set_original_type(int32_t original_type)
Definition: Operation.h:481
virtual void decode_result(const uint8_t **bufp, size_t *remainp)
Decode operation result.
Definition: Operation.cc:356
void decode(const uint8_t **bufp, size_t *remainp, uint16_t definition_version) override
Decode operation.
Definition: Operation.cc:275
std::mutex m_mutex
Mutex for serializing access to members
void set_ephemeral()
Sets the ephemeral flag to true.
Definition: Operation.h:484
virtual uint8_t encoding_version_state() const =0
Returns version of encoding format of state.
void add_exclusivity(const String &exclusivity)
Definition: Operation.h:461
virtual const String graphviz_label()
Human readable operation label used in graphviz output.
Definition: Operation.h:209
virtual void obstructions(DependencySet &obstructions)
Definition: Operation.cc:469
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
uint16_t m_remove_approval_mask
Remove approval mask.
Definition: Operation.h:565
void add_obstruction(const String &obstruction)
Definition: Operation.h:463
virtual int64_t hash_code() const
Definition: Operation.h:455
int32_t m_original_type
Original entity type read from MML (prior to conversion)
Definition: Operation.h:568
EntityHeader header
Entity header
int32_t get_error()
Get error code.
Definition: Operation.h:497
const char * get_text(int32_t state)
Definition: Operation.cc:609
uint16_t get_remove_approval_mask()
Gets the remove approvals bit mask.
Definition: Operation.h:353
const char * RECOVER_SERVER
Definition: Operation.cc:51
Declarations for Event.
int64_t m_hash_code
Hash code uniqely identifying operation.
Definition: Operation.h:589
EventPtr m_event
Pointer to client event (if any) that originated the operation.
Definition: Operation.h:556
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Base class for MetaLog entities.
Definition: MetaLogEntity.h:62
Declaration of ClockT.
uint8_t encoding_version() const override
Returns encoding version.
Definition: Operation.cc:139
virtual void dependencies(DependencySet &dependencies)
Definition: Operation.cc:464
const char * USER
Definition: Operation.cc:50
void set_state(int32_t state)
Definition: Operation.h:473
size_t encoded_length_internal() const override
Returns internal serialized length.
Definition: Operation.cc:143
String get_error_msg()
Get error message.
Definition: Operation.h:505
int32_t m_error
Result error code.
Definition: Operation.h:571
void add_dependency(const String &dependency)
Definition: Operation.h:462
const char * INIT
Definition: Operation.cc:45
virtual void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp)=0
Declarations for MetaLog::Entity.
DependencySet m_obstructions_permanent
Set of permanent obstructions.
Definition: Operation.h:601
void stage_subop(std::shared_ptr< Operation > operation)
Stages a sub operation for execution.
Definition: Operation.cc:536
bool removal_approved()
Checks if all remove approvals have been received.
Definition: Operation.cc:362
bool ephemeral()
Gets the ephemeral flag.
Definition: Operation.h:490
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
Definition: Operation.cc:172
void set_remove_approval_mask(uint16_t mask)
Sets the remove approvals bit mask.
Definition: Operation.h:345
void record_state()
Records operation state to the MML.
Definition: Operation.h:401
const char * METADATA
Definition: Operation.cc:48
const String name() override=0
Name of operation used for exclusivity.
bool m_blocked
Flag indicating if operation is blocked.
Definition: Operation.h:580
ClockT::time_point expiration_time()
Returns operation expiration time.
Definition: Operation.h:340
void complete_error(Exception &e, std::vector< MetaLog::EntityPtr > &additional)
Completes operation with exception.
Definition: Operation.h:437
bool m_ephemeral
Indicates if operation is ephemeral and does not get persisted to MML.
Definition: Operation.h:583
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
Definition: Operation.cc:205
const char * RECOVERY_BLOCKER
Definition: Operation.cc:52
virtual const String label()=0
Human readable label for operation.
void lock()
Locks the entity's mutex.
Definition: MetaLogEntity.h:97
void add_obstruction_permanent(const String &obstruction)
Definition: Operation.h:464
Hypertable definitions
virtual ~Operation()
Destructor.
Definition: Operation.h:165
DependencySet m_obstructions
Set of obstructions.
Definition: Operation.h:598
virtual void exclusivities(DependencySet &exclusivities)
Definition: Operation.cc:459
virtual bool is_perpetual()
Definition: Operation.h:474
DependencySet m_dependencies
Set of dependencies.
Definition: Operation.h:595
void fetch_sub_operations(std::vector< std::shared_ptr< Operation > > &sub_ops)
Definition: Operation.cc:475
void decode_old(const uint8_t **bufp, size_t *remainp, uint16_t definition_version)
Definition: Operation.cc:283
const char * SYSTEM
Definition: Operation.cc:49
Abstract base class for master operations.
Definition: Operation.h:124
const char * RECOVERY
Definition: Operation.cc:53
Operation(ContextPtr &context, int32_t type)
Constructor with operation type specifier.
Definition: Operation.cc:61
void display(std::ostream &os) override
Write human readable string represenation of operation to output stream.
Definition: Operation.cc:79
void complete_error(Exception &e, MetaLog::EntityPtr additional=MetaLog::EntityPtr())
Completes operation with exception.
Definition: Operation.h:448
const char * ROOT
Definition: Operation.cc:47
std::vector< int64_t > m_sub_ops
Vector of sub operations IDs.
Definition: Operation.h:604
This is a generic exception class for Hypertable.
Definition: Error.h:314
virtual void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp)=0
Decode operation state.
bool m_unblock_on_exit
Flag to signal operation to be unblocked on exit (post_run())
Definition: Operation.h:577
virtual void encode_state(uint8_t **bufp) const =0
Encode operation state.
DependencySet m_exclusivities
Set of exclusivities.
Definition: Operation.h:592
std::set< String > DependencySet
Set of dependency string.
Definition: Operation.h:107
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
std::shared_ptr< Operation > OperationPtr
Smart pointer to Operation.
Definition: Operation.h:609
int32_t get_original_type()
Definition: Operation.h:480
bool remove_approval_add(uint16_t approval)
Sets remove approval bits.
Definition: Operation.h:367
void complete_error(int error, const String &msg, std::vector< MetaLog::EntityPtr > &additional)
Completes operation with error.
Definition: Operation.cc:400
Declarations for Context.
virtual size_t encoded_result_length() const
Length of encoded operation result.
Definition: Operation.cc:344
int code() const
Returns the error code.
Definition: Error.h:391
ClockT::time_point m_expiration_time
Expiration time (used by ResponseManager)
Definition: Operation.h:586
Executes user-defined functions when leaving the current scope.