Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 7784ff4

Browse files
committed
Merged pull request #127
2 parents af98c0b + 98b9c56 commit 7784ff4

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

config.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ HHVM_EXTENSION(mongodb
5858
libmongoc/src/mongoc/mongoc-gridfs-file.c libmongoc/src/mongoc/mongoc-gridfs.c
5959
libmongoc/src/mongoc/mongoc-host-list.c
6060
libmongoc/src/mongoc/mongoc-index.c libmongoc/src/mongoc/mongoc-init.c
61-
libmongoc/src/mongoc/mongoc-list.c libmongoc/src/mongoc/mongoc-log.c
61+
libmongoc/src/mongoc/mongoc-list.c
62+
libmongoc/src/mongoc/mongoc-linux-distro-scanner.c
63+
libmongoc/src/mongoc/mongoc-log.c
6264
libmongoc/src/mongoc/mongoc-matcher-op.c libmongoc/src/mongoc/mongoc-matcher.c
6365
libmongoc/src/mongoc/mongoc-memcmp.c
6466
libmongoc/src/mongoc/mongoc-metadata.c

src/MongoDB/Driver/Manager.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ const StaticString
5151
s_MongoDBDriverManager_slaveok("slaveok"),
5252
s_MongoDBDriverManager_readpreference("readpreference"),
5353
s_MongoDBDriverManager_readpreferencetags("readpreferencetags"),
54+
s_MongoDBDriverManager_maxstalenessms("maxstalenessms"),
5455
s_MongoDBDriverManager_readPreference("readPreference"),
5556
s_MongoDBDriverManager_readPreferenceTags("readPreferenceTags"),
57+
s_MongoDBDriverManager_maxStalenessMS("maxStalenessMS"),
5658
s_MongoDBDriverManager_readconcernlevel("readconcernlevel"),
5759
s_MongoDBDriverManager_readConcernLevel("readConcernLevel"),
5860
s_MongoDBDriverManager_mode("mode"),
@@ -134,8 +136,10 @@ static bool hippo_mongo_driver_manager_apply_rp(mongoc_uri_t *uri, const Array o
134136
!options.exists(s_MongoDBDriverManager_slaveok) &&
135137
!options.exists(s_MongoDBDriverManager_readpreference) &&
136138
!options.exists(s_MongoDBDriverManager_readpreferencetags) &&
139+
!options.exists(s_MongoDBDriverManager_maxstalenessms) &&
137140
!options.exists(s_MongoDBDriverManager_readPreference) &&
138-
!options.exists(s_MongoDBDriverManager_readPreferenceTags)
141+
!options.exists(s_MongoDBDriverManager_readPreferenceTags) &&
142+
!options.exists(s_MongoDBDriverManager_maxStalenessMS)
139143
) {
140144
return true;
141145
}
@@ -184,17 +188,37 @@ static bool hippo_mongo_driver_manager_apply_rp(mongoc_uri_t *uri, const Array o
184188
mongoc_read_prefs_set_tags(new_rp, b_tags);
185189
}
186190

191+
/* Validate that readPreferenceTags are not used with PRIMARY readPreference */
187192
if (
188193
mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY &&
189194
!bson_empty(mongoc_read_prefs_get_tags(new_rp))
190195
) {
191196
throw MongoDriver::Utils::throwInvalidArgumentException("Primary read preference mode conflicts with tags");
192197
mongoc_read_prefs_destroy(new_rp);
193-
194198
return false;
195199
}
196200

197-
/* This may be redundant in light of the last check (primary with tags),
201+
/* Handle maxStalenessMS, and make sure it is not combined with PRIMARY readPreference */
202+
if (
203+
(options.exists(s_MongoDBDriverManager_maxstalenessms) && options[s_MongoDBDriverManager_maxstalenessms].isInteger())
204+
||
205+
(options.exists(s_MongoDBDriverManager_maxStalenessMS) && options[s_MongoDBDriverManager_maxStalenessMS].isInteger())
206+
) {
207+
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
208+
throw MongoDriver::Utils::throwInvalidArgumentException("Primary read preference mode conflicts with maxStalenessMS");
209+
mongoc_read_prefs_destroy(new_rp);
210+
211+
return false;
212+
}
213+
214+
if (options.exists(s_MongoDBDriverManager_maxstalenessms)) {
215+
mongoc_read_prefs_set_max_staleness_ms(new_rp, (int32_t) options[s_MongoDBDriverManager_maxstalenessms].toInt64());
216+
} else {
217+
mongoc_read_prefs_set_max_staleness_ms(new_rp, (int32_t) options[s_MongoDBDriverManager_maxStalenessMS].toInt64());
218+
}
219+
}
220+
221+
/* This may be redundant in light of the check for primary with tags,
198222
* but we'll check anyway in case additional validation is implemented. */
199223
if (!mongoc_read_prefs_is_valid(new_rp)) {
200224
throw MongoDriver::Utils::throwInvalidArgumentException("Read preference is not valid");
@@ -337,7 +361,8 @@ static mongoc_uri_t *hippo_mongo_driver_manager_make_uri(const char *dsn, const
337361
!strcasecmp(s_key, "safe") ||
338362
!strcasecmp(s_key, "slaveok") ||
339363
!strcasecmp(s_key, "w") ||
340-
!strcasecmp(s_key, "wtimeoutms")
364+
!strcasecmp(s_key, "wtimeoutms") ||
365+
!strcasecmp(s_key, "maxstalenessms")
341366
) {
342367
continue;
343368
}

tests/MongoDBDriverManager_debugInfo.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ array(2) {
3434
["is_passive"]=>
3535
bool(false)
3636
["last_is_master"]=>
37-
array(8) {
37+
array(9) {
3838
["ismaster"]=>
3939
bool(true)
4040
["maxBsonObjectSize"]=>
@@ -46,12 +46,14 @@ array(2) {
4646
["localTime"]=>
4747
object(MongoDB\BSON\UTCDateTime)#%d (1) {
4848
["milliseconds"]=>
49-
int(%d)
49+
string(%d) "%d"
5050
}
5151
["maxWireVersion"]=>
5252
int(%d)
5353
["minWireVersion"]=>
5454
int(%d)
55+
["readOnly"]=>
56+
bool(false)
5557
["ok"]=>
5658
float(1)
5759
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
MongoDB\Driver\Manager: maxStalenessMS
3+
--FILE--
4+
<?php
5+
// They both should work, and produce no output. We're not testing whether the flag actually does something
6+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxStalenessMS=1231");
7+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxStalenessMS' => 1231 ] );
8+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxstalenessms=1231");
9+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxstalenessms' => 1231 ] );
10+
?>
11+
==DONE==
12+
--EXPECTF--
13+
==DONE==
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager: maxStalenessMS
3+
--FILE--
4+
<?php
5+
try {
6+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxstalenessms=1231");
7+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
8+
echo $e->getMessage(), "\n";
9+
}
10+
11+
try {
12+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxStalenessMS=1231");
13+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
17+
try {
18+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxstalenessms' => 1231 ] );
19+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
23+
try {
24+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxStalenessMS' => 1231 ] );
25+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
?>
29+
--EXPECTF--
30+
Failed to parse MongoDB URI: 'mongodb://localhost/?maxstalenessms=1231'
31+
Failed to parse MongoDB URI: 'mongodb://localhost/?maxStalenessMS=1231'
32+
Primary read preference mode conflicts with maxStalenessMS
33+
Primary read preference mode conflicts with maxStalenessMS

0 commit comments

Comments
 (0)