Skip to content

Commit 5de82e9

Browse files
committed
CXX-162 Configure client at initialization time via Options class
1 parent 0c41a35 commit 5de82e9

22 files changed

+460
-348
lines changed

src/SConscript.client

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ clientSourceBasic = [
8888
'mongo/client/gridfs.cpp',
8989
'mongo/client/init.cpp',
9090
'mongo/client/insert_write_operation.cpp',
91+
'mongo/client/options.cpp',
9192
'mongo/client/sasl_client_authenticate.cpp',
9293
'mongo/client/update_write_operation.cpp',
9394
'mongo/client/wire_protocol_writer.cpp',
9495
'mongo/client/write_concern.cpp',
9596
'mongo/db/jsobj.cpp',
9697
'mongo/db/json.cpp',
9798
'mongo/db/dbmessage.cpp',
98-
'mongo/db/server_options.cpp',
9999
'mongo/logger/log_manager.cpp',
100100
'mongo/logger/log_severity.cpp',
101101
'mongo/logger/logger.cpp',
@@ -198,6 +198,7 @@ clientHeaders = [
198198
'mongo/client/export_macros.h',
199199
'mongo/client/gridfs.h',
200200
'mongo/client/init.h',
201+
'mongo/client/options.h',
201202
'mongo/client/redef_macros.h',
202203
'mongo/client/sasl_client_authenticate.h',
203204
'mongo/client/undef_macros.h',
@@ -206,7 +207,6 @@ clientHeaders = [
206207
'mongo/config.h',
207208
'mongo/db/jsobj.h',
208209
'mongo/db/json.h',
209-
'mongo/db/server_options.h',
210210
'mongo/logger/appender.h',
211211
'mongo/logger/labeled_level.h',
212212
'mongo/logger/log_domain.h',
@@ -249,7 +249,6 @@ clientHeaders = [
249249
'mongo/util/net/operation.h',
250250
'mongo/util/net/sock.h',
251251
'mongo/util/net/ssl_manager.h',
252-
'mongo/util/net/ssl_options.h',
253252
'mongo/util/time_support.h',
254253
'mongo/version.h',
255254
]

src/mongo/client/dbclient.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mongo/client/dbclientcursorshimcursorid.h"
2929
#include "mongo/client/dbclient_writer.h"
3030
#include "mongo/client/insert_write_operation.h"
31+
#include "mongo/client/options.h"
3132
#include "mongo/client/update_write_operation.h"
3233
#include "mongo/client/delete_write_operation.h"
3334
#include "mongo/client/sasl_client_authenticate.h"
@@ -37,7 +38,6 @@
3738
#include "mongo/db/namespace_string.h"
3839
#include "mongo/util/assert_util.h"
3940
#include "mongo/util/net/ssl_manager.h"
40-
#include "mongo/util/net/ssl_options.h"
4141
#include "mongo/util/password_digest.h"
4242

4343
namespace mongo {
@@ -918,11 +918,8 @@ namespace mongo {
918918
}
919919

920920
#ifdef MONGO_SSL
921-
int sslModeVal = sslGlobalParams.sslMode.load();
922-
if (sslModeVal == SSLGlobalParams::SSLMode_preferSSL ||
923-
sslModeVal == SSLGlobalParams::SSLMode_requireSSL) {
921+
if (client::Options::current().SSLEnabled())
924922
return p->secure( sslManager(), _server.host() );
925-
}
926923
#endif
927924
BSONObj info;
928925
bool worked = simpleCommand("admin", &info, "ismaster");

src/mongo/client/dbclient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
#include "mongo/client/dbclientinterface.h"
5555
#include "mongo/client/gridfs.h"
5656
#include "mongo/client/init.h"
57+
#include "mongo/client/options.h"
5758
#include "mongo/client/sasl_client_authenticate.h"
58-
#include "mongo/util/net/ssl_options.h"
5959
#include "mongo/version.h"
6060

6161
#include "mongo/client/undef_macros.h"

src/mongo/client/examples/httpClientTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ void play( std::string url ) {
4545

4646
int main( int argc, const char **argv, char **envp) {
4747

48+
mongo::client::Options options;
49+
4850
#ifdef MONGO_SSL
49-
mongo::sslGlobalParams.sslMode.store(mongo::SSLGlobalParams::SSLMode_requireSSL);
51+
options.setSSLMode(mongo::client::Options::kSSLRequired);
5052
#endif
5153

52-
mongo::Status status = mongo::client::initialize();
54+
mongo::Status status = mongo::client::initialize(options);
5355
if (!status.isOK()) {
5456
std::cout << "Failed to initialize mongodb client. " << status << std::endl;
5557
return EXIT_FAILURE;

src/mongo/client/init.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "mongo/base/initializer.h"
2323
#include "mongo/client/connpool.h"
24+
#include "mongo/client/private/options.h"
2425
#include "mongo/client/replica_set_monitor.h"
2526
#include "mongo/util/background.h"
2627

@@ -30,13 +31,16 @@ namespace client {
3031
namespace {
3132
void callShutdownAtExit() {
3233
// We can't really do anything of value if this returns a non-OK status.
33-
mongo::client::shutdown(kDefaultShutdownGracePeriodMillis);
34+
mongo::client::shutdown();
3435
}
3536
} // namespace
3637

37-
Status initialize(bool atexit) {
38+
Status initialize(const Options& options) {
3839

39-
if (atexit) {
40+
// Copy in the provided options.
41+
setOptions(options);
42+
43+
if (options.callShutdownAtExit()) {
4044
if (std::atexit(&callShutdownAtExit) != 0) {
4145
return Status(
4246
ErrorCodes::InternalError,
@@ -57,9 +61,12 @@ namespace client {
5761
return Status::OK();
5862
}
5963

60-
Status shutdown(int gracePeriodMillis) {
64+
Status shutdown() {
6165
ReplicaSetMonitor::cleanup();
62-
Status s = PeriodicTask::stopRunningPeriodicTasks(gracePeriodMillis);
66+
67+
const int gracePeriod = Options::current().autoShutdownGracePeriodMillis();
68+
Status s = PeriodicTask::stopRunningPeriodicTasks(gracePeriod);
69+
6370
shutdownNetworking();
6471
return s;
6572
}

src/mongo/client/init.h

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "mongo/base/status.h"
2121
#include "mongo/client/export_macros.h"
22+
#include "mongo/client/options.h"
2223

2324
// NOTE: These functions are only intended to be used when linking against the libmongoclient
2425
// library. The below functions are not defined in servers like mongos or mongod, which have
@@ -36,31 +37,26 @@ namespace mongo {
3637
*/
3738
namespace client {
3839

39-
const int kDefaultShutdownGracePeriodMillis = 250;
40-
4140
/**
42-
* Initializes the client driver. If the 'callShutdownAtExit' parameter is true, then
43-
* 'initialize' schedules a call to 'client::shutdown', with a grace period of
44-
* 'kDefaultShutdownGracePeriodMillis', via std::atexit. Failure to shutdown within the
45-
* grace period in the 'atexit' callback leads to a call to abort. If the
46-
* 'callShutDownAtExit' parameter is false, then it is the responsibility of the user of
47-
* the client driver to appropriately sequence a call to 'mongo::client::shutdown' and
48-
* respond to any failure to terminate within the grace period. Note that 'initialize'
49-
* invokes 'runGlobalInitializers', so it is not permitted to explicitly call
50-
* 'runGlobalInitializers' if calling 'initialize'. If a non-OK status is returned by this
51-
* function, the error should be reported and the client driver API must not be used.
41+
* Initializes the client driver, possibly with custom options. See the Options class for
42+
* details on the various fields.
43+
*
44+
* NOTE: Do not call 'initialize' before entering 'main' (i.e. from a static initializer),
45+
* as it relies on all static initialization having been completed.
46+
*
47+
* NOTE: Do not call 'initialize' more than once.
5248
*/
53-
MONGO_CLIENT_API Status MONGO_CLIENT_FUNC initialize(bool callShutdownAtExit = true);
49+
MONGO_CLIENT_API Status MONGO_CLIENT_FUNC initialize(const Options& options = Options());
5450

5551
/**
56-
* Terminates the client driver. If the driver does not terminate within the provided
57-
* grace period (which defaults to kDefaultShutdownGracePeriodMillis), an
58-
* 'ExceededTimeLimit' Status will be returned, in which case it is legal to retry
59-
* 'shutdown'. Other non-OK status values do not admit retrying the operation, and the
60-
* failure to terminate the driver should be reported, and it may be unsafe to exit the
61-
* process by any mechanism which causes normal destruction of static objects.
52+
* Terminates the client driver. If the driver does not terminate within the currently
53+
* configured grace period in the driver options, an 'ExceededTimeLimit' Status will be
54+
* returned, in which case it is legal to retry 'shutdown'. Other non-OK status values do
55+
* not admit retrying the operation. A permanent failure to terminate the driver should be
56+
* logged, and it may be unsafe to exit the process by any mechanism which causes normal
57+
* destruction of static objects.
6258
*/
63-
MONGO_CLIENT_API Status MONGO_CLIENT_FUNC shutdown(int gracePeriodMillis = kDefaultShutdownGracePeriodMillis);
59+
MONGO_CLIENT_API Status MONGO_CLIENT_FUNC shutdown();
6460

6561
} // namespace client
6662
} // namespace mongo

src/mongo/client/options.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* Copyright 2014 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "mongo/platform/basic.h"
17+
18+
#include "mongo/client/private/options.h"
19+
#include "mongo/client/options.h"
20+
21+
namespace mongo {
22+
namespace client {
23+
24+
namespace {
25+
// The unique global options instance.
26+
Options options;
27+
} // namespace
28+
29+
#if !defined(_MSC_EXTENSIONS)
30+
const int Options::kDefaultDefaultLocalThresholdMillis;
31+
const int Options::kDefaultAutoShutdownGracePeriodMillis;
32+
#endif
33+
34+
void setOptions(const Options& newOptions) {
35+
options = newOptions;
36+
}
37+
38+
const Options& Options::current() {
39+
return options;
40+
}
41+
42+
Options::Options()
43+
: _callShutdownAtExit(true)
44+
, _autoShutdownGracePeriodMillis(kDefaultAutoShutdownGracePeriodMillis)
45+
, _sslMode(kSSLDisabled)
46+
, _useFIPSMode(false)
47+
, _sslAllowInvalidCertificates(false)
48+
, _defaultLocalThresholdMillis(kDefaultDefaultLocalThresholdMillis)
49+
, _validateObjects(false)
50+
{}
51+
52+
Options& Options::setCallShutdownAtExit(bool value) {
53+
_callShutdownAtExit = value;
54+
return *this;
55+
}
56+
57+
bool Options::callShutdownAtExit() const {
58+
return _callShutdownAtExit;
59+
}
60+
61+
Options& Options::setAutoShutdownGracePeriodMillis(int millis) {
62+
_autoShutdownGracePeriodMillis = millis;
63+
return *this;
64+
}
65+
66+
int Options::autoShutdownGracePeriodMillis() const {
67+
return _autoShutdownGracePeriodMillis;
68+
}
69+
70+
Options& Options::setDefaultLocalThresholdMillis(int millis) {
71+
_defaultLocalThresholdMillis = millis;
72+
return *this;
73+
}
74+
75+
int Options::defaultLocalThresholdMillis() const {
76+
return _defaultLocalThresholdMillis;
77+
}
78+
79+
Options& Options::setSSLMode(SSLModes sslMode) {
80+
_sslMode = sslMode;
81+
return *this;
82+
}
83+
84+
Options::SSLModes Options::SSLMode() const {
85+
return _sslMode;
86+
}
87+
88+
Options& Options::setFIPSMode(bool value) {
89+
_useFIPSMode = value;
90+
return *this;
91+
}
92+
93+
const bool Options::FIPSMode() const {
94+
return _useFIPSMode;
95+
}
96+
97+
Options& Options::setSSLCAFile(const std::string& fileName) {
98+
_sslCAFile = fileName;
99+
return *this;
100+
}
101+
102+
const std::string& Options::SSLCAFile() const {
103+
return _sslCAFile;
104+
}
105+
106+
Options& Options::setSSLPEMKeyFile(const std::string& fileName) {
107+
_sslPEMKeyFile = fileName;
108+
return *this;
109+
}
110+
111+
const std::string& Options::SSLPEMKeyFile() const {
112+
return _sslPEMKeyFile;
113+
}
114+
115+
Options& Options::setSSLPEMKeyPassword(const std::string& password) {
116+
_sslPEMKeyPassword = password;
117+
return *this;
118+
}
119+
120+
const std::string& Options::SSLPEMKeyPassword() const {
121+
return _sslPEMKeyPassword;
122+
}
123+
124+
Options& Options::setSSLCRLFile(const std::string& fileName) {
125+
_sslCRLFile = fileName;
126+
return *this;
127+
}
128+
129+
const std::string& Options::SSLCRLFile() const {
130+
return _sslCRLFile;
131+
}
132+
133+
Options& Options::setSSLAllowInvalidCertificates(bool value) {
134+
_sslAllowInvalidCertificates = value;
135+
return *this;
136+
}
137+
138+
const bool Options::SSLAllowInvalidCertificates() const {
139+
return _sslAllowInvalidCertificates;
140+
}
141+
142+
Options& Options::setValidateObjects(bool value) {
143+
_validateObjects = value;
144+
return *this;
145+
}
146+
147+
bool Options::validateObjects() const {
148+
return _validateObjects;
149+
}
150+
151+
} // namespace client
152+
} // namespace mongo

0 commit comments

Comments
 (0)