OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SessionInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SESSION_INFO_H
18 #define SESSION_INFO_H
19 
20 #include <atomic>
21 #include <cstdint>
22 #include <ctime>
23 #include <limits>
24 #include <list>
25 #include <map>
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include "Catalog/SysCatalog.h"
36 
37 #include "LeafHostInfo.h"
38 
39 namespace Catalog_Namespace {
40 
41 class Catalog;
42 
43 /*
44  * @type SessionInfo
45  * @brief a user session
46  */
47 class SessionInfo {
48  public:
49  SessionInfo(std::shared_ptr<Catalog> cat,
50  const UserMetadata& user,
51  const ExecutorDeviceType t,
52  const std::string& sid)
53  : catalog_(cat)
54  , currentUser_(user)
56  , session_id_(sid)
57  , last_used_time_(time(0))
58  , start_time_(time(0))
61  : catalog_(s.catalog_)
66 
67  // From https://en.cppreference.com/w/cpp/memory/shared_ptr :
68  // All member functions (including copy constructor and copy assignment) can be
69  // called by multiple threads on different instances of shared_ptr without
70  // additional synchronization even if these instances are copies and share ownership
71  // of the same object. If multiple threads of execution access the same instance of
72  // shared_ptr without synchronization and any of those accesses uses a non-const
73  // member function of shared_ptr then a data race will occur; the shared_ptr
74  // overloads of atomic functions can be used to prevent the data race.
75  Catalog& getCatalog() const {
77  CHECK(catalog_);
78  return *catalog_;
79  }
80  std::shared_ptr<Catalog> get_catalog_ptr() const {
82  return catalog_;
83  }
84  void set_catalog_ptr(std::shared_ptr<Catalog> c) {
86  catalog_ = c;
87  }
88  const UserMetadata& get_currentUser() const { return currentUser_; }
90  return executor_device_type_;
91  }
93  std::string get_session_id() const { return session_id_; }
94  time_t get_last_used_time() const { return last_used_time_; }
95  void update_last_used_time() { last_used_time_ = time(0); }
96  // TODO(max): this method does not belong here
97  bool checkDBAccessPrivileges(const DBObjectType& permissionType,
98  const AccessPrivileges& privs,
99  const std::string& objectName = "") const;
100  time_t get_start_time() const { return start_time_; }
101  std::string const& get_public_session_id() const { return public_session_id_; }
102  operator std::string() const { return public_session_id_; }
103  std::string const& get_connection_info() const { return connection_info_; }
104  void set_connection_info(const std::string& connection) {
105  connection_info_ = connection;
106  }
108 
109  private:
110  // TODO(max): C++20 introduces atomic shared_ptr
111  std::shared_ptr<Catalog> catalog_;
113  std::atomic<ExecutorDeviceType> executor_device_type_;
114  const std::string session_id_;
115  std::atomic<time_t> last_used_time_; // for tracking active session duration
116  std::atomic<time_t> start_time_; // for invalidating session after tolerance period
117  const std::string public_session_id_;
118  std::string
119  connection_info_; // String containing connection protocol (tcp/http) and address
120  std::string public_session_id() const;
122 };
123 
124 std::ostream& operator<<(std::ostream& os, const SessionInfo& session_info);
125 
126 const size_t CALCITE_SESSION_ID_LENGTH = 64;
127 const size_t SESSION_ID_LENGTH = 32;
128 
129 } // namespace Catalog_Namespace
130 
131 #endif /* SESSION_INFO_H */
std::lock_guard< T > lock_guard
std::shared_ptr< Catalog > catalog_
Definition: SessionInfo.h:111
const std::string session_id_
Definition: SessionInfo.h:114
std::string cat(Ts &&...args)
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:143
std::string const & get_public_session_id() const
Definition: SessionInfo.h:101
DBObjectType
Definition: DBObject.h:40
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:57
const std::string public_session_id_
Definition: SessionInfo.h:117
std::atomic< time_t > last_used_time_
Definition: SessionInfo.h:115
SessionInfo(const SessionInfo &s)
Definition: SessionInfo.h:60
std::string public_session_id() const
Definition: SessionInfo.cpp:43
ExecutorDeviceType
std::atomic< time_t > start_time_
Definition: SessionInfo.h:116
std::shared_lock< T > shared_lock
std::atomic< ExecutorDeviceType > executor_device_type_
Definition: SessionInfo.h:113
This file contains the class specification and related data structures for SysCatalog.
SessionInfo(std::shared_ptr< Catalog > cat, const UserMetadata &user, const ExecutorDeviceType t, const std::string &sid)
Definition: SessionInfo.h:49
std::string const & get_connection_info() const
Definition: SessionInfo.h:103
const size_t CALCITE_SESSION_ID_LENGTH
Definition: SessionInfo.h:126
std::shared_ptr< Catalog > get_catalog_ptr() const
Definition: SessionInfo.h:80
const size_t SESSION_ID_LENGTH
Definition: SessionInfo.h:127
std::string get_session_id() const
Definition: SessionInfo.h:93
Catalog & getCatalog() const
Definition: SessionInfo.h:75
heavyai::shared_mutex mtx_
Definition: SessionInfo.h:121
time_t get_start_time() const
Definition: SessionInfo.h:100
heavyai::shared_mutex & getLock()
Definition: SessionInfo.h:107
#define CHECK(condition)
Definition: Logger.h:291
void set_executor_device_type(ExecutorDeviceType t)
Definition: SessionInfo.h:92
bool checkDBAccessPrivileges(const DBObjectType &permissionType, const AccessPrivileges &privs, const std::string &objectName="") const
Definition: SessionInfo.cpp:24
void set_catalog_ptr(std::shared_ptr< Catalog > c)
Definition: SessionInfo.h:84
std::shared_timed_mutex shared_mutex
time_t get_last_used_time() const
Definition: SessionInfo.h:94
const UserMetadata & get_currentUser() const
Definition: SessionInfo.h:88
const ExecutorDeviceType get_executor_device_type() const
Definition: SessionInfo.h:89
void set_connection_info(const std::string &connection)
Definition: SessionInfo.h:104