/Users/deen/code/yugabyte-db/src/yb/master/scoped_leader_shared_lock.cc
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 | | #include "yb/master/scoped_leader_shared_lock.h" |
33 | | |
34 | | #include "yb/consensus/consensus.h" |
35 | | #include "yb/consensus/metadata.pb.h" |
36 | | |
37 | | #include "yb/master/catalog_manager.h" |
38 | | #include "yb/master/master.h" |
39 | | #include "yb/master/sys_catalog.h" |
40 | | |
41 | | #include "yb/tablet/tablet_peer.h" |
42 | | |
43 | | #include "yb/util/debug-util.h" |
44 | | #include "yb/util/shared_lock.h" |
45 | | #include "yb/util/status_format.h" |
46 | | #include "yb/util/tsan_util.h" |
47 | | |
48 | | using namespace std::literals; |
49 | | |
50 | | constexpr int32_t kMasterLogLockWarningMsDefault = |
51 | | ::yb::RegularBuildVsSanitizers<int32_t>(1000, 3000); |
52 | | |
53 | | DEFINE_int32(master_log_lock_warning_ms, kMasterLogLockWarningMsDefault, |
54 | | "Print warnings if the master leader shared lock is held for longer than this amount " |
55 | | "of time. Note that this is a shared lock, so these warnings simply indicate " |
56 | | "long-running master operations that could delay system catalog loading by a new " |
57 | | "master leader."); |
58 | | |
59 | | constexpr int32_t kMasterLeaderLockStackTraceMsDefault = |
60 | | ::yb::RegularBuildVsSanitizers<int32_t>(3000, 9000); |
61 | | |
62 | | DEFINE_int32(master_leader_lock_stack_trace_ms, kMasterLeaderLockStackTraceMsDefault, |
63 | | "Dump a stack trace if the master leader shared lock is held for longer than this " |
64 | | "of time. Also see master_log_lock_warning_ms."); |
65 | | |
66 | | using yb::consensus::Consensus; |
67 | | using yb::consensus::ConsensusStatePB; |
68 | | |
69 | | using yb::consensus::CONSENSUS_CONFIG_COMMITTED; |
70 | | |
71 | | namespace yb { |
72 | | namespace master { |
73 | | |
74 | | ScopedLeaderSharedLock::ScopedLeaderSharedLock( |
75 | | CatalogManager* catalog, |
76 | | const char* file_name, |
77 | | int line_number, |
78 | | const char* function_name) |
79 | | : catalog_(DCHECK_NOTNULL(catalog)), |
80 | | leader_shared_lock_(catalog->leader_lock_, std::try_to_lock), |
81 | | start_(std::chrono::steady_clock::now()), |
82 | | file_name_(file_name), |
83 | | line_number_(line_number), |
84 | 6.55M | function_name_(function_name) { |
85 | 6.55M | int64_t catalog_leader_ready_term; |
86 | 6.55M | { |
87 | | // Check if the catalog manager is running. |
88 | 6.55M | std::lock_guard<simple_spinlock> l(catalog_->state_lock_); |
89 | 6.55M | if (PREDICT_FALSE(catalog_->state_ != CatalogManager::kRunning)) { |
90 | 402k | catalog_status_ = STATUS_SUBSTITUTE(ServiceUnavailable, |
91 | 402k | "Catalog manager is not initialized. State: $0", catalog_->state_); |
92 | 402k | return; |
93 | 402k | } |
94 | 6.15M | catalog_leader_ready_term = catalog_->leader_ready_term_; |
95 | 6.15M | } |
96 | | |
97 | 6.15M | string uuid = catalog_->master_->fs_manager()->uuid(); |
98 | 6.15M | if (PREDICT_FALSE(catalog_->master_->IsShellMode())) { |
99 | | // Consensus and other internal fields should not be checked when in shell mode as they may be |
100 | | // in transition. |
101 | 197 | leader_status_ = STATUS_SUBSTITUTE(IllegalState, |
102 | 197 | "Catalog manager of $0 is in shell mode, not the leader.", uuid); |
103 | 197 | return; |
104 | 197 | } |
105 | | |
106 | | // Check if the catalog manager is the leader. |
107 | 6.15M | Consensus* consensus = catalog_->sys_catalog_->tablet_peer()->consensus(); |
108 | 6.15M | ConsensusStatePB cstate = consensus->ConsensusState(CONSENSUS_CONFIG_COMMITTED); |
109 | 6.15M | if (PREDICT_FALSE(!cstate.has_leader_uuid() || cstate.leader_uuid() != uuid)) { |
110 | 3.52M | leader_status_ = STATUS_FORMAT(IllegalState, |
111 | 3.52M | "Not the leader. Local UUID: $0, Consensus state: $1", |
112 | 3.52M | uuid, cstate); |
113 | 3.52M | return; |
114 | 3.52M | } |
115 | | // TODO: deduplicate the leadership check above and below (one is committed, one is active). |
116 | 2.62M | const Status s = consensus->CheckIsActiveLeaderAndHasLease(); |
117 | 2.62M | if (!s.ok()) { |
118 | 1.82k | leader_status_ = s; |
119 | 1.82k | return; |
120 | 1.82k | } |
121 | 2.62M | if (PREDICT_FALSE(catalog_leader_ready_term != cstate.current_term())) { |
122 | | // Normally we use LeaderNotReadyToServe to indicate that the leader has not replicated its |
123 | | // NO_OP entry or the previous leader's lease has not expired yet, and the handling logic is to |
124 | | // to retry on the same server. |
125 | 279k | leader_status_ = STATUS_SUBSTITUTE(LeaderNotReadyToServe, |
126 | 279k | "Leader not yet ready to serve requests: " |
127 | 279k | "leader_ready_term_ = $0; cstate.current_term = $1", |
128 | 279k | catalog_leader_ready_term, cstate.current_term()); |
129 | 279k | return; |
130 | 279k | } |
131 | 2.34M | if (PREDICT_FALSE(!leader_shared_lock_.owns_lock())) { |
132 | 0 | leader_status_ = STATUS_SUBSTITUTE(ServiceUnavailable, |
133 | 0 | "Couldn't get leader_lock_ in shared mode. Leader still loading catalog tables." |
134 | 0 | "leader_ready_term_ = $0; cstate.current_term = $1", |
135 | 0 | catalog_leader_ready_term, cstate.current_term()); |
136 | 0 | return; |
137 | 0 | } |
138 | 2.34M | } |
139 | | |
140 | | ScopedLeaderSharedLock::ScopedLeaderSharedLock( |
141 | | enterprise::CatalogManager* catalog, |
142 | | const char* file_name, |
143 | | int line_number, |
144 | | const char* function_name) |
145 | | : ScopedLeaderSharedLock( |
146 | 6.54M | static_cast<CatalogManager*>(catalog), file_name, line_number, function_name) { |
147 | 6.54M | } Unexecuted instantiation: _ZN2yb6master22ScopedLeaderSharedLockC2EPNS0_10enterprise14CatalogManagerEPKciS6_ _ZN2yb6master22ScopedLeaderSharedLockC1EPNS0_10enterprise14CatalogManagerEPKciS6_ Line | Count | Source | 146 | 6.54M | static_cast<CatalogManager*>(catalog), file_name, line_number, function_name) { | 147 | 6.54M | } |
|
148 | | |
149 | 6.56M | ScopedLeaderSharedLock::~ScopedLeaderSharedLock() { |
150 | 6.56M | Unlock(); |
151 | 6.56M | } |
152 | | |
153 | 6.81M | void ScopedLeaderSharedLock::Unlock() { |
154 | 6.81M | if (leader_shared_lock_.owns_lock()) { |
155 | 6.29M | { |
156 | 6.29M | decltype(leader_shared_lock_) lock; |
157 | 6.29M | lock.swap(leader_shared_lock_); |
158 | 6.29M | } |
159 | 6.29M | auto finish = std::chrono::steady_clock::now(); |
160 | | |
161 | 6.29M | bool need_stack_trace = finish > start_ + 1ms * FLAGS_master_leader_lock_stack_trace_ms; |
162 | 6.29M | bool need_warning = |
163 | 6.29M | need_stack_trace || (finish > start_ + 1ms * FLAGS_master_log_lock_warning_ms); |
164 | 6.29M | if (need_warning) { |
165 | 31 | LOG(WARNING) |
166 | 31 | << "Long lock of catalog manager (" << file_name_ << ":" << line_number_ << ", " |
167 | 31 | << function_name_ << "): " << AsString(finish - start_) |
168 | 26 | << (need_stack_trace ? "\n" + GetStackTrace() : ""); |
169 | 31 | } |
170 | 6.29M | } |
171 | 6.81M | } |
172 | | |
173 | | } // namespace master |
174 | | } // namespace yb |