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

Commit beae7bf

Browse files
committed
HHVM-239: Allow users to set a limit on acceptable staleness
1 parent 9b5ad1f commit beae7bf

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/MongoDB/Driver/Manager.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const StaticString
5353
s_MongoDBDriverManager_readpreferencetags("readpreferencetags"),
5454
s_MongoDBDriverManager_readPreference("readPreference"),
5555
s_MongoDBDriverManager_readPreferenceTags("readPreferenceTags"),
56+
s_MongoDBDriverManager_maxStalenessMS("maxStalenessMS"),
5657
s_MongoDBDriverManager_readconcernlevel("readconcernlevel"),
5758
s_MongoDBDriverManager_readConcernLevel("readConcernLevel"),
5859
s_MongoDBDriverManager_mode("mode"),
@@ -135,7 +136,8 @@ static bool hippo_mongo_driver_manager_apply_rp(mongoc_uri_t *uri, const Array o
135136
!options.exists(s_MongoDBDriverManager_readpreference) &&
136137
!options.exists(s_MongoDBDriverManager_readpreferencetags) &&
137138
!options.exists(s_MongoDBDriverManager_readPreference) &&
138-
!options.exists(s_MongoDBDriverManager_readPreferenceTags)
139+
!options.exists(s_MongoDBDriverManager_readPreferenceTags) &&
140+
!options.exists(s_MongoDBDriverManager_maxStalenessMS)
139141
) {
140142
return true;
141143
}
@@ -184,17 +186,29 @@ static bool hippo_mongo_driver_manager_apply_rp(mongoc_uri_t *uri, const Array o
184186
mongoc_read_prefs_set_tags(new_rp, b_tags);
185187
}
186188

189+
/* Validate that readPreferenceTags are not used with PRIMARY readPreference */
187190
if (
188191
mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY &&
189192
!bson_empty(mongoc_read_prefs_get_tags(new_rp))
190193
) {
191194
throw MongoDriver::Utils::throwInvalidArgumentException("Primary read preference mode conflicts with tags");
192195
mongoc_read_prefs_destroy(new_rp);
193-
194196
return false;
195197
}
196198

197-
/* This may be redundant in light of the last check (primary with tags),
199+
/* Handle maxStalenessMS, and make sure it is not combined with PRIMARY readPreference */
200+
if (options.exists(s_MongoDBDriverManager_maxStalenessMS) && options[s_MongoDBDriverManager_maxStalenessMS].isInteger()) {
201+
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
202+
throw MongoDriver::Utils::throwInvalidArgumentException("Primary read preference mode conflicts with maxStalenessMS");
203+
mongoc_read_prefs_destroy(new_rp);
204+
205+
return false;
206+
}
207+
208+
mongoc_read_prefs_set_max_staleness_ms(new_rp, (int32_t) options[s_MongoDBDriverManager_maxStalenessMS].toInt64());
209+
}
210+
211+
/* This may be redundant in light of the check for primary with tags,
198212
* but we'll check anyway in case additional validation is implemented. */
199213
if (!mongoc_read_prefs_is_valid(new_rp)) {
200214
throw MongoDriver::Utils::throwInvalidArgumentException("Read preference is not valid");
@@ -337,7 +351,8 @@ static mongoc_uri_t *hippo_mongo_driver_manager_make_uri(const char *dsn, const
337351
!strcasecmp(s_key, "safe") ||
338352
!strcasecmp(s_key, "slaveok") ||
339353
!strcasecmp(s_key, "w") ||
340-
!strcasecmp(s_key, "wtimeoutms")
354+
!strcasecmp(s_key, "wtimeoutms") ||
355+
!strcasecmp(s_key, "maxstalenessms")
341356
) {
342357
continue;
343358
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
?>
9+
==DONE==
10+
--EXPECTF--
11+
==DONE==
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
--EXPECTF--
18+
Failed to parse MongoDB URI: 'mongodb://localhost/?maxStalenessMS=1231'
19+
Primary read preference mode conflicts with maxStalenessMS

0 commit comments

Comments
 (0)