/Users/deen/code/yugabyte-db/src/yb/util/fault_injection.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_UTIL_FAULT_INJECTION_H |
33 | | #define YB_UTIL_FAULT_INJECTION_H |
34 | | |
35 | | #include "yb/gutil/macros.h" |
36 | | |
37 | | // With some probability, crash at the current point in the code by issuing LOG(FATAL). |
38 | | // |
39 | | // The probability is determined by the 'fraction_flag' argument. |
40 | | // |
41 | | // Typical usage: |
42 | | // |
43 | | // DEFINE_double(fault_crash_before_foo, 0.0, |
44 | | // "Fraction of the time when we will crash before doing foo"); |
45 | | // TAG_FLAG(fault_crash_before_foo, unsafe); |
46 | | // |
47 | | // This macro should be fast enough to run even in hot code paths. |
48 | | #define MAYBE_FAULT(fraction_flag) \ |
49 | 12.7M | yb::fault_injection::MaybeFault(AS_STRING(fraction_flag), fraction_flag) |
50 | | |
51 | | // Inject a uniformly random amount of latency between 0 and the configured number of milliseconds. |
52 | | // |
53 | | // As with above, if the flag is configured to be <= 0, then this will be evaluated inline and |
54 | | // should be fast, even in hot code path. |
55 | | #define MAYBE_INJECT_RANDOM_LATENCY(max_ms_flag) \ |
56 | | yb::fault_injection::MaybeInjectRandomLatency(max_ms_flag); |
57 | | |
58 | | // Implementation details below. Use the MAYBE_FAULT macro instead. |
59 | | namespace yb { |
60 | | namespace fault_injection { |
61 | | |
62 | | // Out-of-line implementation. |
63 | | void DoMaybeFault(const char* fault_str, double fraction); |
64 | | void DoInjectRandomLatency(double max_latency); |
65 | | |
66 | 11.7M | inline void MaybeFault(const char* fault_str, double fraction) { |
67 | 11.7M | if (PREDICT_TRUE(fraction <= 0)) return; |
68 | 18.4E | DoMaybeFault(fault_str, fraction); |
69 | 18.4E | } |
70 | | |
71 | 1.62k | inline void MaybeInjectRandomLatency(double max_latency) { |
72 | 1.62k | if (PREDICT_TRUE(max_latency <= 0)) return; |
73 | 0 | DoInjectRandomLatency(max_latency); |
74 | 0 | } |
75 | | |
76 | | } // namespace fault_injection |
77 | | } // namespace yb |
78 | | #endif /* YB_UTIL_FAULT_INJECTION_H */ |