Skip to content

Commit

Permalink
fix(parallelCollectionScan): do not use implicit sessions on cursors
Browse files Browse the repository at this point in the history
When we removed the session use from parallelCollectionScan,
we introduced an error where parallelCollectionScan cursors would
attempt to use implicit sessions on their cursors.
  • Loading branch information
daprahamian authored Jun 20, 2018
1 parent 50a9f65 commit 2de470a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,15 @@ function Cursor(bson, ns, cmd, options, topology, topologyOptions) {
promiseLibrary: promiseLibrary,
// Current doc
currentDoc: null,
// Optional ClientSession
session: options.session
// explicitlyIgnoreSession
explicitlyIgnoreSession: options.explicitlyIgnoreSession
};

// Optional ClientSession
if (!options.explicitlyIgnoreSession && options.session) {
this.s.session = options.session;
}

// Translate correctly
if (this.s.options.noCursorTimeout === true) {
this.addCursorFlag('noCursorTimeout', true);
Expand Down Expand Up @@ -211,7 +216,7 @@ for (let name in CoreCursor.prototype) {
}

Cursor.prototype._initImplicitSession = function() {
if (!this.s.session && this.s.topology.hasSessionSupport()) {
if (!this.s.explicitlyIgnoreSession && !this.s.session && this.s.topology.hasSessionSupport()) {
this.s.session = this.s.topology.startSession({ owner: this });
this.cursorState.session = this.s.session;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/operations/collection_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ function parallelCollectionScan(coll, options, callback) {
null
);

options = Object.assign({ explicitlyIgnoreSession: true }, options);

const cursors = [];
// Add the raw back to the option
if (raw) options.raw = raw;
Expand Down
54 changes: 54 additions & 0 deletions test/functional/find_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const test = require('./shared').assert;
const setupDatabase = require('./shared').setupDatabase;
const expect = require('chai').expect;
const MongoClient = require('../../lib/mongo_client');

describe('Find', function() {
before(function() {
Expand Down Expand Up @@ -2624,6 +2625,59 @@ describe('Find', function() {
}
});

it('Should not use a session when using parallelCollectionScan', {
metadata: {
requires: {
mongodb: '>=3.6.0',
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
}
},
test: function(done) {
const configuration = this.configuration;
const client = new MongoClient(configuration.url());

client.connect(function(err, client) {
var db = client.db(configuration.db);
var docs = [];

// Insert some documents
for (var i = 0; i < 1000; i++) {
docs.push({ a: i });
}

// Get the collection
var collection = db.collection('parallelCollectionScan_4');
// Insert 1000 documents in a batch
collection.insert(docs, function(err) {
expect(err).to.be.null;
var numCursors = 1;

// Execute parallelCollectionScan command
collection.parallelCollectionScan({ numCursors: numCursors }, function(err, cursors) {
expect(err).to.be.null;
expect(cursors)
.to.be.an('array')
.with.lengthOf(1);

const cursor = cursors[0];

expect(cursor).to.not.have.nested.property('s.session');
expect(cursor)
.to.have.nested.property('s.explicitlyIgnoreSession')
.that.equals(true);

cursor.toArray(err => {
expect(err).to.be.null;
expect(cursor).to.not.have.nested.property('s.session');

cursor.close().then(() => client.close().then(() => done()));
});
});
});
});
}
});

it('Should correctly sort using text search on 2.6 or higher in find', {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
Expand Down

0 comments on commit 2de470a

Please sign in to comment.