fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.9 KiB
141 lines
4.9 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// 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).
|
||
11 years ago
|
#pragma once
|
||
|
|
||
10 years ago
|
#include <assert.h>
|
||
8 years ago
|
#include <functional>
|
||
11 years ago
|
#include <mutex>
|
||
|
#include <string>
|
||
9 years ago
|
#include <thread>
|
||
11 years ago
|
#include <vector>
|
||
|
|
||
10 years ago
|
// 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
|
||
|
extern int rocksdb_kill_odds;
|
||
9 years ago
|
// If kill point has a prefix on this list, will skip killing.
|
||
|
extern std::vector<std::string> rocksdb_kill_prefix_blacklist;
|
||
10 years ago
|
|
||
|
#ifdef NDEBUG
|
||
|
// empty in release build
|
||
9 years ago
|
#define TEST_KILL_RANDOM(kill_point, rocksdb_kill_odds)
|
||
10 years ago
|
#else
|
||
|
|
||
|
namespace rocksdb {
|
||
7 years ago
|
// Kill the process with probability 1/odds for testing.
|
||
9 years ago
|
extern void TestKillRandom(std::string kill_point, int odds,
|
||
|
const std::string& srcfile, int srcline);
|
||
10 years ago
|
|
||
|
// 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
|
||
|
|
||
9 years ago
|
#define TEST_KILL_RANDOM(kill_point, rocksdb_kill_odds) \
|
||
|
{ \
|
||
|
if (rocksdb_kill_odds > 0) { \
|
||
|
TestKillRandom(kill_point, rocksdb_kill_odds, __FILE__, __LINE__); \
|
||
|
} \
|
||
10 years ago
|
}
|
||
|
} // namespace rocksdb
|
||
|
#endif
|
||
|
|
||
11 years ago
|
#ifdef NDEBUG
|
||
|
#define TEST_SYNC_POINT(x)
|
||
7 years ago
|
#define TEST_IDX_SYNC_POINT(x, index)
|
||
10 years ago
|
#define TEST_SYNC_POINT_CALLBACK(x, y)
|
||
7 years ago
|
#define INIT_SYNC_POINT_SINGLETONS()
|
||
11 years ago
|
#else
|
||
|
|
||
11 years ago
|
namespace rocksdb {
|
||
|
|
||
11 years ago
|
// 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.
|
||
9 years ago
|
// Refer to (DBTest,TransactionLogIteratorRace), for an example use case.
|
||
11 years ago
|
|
||
|
class SyncPoint {
|
||
|
public:
|
||
|
static SyncPoint* GetInstance();
|
||
|
|
||
7 years ago
|
SyncPoint(const SyncPoint&) = delete;
|
||
|
SyncPoint& operator=(const SyncPoint&) = delete;
|
||
|
~SyncPoint();
|
||
|
|
||
9 years ago
|
struct SyncPointPair {
|
||
11 years ago
|
std::string predecessor;
|
||
|
std::string successor;
|
||
|
};
|
||
9 years ago
|
|
||
11 years ago
|
// call once at the beginning of a test to setup the dependency between
|
||
|
// sync points
|
||
9 years ago
|
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);
|
||
11 years ago
|
|
||
7 years ago
|
// 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.
|
||
7 years ago
|
void SetCallBack(const std::string& point,
|
||
|
const std::function<void(void*)>& callback);
|
||
8 years ago
|
|
||
|
// Clear callback function by point
|
||
7 years ago
|
void ClearCallBack(const std::string& point);
|
||
8 years ago
|
|
||
10 years ago
|
// Clear all call back functions.
|
||
|
void ClearAllCallBacks();
|
||
|
|
||
11 years ago
|
// 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.
|
||
7 years ago
|
// And/or call registered callback function, with argument `cb_arg`
|
||
10 years ago
|
void Process(const std::string& point, void* cb_arg = nullptr);
|
||
11 years ago
|
|
||
|
// TODO: it might be useful to provide a function that blocks until all
|
||
|
// sync points are cleared.
|
||
|
|
||
7 years ago
|
// We want this to be public so we can
|
||
|
// subclass the implementation
|
||
|
struct Data;
|
||
|
|
||
11 years ago
|
private:
|
||
7 years ago
|
// Singleton
|
||
|
SyncPoint();
|
||
|
Data* impl_;
|
||
11 years ago
|
};
|
||
|
|
||
|
} // namespace rocksdb
|
||
|
|
||
|
// Use TEST_SYNC_POINT to specify sync points inside code base.
|
||
7 years ago
|
// Sync points can have happens-after depedency on other sync points,
|
||
11 years ago
|
// 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.
|
||
|
#define TEST_SYNC_POINT(x) rocksdb::SyncPoint::GetInstance()->Process(x)
|
||
7 years ago
|
#define TEST_IDX_SYNC_POINT(x, index) \
|
||
|
rocksdb::SyncPoint::GetInstance()->Process(x + std::to_string(index))
|
||
10 years ago
|
#define TEST_SYNC_POINT_CALLBACK(x, y) \
|
||
|
rocksdb::SyncPoint::GetInstance()->Process(x, y)
|
||
7 years ago
|
#define INIT_SYNC_POINT_SINGLETONS() \
|
||
|
(void)rocksdb::SyncPoint::GetInstance();
|
||
11 years ago
|
#endif // NDEBUG
|