/Users/deen/code/yugabyte-db/src/yb/tablet/transaction_loader.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef YB_TABLET_TRANSACTION_LOADER_H |
15 | | #define YB_TABLET_TRANSACTION_LOADER_H |
16 | | |
17 | | #include <condition_variable> |
18 | | #include <thread> |
19 | | |
20 | | #include "yb/common/transaction.h" |
21 | | |
22 | | #include "yb/gutil/thread_annotations.h" |
23 | | |
24 | | #include "yb/docdb/docdb.h" |
25 | | |
26 | | namespace yb { |
27 | | |
28 | | class OneWayBitmap; |
29 | | class RWOperationCounter; |
30 | | |
31 | | namespace tablet { |
32 | | |
33 | | class TransactionStatusResolver; |
34 | | struct TransactionalBatchData; |
35 | | |
36 | | struct ApplyStateWithCommitHt { |
37 | | docdb::ApplyTransactionState state; |
38 | | HybridTime commit_ht; |
39 | | |
40 | 0 | std::string ToString() const { |
41 | 0 | return YB_STRUCT_TO_STRING(state, commit_ht); |
42 | 0 | } |
43 | | }; |
44 | | |
45 | | using ApplyStatesMap = std::unordered_map< |
46 | | TransactionId, ApplyStateWithCommitHt, TransactionIdHash>; |
47 | | |
48 | | class TransactionLoaderContext { |
49 | | public: |
50 | 44.6k | virtual ~TransactionLoaderContext() = default; |
51 | | |
52 | | virtual TransactionStatusResolver& AddStatusResolver() = 0; |
53 | | virtual const std::string& LogPrefix() const = 0; |
54 | | virtual void CompleteLoad(const std::function<void()>& functor) = 0; |
55 | | virtual void LoadTransaction( |
56 | | TransactionMetadata&& metadata, |
57 | | TransactionalBatchData&& last_batch_data, |
58 | | OneWayBitmap&& replicated_batches, |
59 | | const ApplyStateWithCommitHt* pending_apply) = 0; |
60 | | virtual void LoadFinished(const ApplyStatesMap& pending_applies) = 0; |
61 | | }; |
62 | | |
63 | | class TransactionLoader { |
64 | | public: |
65 | | TransactionLoader(TransactionLoaderContext* context, const scoped_refptr<MetricEntity>& entity); |
66 | | ~TransactionLoader(); |
67 | | |
68 | | void Start(RWOperationCounter* pending_op_counter, const docdb::DocDB& db); |
69 | | |
70 | 3.53M | bool complete() const { |
71 | 3.53M | return all_loaded_.load(std::memory_order_acquire); |
72 | 3.53M | } |
73 | | |
74 | | void WaitLoaded(const TransactionId& id); |
75 | | void WaitAllLoaded(); |
76 | | |
77 | | void Shutdown(); |
78 | | |
79 | | private: |
80 | | class Executor; |
81 | | friend class Executor; |
82 | | |
83 | | TransactionLoaderContext& context_; |
84 | | const scoped_refptr<MetricEntity> entity_; |
85 | | |
86 | | std::unique_ptr<Executor> executor_; |
87 | | |
88 | | std::mutex mutex_; |
89 | | std::condition_variable load_cond_; |
90 | | TransactionId last_loaded_ GUARDED_BY(mutex_) = TransactionId::Nil(); |
91 | | std::atomic<bool> all_loaded_{false}; |
92 | | std::thread load_thread_; |
93 | | }; |
94 | | |
95 | | } // namespace tablet |
96 | | } // namespace yb |
97 | | |
98 | | #endif // YB_TABLET_TRANSACTION_LOADER_H |