Storage Engine API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wiredtiger_session_cache.h
Go to the documentation of this file.
1 // wiredtiger_session_cache.h
2 
32 #pragma once
33 
34 #include <list>
35 #include <string>
36 
37 #include <wiredtiger.h>
38 
41 #include "mongo/platform/atomic_word.h"
42 #include "mongo/stdx/mutex.h"
43 #include "mongo/util/concurrency/spin_lock.h"
44 
45 namespace mongo {
46 
47 class WiredTigerKVEngine;
48 class WiredTigerSessionCache;
49 
51 public:
52  WiredTigerCachedCursor(uint64_t id, uint64_t gen, WT_CURSOR* cursor)
53  : _id(id), _gen(gen), _cursor(cursor) {}
54 
55  uint64_t _id; // Source ID, assigned to each URI
56  uint64_t _gen; // Generation, used to age out old cursors
57  WT_CURSOR* _cursor;
58 };
59 
66 public:
74  WiredTigerSession(WT_CONNECTION* conn, uint64_t epoch = 0, uint64_t cursorEpoch = 0);
75 
84  WiredTigerSession(WT_CONNECTION* conn,
86  uint64_t epoch = 0,
87  uint64_t cursorEpoch = 0);
88 
90 
91  WT_SESSION* getSession() const {
92  return _session;
93  }
94 
95  WT_CURSOR* getCursor(const std::string& uri, uint64_t id, bool forRecordStore);
96 
97  void releaseCursor(uint64_t id, WT_CURSOR* cursor);
98 
99  void closeCursorsForQueuedDrops(WiredTigerKVEngine* engine);
100 
105  void closeAllCursors(const std::string& uri);
106 
107  int cursorsOut() const {
108  return _cursorsOut;
109  }
110 
112  return _dropQueuedIdentsAtSessionEnd;
113  }
114 
115  void dropQueuedIdentsAtSessionEndAllowed(bool dropQueuedIdentsAtSessionEnd) {
116  _dropQueuedIdentsAtSessionEnd = dropQueuedIdentsAtSessionEnd;
117  }
118 
119  static uint64_t genTableId();
120 
124  static const uint64_t kMetadataTableId = 0;
125 
126 private:
128 
129  // The cursor cache is a list of pairs that contain an ID and cursor
130  typedef std::list<WiredTigerCachedCursor> CursorCache;
131 
132  // Used internally by WiredTigerSessionCache
133  uint64_t _getEpoch() const {
134  return _epoch;
135  }
136 
137  // Used internally by WiredTigerSessionCache
138  uint64_t _getCursorEpoch() const {
139  return _cursorEpoch;
140  }
141 
142  const uint64_t _epoch;
143  uint64_t _cursorEpoch;
145  WT_SESSION* _session; // owned
146  CursorCache _cursors; // owned
147  uint64_t _cursorGen;
149  bool _dropQueuedIdentsAtSessionEnd = true;
150 };
151 
157 public:
159  WiredTigerSessionCache(WT_CONNECTION* conn);
161 
166  public:
167  void operator()(WiredTigerSession* session) const;
168  };
169 
173  static bool isEngineCachingCursors();
174 
180  std::unique_ptr<WiredTigerSession, WiredTigerSessionDeleter> getSession();
181 
186  void closeAll();
187 
191  void closeCursorsForQueuedDrops();
192 
197  void closeAllCursors(const std::string& uri);
198 
204  void shuttingDown();
205 
206  bool isEphemeral();
212  void waitUntilDurable(bool forceCheckpoint, bool stableCheckpoint);
213 
226  void waitUntilPreparedUnitOfWorkCommitsOrAborts(OperationContext* opCtx);
227 
232  void notifyPreparedUnitOfWorkHasCommittedOrAborted();
233 
234  WT_CONNECTION* conn() const {
235  return _conn;
236  }
237 
239  return _snapshotManager;
240  }
242  return _snapshotManager;
243  }
244 
246 
247  uint64_t getCursorEpoch() const {
248  return _cursorEpoch.load();
249  }
250 
252  return _engine;
253  }
254 
255 private:
256  WiredTigerKVEngine* _engine; // not owned, might be NULL
257  WT_CONNECTION* _conn; // not owned
259 
260  // Used as follows:
261  // The low 31 bits are a count of active calls to releaseSession.
262  // The high bit is a flag that is set if and only if we're shutting down.
263  AtomicUInt32 _shuttingDown;
264  static const uint32_t kShuttingDownMask = 1 << 31;
265 
266  stdx::mutex _cacheLock;
267  typedef std::vector<WiredTigerSession*> SessionCache;
268  SessionCache _sessions;
269 
270  // Bumped when all open sessions need to be closed
271  AtomicUInt64 _epoch; // atomic so we can check it outside of the lock
272 
273  // Bumped when all open cursors need to be closed
274  AtomicUInt64 _cursorEpoch; // atomic so we can check it outside of the lock
275 
276  // Counter and critical section mutex for waitUntilDurable
277  AtomicUInt32 _lastSyncTime;
278  stdx::mutex _lastSyncMutex;
279 
280  // Mutex and cond var for waiting on prepare commit or abort.
282  stdx::condition_variable _prepareCommittedOrAbortedCond;
284 
285  // Protects _journalListener.
287  // Notified when we commit to the journal.
289 
290  WT_SESSION* _waitUntilDurableSession = nullptr; // owned, and never explicitly closed
291  // (uses connection close to clean up)
292 
297  void releaseSession(WiredTigerSession* session);
298 };
299 
303 typedef std::unique_ptr<WiredTigerSession,
306 } // namespace
stdx::condition_variable _prepareCommittedOrAbortedCond
Definition: wiredtiger_session_cache.h:282
static NoOpJournalListener instance
Definition: journal_listener.h:70
WT_CONNECTION * conn() const
Definition: wiredtiger_session_cache.h:234
const uint64_t _epoch
Definition: wiredtiger_session_cache.h:142
WT_SESSION * _session
Definition: wiredtiger_session_cache.h:145
KVEngine * engine
Definition: kv_engine_test_timestamps.cpp:205
void setJournalListener(JournalListener *jl)
Definition: dur.cpp:906
CursorCache _cursors
Definition: wiredtiger_session_cache.h:146
uint64_t getCursorEpoch() const
Definition: wiredtiger_session_cache.h:247
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
WT_CURSOR * _cursor
Definition: wiredtiger_session_cache.h:57
int cursorsOut() const
Definition: wiredtiger_session_cache.h:107
std::list< WiredTigerCachedCursor > CursorCache
Definition: wiredtiger_session_cache.h:130
stdx::mutex _journalListenerMutex
Definition: wiredtiger_session_cache.h:286
AtomicUInt64 _epoch
Definition: wiredtiger_session_cache.h:271
WiredTigerCachedCursor(uint64_t id, uint64_t gen, WT_CURSOR *cursor)
Definition: wiredtiger_session_cache.h:52
WiredTigerSessionCache * _cache
Definition: wiredtiger_session_cache.h:144
This is a structure that caches 1 cursor for each uri.
Definition: wiredtiger_session_cache.h:65
uint64_t _cursorEpoch
Definition: wiredtiger_session_cache.h:143
const WiredTigerSnapshotManager & snapshotManager() const
Definition: wiredtiger_session_cache.h:241
uint64_t _cursorGen
Definition: wiredtiger_session_cache.h:147
AtomicUInt64 _cursorEpoch
Definition: wiredtiger_session_cache.h:274
uint64_t _getEpoch() const
Definition: wiredtiger_session_cache.h:133
bool isDropQueuedIdentsAtSessionEndAllowed() const
Definition: wiredtiger_session_cache.h:111
AtomicUInt32 _shuttingDown
Definition: wiredtiger_session_cache.h:263
Definition: wiredtiger_kv_engine.h:65
WiredTigerKVEngine * _engine
Definition: wiredtiger_session_cache.h:256
std::unique_ptr< EphemeralForTestEngine > _engine
Definition: ephemeral_for_test_engine_test.cpp:53
This class allows for the storageEngine to alert the rest of the system about journaled write progres...
Definition: journal_listener.h:48
Definition: wiredtiger_session_cache.h:50
stdx::mutex _cacheLock
Definition: wiredtiger_session_cache.h:266
WiredTigerKVEngine * getKVEngine() const
Definition: wiredtiger_session_cache.h:251
SessionCache _sessions
Definition: wiredtiger_session_cache.h:268
std::vector< WiredTigerSession * > SessionCache
Definition: wiredtiger_session_cache.h:267
stdx::mutex _prepareCommittedOrAbortedMutex
Definition: wiredtiger_session_cache.h:281
WiredTigerSnapshotManager & snapshotManager()
Definition: wiredtiger_session_cache.h:238
WT_SESSION * getSession() const
Definition: wiredtiger_session_cache.h:91
void dropQueuedIdentsAtSessionEndAllowed(bool dropQueuedIdentsAtSessionEnd)
Definition: wiredtiger_session_cache.h:115
stdx::mutex _lastSyncMutex
Definition: wiredtiger_session_cache.h:278
int _cursorsOut
Definition: wiredtiger_session_cache.h:148
uint64_t _id
Definition: wiredtiger_session_cache.h:55
std::uint64_t _lastCommitOrAbortCounter
Definition: wiredtiger_session_cache.h:283
AtomicUInt32 _lastSyncTime
Definition: wiredtiger_session_cache.h:277
WT_CONNECTION * _conn
Definition: wiredtiger_prefixed_index_test.cpp:100
This deleter automatically releases WiredTigerSession objects when no longer needed.
Definition: wiredtiger_session_cache.h:165
uint64_t _gen
Definition: wiredtiger_session_cache.h:56
This cache implements a shared pool of WiredTiger sessions with the goal to amortize the cost of sess...
Definition: wiredtiger_session_cache.h:156
WiredTigerSnapshotManager _snapshotManager
Definition: wiredtiger_session_cache.h:258
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
std::unique_ptr< WiredTigerSession, typename WiredTigerSessionCache::WiredTigerSessionDeleter > UniqueWiredTigerSession
A unique handle type for WiredTigerSession pointers obtained from a WiredTigerSessionCache.
Definition: wiredtiger_session_cache.h:305
uint64_t _getCursorEpoch() const
Definition: wiredtiger_session_cache.h:138
WT_CONNECTION * _conn
Definition: wiredtiger_session_cache.h:257
Definition: wiredtiger_snapshot_manager.h:45
std::string uri
Definition: wiredtiger_standard_record_store_test.cpp:367