0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FailureInducer.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 
50 #ifndef Common_FailureInducer_h
51 #define Common_FailureInducer_h
52 
53 #include <Common/String.h>
54 #include <Common/StringExt.h>
55 
56 #include <mutex>
57 #include <unordered_map>
58 
59 namespace Hypertable {
60 
66  public:
76 
80  static bool enabled() { return instance != 0; }
81 
88  void parse_option(String spec);
89 
93  void maybe_fail(const String &label);
94 
97  bool failure_signalled(const String &label);
98 
100  void clear();
101 
102  private:
104  void parse_option_single(String option);
105 
109  uint32_t iteration;
118  };
119 
120  typedef std::unordered_map<String, failure_inducer_state *> StateMap;
121 
124 
126  StateMap m_state_map;
127  };
128 
129 }
130 
131 /* Convenience macro which tests if a failure should be induced; if yes then an
132  * error is thrown/returned or the application pauses/exits
133  */
134 #define HT_MAYBE_FAIL(_label_) \
135  if (Hypertable::FailureInducer::enabled()) { \
136  Hypertable::FailureInducer::instance->maybe_fail(_label_); \
137  }
138 
139 /* Convenience macro which tests if a failure should be induced induced;
140  * if yes then an error is thrown/returned or the application pauses/exits
141  */
142 #define HT_MAYBE_FAIL_X(_label_, _exp_) \
143  if (Hypertable::FailureInducer::enabled() && (_exp_)) { \
144  Hypertable::FailureInducer::instance->maybe_fail(_label_); \
145  }
146 
147 /* Convenience macro for executing arbitrary code if a failure is induced */
148 #define HT_FAILURE_SIGNALLED(_label_) \
149  Hypertable::FailureInducer::enabled() && \
150  Hypertable::FailureInducer::instance->failure_signalled(_label_)
151 
152 #endif // Common_FailureInducer_h
static std::mutex mutex
Definition: Logger.cc:43
uint32_t iteration
Current iteration of the failure.
void clear()
Clears the failure inducer.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
int failure_type
The failure type; an enum in FailureInducer.cc.
std::mutex m_mutex
A mutex to serialize access.
uint32_t trigger_iteration
Number of iterations after which the failure is triggered.
static FailureInducer * instance
This is a singleton class.
static bool enabled()
Returns true if the FailureInducer is enabled (= if an instance was allocated)
void maybe_fail(const String &label)
Tests and executes the induced failures.
void parse_option(String spec)
Parses a spec string (as explained above) and stores it in an internal structure. ...
std::unordered_map< String, failure_inducer_state * > StateMap
int pause_millis
Milliseconds to pause (if type is FAILURE_TYPE_PAUSE)
Hypertable definitions
StateMap m_state_map
A list of all failure settings.
A String class based on std::string.
bool failure_signalled(const String &label)
Returns true if a failure was signalled.
void parse_option_single(String option)
Helper function to parse a single option.
String extensions and helpers: sets, maps, append operators etc.
Internal structure to store a single failure setting.
int error_code
The error code which is thrown (if type is FAILURE_TYPE_THROW)