YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/sync_point.cc
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#include "yb/rocksdb/util/sync_point.h"
22
#include "yb/rocksdb/port/port.h"
23
#include "yb/rocksdb/util/random.h"
24
25
int rocksdb_kill_odds = 0;
26
std::vector<std::string> rocksdb_kill_prefix_blacklist;
27
28
#ifndef NDEBUG
29
namespace rocksdb {
30
31
void TestKillRandom(std::string kill_point, int odds,
32
0
                    const std::string& srcfile, int srcline) {
33
0
  for (auto& p : rocksdb_kill_prefix_blacklist) {
34
0
    if (kill_point.substr(0, p.length()) == p) {
35
0
      return;
36
0
    }
37
0
  }
38
39
0
  assert(odds > 0);
40
0
  if (odds % 7 == 0) {
41
    // class Random uses multiplier 16807, which is 7^5. If odds are
42
    // multiplier of 7, there might be limited values generated.
43
0
    odds++;
44
0
  }
45
0
  auto* r = Random::GetTLSInstance();
46
0
  bool crash = r->OneIn(odds);
47
0
  if (crash) {
48
0
    port::Crash(srcfile, srcline);
49
0
  }
50
0
}
51
52
160M
SyncPoint* SyncPoint::GetInstance() {
53
160M
  static SyncPoint sync_point;
54
160M
  return &sync_point;
55
160M
}
56
57
592
void SyncPoint::LoadDependency(const std::vector<Dependency>& dependencies) {
58
592
  std::unique_lock<std::mutex> lock(mutex_);
59
592
  successors_.clear();
60
592
  predecessors_.clear();
61
592
  cleared_points_.clear();
62
82
  for (const auto& dependency : dependencies) {
63
82
    successors_[dependency.predecessor].push_back(dependency.successor);
64
82
    predecessors_[dependency.successor].push_back(dependency.predecessor);
65
82
  }
66
592
  cv_.notify_all();
67
592
}
68
69
35.9M
bool SyncPoint::PredecessorsAllCleared(const std::string& point) {
70
18.8k
  for (const auto& pred : predecessors_[point]) {
71
18.8k
    if (cleared_points_.count(pred) == 0) {
72
18.7k
      return false;
73
18.7k
    }
74
18.8k
  }
75
35.9M
  return true;
76
35.9M
}
77
78
void SyncPoint::SetCallBack(const std::string point,
79
395
                            std::function<void(void*)> callback) {
80
395
  std::unique_lock<std::mutex> lock(mutex_);
81
395
  callbacks_[point] = callback;
82
395
}
83
84
572
void SyncPoint::ClearAllCallBacks() {
85
572
  std::unique_lock<std::mutex> lock(mutex_);
86
572
  while (num_callbacks_running_ > 0) {
87
0
    cv_.wait(lock);
88
0
  }
89
572
  callbacks_.clear();
90
572
}
91
92
249
void SyncPoint::EnableProcessing() {
93
249
  std::unique_lock<std::mutex> lock(mutex_);
94
249
  enabled_ = true;
95
249
}
96
97
817
void SyncPoint::DisableProcessing() {
98
817
  std::unique_lock<std::mutex> lock(mutex_);
99
817
  enabled_ = false;
100
817
}
101
102
28
void SyncPoint::ClearTrace() {
103
28
  std::unique_lock<std::mutex> lock(mutex_);
104
28
  cleared_points_.clear();
105
28
}
106
107
151M
void SyncPoint::Process(const std::string& point, void* cb_arg) {
108
151M
  std::unique_lock<std::mutex> lock(mutex_);
109
110
151M
  if (!enabled_) return;
111
112
35.7M
  auto callback_pair = callbacks_.find(point);
113
35.7M
  if (callback_pair != callbacks_.end()) {
114
11.0k
    num_callbacks_running_++;
115
11.0k
    lock.unlock();
116
11.0k
    callback_pair->second(cb_arg);
117
11.0k
    lock.lock();
118
11.0k
    num_callbacks_running_--;
119
11.0k
    cv_.notify_all();
120
11.0k
  }
121
122
35.8M
  while (!PredecessorsAllCleared(point)) {
123
18.7k
    cv_.wait(lock);
124
18.7k
  }
125
126
35.7M
  cleared_points_.insert(point);
127
35.7M
  cv_.notify_all();
128
35.7M
}
129
}  // namespace rocksdb
130
#endif  // NDEBUG