MongoDB C++ Driver  legacy-1.1.2
fail_point.h
1 /* Copyright 2014 MongoDB Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <boost/thread/mutex.hpp>
19 
20 #include "mongo/base/disallow_copying.h"
21 #include "mongo/db/jsobj.h"
22 #include "mongo/platform/atomic_word.h"
23 
24 namespace mongo {
54 class FailPoint {
55  MONGO_DISALLOW_COPYING(FailPoint);
56 
57 public:
58  typedef AtomicUInt32::WordType ValType;
59  enum Mode { off, alwaysOn, random, nTimes, numModes };
60  enum RetCode { fastOff = 0, slowOff, slowOn };
61 
62  FailPoint();
63 
69  inline bool shouldFail() {
70  RetCode ret = shouldFailOpenBlock();
71 
72  if (MONGO_likely(ret == fastOff)) {
73  return false;
74  }
75 
77  return ret == slowOn;
78  }
79 
87  inline RetCode shouldFailOpenBlock() {
88  if (MONGO_likely((_fpInfo.loadRelaxed() & ACTIVE_BIT) == 0)) {
89  return fastOff;
90  }
91 
92  return slowShouldFailOpenBlock();
93  }
94 
99  void shouldFailCloseBlock();
100 
118  void setMode(Mode mode, ValType val = 0, const BSONObj& extra = BSONObj());
119 
123  BSONObj toBSON() const;
124 
125 private:
126  static const ValType ACTIVE_BIT = 1 << 31;
127  static const ValType REF_COUNTER_MASK = ~ACTIVE_BIT;
128 
129  // Bit layout:
130  // 31: tells whether this fail point is active.
131  // 0~30: unsigned ref counter for active dynamic instances.
132  AtomicUInt32 _fpInfo;
133 
134  // Invariant: These should be read only if ACTIVE_BIT of _fpInfo is set.
135  Mode _mode;
136  AtomicInt32 _timesOrPeriod;
137  BSONObj _data;
138 
139  // protects _mode, _timesOrPeriod, _data
140  mutable boost::mutex _modMutex;
141 
145  void enableFailPoint();
146 
150  void disableFailPoint();
151 
155  RetCode slowShouldFailOpenBlock();
156 
161  const BSONObj& getData() const;
162 
163  friend class ScopedFailPoint;
164 };
165 
172  MONGO_DISALLOW_COPYING(ScopedFailPoint);
173 
174 public:
175  ScopedFailPoint(FailPoint* failPoint);
176  ~ScopedFailPoint();
177 
181  inline bool isActive() {
182  if (_once) {
183  return false;
184  }
185 
186  _once = true;
187 
188  FailPoint::RetCode ret = _failPoint->shouldFailOpenBlock();
189  _shouldClose = ret != FailPoint::fastOff;
190  return ret == FailPoint::slowOn;
191  }
192 
197  const BSONObj& getData() const;
198 
199 private:
200  FailPoint* _failPoint;
201  bool _once;
202  bool _shouldClose;
203 };
204 
205 #define MONGO_FAIL_POINT(symbol) MONGO_unlikely(symbol.shouldFail())
206 
211 #define MONGO_FAIL_POINT_BLOCK(symbol, blockSymbol) \
212  for (mongo::ScopedFailPoint blockSymbol(&symbol); MONGO_unlikely(blockSymbol.isActive());)
213 }
bool shouldFail()
Note: This is not side-effect free - it can change the state to OFF after calling.
Definition: fail_point.h:69
Helper class for making sure that FailPoint::shouldFailCloseBlock is called when FailPoint::shouldFai...
Definition: fail_point.h:171
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSON classes.
bool isActive()
Definition: fail_point.h:181
RetCode shouldFailOpenBlock()
Checks whether fail point is active and increments the reference counter without decrementing it...
Definition: fail_point.h:87
A simple thread-safe fail point implementation that can be activated and deactivated, as well as embed temporary data into it.
Definition: fail_point.h:54
void shouldFailCloseBlock()
Decrements the reference counter.
BSONObj toBSON() const
void setMode(Mode mode, ValType val=0, const BSONObj &extra=BSONObj())
Changes the settings of this fail point.
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
const BSONObj & getData() const