Permalink
Browse files

Merged in master

  • Loading branch information...
2 parents 7145c47 + 53a6af0 commit 7fb81d6060562d54c3472e7b60216d29fc854e8c @christkv committed Apr 1, 2012
View
@@ -1,5 +1,5 @@
.DS_Store
-*.swp
+*.sw*
*.seed
*.tmp
.project
View
@@ -5,6 +5,8 @@
- Corrupt GridFS files when chunkSize < fileSize, fixed concurrency issue (Issue #555)
- Handle dead tailable cursors (Issue #568, https://github.com/aheckmann)
- Connection pools handles closing themselves down and clearing the state
+- Check bson size of documents against maxBsonSize and throw client error instead of server error, (Issue #553)
+- Returning update status document at the end of the callback for updates, (Issue #569)
0.9.9.7 2012-03-16
------------------
@@ -419,13 +419,14 @@ Collection.prototype.update = function update(selector, document, options, callb
this.db._executeUpdateCommand(updateCommand, commandOptions, function (err, error) {
error = error && error.documents;
if(!callback) return;
-
+
if(err) {
callback(err);
} else if(error[0].err || error[0].errmsg) {
callback(self.db.wrap(error[0]));
} else {
- callback(null, error[0].n);
+ // Perform the callback
+ callback(null, error[0].n, error[0]);
}
});
} else {
@@ -37,7 +37,7 @@ var Connection = exports.Connection = function(id, socketOptions) {
}
// Set max bson size
-Connection.DEFAULT_MAX_BSON_SIZE = 16777216;
+Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
// Inherit event emitter so we can emit stuff wohoo
inherits(Connection, EventEmitter);
@@ -81,10 +81,7 @@ Connection.prototype.start = function() {
// Start socket
this.connection.connect(this.socketOptions.port, this.socketOptions.host);
} else {
- // // Create a new stream
- // this.connection = new net.Stream();
- // // Create new connection instance
- // this.connection = new net.Socket();
+ // Create new connection instance
this.connection = net.createConnection(this.socketOptions.port, this.socketOptions.host);
// Set options on the socket
this.connection.setTimeout(this.socketOptions.timeout);
@@ -110,8 +107,6 @@ Connection.prototype.start = function() {
this.connection.on("timeout", timeoutHandler(this));
this.connection.on("drain", drainHandler(this));
this.connection.on("close", closeHandler(this));
- // // Start socket
- // this.connection.connect(this.socketOptions.port, this.socketOptions.host);
}
}
@@ -122,19 +117,18 @@ Connection.prototype.isConnected = function() {
// Write the data out to the socket
Connection.prototype.write = function(command, callback) {
- // console.log("=====================================================")
- // console.log("this.maxBsonSize = " + this.maxBsonSize)
-
try {
// If we have a list off commands to be executed on the same socket
if(Array.isArray(command)) {
for(var i = 0; i < command.length; i++) {
var binaryCommand = command[i].toBinary()
+ if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
} else {
var binaryCommand = command.toBinary()
+ if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
@@ -391,7 +385,6 @@ var errorHandler = function(self) {
var closeHandler = function(self) {
return function(hadError) {
- // console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
// If we have an error during the connection phase
if(hadError && !self.connected) {
// Set disconnected
@@ -32,9 +32,7 @@ var ConnectionPool = exports.ConnectionPool = function(host, port, poolSize, bso
utils.setIntegerParameter(this.socketOptions, 'bufferSize', 0);
// Internal structures
- this.openConnections = [];
- // this.connections = [];
-
+ this.openConnections = [];
// Assign connection id's
this.connectionId = 0;
@@ -89,9 +87,6 @@ var _connect = function(_self) {
// Error handler
connection.on("error", function(err, connection) {
- // console.log("-------------------------------------------------------- error :: " + _self.openConnections.indexOf(this))
- // console.dir(this)
-
numberOfErrors++;
// If we are already disconnected ignore the event
if(connectionStatus != 'disconnected' && _self.listeners("error").length > 0) {
@@ -104,19 +99,10 @@ var _connect = function(_self) {
_self._poolState = 'disconnected';
// Stop
_self.stop();
-
- // // Clean up
- // _self.openConnections = [];
- // _self.connections = [];
});
// Close handler
connection.on("close", function() {
- // console.log("-------------------------------------------------------- close")
- // console.dir(this)
- // Only close when we have no more connections
- // if(_self.minPoolSize)
-
// If we are already disconnected ignore the event
if(connectionStatus !== 'disconnected' && _self.listeners("close").length > 0) {
_self.emit("close");
@@ -128,15 +114,10 @@ var _connect = function(_self) {
_self._poolState = 'disconnected';
// Stop
_self.stop();
- // // Clean up
- // _self.openConnections = [];
- // _self.connections = [];
});
// Timeout handler
connection.on("timeout", function(err, connection) {
- // console.log("-------------------------------------------------------- timeout")
- // console.dir(this)
// If we are already disconnected ignore the event
if(connectionStatus !== 'disconnected' && _self.listeners("timeout").length > 0) {
_self.emit("timeout", err);
@@ -148,18 +129,12 @@ var _connect = function(_self) {
_self._poolState = 'disconnected';
// Stop
_self.stop();
- // // Clean up
- // _self.openConnections = [];
- // _self.connections = [];
});
// Parse error, needs a complete shutdown of the pool
connection.on("parseError", function() {
- // console.log("-------------------------------------------------------- parseError")
- // console.dir(this)
// If we are already disconnected ignore the event
if(connectionStatus !== 'disconnected' && _self.listeners("parseError").length > 0) {
- // if(connectionStatus == 'connected') {
_self.emit("parseError", new Error("parseError occured"));
}
@@ -221,9 +196,7 @@ ConnectionPool.prototype.stop = function(removeListeners) {
}
// Clean up
- // this.connectionsWithErrors = [];
this.openConnections = [];
- // this.connections = [];
}
// Check the status of the connection
@@ -239,7 +212,6 @@ ConnectionPool.prototype.checkoutConnection = function(id) {
}
ConnectionPool.prototype.getAllConnections = function() {
- // return this.connections;
return this.openConnections;
}
@@ -21,18 +21,20 @@ const STATE_DOWN = 8;
const STATE_ROLLBACK = 9;
/**
-* ReplSetServers constructor provides master-slave functionality
+* ReplSet constructor provides master-slave functionality
*
* @param serverArr{Array of type Server}
* @return constructor of ServerCluster
*
*/
-var ReplSetServers = exports.ReplSetServers = function(servers, options) {
- // Set up event emitter
- EventEmitter.call(this);
+var ReplSet = exports.ReplSet = function(servers, options) {
// Set up basic
- if(!(this instanceof ReplSetServers)) return new ReplSetServers(server, options);
-
+ if(!(this instanceof ReplSet))
+ return new ReplSet(servers, options);
+
+ // Set up event emitter
+ EventEmitter.call(this);
+
var self = this;
// Contains the master server entry
this.options = options == null ? {} : options;
@@ -230,10 +232,10 @@ var ReplSetServers = exports.ReplSetServers = function(servers, options) {
this._replicasetTimeoutId = null;
};
-inherits(ReplSetServers, EventEmitter);
+inherits(ReplSet, EventEmitter);
// Allow setting the read preference at the replicaset level
-ReplSetServers.prototype.setReadPreference = function(preference) {
+ReplSet.prototype.setReadPreference = function(preference) {
// Set read preference
this._readPreference = preference;
// Ensure slaveOk is correct for secodnaries read preference and tags
@@ -244,15 +246,15 @@ ReplSetServers.prototype.setReadPreference = function(preference) {
}
// Return the used state
-ReplSetServers.prototype._isUsed = function() {
+ReplSet.prototype._isUsed = function() {
return this._used;
}
-ReplSetServers.prototype.setTarget = function(target) {
+ReplSet.prototype.setTarget = function(target) {
this.target = target;
};
-ReplSetServers.prototype.isConnected = function() {
+ReplSet.prototype.isConnected = function() {
// Return the state of the replicaset server
return this.primary != null && this._state.master != null && this._state.master.isConnected();
}
@@ -261,14 +263,14 @@ Server.prototype.isSetMember = function() {
return false;
}
-ReplSetServers.prototype.isPrimary = function(config) {
+ReplSet.prototype.isPrimary = function(config) {
return this.readSecondary && this.secondaries.length > 0 ? false : true;
}
-ReplSetServers.prototype.isReadPrimary = ReplSetServers.prototype.isPrimary;
+ReplSet.prototype.isReadPrimary = ReplSet.prototype.isPrimary;
// Clean up dead connections
-var cleanupConnections = ReplSetServers.cleanupConnections = function(connections, addresses, byTags) {
+var cleanupConnections = ReplSet.cleanupConnections = function(connections, addresses, byTags) {
// Ensure we don't have entries in our set with dead connections
var keys = Object.keys(connections);
for(var i = 0; i < keys.length; i++) {
@@ -286,7 +288,7 @@ var cleanupConnections = ReplSetServers.cleanupConnections = function(connection
}
}
-var cleanupTags = ReplSetServers._cleanupTags = function(server, byTags) {
+var cleanupTags = ReplSet._cleanupTags = function(server, byTags) {
var serverTagKeys = Object.keys(server.tags);
// Iterate over all server tags and remove any instances for that tag that matches the current
// server
@@ -302,8 +304,8 @@ var cleanupTags = ReplSetServers._cleanupTags = function(server, byTags) {
var cleanInstances = [];
// We got instances for the particular tag set
var instances = byTags[serverTagKeys[i]][value];
- for(var j = 0; j < instances.length; j++) {
- var serverInstance = instances[j];
+ for(var j = 0, jlen = instances.length; j < jlen; j++) {
+ var serverInstance = instances[j];
// If we did not find an instance add it to the clean instances
if((serverInstance.host + ":" + serverInstance.port) !== (server.host + ":" + server.port)) {
cleanInstances.push(serverInstance);
@@ -316,7 +318,7 @@ var cleanupTags = ReplSetServers._cleanupTags = function(server, byTags) {
}
}
-ReplSetServers.prototype.allServerInstances = function() {
+ReplSet.prototype.allServerInstances = function() {
var self = this;
// Close all the servers (concatenate entire list of servers first for ease)
var allServers = self._state.master != null ? [self._state.master] : [];
@@ -358,11 +360,7 @@ var __executeAllCallbacksWithError = function(dbInstance, error) {
}
}
-ReplSetServers.prototype.connect = function(parent, options, callback) {
- // console.log("=============================== replicaset connect")
- // console.dir(options)
- // console.dir(callback)
-
+ReplSet.prototype.connect = function(parent, options, callback) {
var self = this;
var dateStamp = new Date().getTime();
if('function' === typeof options) callback = options, options = {};
@@ -1051,14 +1049,14 @@ ReplSetServers.prototype.connect = function(parent, options, callback) {
};
}
-ReplSetServers.prototype.checkoutWriter = function() {
+ReplSet.prototype.checkoutWriter = function() {
// Establish connection
var connection = this._state.master != null ? this._state.master.checkoutWriter() : null;
// Return the connection
return connection;
}
-ReplSetServers.prototype.checkoutReader = function() {
+ReplSet.prototype.checkoutReader = function() {
var connection = null;
// If we have specified to read from a secondary server grab a random one and read
// from it, otherwise just pass the primary connection
@@ -1111,7 +1109,7 @@ ReplSetServers.prototype.checkoutReader = function() {
return connection;
}
-ReplSetServers.prototype.allRawConnections = function() {
+ReplSet.prototype.allRawConnections = function() {
// Neeed to build a complete list of all raw connections, start with master server
var allConnections = [];
// Get connection object
@@ -1136,7 +1134,7 @@ ReplSetServers.prototype.allRawConnections = function() {
return allConnections;
}
-ReplSetServers.prototype.enableRecordQueryStats = function(enable) {
+ReplSet.prototype.enableRecordQueryStats = function(enable) {
// Set the global enable record query stats
this.recordQueryStats = enable;
// Ensure all existing servers already have the flag set, even if the
@@ -1154,11 +1152,11 @@ ReplSetServers.prototype.enableRecordQueryStats = function(enable) {
}
}
-ReplSetServers.prototype.disconnect = function(callback) {
+ReplSet.prototype.disconnect = function(callback) {
this.close(callback);
}
-ReplSetServers.prototype.close = function(callback) {
+ReplSet.prototype.close = function(callback) {
var self = this;
// Set server status as disconnected
this._serverState = 'disconnected';
@@ -1207,4 +1205,7 @@ ReplSetServers.prototype.close = function(callback) {
}
}
}
-}
+}
+
+// Backward compatibility
+exports.ReplSetServers = ReplSet;
Oops, something went wrong.

0 comments on commit 7fb81d6

Please sign in to comment.