OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CachedSessionStore Class Reference
+ Inheritance diagram for CachedSessionStore:
+ Collaboration diagram for CachedSessionStore:

Public Member Functions

 CachedSessionStore (int idle_session_duration, int max_session_duration, int capacity, DisconnectCallback disconnect_callback)
 
SessionInfoPtr add (const Catalog_Namespace::UserMetadata &user_meta, std::shared_ptr< Catalog > cat, ExecutorDeviceType device) override
 
SessionInfoPtr get (const std::string &session_id) override
 
heavyai::shared_mutexgetLock () override
 
void eraseIf (std::function< bool(const SessionInfoPtr &)> predicate) override
 
 ~CachedSessionStore () override
 
- Public Member Functions inherited from Catalog_Namespace::SessionsStore
void erase (const std::string &session_id)
 
void eraseByUser (const std::string &user_name)
 
void eraseByDB (const std::string &db_name)
 
std::vector< SessionInfoPtrgetAllSessions ()
 
std::vector< SessionInfoPtrgetUserSessions (const std::string &user_name)
 
SessionInfoPtr getByPublicID (const std::string &public_id)
 
virtual ~SessionsStore ()=default
 
SessionInfo getSessionCopy (const std::string &session_id)
 
void disconnect (const std::string session_id)
 

Protected Member Functions

void eraseUnlocked (const std::string &session_id) override
 
bool isSessionInUse (const SessionInfoPtr &session_ptr) override
 
SessionInfoPtr getUnlocked (const std::string &session_id) override
 
DisconnectCallback getDisconnectCallback () override
 
std::vector< SessionInfoPtrgetIf (std::function< bool(const SessionInfoPtr &)> predicate) override
 
- Protected Member Functions inherited from Catalog_Namespace::SessionsStore
bool isSessionExpired (const SessionInfoPtr &session_ptr, int idle_session_duration, int max_session_duration)
 

Private Attributes

std::unordered_map
< std::string, SessionInfoPtr
sessions_
 
heavyai::shared_mutex mtx_
 
const int idle_session_duration_
 
const int max_session_duration_
 
const int capacity_
 
DisconnectCallback disconnect_callback_
 

Additional Inherited Members

- Static Public Member Functions inherited from Catalog_Namespace::SessionsStore
static std::unique_ptr
< SessionsStore
create (const std::string &base_path, size_t n_workers, int idle_session_duration, int max_session_duration, int capacity, DisconnectCallback disconnect_callback)
 

Detailed Description

Definition at line 114 of file SessionsStore.cpp.

Constructor & Destructor Documentation

CachedSessionStore::CachedSessionStore ( int  idle_session_duration,
int  max_session_duration,
int  capacity,
DisconnectCallback  disconnect_callback 
)
inline

Definition at line 116 of file SessionsStore.cpp.

120  : idle_session_duration_(idle_session_duration)
121  , max_session_duration_(max_session_duration)
122  , capacity_(capacity > 0 ? capacity : INT_MAX)
123  , disconnect_callback_(disconnect_callback) {}
const int max_session_duration_
const int idle_session_duration_
DisconnectCallback disconnect_callback_
CachedSessionStore::~CachedSessionStore ( )
inlineoverride

Definition at line 196 of file SessionsStore.cpp.

196  {
197  std::lock_guard lg(mtx_);
198  sessions_.clear();
199  }
std::lock_guard< T > lock_guard
std::unordered_map< std::string, SessionInfoPtr > sessions_
heavyai::shared_mutex mtx_

Member Function Documentation

SessionInfoPtr CachedSessionStore::add ( const Catalog_Namespace::UserMetadata user_meta,
std::shared_ptr< Catalog cat,
ExecutorDeviceType  device 
)
inlineoverridevirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 125 of file SessionsStore.cpp.

References cat(), generate_random_string(), Catalog_Namespace::SESSION_ID_LENGTH, and UNREACHABLE.

127  {
129  if (int(sessions_.size()) >= capacity_) {
130  std::vector<SessionInfoPtr> expired_sessions;
131  for (auto it = sessions_.begin(); it != sessions_.end(); it++) {
133  expired_sessions.push_back(it->second);
134  }
135  }
136  for (auto& session_ptr : expired_sessions) {
137  try {
138  disconnect_callback_(session_ptr);
139  eraseUnlocked(session_ptr->get_session_id());
140  } catch (const std::exception& e) {
141  eraseUnlocked(session_ptr->get_session_id());
142  throw e;
143  }
144  }
145  }
146  if (int(sessions_.size()) < capacity_) {
147  do {
149  if (sessions_.count(session_id) != 0) {
150  continue;
151  }
152  auto session_ptr = std::make_shared<Catalog_Namespace::SessionInfo>(
153  cat, user_meta, device, session_id);
154  sessions_[session_id] = session_ptr;
155  return session_ptr;
156  } while (true);
157  UNREACHABLE();
158  }
159  throw std::runtime_error("Too many active sessions");
160  }
std::lock_guard< T > lock_guard
std::string cat(Ts &&...args)
const int max_session_duration_
#define UNREACHABLE()
Definition: Logger.h:338
std::unordered_map< std::string, SessionInfoPtr > sessions_
const int idle_session_duration_
void eraseUnlocked(const std::string &session_id) override
std::string generate_random_string(const size_t len)
bool isSessionExpired(const SessionInfoPtr &session_ptr, int idle_session_duration, int max_session_duration)
const size_t SESSION_ID_LENGTH
Definition: SessionInfo.h:127
DisconnectCallback disconnect_callback_
heavyai::shared_mutex mtx_

+ Here is the call graph for this function:

void CachedSessionStore::eraseIf ( std::function< bool(const SessionInfoPtr &)>  predicate)
inlineoverridevirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 185 of file SessionsStore.cpp.

185  {
187  for (auto it = sessions_.begin(); it != sessions_.end();) {
188  if (predicate(it->second)) {
189  it = sessions_.erase(it);
190  } else {
191  it++;
192  }
193  }
194  }
std::lock_guard< T > lock_guard
std::unordered_map< std::string, SessionInfoPtr > sessions_
heavyai::shared_mutex mtx_
void CachedSessionStore::eraseUnlocked ( const std::string &  session_id)
inlineoverrideprotectedvirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 202 of file SessionsStore.cpp.

202  {
203  sessions_.erase(session_id);
204  }
std::unordered_map< std::string, SessionInfoPtr > sessions_
SessionInfoPtr CachedSessionStore::get ( const std::string &  session_id)
inlineoverridevirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 162 of file SessionsStore.cpp.

References Catalog_Namespace::SessionsStore::isSessionExpired().

162  {
164  auto session_ptr = getUnlocked(session_id);
165  if (session_ptr) {
166  if (SessionsStore::isSessionExpired(
168  try {
169  disconnect_callback_(session_ptr);
170  eraseUnlocked(session_ptr->get_session_id());
171  } catch (const std::exception& e) {
172  eraseUnlocked(session_ptr->get_session_id());
173  throw e;
174  }
175  return nullptr;
176  }
177  session_ptr->update_last_used_time();
178  return session_ptr;
179  }
180  return nullptr;
181  }
std::lock_guard< T > lock_guard
const int max_session_duration_
const int idle_session_duration_
void eraseUnlocked(const std::string &session_id) override
DisconnectCallback disconnect_callback_
SessionInfoPtr getUnlocked(const std::string &session_id) override
heavyai::shared_mutex mtx_

+ Here is the call graph for this function:

DisconnectCallback CachedSessionStore::getDisconnectCallback ( )
inlineoverrideprotectedvirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 217 of file SessionsStore.cpp.

217 { return disconnect_callback_; }
DisconnectCallback disconnect_callback_
std::vector<SessionInfoPtr> CachedSessionStore::getIf ( std::function< bool(const SessionInfoPtr &)>  predicate)
inlineoverrideprotectedvirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 219 of file SessionsStore.cpp.

220  {
221  std::vector<SessionInfoPtr> out;
223  for (auto& [_, session] : sessions_) {
224  heavyai::shared_lock<heavyai::shared_mutex> session_lock(session->getLock());
225  if (predicate(session)) {
226  out.push_back(session);
227  }
228  }
229  return out;
230  }
std::unordered_map< std::string, SessionInfoPtr > sessions_
std::shared_lock< T > shared_lock
heavyai::shared_mutex & getLock() override
heavyai::shared_mutex& CachedSessionStore::getLock ( )
inlineoverridevirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 183 of file SessionsStore.cpp.

183 { return mtx_; }
heavyai::shared_mutex mtx_
SessionInfoPtr CachedSessionStore::getUnlocked ( const std::string &  session_id)
inlineoverrideprotectedvirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 210 of file SessionsStore.cpp.

210  {
211  if (auto session_it = sessions_.find(session_id); session_it != sessions_.end()) {
212  return session_it->second;
213  }
214  return nullptr;
215  }
std::unordered_map< std::string, SessionInfoPtr > sessions_
bool CachedSessionStore::isSessionInUse ( const SessionInfoPtr session_ptr)
inlineoverrideprotectedvirtual

Implements Catalog_Namespace::SessionsStore.

Definition at line 206 of file SessionsStore.cpp.

206  {
207  return session_ptr.use_count() > 2;
208  }

Member Data Documentation

const int CachedSessionStore::capacity_
private

Definition at line 237 of file SessionsStore.cpp.

DisconnectCallback CachedSessionStore::disconnect_callback_
private

Definition at line 238 of file SessionsStore.cpp.

const int CachedSessionStore::idle_session_duration_
private

Definition at line 235 of file SessionsStore.cpp.

const int CachedSessionStore::max_session_duration_
private

Definition at line 236 of file SessionsStore.cpp.

heavyai::shared_mutex CachedSessionStore::mtx_
mutableprivate

Definition at line 234 of file SessionsStore.cpp.

std::unordered_map<std::string, SessionInfoPtr> CachedSessionStore::sessions_
private

Definition at line 233 of file SessionsStore.cpp.


The documentation for this class was generated from the following file: