/Users/deen/code/yugabyte-db/src/yb/tablet/transaction_coordinator.h
Line | Count | Source |
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 | | |
16 | | #ifndef YB_TABLET_TRANSACTION_COORDINATOR_H |
17 | | #define YB_TABLET_TRANSACTION_COORDINATOR_H |
18 | | |
19 | | #include <future> |
20 | | #include <memory> |
21 | | |
22 | | #include "yb/client/client_fwd.h" |
23 | | |
24 | | #include "yb/common/hybrid_time.h" |
25 | | |
26 | | #include "yb/gutil/ref_counted.h" |
27 | | |
28 | | #include "yb/server/server_fwd.h" |
29 | | |
30 | | #include "yb/tablet/tablet_fwd.h" |
31 | | |
32 | | #include "yb/tserver/tserver_fwd.h" |
33 | | |
34 | | #include "yb/util/metrics_fwd.h" |
35 | | #include "yb/util/status_fwd.h" |
36 | | #include "yb/util/enums.h" |
37 | | |
38 | | namespace google { |
39 | | namespace protobuf { |
40 | | template <class T> |
41 | | class RepeatedPtrField; |
42 | | } |
43 | | } |
44 | | |
45 | | |
46 | | namespace yb { |
47 | | namespace tablet { |
48 | | |
49 | | // Get current transaction timeout. |
50 | | std::chrono::microseconds GetTransactionTimeout(); |
51 | | |
52 | | // Context for transaction coordinator. I.e. access to external facilities required by |
53 | | // transaction coordinator to do its job. |
54 | | class TransactionCoordinatorContext { |
55 | | public: |
56 | | virtual const std::string& tablet_id() const = 0; |
57 | | virtual const std::shared_future<client::YBClient*>& client_future() const = 0; |
58 | | virtual int64_t LeaderTerm() const = 0; |
59 | | virtual const server::ClockPtr& clock_ptr() const = 0; |
60 | | virtual Result<HybridTime> LeaderSafeTime() const = 0; |
61 | | |
62 | | // Returns current hybrid time lease expiration. |
63 | | // Valid only if we are leader. |
64 | | virtual HybridTime HtLeaseExpiration() const = 0; |
65 | | |
66 | | virtual void UpdateClock(HybridTime hybrid_time) = 0; |
67 | | virtual std::unique_ptr<UpdateTxnOperation> CreateUpdateTransaction( |
68 | | TransactionStatePB* request) = 0; |
69 | | virtual void SubmitUpdateTransaction( |
70 | | std::unique_ptr<UpdateTxnOperation> operation, int64_t term) = 0; |
71 | | |
72 | 33.1M | server::Clock& clock() const { |
73 | 33.1M | return *clock_ptr(); |
74 | 33.1M | } |
75 | | |
76 | | protected: |
77 | 74.1k | ~TransactionCoordinatorContext() {} |
78 | | }; |
79 | | |
80 | | typedef std::function<void(Result<TransactionStatusResult>)> TransactionAbortCallback; |
81 | | |
82 | | // Coordinates all transactions managed by specific tablet, i.e. all transactions |
83 | | // that selected this tablet as status tablet for it. |
84 | | // Also it handles running transactions, i.e. transactions that has intents in appropriate tablet. |
85 | | // Each tablet has separate transaction coordinator. |
86 | | class TransactionCoordinator { |
87 | | public: |
88 | | TransactionCoordinator(const std::string& permanent_uuid, |
89 | | TransactionCoordinatorContext* context, |
90 | | Counter* expired_metric); |
91 | | ~TransactionCoordinator(); |
92 | | |
93 | | // Used to pass arguments to ProcessReplicated. |
94 | | struct ReplicatedData { |
95 | | int64_t leader_term; |
96 | | const TransactionStatePB& state; |
97 | | const OpId& op_id; |
98 | | HybridTime hybrid_time; |
99 | | |
100 | | std::string ToString() const; |
101 | | }; |
102 | | |
103 | | // Process new transaction state. |
104 | | CHECKED_STATUS ProcessReplicated(const ReplicatedData& data); |
105 | | |
106 | | struct AbortedData { |
107 | | const TransactionStatePB& state; |
108 | | const OpId& op_id; |
109 | | |
110 | | std::string ToString() const; |
111 | | }; |
112 | | |
113 | | // Process transaction state replication aborted. |
114 | | void ProcessAborted(const AbortedData& data); |
115 | | |
116 | | // Handles new request for transaction update. |
117 | | void Handle(std::unique_ptr<tablet::UpdateTxnOperation> request, int64_t term); |
118 | | |
119 | | // Prepares log garbage collection. Return min index that should be preserved. |
120 | | int64_t PrepareGC(std::string* details = nullptr); |
121 | | |
122 | | // Starts background processes of transaction coordinator. |
123 | | void Start(); |
124 | | |
125 | | // Stop background processes of transaction coordinator. |
126 | | // And like most of other Shutdowns in our codebase it wait until shutdown completes. |
127 | | void Shutdown(); |
128 | | |
129 | | CHECKED_STATUS GetStatus(const google::protobuf::RepeatedPtrField<std::string>& transaction_ids, |
130 | | CoarseTimePoint deadline, |
131 | | tserver::GetTransactionStatusResponsePB* response); |
132 | | |
133 | | void Abort(const std::string& transaction_id, int64_t term, TransactionAbortCallback callback); |
134 | | |
135 | | std::string DumpTransactions(); |
136 | | |
137 | | // Returns count of managed transactions. Used in tests. |
138 | | size_t test_count_transactions() const; |
139 | | |
140 | | private: |
141 | | class Impl; |
142 | | std::unique_ptr<Impl> impl_; |
143 | | }; |
144 | | |
145 | | } // namespace tablet |
146 | | } // namespace yb |
147 | | |
148 | | #endif // YB_TABLET_TRANSACTION_COORDINATOR_H |