Skip to content

Commit

Permalink
SERVER-15187 Add OperationContext parameter to restoreState
Browse files Browse the repository at this point in the history
Storage engines might need the current OperationContext to restore their
state.

For example, BDBRecordIterator needs access to the current underlying
transaction to recreate an underlying cursor. Having the BDBRecordIterator
save a pointer to the OperationContext it was created with won't work,
because the OperationContext is destroyed and a new one created between
saveState and restoreState.
  • Loading branch information
dpercy authored and GeertBosch committed Sep 16, 2014
1 parent 9020229 commit 0b16754
Show file tree
Hide file tree
Showing 24 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/mongo/db/exec/collection_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace mongo {
void CollectionScan::restoreState(OperationContext* opCtx) {
++_commonStats.unyields;
if (NULL != _iter) {
if (!_iter->restoreState()) {
if (!_iter->restoreState(opCtx)) {
warning() << "Collection dropped or state deleted during yield of CollectionScan";
_nsDropped = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mongo/db/exec/count_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace mongo {
++_commonStats.unyields;
if (_hitEnd || (NULL == _btreeCursor.get())) { return; }

if (!_btreeCursor->restorePosition().isOK()) {
if (!_btreeCursor->restorePosition( opCtx ).isOK()) {
_hitEnd = true;
return;
}
Expand All @@ -176,7 +176,7 @@ namespace mongo {
return;
}

if (!_endCursor->restorePosition().isOK()) {
if (!_endCursor->restorePosition( opCtx ).isOK()) {
_hitEnd = true;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/exec/distinct_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace mongo {

// We can have a valid position before we check isEOF(), restore the position, and then be
// EOF upon restore.
if (!_btreeCursor->restorePosition().isOK() || _btreeCursor->isEOF()) {
if (!_btreeCursor->restorePosition( opCtx ).isOK() || _btreeCursor->isEOF()) {
_hitEnd = true;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/exec/index_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace mongo {

// We can have a valid position before we check isEOF(), restore the position, and then be
// EOF upon restore.
if (!_indexCursor->restorePosition().isOK() || _indexCursor->isEOF()) {
if (!_indexCursor->restorePosition( opCtx ).isOK() || _indexCursor->isEOF()) {
_hitEnd = true;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/exec/multi_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace mongo {

void MultiIteratorStage::restoreState(OperationContext* opCtx) {
for (size_t i = 0; i < _iterators.size(); i++) {
if (!_iterators[i]->restoreState()) {
if (!_iterators[i]->restoreState(opCtx)) {
kill();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/exec/oplogstart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace mongo {
}

for (size_t i = 0; i < _subIterators.size(); i++) {
if (!_subIterators[i]->restoreState()) {
if (!_subIterators[i]->restoreState(opCtx)) {
_subIterators.erase(_subIterators.begin() + i);
// need to hit same i on next pass through loop
i--;
Expand Down
4 changes: 2 additions & 2 deletions src/mongo/db/index/btree_index_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ namespace mongo {
return Status::OK();
}

Status BtreeIndexCursor::restorePosition() {
_cursor->restorePosition();
Status BtreeIndexCursor::restorePosition(OperationContext* txn) {
_cursor->restorePosition(txn);
return Status::OK();
}

Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/index/btree_index_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace mongo {

virtual Status savePosition();

virtual Status restorePosition();
virtual Status restorePosition(OperationContext* txn);

virtual std::string toString();

Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/index/index_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace mongo {
* Restore the saved position. Errors if there is no saved position.
* The cursor may be EOF after a restore.
*/
virtual Status restorePosition() = 0;
virtual Status restorePosition(OperationContext* txn) = 0;

// Return a std::string describing the cursor.
virtual std::string toString() = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/mongo/db/storage/heap1/heap1_btree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ namespace {
_savedLoc = _it->loc;
}

virtual void restorePosition() {
virtual void restorePosition(OperationContext* txn) {
if (_savedAtEnd) {
_it = _data.end();
}
Expand Down Expand Up @@ -385,7 +385,7 @@ namespace {
_savedLoc = _it->loc;
}

virtual void restorePosition() {
virtual void restorePosition(OperationContext* txn) {
if (_savedAtEnd) {
_it = _data.rend();
}
Expand Down
5 changes: 3 additions & 2 deletions src/mongo/db/storage/heap1/record_store_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ namespace mongo {
void HeapRecordIterator::saveState() {
}

bool HeapRecordIterator::restoreState() {
bool HeapRecordIterator::restoreState(OperationContext* txn) {
_txn = txn;
return !_killedByInvalidate;
}

Expand Down Expand Up @@ -499,7 +500,7 @@ namespace mongo {
void HeapRecordReverseIterator::saveState() {
}

bool HeapRecordReverseIterator::restoreState() {
bool HeapRecordReverseIterator::restoreState(OperationContext* txn) {
return !_killedByInvalidate;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mongo/db/storage/heap1/record_store_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ namespace mongo {

virtual void saveState();

virtual bool restoreState();
virtual bool restoreState(OperationContext* txn);

virtual RecordData dataFor( const DiskLoc& loc ) const;

Expand Down Expand Up @@ -225,7 +225,7 @@ namespace mongo {

virtual void saveState();

virtual bool restoreState();
virtual bool restoreState(OperationContext* txn);

virtual RecordData dataFor( const DiskLoc& loc ) const;

Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/storage/mmap_v1/btree/btree_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ namespace mongo {
}
}

virtual void restorePosition() {
virtual void restorePosition(OperationContext* txn) {
if (!_bucket.isNull()) {
_btree->restorePosition(_txn,
_savedKey,
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/storage/mmap_v1/record_store_v1_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ namespace mongo {

virtual void saveState() {}

virtual bool restoreState() { return true; }
virtual bool restoreState(OperationContext* txn) { return true; }

virtual RecordData dataFor( const DiskLoc& loc ) const { return _rs->dataFor(_txn, loc); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ namespace mongo {
void CappedRecordStoreV1Iterator::saveState() {
}

bool CappedRecordStoreV1Iterator::restoreState() {
bool CappedRecordStoreV1Iterator::restoreState(OperationContext* txn) {
_txn = txn;
// If invalidate invalidated the DiskLoc we relied on, give up now.
if (_killedByInvalidate) {
_recordStore = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace mongo {

virtual void invalidate(const DiskLoc& dl);
virtual void saveState();
virtual bool restoreState();
virtual bool restoreState(OperationContext* txn);

virtual RecordData dataFor( const DiskLoc& loc ) const;
private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace mongo {

virtual void invalidate(const DiskLoc& dl);
virtual void saveState() { }
virtual bool restoreState() {
virtual bool restoreState(OperationContext* txn) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ namespace mongo {
void SimpleRecordStoreV1Iterator::saveState() {
}

bool SimpleRecordStoreV1Iterator::restoreState() {
bool SimpleRecordStoreV1Iterator::restoreState(OperationContext* txn) {
_txn = txn;
// if the collection is dropped, then the cursor should be destroyed
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace mongo {

virtual void invalidate(const DiskLoc& dl);
virtual void saveState();
virtual bool restoreState();
virtual bool restoreState(OperationContext* txn);

virtual RecordData dataFor( const DiskLoc& loc ) const;

Expand Down
4 changes: 3 additions & 1 deletion src/mongo/db/storage/record_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ namespace mongo {
virtual void saveState() = 0;

// Returns true if collection still exists, false otherwise.
virtual bool restoreState() = 0;
// The state of the iterator may be restored into a different context
// than the one it was created in.
virtual bool restoreState(OperationContext* txn) = 0;

// normally this will just go back to the RecordStore and convert
// but this gives the iterator an oppurtnity to optimize
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/db/storage/rocks/rocks_record_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ namespace mongo {
}
}

bool RocksRecordStore::Iterator::restoreState() {
bool RocksRecordStore::Iterator::restoreState(OperationContext* txn) {
_txn = txn;
if ( !_reseekKeyValid ) {
_iterator.reset( NULL );
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/storage/rocks/rocks_record_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace mongo {
virtual DiskLoc getNext();
virtual void invalidate(const DiskLoc& dl);
virtual void saveState();
virtual bool restoreState();
virtual bool restoreState(OperationContext* txn);
virtual RecordData dataFor( const DiskLoc& loc ) const;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/storage/sorted_data_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace mongo {
//
virtual void savePosition() = 0;

virtual void restorePosition() = 0;
virtual void restorePosition(OperationContext* txn) = 0;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/mongo/db/storage/sorted_data_interface_test_harness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ namespace mongo {
n++;
cursor->advance();
cursor->savePosition();
cursor->restorePosition();
cursor->restorePosition( opCtx.get() );
}
ASSERT_EQUALS( N, n );
}
Expand Down Expand Up @@ -332,7 +332,7 @@ namespace mongo {
n++;
cursor->advance();
cursor->savePosition();
cursor->restorePosition();
cursor->restorePosition( opCtx.get() );
}
ASSERT_EQUALS( N, n );
}
Expand Down

0 comments on commit 0b16754

Please sign in to comment.