/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_txn_manager.h
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 | | // No include guards here because this file is expected to be included multiple times. |
15 | | |
16 | | #ifndef YB_YQL_PGGATE_PG_TXN_MANAGER_H_ |
17 | | #define YB_YQL_PGGATE_PG_TXN_MANAGER_H_ |
18 | | |
19 | | #include <mutex> |
20 | | |
21 | | #include "yb/client/client_fwd.h" |
22 | | #include "yb/client/transaction.h" |
23 | | #include "yb/common/clock.h" |
24 | | #include "yb/common/transaction.h" |
25 | | #include "yb/gutil/ref_counted.h" |
26 | | |
27 | | #include "yb/tserver/pg_client.pb.h" |
28 | | #include "yb/tserver/tserver_fwd.h" |
29 | | #include "yb/tserver/tserver_util_fwd.h" |
30 | | |
31 | | #include "yb/util/enums.h" |
32 | | |
33 | | #include "yb/yql/pggate/pg_gate_fwd.h" |
34 | | #include "yb/yql/pggate/pg_callbacks.h" |
35 | | |
36 | | namespace yb { |
37 | | namespace pggate { |
38 | | |
39 | | // These should match XACT_READ_UNCOMMITED, XACT_READ_COMMITED, XACT_REPEATABLE_READ, |
40 | | // XACT_SERIALIZABLE from xact.h. Please do not change this enum. |
41 | | YB_DEFINE_ENUM( |
42 | | PgIsolationLevel, |
43 | | ((READ_UNCOMMITED, 0)) |
44 | | ((READ_COMMITTED, 1)) |
45 | | ((REPEATABLE_READ, 2)) |
46 | | ((SERIALIZABLE, 3)) |
47 | | ); |
48 | | |
49 | | class PgTxnManager : public RefCountedThreadSafe<PgTxnManager> { |
50 | | public: |
51 | | PgTxnManager(PgClient* pg_client, |
52 | | scoped_refptr<ClockBase> clock, |
53 | | const tserver::TServerSharedObject* tserver_shared_object, |
54 | | PgCallbacks pg_callbacks); |
55 | | |
56 | | virtual ~PgTxnManager(); |
57 | | |
58 | | CHECKED_STATUS BeginTransaction(); |
59 | | CHECKED_STATUS CalculateIsolation( |
60 | | bool read_only_op, TxnPriorityRequirement txn_priority_requirement); |
61 | | CHECKED_STATUS RecreateTransaction(); |
62 | | CHECKED_STATUS RestartTransaction(); |
63 | | CHECKED_STATUS ResetTransactionReadPoint(); |
64 | | CHECKED_STATUS RestartReadPoint(); |
65 | | CHECKED_STATUS CommitTransaction(); |
66 | | CHECKED_STATUS AbortTransaction(); |
67 | | CHECKED_STATUS SetPgIsolationLevel(int isolation); |
68 | | PgIsolationLevel GetPgIsolationLevel(); |
69 | | CHECKED_STATUS SetReadOnly(bool read_only); |
70 | | CHECKED_STATUS EnableFollowerReads(bool enable_follower_reads, int32_t staleness); |
71 | | CHECKED_STATUS SetDeferrable(bool deferrable); |
72 | | CHECKED_STATUS EnterSeparateDdlTxnMode(); |
73 | | CHECKED_STATUS ExitSeparateDdlTxnMode(Commit commit); |
74 | | |
75 | 3.76M | bool IsDdlMode() const { return ddl_mode_; } |
76 | 21.0M | bool IsTxnInProgress() const { return txn_in_progress_; } |
77 | 61.7k | IsolationLevel GetIsolationLevel() const { return isolation_level_; } |
78 | 1.58M | bool ShouldUseFollowerReads() const { return read_time_for_follower_reads_.is_valid(); } |
79 | | |
80 | | void SetupPerformOptions(tserver::PgPerformOptionsPB* options); |
81 | | |
82 | | private: |
83 | | YB_STRONGLY_TYPED_BOOL(NeedsHigherPriorityTxn); |
84 | | YB_STRONGLY_TYPED_BOOL(SavePriority); |
85 | | |
86 | | void ResetTxnAndSession(); |
87 | | void StartNewSession(); |
88 | | Status UpdateReadTimeForFollowerReadsIfRequired(); |
89 | | Status RecreateTransaction(SavePriority save_priority); |
90 | | |
91 | | static uint64_t NewPriority(TxnPriorityRequirement txn_priority_requirement); |
92 | | |
93 | | std::string TxnStateDebugStr() const; |
94 | | |
95 | | CHECKED_STATUS FinishTransaction(Commit commit); |
96 | | |
97 | | // ---------------------------------------------------------------------------------------------- |
98 | | |
99 | | PgClient* client_; |
100 | | scoped_refptr<ClockBase> clock_; |
101 | | const tserver::TServerSharedObject* const tserver_shared_object_; |
102 | | |
103 | | bool txn_in_progress_ = false; |
104 | | IsolationLevel isolation_level_ = IsolationLevel::NON_TRANSACTIONAL; |
105 | | uint64_t txn_serial_no_ = 0; |
106 | | bool need_restart_ = false; |
107 | | bool need_defer_read_point_ = false; |
108 | | tserver::ReadTimeManipulation read_time_manipulation_ = tserver::ReadTimeManipulation::NONE; |
109 | | |
110 | | // Postgres transaction characteristics. |
111 | | PgIsolationLevel pg_isolation_level_ = PgIsolationLevel::REPEATABLE_READ; |
112 | | bool read_only_ = false; |
113 | | bool enable_follower_reads_ = false; |
114 | | uint64_t follower_read_staleness_ms_ = 0; |
115 | | HybridTime read_time_for_follower_reads_; |
116 | | bool deferrable_ = false; |
117 | | |
118 | | bool ddl_mode_ = false; |
119 | | |
120 | | // On a transaction conflict error we want to recreate the transaction with the same priority as |
121 | | // the last transaction. This avoids the case where the current transaction gets a higher priority |
122 | | // and cancels the other transaction. |
123 | | uint64_t priority_ = 0; |
124 | | SavePriority use_saved_priority_ = SavePriority::kFalse; |
125 | | |
126 | | std::unique_ptr<tserver::TabletServerServiceProxy> tablet_server_proxy_; |
127 | | |
128 | | PgCallbacks pg_callbacks_; |
129 | | |
130 | | DISALLOW_COPY_AND_ASSIGN(PgTxnManager); |
131 | | }; |
132 | | |
133 | | } // namespace pggate |
134 | | } // namespace yb |
135 | | #endif // YB_YQL_PGGATE_PG_TXN_MANAGER_H_ |