/Users/deen/code/yugabyte-db/src/yb/common/transaction.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | #include "yb/common/transaction.h" |
16 | | |
17 | | #include "yb/common/common.pb.h" |
18 | | |
19 | | #include "yb/util/result.h" |
20 | | #include "yb/util/tsan_util.h" |
21 | | |
22 | | using namespace std::literals; |
23 | | |
24 | | DEFINE_int64(transaction_rpc_timeout_ms, 5000 * yb::kTimeMultiplier, |
25 | | "Timeout used by transaction related RPCs in milliseconds."); |
26 | | |
27 | | namespace yb { |
28 | | |
29 | | YB_STRONGLY_TYPED_UUID_IMPL(TransactionId); |
30 | | |
31 | | const char* kGlobalTransactionsTableName = "transactions"; |
32 | | const std::string kMetricsSnapshotsTableName = "metrics"; |
33 | | const std::string kTransactionTablePrefix = "transactions_"; |
34 | | |
35 | | TransactionStatusResult::TransactionStatusResult(TransactionStatus status_, HybridTime status_time_) |
36 | 2.08M | : TransactionStatusResult(status_, status_time_, AbortedSubTransactionSet()) {} Unexecuted instantiation: yb::TransactionStatusResult::TransactionStatusResult(yb::TransactionStatus, yb::HybridTime) yb::TransactionStatusResult::TransactionStatusResult(yb::TransactionStatus, yb::HybridTime) Line | Count | Source | 36 | 2.08M | : TransactionStatusResult(status_, status_time_, AbortedSubTransactionSet()) {} |
|
37 | | |
38 | | TransactionStatusResult::TransactionStatusResult( |
39 | | TransactionStatus status_, HybridTime status_time_, |
40 | | AbortedSubTransactionSet aborted_subtxn_set_) |
41 | 2.21M | : status(status_), status_time(status_time_), aborted_subtxn_set(aborted_subtxn_set_) { |
42 | 18.4E | DCHECK(status == TransactionStatus::ABORTED || status_time.is_valid()) |
43 | 18.4E | << "Status: " << status << ", status_time: " << status_time; |
44 | 2.21M | } |
45 | | |
46 | 2.56M | Result<TransactionMetadata> TransactionMetadata::FromPB(const TransactionMetadataPB& source) { |
47 | 2.56M | TransactionMetadata result; |
48 | 2.56M | auto id = FullyDecodeTransactionId(source.transaction_id()); |
49 | 2.56M | RETURN_NOT_OK(id); |
50 | 2.56M | result.transaction_id = *id; |
51 | 2.56M | if (source.has_isolation()) { |
52 | 2.56M | result.isolation = source.isolation(); |
53 | 2.56M | result.status_tablet = source.status_tablet(); |
54 | 2.56M | result.priority = source.priority(); |
55 | 2.56M | result.start_time = HybridTime(source.start_hybrid_time()); |
56 | 2.56M | } |
57 | | |
58 | 2.56M | if (source.has_locality()) { |
59 | 2.55M | result.locality = source.locality(); |
60 | 2.55M | } else { |
61 | 5.68k | result.locality = TransactionLocality::GLOBAL; |
62 | 5.68k | } |
63 | | |
64 | 2.56M | return result; |
65 | 2.56M | } |
66 | | |
67 | 1.05M | void TransactionMetadata::ToPB(TransactionMetadataPB* dest) const { |
68 | 1.05M | if (isolation != IsolationLevel::NON_TRANSACTIONAL) { |
69 | 1.05M | ForceToPB(dest); |
70 | 1.05M | } else { |
71 | 1 | TransactionIdToPB(dest); |
72 | 1 | } |
73 | 1.05M | } |
74 | | |
75 | 1.66M | void TransactionMetadata::TransactionIdToPB(TransactionMetadataPB* dest) const { |
76 | 1.66M | dest->set_transaction_id(transaction_id.data(), transaction_id.size()); |
77 | 1.66M | } |
78 | | |
79 | 1.05M | void TransactionMetadata::ForceToPB(TransactionMetadataPB* dest) const { |
80 | 1.05M | TransactionIdToPB(dest); |
81 | 1.05M | dest->set_isolation(isolation); |
82 | 1.05M | dest->set_status_tablet(status_tablet); |
83 | 1.05M | dest->set_priority(priority); |
84 | 1.05M | dest->set_start_hybrid_time(start_time.ToUint64()); |
85 | 1.05M | dest->set_locality(locality); |
86 | 1.05M | } |
87 | | |
88 | 0 | bool operator==(const TransactionMetadata& lhs, const TransactionMetadata& rhs) { |
89 | 0 | return lhs.transaction_id == rhs.transaction_id && |
90 | 0 | lhs.isolation == rhs.isolation && |
91 | 0 | lhs.status_tablet == rhs.status_tablet && |
92 | 0 | lhs.priority == rhs.priority && |
93 | 0 | lhs.start_time == rhs.start_time && |
94 | 0 | lhs.locality == rhs.locality; |
95 | 0 | } |
96 | | |
97 | 5.10k | std::ostream& operator<<(std::ostream& out, const TransactionMetadata& metadata) { |
98 | 5.10k | return out << metadata.ToString(); |
99 | 5.10k | } |
100 | | |
101 | 52.8k | void SubTransactionMetadata::ToPB(SubTransactionMetadataPB* dest) const { |
102 | 52.8k | dest->set_subtransaction_id(subtransaction_id); |
103 | 52.8k | aborted.ToPB(dest->mutable_aborted()->mutable_set()); |
104 | 52.8k | } |
105 | | |
106 | | Result<SubTransactionMetadata> SubTransactionMetadata::FromPB( |
107 | 4.55M | const SubTransactionMetadataPB& source) { |
108 | 4.55M | return SubTransactionMetadata { |
109 | 4.55M | .subtransaction_id = source.has_subtransaction_id() |
110 | 4.55M | ? source.subtransaction_id()53.2k |
111 | 4.55M | : kMinSubTransactionId4.50M , |
112 | 4.55M | .aborted = VERIFY_RESULT(AbortedSubTransactionSet::FromPB(source.aborted().set())), |
113 | 4.55M | }; |
114 | 4.55M | } |
115 | | |
116 | 53.1k | bool SubTransactionMetadata::IsDefaultState() const { |
117 | 53.1k | DCHECK(subtransaction_id >= kMinSubTransactionId); |
118 | 53.1k | return subtransaction_id == kMinSubTransactionId && aborted.IsEmpty()326 ; |
119 | 53.1k | } |
120 | | |
121 | 0 | std::ostream& operator<<(std::ostream& out, const SubTransactionMetadata& metadata) { |
122 | 0 | return out << metadata.ToString(); |
123 | 0 | } |
124 | | |
125 | 2.14M | MonoDelta TransactionRpcTimeout() { |
126 | 2.14M | return FLAGS_transaction_rpc_timeout_ms * 1ms * kTimeMultiplier; |
127 | 2.14M | } |
128 | | |
129 | | // TODO(dtxn) correct deadline should be calculated and propagated. |
130 | 1.73M | CoarseTimePoint TransactionRpcDeadline() { |
131 | 1.73M | return CoarseMonoClock::Now() + TransactionRpcTimeout(); |
132 | 1.73M | } |
133 | | |
134 | | TransactionOperationContext::TransactionOperationContext() |
135 | 11.2M | : transaction_id(TransactionId::Nil()), txn_status_manager(nullptr) {} |
136 | | |
137 | | TransactionOperationContext::TransactionOperationContext( |
138 | | const TransactionId& transaction_id_, TransactionStatusManager* txn_status_manager_) |
139 | | : transaction_id(transaction_id_), |
140 | 402k | txn_status_manager(DCHECK_NOTNULL(txn_status_manager_)) {} |
141 | | |
142 | | TransactionOperationContext::TransactionOperationContext( |
143 | | const TransactionId& transaction_id_, |
144 | | SubTransactionMetadata&& subtransaction_, |
145 | | TransactionStatusManager* txn_status_manager_) |
146 | | : transaction_id(transaction_id_), |
147 | | subtransaction(std::move(subtransaction_)), |
148 | 4.54M | txn_status_manager(DCHECK_NOTNULL(txn_status_manager_)) {} |
149 | | |
150 | 0 | bool TransactionOperationContext::transactional() const { |
151 | 0 | return !transaction_id.IsNil(); |
152 | 0 | } |
153 | | |
154 | | } // namespace yb |