/Users/deen/code/yugabyte-db/src/yb/docdb/lock_batch.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/docdb/lock_batch.h" |
15 | | |
16 | | #include "yb/docdb/shared_lock_manager.h" |
17 | | |
18 | | #include "yb/util/status_format.h" |
19 | | |
20 | | DEFINE_bool(dump_lock_keys, true, |
21 | | "Whether to add keys to error message when lock batch timed out"); |
22 | | |
23 | | namespace yb { |
24 | | namespace docdb { |
25 | | |
26 | | LockBatch::LockBatch(SharedLockManager* lock_manager, LockBatchEntries&& key_to_intent_type, |
27 | | CoarseTimePoint deadline) |
28 | 1.74M | : data_(std::move(key_to_intent_type), lock_manager) { |
29 | 1.74M | if (!empty() && !lock_manager->Lock(&data_.key_to_type, deadline)) { |
30 | 46 | data_.shared_lock_manager = nullptr; |
31 | 46 | std::string batch_str; |
32 | 46 | if (FLAGS_dump_lock_keys) { |
33 | 46 | batch_str = Format(", batch: $0", data_.key_to_type); |
34 | 46 | } |
35 | 46 | data_.key_to_type.clear(); |
36 | 46 | data_.status = STATUS_FORMAT( |
37 | 46 | TryAgain, "Failed to obtain locks until deadline: $0$1", deadline, batch_str); |
38 | 46 | } |
39 | 1.74M | } |
40 | | |
41 | 10.4M | LockBatch::~LockBatch() { |
42 | 10.4M | Reset(); |
43 | 10.4M | } |
44 | | |
45 | 20.8M | void LockBatch::Reset() { |
46 | 20.8M | if (!empty()) { |
47 | 76 | VLOG(1) << "Auto-unlocking a LockBatch with " << size() << " keys"; |
48 | 1.74M | DCHECK_NOTNULL(data_.shared_lock_manager)->Unlock(data_.key_to_type); |
49 | 1.74M | data_.key_to_type.clear(); |
50 | 1.74M | } |
51 | 20.8M | } |
52 | | |
53 | 8.60M | void LockBatch::MoveFrom(LockBatch* other) { |
54 | 8.60M | Reset(); |
55 | 8.60M | data_ = std::move(other->data_); |
56 | | // Explicitly clear other key_to_type to avoid extra unlock when it is destructed. We use |
57 | | // key_to_type emptiness to mark that it does not hold a lock. |
58 | 8.60M | other->data_.key_to_type.clear(); |
59 | 8.60M | } |
60 | | |
61 | 95 | std::string LockBatchEntry::ToString() const { |
62 | 95 | return Format("{ key: $0 intent_types: $1 }", key.as_slice().ToDebugHexString(), intent_types); |
63 | 95 | } |
64 | | |
65 | | } // namespace docdb |
66 | | } // namespace yb |