YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/sync_point.h
Line
Count
Source
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
//  Copyright (c) 2014, Facebook, Inc.  All rights reserved.
33
//  This source code is licensed under the BSD-style license found in the
34
//  LICENSE file in the root directory of this source tree. An additional grant
35
//  of patent rights can be found in the PATENTS file in the same directory.
36
#pragma once
37
38
#include <string>
39
#include <unordered_map>
40
#include <unordered_set>
41
#include <vector>
42
43
#include "yb/util/condition_variable.h"
44
#include "yb/util/mutex.h"
45
46
#ifdef NDEBUG
47
#define TEST_SYNC_POINT(x)
48
#else
49
50
namespace yb {
51
52
// This class provides facility to reproduce race conditions deterministically
53
// in unit tests.
54
// Developer could specify sync points in the codebase via TEST_SYNC_POINT.
55
// Each sync point represents a position in the execution stream of a thread.
56
// In the unit test, 'Happens After' relationship among sync points could be
57
// setup via SyncPoint::LoadDependency, to reproduce a desired interleave of
58
// threads execution.
59
60
class SyncPoint {
61
 public:
62
  static SyncPoint* GetInstance();
63
64
  struct Dependency {
65
    Dependency(std::string predecessor, std::string successor);
66
67
    std::string predecessor_;
68
    std::string successor_;
69
  };
70
  // call once at the beginning of a test to setup the dependency between
71
  // sync points
72
  void LoadDependency(const std::vector<Dependency>& dependencies);
73
74
  // enable sync point processing (disabled on startup)
75
  void EnableProcessing();
76
77
  // disable sync point processing
78
  void DisableProcessing();
79
80
  // remove the execution trace of all sync points
81
  void ClearTrace();
82
83
  // triggered by TEST_SYNC_POINT, blocking execution until all predecessors
84
  // are executed.
85
  void Process(const std::string& point);
86
87
  // TODO: it might be useful to provide a function that blocks until all
88
  // sync points are cleared.
89
90
 private:
91
  SyncPoint();
92
93
  bool PredecessorsAllCleared(const std::string& point);
94
95
  // successor/predecessor map loaded from LoadDependency
96
  std::unordered_map<std::string, std::vector<std::string> > successors_;
97
  std::unordered_map<std::string, std::vector<std::string> > predecessors_;
98
99
  Mutex mutex_;
100
  ConditionVariable cv_;
101
  // sync points that have been passed through
102
  std::unordered_set<std::string> cleared_points_;
103
  bool enabled_;
104
};
105
106
}  // namespace yb
107
108
// Use TEST_SYNC_POINT to specify sync points inside code base.
109
// Sync points can have happens-after depedency on other sync points,
110
// configured at runtime via SyncPoint::LoadDependency. This could be
111
// utilized to re-produce race conditions between threads.
112
// TEST_SYNC_POINT is no op in release build.
113
2
#define TEST_SYNC_POINT(x) yb::SyncPoint::GetInstance()->Process(x)
114
#endif  // NDEBUG