sync_point.h 6.3 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5 6
#pragma once

7
#include <assert.h>
8

9
#include <functional>
10 11
#include <mutex>
#include <string>
12
#include <thread>
13 14
#include <vector>

15
#include "rocksdb/rocksdb_namespace.h"
16
#include "rocksdb/slice.h"
17

18 19
#ifdef NDEBUG
// empty in release build
S
sdong 已提交
20 21
#define TEST_KILL_RANDOM_WITH_WEIGHT(kill_point, rocksdb_kill_odds_weight)
#define TEST_KILL_RANDOM(kill_point)
22 23
#else

24
namespace ROCKSDB_NAMESPACE {
25 26 27 28 29 30

// To avoid crashing always at some frequently executed codepaths (during
// kill random test), use this factor to reduce odds
#define REDUCE_ODDS 2
#define REDUCE_ODDS2 4

S
sdong 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// A class used to pass when a kill point is reached.
struct KillPoint {
 public:
  // This is only set from db_stress.cc and for testing only.
  // If non-zero, kill at various points in source code with probability 1/this
  int rocksdb_kill_odds = 0;
  // If kill point has a prefix on this list, will skip killing.
  std::vector<std::string> rocksdb_kill_exclude_prefixes;
  // Kill the process with probability 1/odds for testing.
  void TestKillRandom(std::string kill_point, int odds,
                      const std::string& srcfile, int srcline);

  static KillPoint* GetInstance();
};

#define TEST_KILL_RANDOM_WITH_WEIGHT(kill_point, rocksdb_kill_odds_weight) \
  {                                                                        \
    KillPoint::GetInstance()->TestKillRandom(                              \
        kill_point, rocksdb_kill_odds_weight, __FILE__, __LINE__);         \
50
  }
S
sdong 已提交
51
#define TEST_KILL_RANDOM(kill_point) TEST_KILL_RANDOM_WITH_WEIGHT(kill_point, 1)
52
}  // namespace ROCKSDB_NAMESPACE
S
sdong 已提交
53

54 55
#endif

I
Igor Canadi 已提交
56 57
#ifdef NDEBUG
#define TEST_SYNC_POINT(x)
58
#define TEST_IDX_SYNC_POINT(x, index)
59
#define TEST_SYNC_POINT_CALLBACK(x, y)
60
#define INIT_SYNC_POINT_SINGLETONS()
I
Igor Canadi 已提交
61 62
#else

63
namespace ROCKSDB_NAMESPACE {
I
Igor Canadi 已提交
64

65 66 67 68 69 70 71
// This class provides facility to reproduce race conditions deterministically
// in unit tests.
// Developer could specify sync points in the codebase via TEST_SYNC_POINT.
// Each sync point represents a position in the execution stream of a thread.
// In the unit test, 'Happens After' relationship among sync points could be
// setup via SyncPoint::LoadDependency, to reproduce a desired interleave of
// threads execution.
C
charsyam 已提交
72
// Refer to (DBTest,TransactionLogIteratorRace), for an example use case.
73 74 75 76 77

class SyncPoint {
 public:
  static SyncPoint* GetInstance();

78 79 80 81
  SyncPoint(const SyncPoint&) = delete;
  SyncPoint& operator=(const SyncPoint&) = delete;
  ~SyncPoint();

82
  struct SyncPointPair {
83 84 85
    std::string predecessor;
    std::string successor;
  };
86

87 88
  // call once at the beginning of a test to setup the dependency between
  // sync points
89 90 91 92 93 94 95 96
  void LoadDependency(const std::vector<SyncPointPair>& dependencies);

  // call once at the beginning of a test to setup the dependency between
  // sync points and setup markers indicating the successor is only enabled
  // when it is processed on the same thread as the predecessor.
  // When adding a marker, it implicitly adds a dependency for the marker pair.
  void LoadDependencyAndMarkers(const std::vector<SyncPointPair>& dependencies,
                                const std::vector<SyncPointPair>& markers);
97

98 99 100
  // The argument to the callback is passed through from
  // TEST_SYNC_POINT_CALLBACK(); nullptr if TEST_SYNC_POINT or
  // TEST_IDX_SYNC_POINT was used.
101 102
  void SetCallBack(const std::string& point,
                   const std::function<void(void*)>& callback);
103 104

  // Clear callback function by point
105
  void ClearCallBack(const std::string& point);
106

107 108 109
  // Clear all call back functions.
  void ClearAllCallBacks();

110 111 112 113 114 115 116 117 118 119 120
  // enable sync point processing (disabled on startup)
  void EnableProcessing();

  // disable sync point processing
  void DisableProcessing();

  // remove the execution trace of all sync points
  void ClearTrace();

  // triggered by TEST_SYNC_POINT, blocking execution until all predecessors
  // are executed.
121
  // And/or call registered callback function, with argument `cb_arg`
122 123 124 125 126 127 128 129 130 131
  void Process(const Slice& point, void* cb_arg = nullptr);

  // template gets length of const string at compile time,
  //  avoiding strlen() at runtime
  template <size_t kLen>
  void Process(const char (&point)[kLen], void* cb_arg = nullptr) {
    static_assert(kLen > 0, "Must not be empty");
    assert(point[kLen - 1] == '\0');
    Process(Slice(point, kLen - 1), cb_arg);
  }
132 133 134 135

  // TODO: it might be useful to provide a function that blocks until all
  // sync points are cleared.

136 137 138 139
  // We want this to be public so we can
  // subclass the implementation
  struct Data;

140
 private:
141 142 143
   // Singleton
  SyncPoint();
  Data*  impl_;
144 145
};

M
mrambacher 已提交
146 147 148
// Sets up sync points to mock direct IO instead of actually issuing direct IO
// to the file system.
void SetupSyncPointsToMockDirectIO();
149
}  // namespace ROCKSDB_NAMESPACE
150 151

// Use TEST_SYNC_POINT to specify sync points inside code base.
152
// Sync points can have happens-after dependency on other sync points,
153 154 155 156
// configured at runtime via SyncPoint::LoadDependency. This could be
// utilized to re-produce race conditions between threads.
// See TransactionLogIteratorRace in db_test.cc for an example use case.
// TEST_SYNC_POINT is no op in release build.
157 158 159 160 161
#define TEST_SYNC_POINT(x) \
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->Process(x)
#define TEST_IDX_SYNC_POINT(x, index)                      \
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->Process(x + \
                                                       std::to_string(index))
162
#define TEST_SYNC_POINT_CALLBACK(x, y) \
163
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->Process(x, y)
164
#define INIT_SYNC_POINT_SINGLETONS() \
165
  (void)ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
I
Igor Canadi 已提交
166
#endif  // NDEBUG
167 168 169 170 171 172 173 174 175 176 177 178 179 180

// Callback sync point for any read IO errors that should be ignored by
// the fault injection framework
// Disable in release mode
#ifdef NDEBUG
#define IGNORE_STATUS_IF_ERROR(_status_)
#else
#define IGNORE_STATUS_IF_ERROR(_status_)            \
  {                                                 \
    if (!_status_.ok()) {                           \
      TEST_SYNC_POINT("FaultInjectionIgnoreError"); \
    }                                               \
  }
#endif  // NDEBUG