YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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)(AlterTable)(BackfillIndex)(CreateDatabase)(CreateTable)(CreateTablegroup) \
45
    (DropDatabase)(DropTable)(DropTablegroup)(FinishTransaction)(RollbackSubTransaction) \
46
    (SetActiveSubTransaction)(TruncateTable)
47
48
using PgClientSessionOperations = std::vector<std::shared_ptr<client::YBPgsqlOp>>;
49
class PgClientSessionLocker;
50
51
class PgClientSession {
52
 public:
53
  PgClientSession(
54
      client::YBClient* client, const scoped_refptr<ClockBase>& clock,
55
      std::reference_wrapper<const TransactionPoolProvider> transaction_pool_provider,
56
      PgTableCache* table_cache, uint64_t id);
57
58
  uint64_t id() const;
59
60
  CHECKED_STATUS Perform(
61
      const PgPerformRequestPB& req, PgPerformResponsePB* resp, rpc::RpcContext* context);
62
63
  #define PG_CLIENT_SESSION_METHOD_DECLARE(r, data, method) \
64
  CHECKED_STATUS method( \
65
      const BOOST_PP_CAT(BOOST_PP_CAT(Pg, method), RequestPB)& req, \
66
      BOOST_PP_CAT(BOOST_PP_CAT(Pg, method), ResponsePB)* resp, \
67
      rpc::RpcContext* context);
68
69
  BOOST_PP_SEQ_FOR_EACH(PG_CLIENT_SESSION_METHOD_DECLARE, ~, PG_CLIENT_SESSION_METHODS);
70
71
 private:
72
  friend class PgClientSessionLocker;
73
74
  std::string LogPrefix();
75
76
  Result<const TransactionMetadata*> GetDdlTransactionMetadata(bool use_transaction);
77
  CHECKED_STATUS BeginTransactionIfNecessary(const PgPerformOptionsPB& options);
78
  Result<client::YBTransactionPtr> RestartTransaction(
79
      client::YBSession* session, client::YBTransaction* transaction);
80
81
  Result<client::YBSession*> SetupSession(const PgPerformRequestPB& req);
82
  CHECKED_STATUS ProcessResponse(
83
      const PgClientSessionOperations& operations, const PgPerformRequestPB& req,
84
      PgPerformResponsePB* resp, rpc::RpcContext* context);
85
  void ProcessReadTimeManipulation(ReadTimeManipulation manipulation);
86
87
  client::YBClient& client();
88
89
  client::YBClient& client_;
90
  const TransactionPoolProvider& transaction_pool_provider_;
91
  PgTableCache& table_cache_;
92
  const uint64_t id_;
93
94
  std::mutex mutex_;
95
  client::YBSessionPtr session_;
96
  client::YBTransactionPtr txn_;
97
  uint64_t txn_serial_no_ = 0;
98
  boost::optional<uint64_t> saved_priority_;
99
  client::YBSessionPtr ddl_session_;
100
  client::YBTransactionPtr ddl_txn_;
101
  TransactionMetadata ddl_txn_metadata_;
102
103
  client::YBSessionPtr catalog_session_;
104
};
105
106
class PgClientSessionLocker {
107
 public:
108
  explicit PgClientSessionLocker(PgClientSession* session)
109
932k
      : session_(session), lock_(session->mutex_) {}
110
111
932k
  PgClientSession* operator->() const {
112
932k
    return session_;
113
932k
  }
114
115
0
  void Unlock() {
116
0
    lock_.unlock();
117
0
  }
118
119
 private:
120
  PgClientSession* session_;
121
  std::unique_lock<std::mutex> lock_;
122
};
123
124
}  // namespace tserver
125
}  // namespace yb
126
127
#endif  // YB_TSERVER_PG_CLIENT_SESSION_H