summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron <aaron@10gen.com>2011-08-07 15:09:19 -0700
committerAaron <aaron@10gen.com>2011-08-07 15:09:50 -0700
commit94412807e6ac42dfe21ba62a4f39c2fdc55ed2cd (patch)
tree7d818cdf2e862e7faa2d45d9b42b80b9ab35ca0e
parent36f0f03adbffcd8a63d6f7d45ce6cd0fef51b930 (diff)
downloadmongo-94412807e6ac42dfe21ba62a4f39c2fdc55ed2cd.tar.gz
SERVER-3249 add so_timeout support in DBClientReplicaSet
-rw-r--r--client/dbclient.cpp2
-rw-r--r--client/dbclient_rs.cpp9
-rw-r--r--client/dbclient_rs.h9
-rw-r--r--client/examples/rs.cpp20
4 files changed, 28 insertions, 12 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp
index 0c9f08efedd..dadf7e4f38a 100644
--- a/client/dbclient.cpp
+++ b/client/dbclient.cpp
@@ -80,7 +80,7 @@ namespace mongo {
case PAIR:
case SET: {
- DBClientReplicaSet * set = new DBClientReplicaSet( _setName , _servers );
+ DBClientReplicaSet * set = new DBClientReplicaSet( _setName , _servers , socketTimeout );
if( ! set->connect() ) {
delete set;
errmsg = "connect failed to set ";
diff --git a/client/dbclient_rs.cpp b/client/dbclient_rs.cpp
index 82df069da03..cd184b763f5 100644
--- a/client/dbclient_rs.cpp
+++ b/client/dbclient_rs.cpp
@@ -475,8 +475,9 @@ namespace mongo {
// ----- DBClientReplicaSet ---------
// --------------------------------
- DBClientReplicaSet::DBClientReplicaSet( const string& name , const vector<HostAndPort>& servers )
- : _monitor( ReplicaSetMonitor::get( name , servers ) ) {
+ DBClientReplicaSet::DBClientReplicaSet( const string& name , const vector<HostAndPort>& servers, double so_timeout )
+ : _monitor( ReplicaSetMonitor::get( name , servers ) ),
+ _so_timeout( so_timeout ) {
}
DBClientReplicaSet::~DBClientReplicaSet() {
@@ -493,7 +494,7 @@ namespace mongo {
}
_masterHost = _monitor->getMaster();
- _master.reset( new DBClientConnection( true , this ) );
+ _master.reset( new DBClientConnection( true , this , _so_timeout ) );
string errmsg;
if ( ! _master->connect( _masterHost , errmsg ) ) {
_monitor->notifyFailure( _masterHost );
@@ -516,7 +517,7 @@ namespace mongo {
_slaveHost = h;
}
- _slave.reset( new DBClientConnection( true , this ) );
+ _slave.reset( new DBClientConnection( true , this , _so_timeout ) );
_slave->connect( _slaveHost );
_auth( _slave.get() );
return _slave.get();
diff --git a/client/dbclient_rs.h b/client/dbclient_rs.h
index cc6a97c8ab7..0bc61b7a272 100644
--- a/client/dbclient_rs.h
+++ b/client/dbclient_rs.h
@@ -176,7 +176,7 @@ namespace mongo {
public:
/** Call connect() after constructing. autoReconnect is always on for DBClientReplicaSet connections. */
- DBClientReplicaSet( const string& name , const vector<HostAndPort>& servers );
+ DBClientReplicaSet( const string& name , const vector<HostAndPort>& servers, double so_timeout=0 );
virtual ~DBClientReplicaSet();
/** Returns false if nomember of the set were reachable, or neither is
@@ -236,10 +236,7 @@ namespace mongo {
// ----- informational ----
- /**
- * timeout not supported in DBClientReplicaSet yet
- */
- double getSoTimeout() const { return 0; }
+ double getSoTimeout() const { return _so_timeout; }
string toString() { return getServerAddress(); }
@@ -274,6 +271,8 @@ namespace mongo {
HostAndPort _slaveHost;
scoped_ptr<DBClientConnection> _slave;
+
+ double _so_timeout;
/**
* for storing authentication info
diff --git a/client/examples/rs.cpp b/client/examples/rs.cpp
index 65fff8d2948..3307d87b56b 100644
--- a/client/examples/rs.cpp
+++ b/client/examples/rs.cpp
@@ -57,14 +57,19 @@ int main( int argc , const char ** argv ) {
unsigned nThreads = 1;
bool print = false;
+ bool testTimeout = false;
for ( int i=1; i<argc; i++ ) {
if ( mongoutils::str::equals( "--threads" , argv[i] ) ) {
nThreads = atoi( argv[++i] );
}
- else if ( mongoutils::str::equals( "--print" , argv[1] ) ) {
+ else if ( mongoutils::str::equals( "--print" , argv[i] ) ) {
print = true;
}
+ // Run a special mode to demonstrate the DBClientReplicaSet so_timeout option.
+ else if ( mongoutils::str::equals( "--testTimeout" , argv[i] ) ) {
+ testTimeout = true;
+ }
else {
cerr << "unknown option: " << argv[i] << endl;
return 1;
@@ -79,7 +84,7 @@ int main( int argc , const char ** argv ) {
return 1;
}
- DBClientReplicaSet * conn = (DBClientReplicaSet*)cs.connect( errmsg );
+ DBClientReplicaSet * conn = dynamic_cast<DBClientReplicaSet*>(cs.connect( errmsg, testTimeout ? 10 : 0 ));
if ( ! conn ) {
cout << "error connecting: " << errmsg << endl;
return 2;
@@ -88,6 +93,17 @@ int main( int argc , const char ** argv ) {
string collName = "test.rs1";
conn->dropCollection( collName );
+
+ if ( testTimeout ) {
+ conn->insert( collName, BSONObj() );
+ try {
+ conn->count( collName, BSON( "$where" << "sleep(40000)" ) );
+ } catch( DBException& ) {
+ return 0;
+ }
+ cout << "expected socket exception" << endl;
+ return 1;
+ }
vector<boost::shared_ptr<boost::thread> > threads;
for ( unsigned i=0; i<nThreads; i++ ) {