/Users/deen/code/yugabyte-db/src/yb/tserver/pg_client_session.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_TSERVER_PG_CLIENT_SESSION_H |
15 | | #define YB_TSERVER_PG_CLIENT_SESSION_H |
16 | | |
17 | | #include <stdint.h> |
18 | | |
19 | | #include <functional> |
20 | | #include <mutex> |
21 | | #include <set> |
22 | | #include <string> |
23 | | #include <type_traits> |
24 | | #include <unordered_set> |
25 | | #include <utility> |
26 | | |
27 | | #include <boost/preprocessor/seq/for_each.hpp> |
28 | | #include <boost/range/iterator_range.hpp> |
29 | | |
30 | | #include "yb/client/client_fwd.h" |
31 | | |
32 | | #include "yb/common/entity_ids.h" |
33 | | #include "yb/common/transaction.h" |
34 | | |
35 | | #include "yb/rpc/rpc_fwd.h" |
36 | | |
37 | | #include "yb/tserver/tserver_fwd.h" |
38 | | #include "yb/tserver/pg_client.pb.h" |
39 | | |
40 | | namespace yb { |
41 | | namespace tserver { |
42 | | |
43 | | #define PG_CLIENT_SESSION_METHODS \ |
44 | | (AlterDatabase) \ |
45 | | (AlterTable) \ |
46 | | (BackfillIndex) \ |
47 | | (CreateDatabase) \ |
48 | | (CreateTable) \ |
49 | | (CreateTablegroup) \ |
50 | | (DeleteDBSequences) \ |
51 | | (DeleteSequenceTuple) \ |
52 | | (DropDatabase) \ |
53 | | (DropTable) \ |
54 | | (DropTablegroup) \ |
55 | | (FinishTransaction) \ |
56 | | (InsertSequenceTuple) \ |
57 | | (ReadSequenceTuple) \ |
58 | | (RollbackSubTransaction) \ |
59 | | (SetActiveSubTransaction) \ |
60 | | (TruncateTable) \ |
61 | | (UpdateSequenceTuple) \ |
62 | | /**/ |
63 | | |
64 | | using PgClientSessionOperations = std::vector<std::shared_ptr<client::YBPgsqlOp>>; |
65 | | class PgClientSessionLocker; |
66 | | |
67 | | YB_DEFINE_ENUM(PgClientSessionKind, (kPlain)(kDdl)(kCatalog)(kSequence)); |
68 | | |
69 | | class PgClientSession { |
70 | | public: |
71 | | PgClientSession( |
72 | | client::YBClient* client, const scoped_refptr<ClockBase>& clock, |
73 | | std::reference_wrapper<const TransactionPoolProvider> transaction_pool_provider, |
74 | | PgTableCache* table_cache, uint64_t id); |
75 | | |
76 | | uint64_t id() const; |
77 | | |
78 | | CHECKED_STATUS Perform( |
79 | | const PgPerformRequestPB& req, PgPerformResponsePB* resp, rpc::RpcContext* context); |
80 | | |
81 | | #define PG_CLIENT_SESSION_METHOD_DECLARE(r, data, method) \ |
82 | | CHECKED_STATUS method( \ |
83 | | const BOOST_PP_CAT(BOOST_PP_CAT(Pg, method), RequestPB)& req, \ |
84 | | BOOST_PP_CAT(BOOST_PP_CAT(Pg, method), ResponsePB)* resp, \ |
85 | | rpc::RpcContext* context); |
86 | | |
87 | | BOOST_PP_SEQ_FOR_EACH(PG_CLIENT_SESSION_METHOD_DECLARE, ~, PG_CLIENT_SESSION_METHODS); |
88 | | |
89 | | private: |
90 | | friend class PgClientSessionLocker; |
91 | | |
92 | | std::string LogPrefix(); |
93 | | |
94 | | Result<const TransactionMetadata*> GetDdlTransactionMetadata( |
95 | | bool use_transaction, CoarseTimePoint deadline); |
96 | | CHECKED_STATUS BeginTransactionIfNecessary( |
97 | | const PgPerformOptionsPB& options, CoarseTimePoint deadline); |
98 | | Result<client::YBTransactionPtr> RestartTransaction( |
99 | | client::YBSession* session, client::YBTransaction* transaction); |
100 | | |
101 | | Result<client::YBSession*> SetupSession(const PgPerformRequestPB& req, CoarseTimePoint deadline); |
102 | | CHECKED_STATUS ProcessResponse( |
103 | | const PgClientSessionOperations& operations, const PgPerformRequestPB& req, |
104 | | PgPerformResponsePB* resp, rpc::RpcContext* context); |
105 | | void ProcessReadTimeManipulation(ReadTimeManipulation manipulation); |
106 | | |
107 | | client::YBClient& client(); |
108 | | client::YBSessionPtr& EnsureSession(PgClientSessionKind kind); |
109 | | client::YBSessionPtr& Session(PgClientSessionKind kind); |
110 | | client::YBTransactionPtr& Transaction(PgClientSessionKind kind); |
111 | | |
112 | | client::YBClient& client_; |
113 | | scoped_refptr<ClockBase> clock_; |
114 | | const TransactionPoolProvider& transaction_pool_provider_; |
115 | | PgTableCache& table_cache_; |
116 | | const uint64_t id_; |
117 | | |
118 | | std::mutex mutex_; |
119 | | struct SessionData { |
120 | | client::YBSessionPtr session; |
121 | | client::YBTransactionPtr transaction; |
122 | | }; |
123 | | std::array<SessionData, kPgClientSessionKindMapSize> sessions_; |
124 | | uint64_t txn_serial_no_ = 0; |
125 | | boost::optional<uint64_t> saved_priority_; |
126 | | TransactionMetadata ddl_txn_metadata_; |
127 | | }; |
128 | | |
129 | | class PgClientSessionLocker { |
130 | | public: |
131 | | explicit PgClientSessionLocker(PgClientSession* session) |
132 | 2.48M | : session_(session), lock_(session->mutex_) {} |
133 | | |
134 | 2.48M | PgClientSession* operator->() const { |
135 | 2.48M | return session_; |
136 | 2.48M | } |
137 | | |
138 | 0 | void Unlock() { |
139 | 0 | lock_.unlock(); |
140 | 0 | } |
141 | | |
142 | | private: |
143 | | PgClientSession* session_; |
144 | | std::unique_lock<std::mutex> lock_; |
145 | | }; |
146 | | |
147 | | } // namespace tserver |
148 | | } // namespace yb |
149 | | |
150 | | #endif // YB_TSERVER_PG_CLIENT_SESSION_H |