Skip to content

Commit afcb021

Browse files
committed
CXX-151 Backport server rc2..rc3 changes
1 parent 9755529 commit afcb021

15 files changed

+317
-141
lines changed

src/mongo/client/connpool.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,23 @@ namespace mongo {
4141
}
4242
}
4343

44-
void PoolForHost::done( DBConnectionPool * pool, DBClientBase * c ) {
45-
if (c->isFailed()) {
46-
reportBadConnectionAt(c->getSockCreationMicroSec());
47-
pool->onDestroy(c);
48-
delete c;
49-
}
50-
else if (_pool.size() >= _maxPerHost ||
51-
c->getSockCreationMicroSec() < _minValidCreationTimeMicroSec) {
44+
void PoolForHost::done(DBConnectionPool* pool, DBClientBase* c) {
45+
46+
bool isFailed = c->isFailed();
47+
48+
// Remember that this host had a broken connection for later
49+
if (isFailed) reportBadConnectionAt(c->getSockCreationMicroSec());
50+
51+
if (isFailed ||
52+
// Another (later) connection was reported as broken to this host
53+
(c->getSockCreationMicroSec() < _minValidCreationTimeMicroSec) ||
54+
// We have a pool size that we need to enforce
55+
(_maxPoolSize >= 0 && static_cast<int>(_pool.size()) >= _maxPoolSize)) {
5256
pool->onDestroy(c);
5357
delete c;
5458
}
5559
else {
60+
// The connection is probably fine, save for later
5661
_pool.push(c);
5762
}
5863
}
@@ -166,23 +171,25 @@ namespace mongo {
166171
}
167172
}
168173

169-
unsigned PoolForHost::_maxPerHost = 50;
170-
171174
// ------ DBConnectionPool ------
172175

173176
DBConnectionPool pool;
174177

175-
DBConnectionPool::DBConnectionPool()
178+
const int PoolForHost::kPoolSizeUnlimited(-1);
179+
180+
DBConnectionPool::DBConnectionPool()
176181
: _mutex("DBConnectionPool") ,
177182
_name( "dbconnectionpool" ) ,
178-
_hooks( new list<DBConnectionHook*>() ) {
183+
_maxPoolSize(PoolForHost::kPoolSizeUnlimited) ,
184+
_hooks( new list<DBConnectionHook*>() ) {
179185
}
180186

181187
DBClientBase* DBConnectionPool::_get(const string& ident , double socketTimeout ) {
182188
uassert(17382, "Can't use connection pool during shutdown",
183189
!inShutdown());
184190
scoped_lock L(_mutex);
185191
PoolForHost& p = _pools[PoolKey(ident,socketTimeout)];
192+
p.setMaxPoolSize(_maxPoolSize);
186193
p.initializeHostName(ident);
187194
return p.get( this , socketTimeout );
188195
}
@@ -191,6 +198,7 @@ namespace mongo {
191198
{
192199
scoped_lock L(_mutex);
193200
PoolForHost& p = _pools[PoolKey(host,socketTimeout)];
201+
p.setMaxPoolSize(_maxPoolSize);
194202
p.initializeHostName(host);
195203
p.createdOne( conn );
196204
}

src/mongo/client/connpool.h

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,38 @@ namespace mongo {
3535
*/
3636
class MONGO_CLIENT_API PoolForHost {
3737
public:
38-
PoolForHost()
39-
: _created(0), _minValidCreationTimeMicroSec(0) {}
4038

41-
PoolForHost( const PoolForHost& other ) {
39+
// Sentinel value indicating pool has no cleanup limit
40+
static const int kPoolSizeUnlimited;
41+
42+
PoolForHost() :
43+
_created(0),
44+
_minValidCreationTimeMicroSec(0),
45+
_type(ConnectionString::INVALID),
46+
_maxPoolSize(kPoolSizeUnlimited) {
47+
}
48+
49+
PoolForHost(const PoolForHost& other) :
50+
_created(other._created),
51+
_minValidCreationTimeMicroSec(other._minValidCreationTimeMicroSec),
52+
_type(other._type),
53+
_maxPoolSize(other._maxPoolSize) {
54+
verify(_created == 0);
4255
verify(other._pool.size() == 0);
43-
_created = other._created;
44-
_minValidCreationTimeMicroSec = other._minValidCreationTimeMicroSec;
45-
verify( _created == 0 );
4656
}
4757

4858
~PoolForHost();
4959

60+
/**
61+
* Returns the maximum number of connections stored in the pool
62+
*/
63+
int getMaxPoolSize() { return _maxPoolSize; }
64+
65+
/**
66+
* Sets the maximum number of connections stored in the pool
67+
*/
68+
void setMaxPoolSize( int maxPoolSize ) { _maxPoolSize = maxPoolSize; }
69+
5070
int numAvailable() const { return (int)_pool.size(); }
5171

5272
void createdOne( DBClientBase * base );
@@ -85,8 +105,6 @@ namespace mongo {
85105
*/
86106
void initializeHostName(const std::string& hostName);
87107

88-
static void setMaxPerHost( unsigned max ) { _maxPerHost = max; }
89-
static unsigned getMaxPerHost() { return _maxPerHost; }
90108
private:
91109

92110
struct StoredConnection {
@@ -105,7 +123,8 @@ namespace mongo {
105123
uint64_t _minValidCreationTimeMicroSec;
106124
ConnectionString::ConnectionType _type;
107125

108-
static unsigned _maxPerHost;
126+
// The maximum number of connections we'll save in the pool
127+
int _maxPoolSize;
109128
};
110129

111130
class DBConnectionHook {
@@ -141,6 +160,22 @@ namespace mongo {
141160
/** right now just controls some asserts. defaults to "dbconnectionpool" */
142161
void setName( const string& name ) { _name = name; }
143162

163+
/**
164+
* Returns the maximum number of connections pooled per-host
165+
*
166+
* This setting only applies to new host connection pools, previously-pooled host pools are
167+
* unaffected.
168+
*/
169+
int getMaxPoolSize() { return _maxPoolSize; }
170+
171+
/**
172+
* Sets the maximum number of connections pooled per-host.
173+
*
174+
* This setting only applies to new host connection pools, previously-pooled host pools are
175+
* unaffected.
176+
*/
177+
void setMaxPoolSize( int maxPoolSize ) { _maxPoolSize = maxPoolSize; }
178+
144179
void onCreate( DBClientBase * conn );
145180
void onHandedOut( DBClientBase * conn );
146181
void onDestroy( DBClientBase * conn );
@@ -204,6 +239,11 @@ namespace mongo {
204239
mongo::mutex _mutex;
205240
string _name;
206241

242+
// The maximum number of connections we'll save in the pool per-host
243+
// PoolForHost::kPoolSizeUnlimited is a sentinel value meaning "no limit"
244+
// 0 effectively disables the pool
245+
int _maxPoolSize;
246+
207247
PoolMap _pools;
208248

209249
// pointers owned by me, right now they leak on shutdown

src/mongo/client/gridfs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
namespace mongo {
4040

41-
const unsigned DEFAULT_CHUNK_SIZE = 256 * 1024;
41+
const unsigned DEFAULT_CHUNK_SIZE = 255 * 1024;
4242

4343
GridFSChunk::GridFSChunk( BSONObj o ) {
4444
_data = o;

src/mongo/client/init.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdlib>
2121

2222
#include "mongo/base/initializer.h"
23+
#include "mongo/client/connpool.h"
2324
#include "mongo/client/replica_set_monitor.h"
2425
#include "mongo/util/background.h"
2526

@@ -47,6 +48,10 @@ namespace client {
4748
if (!result.isOK())
4849
return result;
4950

51+
// Setup default pool parameters
52+
mongo::pool.setName("connection pool");
53+
mongo::pool.setMaxPoolSize(50);
54+
5055
PeriodicTask::startRunningPeriodicTasks();
5156

5257
return Status::OK();

src/mongo/client/sasl_client_session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace {
4444

4545
void* saslOurCalloc(SaslAllocSize count, SaslAllocSize size) {
4646
void* ptr = calloc(count, size);
47-
if (!ptr) printStackAndExit(0);
47+
if (!ptr) abort();
4848
return ptr;
4949
}
5050

src/mongo/client/scoped_db_conn_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ namespace mongo {
250250
DummyServerFixture() {
251251
_server = new TCPServer(TARGET_PORT);
252252
_thread = new boost::thread(boost::ref(*_server));
253-
_maxPoolSizePerHost = mongo::PoolForHost::getMaxPerHost();
253+
_maxPoolSizePerHost = mongo::pool.getMaxPoolSize();
254254
}
255255

256256
~DummyServerFixture() {
257-
mongo::PoolForHost::setMaxPerHost(_maxPoolSizePerHost);
257+
mongo::pool.setMaxPoolSize(_maxPoolSizePerHost);
258258
mongo::ScopedDbConnection::clearPool();
259259

260260
_server->stop();
@@ -390,7 +390,7 @@ namespace mongo {
390390
}
391391

392392
TEST_F(DummyServerFixture, InvalidateBadConnEvenWhenPoolIsFull) {
393-
mongo::PoolForHost::setMaxPerHost(2);
393+
mongo::pool.setMaxPoolSize(2);
394394

395395
ScopedDbConnection conn1(TARGET_HOST);
396396
ScopedDbConnection conn2(TARGET_HOST);

src/mongo/util/allocator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#pragma once
1919

20-
#include "mongo/util/signal_handlers.h"
20+
#include <stdlib.h>
2121

2222
// we need the "real" malloc here
2323
#include "mongo/client/undef_macros.h"
@@ -26,13 +26,13 @@ namespace mongo {
2626

2727
inline void * ourmalloc(size_t size) {
2828
void *x = malloc(size);
29-
if ( x == 0 ) printStackAndExit(0);
29+
if ( x == 0 ) abort();
3030
return x;
3131
}
3232

3333
inline void * ourrealloc(void *ptr, size_t size) {
3434
void *x = realloc(ptr, size);
35-
if ( x == 0 ) printStackAndExit(0);
35+
if ( x == 0 ) abort();
3636
return x;
3737
}
3838

src/mongo/util/log.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ using namespace std;
3838
// TODO: Extra log context appending, and re-enable log_user_*.js
3939
// TODO: Eliminate cout/cerr.
4040
// TODO: LogIndent (for mongodump).
41-
// TODO: Eliminate rawOut.
4241

4342
namespace mongo {
4443

@@ -116,41 +115,6 @@ namespace mongo {
116115
return s.str();
117116
}
118117

119-
namespace {
120-
bool rawOutToStderr = false;
121-
} // namespace
122-
123-
void setRawOutToStderr() {
124-
rawOutToStderr = true;
125-
}
126-
127-
/*
128-
* NOTE(schwerin): Called from signal handlers; should not be taking locks or allocating
129-
* memory.
130-
*/
131-
void rawOut(const StringData &s) {
132-
// Can't use STDxxx_FILENO macros since they don't exist on windows.
133-
const int fd = rawOutToStderr
134-
? 2 // STDERR_FILENO
135-
: 1 // STDOUT_FILENO
136-
;
137-
138-
const char* ptr = s.rawData();
139-
size_t bytesRemaining = s.size();
140-
while (bytesRemaining) {
141-
#ifdef _WIN32
142-
int ret = _write(fd, ptr, bytesRemaining);
143-
#else
144-
ssize_t ret = write(fd, ptr, bytesRemaining);
145-
#endif
146-
if (ret < 0)
147-
return; // Nothing to do. Can't even log since that is what is failing.
148-
149-
ptr += ret;
150-
bytesRemaining -= ret;
151-
}
152-
}
153-
154118
void logContext(const char *errmsg) {
155119
if ( errmsg ) {
156120
problem() << errmsg << endl;

src/mongo/util/log.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,6 @@ namespace logger {
127127
extern Tee* const startupWarningsLog; // Things put here get reported in MMS
128128

129129
string errnoWithDescription(int errorcode = -1);
130-
void rawOut( const StringData &s );
131-
132-
/*
133-
* Redirects the output of "rawOut" to stderr. The default is stdout.
134-
*
135-
* NOTE: This needs to be here because the tools such as mongoexport and mongodump sometimes
136-
* send data to stdout and share this code, so they need to be able to redirect output to
137-
* stderr. Eventually rawOut should be replaced with something better and our tools should not
138-
* need to call internal server shutdown functions.
139-
*
140-
* NOTE: This function is not thread safe and should not be called from a multithreaded context.
141-
*/
142-
void setRawOutToStderr();
143130

144131
/**
145132
* Write the current context (backtrace), along with the optional "msg".

src/mongo/util/net/sock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ namespace mongo {
874874
<< " remote host " << remoteString() << ")" << endl;
875875
DEV {
876876
std::string hex = hexdump(testBuf, recvd);
877-
error() << "Hex Dump of stale log data: " << hex << endl;
877+
error() << "Hex dump of stale log data: " << hex << endl;
878878
}
879879
dassert( false );
880880
}

src/mongo/util/net/ssl_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ namespace mongo {
589589

590590
X509* x509 = PEM_read_bio_X509(in, NULL, &SSLManager::password_cb, this);
591591
if (NULL == x509) {
592-
error() << "cannot retreive certificate from keyfile: " << keyFile << ' ' <<
592+
error() << "cannot retrieve certificate from keyfile: " << keyFile << ' ' <<
593593
getSSLErrorMessage(ERR_get_error()) << endl;
594594
return false;
595595
}

src/mongo/util/net/ssl_options.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,11 @@ namespace mongo {
7676

7777
Status storeSSLServerOptions(const moe::Environment& params);
7878

79+
/**
80+
* Canonicalize SSL options for the given environment that have different representations with
81+
* the same logical meaning
82+
*/
83+
Status canonicalizeSSLServerOptions(moe::Environment* params);
84+
7985
Status storeSSLClientOptions(const moe::Environment& params);
8086
}

0 commit comments

Comments
 (0)