Skip to content

Commit 0e34eff

Browse files
committed
Added better support for domains in 0.10.X and 0.11.X, added some fixes for write concern errors in downconversion
1 parent d49eb35 commit 0e34eff

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

lib/mongodb/collection/batch/common.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,19 @@ var mergeBatchResults = function(ordered, batch, bulkResult, err, result) {
244244
return;
245245
}
246246

247-
//
248-
// NEEDED to pass tests as some write errors are
249-
// returned as write concern errors (j write on non journal mongod)
250-
// also internal error code 75 is still making it out as a write concern error
251-
//
252-
if(result && result.writeConcernError
253-
&& (result.writeConcernError.code == 2 || result.writeConcernError.code == 75)) {
254-
console.log("Should not happen as errors 2 and 75 should be top level errors");
255-
256-
bulkResult.ok = 0;
257-
bulkResult.error = utils.toError(new WriteConcernError(result.writeConcernError));
258-
return;
259-
}
247+
// //
248+
// // NEEDED to pass tests as some write errors are
249+
// // returned as write concern errors (j write on non journal mongod)
250+
// // also internal error code 75 is still making it out as a write concern error
251+
// //
252+
// if(result && result.writeConcernError
253+
// && (result.writeConcernError.code == 2 || result.writeConcernError.code == 75)) {
254+
// console.log("Should not happen as errors 2 and 75 should be top level errors");
255+
256+
// bulkResult.ok = 0;
257+
// bulkResult.error = utils.toError(new WriteConcernError(result.writeConcernError));
258+
// return;
259+
// }
260260

261261
// If we have an insert Batch type
262262
if(batch.batchType == INSERT) {
@@ -324,7 +324,8 @@ var mergeLegacyResults = function(_ordered, _op, _batch, _results, _result, _ind
324324
if(_results.ok == 0) return false;
325325

326326
// Handle error
327-
if(_result.errmsg || _result.err || _result instanceof Error) {
327+
if((_result.errmsg || _result.err || _result instanceof Error)
328+
&& (_result.wtimeout == null && _result.jnote == null && _result.wnote == null)) {
328329
var code = _result.code || UNKNOWN_ERROR; // Returned error code or unknown code
329330
var errmsg = _result.errmsg || _result.err;
330331
errmsg = errmsg || _result.message;
@@ -378,6 +379,14 @@ var mergeLegacyResults = function(_ordered, _op, _batch, _results, _result, _ind
378379
_results.nRemoved = _results.nRemoved + _result;
379380
}
380381

382+
// We have a write concern error, add a write concern error to the results
383+
if(_result.wtimeout != null || _result.jnote != null || _result.wnote != null) {
384+
var error = _result.err || _result.errmsg || _result.wnote || _result.jnote || _result.wtimeout;
385+
var code = _result.code || WRITE_CONCERN_ERROR;
386+
// Push a write concern error to the list
387+
_results.writeConcernErrors.push(new WriteConcernError({errmsg: error, code: code}));
388+
}
389+
381390
// We have an upserted field (might happen with a write concern error)
382391
if(_result.upserted) {
383392
_results.upserted.push({

lib/mongodb/db.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,18 @@ Db.prototype.stats = function stats(options, callback) {
15731573
this.command(commandObject, options, callback);
15741574
}
15751575

1576+
/**
1577+
* @ignore
1578+
*/
1579+
var bindToCurrentDomain = function(callback) {
1580+
var domain = process.domain;
1581+
if(domain == null || callback == null) {
1582+
return callback;
1583+
} else {
1584+
return domain.bind(callback);
1585+
}
1586+
}
1587+
15761588
/**
15771589
* @ignore
15781590
*/
@@ -1599,6 +1611,7 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
15991611

16001612
// If we got a callback object
16011613
if(typeof callback === 'function' && !onAll) {
1614+
callback = bindToCurrentDomain(callback);
16021615
// Override connection if we passed in a specific connection
16031616
var connection = specifiedConnection != null ? specifiedConnection : null;
16041617

@@ -1633,6 +1646,7 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
16331646
}
16341647
});
16351648
} else if(typeof callback === 'function' && onAll) {
1649+
callback = bindToCurrentDomain(callback);
16361650
var connections = self.serverConfig.allRawConnections();
16371651
var numberOfEntries = connections.length;
16381652
// Go through all the connections
@@ -1800,6 +1814,7 @@ var __executeInsertCommand = function(self, db_command, options, callback) {
18001814

18011815
// Ensure we have a valid connection
18021816
if(typeof callback === 'function') {
1817+
callback = bindToCurrentDomain(callback);
18031818
// Ensure we have a valid connection
18041819
if(connection == null) {
18051820
return callback(new Error("no open connections"));

test/tests/repl_set/batch_api_tests.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ exports['Should fail due to w:5 and wtimeout:1 with ordered batch api'] = functi
4444
// Execute the operations
4545
batch.execute({w:5, wtimeout:1}, function(err, result) {
4646
test.equal(2, result.nInserted);
47-
test.equal(0, result.nUpdated);
47+
test.equal(0, result.nMatched);
4848
test.equal(0, result.nUpserted);
4949
test.equal(0, result.nRemoved);
5050
test.equal(0, result.nModified);
@@ -112,7 +112,7 @@ exports['Should fail due to w:5 and wtimeout:1 combined with duplicate key error
112112
// Execute the operations
113113
batch.execute({w:5, wtimeout:1}, function(err, result) {
114114
test.equal(1, result.nInserted);
115-
test.equal(0, result.nUpdated);
115+
test.equal(0, result.nMatched);
116116
test.equal(1, result.nUpserted);
117117
test.equal(0, result.nRemoved);
118118
test.equal(0, result.nModified);
@@ -192,7 +192,7 @@ exports['Should fail due to w:5 and wtimeout:1 with unordered batch api'] = func
192192
// Execute the operations
193193
batch.execute({w:5, wtimeout:1}, function(err, result) {
194194
test.equal(2, result.nInserted);
195-
test.equal(0, result.nUpdated);
195+
test.equal(0, result.nMatched);
196196
test.equal(1, result.nUpserted);
197197
test.equal(0, result.nRemoved);
198198
test.equal(0, result.nModified);
@@ -259,8 +259,11 @@ exports['Should fail due to w:5 and wtimeout:1 combined with duplicate key error
259259

260260
// Execute the operations
261261
batch.execute({w:5, wtimeout:1}, function(err, result) {
262+
console.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
263+
console.dir(result.getRawResponse())
264+
262265
test.equal(1, result.nInserted);
263-
test.equal(0, result.nUpdated);
266+
test.equal(0, result.nMatched);
264267
test.equal(1, result.nUpserted);
265268
test.equal(0, result.nRemoved);
266269
test.equal(0, result.nModified);

0 commit comments

Comments
 (0)