Skip to content

PHPC-752: Allow users to set a limit on acceptable staleness #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@ if test "$MONGODB" != "no"; then
mongoc-cursor-cursorid.c \
mongoc-cursor-transform.c \
mongoc-database.c \
mongoc-linux-distro-scanner.c \
mongoc-find-and-modify.c \
mongoc-host-list.c \
mongoc-init.c \
mongoc-gridfs.c \
mongoc-gridfs-file.c \
mongoc-gridfs-file-page.c \
mongoc-gridfs-file-list.c \
mongoc-handshake.c \
mongoc-index.c \
mongoc-list.c \
mongoc-log.c \
Expand Down Expand Up @@ -414,6 +416,8 @@ PHP_ARG_WITH(libmongoc, whether to use system libmongoc,
AC_SUBST(MONGOC_ENABLE_SSL_SECURE_CHANNEL, 0)
AC_SUBST(MONGOC_ENABLE_CRYPTO_CNG, 0)

AC_SUBST(MONGOC_ENABLE_SSL_LIBRESSL, 0)

AC_SUBST(MONGOC_NO_AUTOMATIC_GLOBALS, 1)
AC_SUBST(MONGOC_EXPERIMENTAL_FEATURES, 0)
fi
Expand Down
35 changes: 28 additions & 7 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,9 @@ static mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options
!strcasecmp(key, "safe") ||
!strcasecmp(key, "slaveok") ||
!strcasecmp(key, "w") ||
!strcasecmp(key, "wtimeoutms")) {
!strcasecmp(key, "wtimeoutms") ||
!strcasecmp(key, "maxstalenessms")
) {
continue;
}

Expand Down Expand Up @@ -1090,7 +1092,9 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option

if (!bson_iter_init_find_case(&iter, options, "slaveok") &&
!bson_iter_init_find_case(&iter, options, "readpreference") &&
!bson_iter_init_find_case(&iter, options, "readpreferencetags")) {
!bson_iter_init_find_case(&iter, options, "readpreferencetags") &&
!bson_iter_init_find_case(&iter, options, "maxstalenessms")
) {
return true;
}

Expand Down Expand Up @@ -1141,6 +1145,19 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
return false;
}

/* Handle maxStalenessMS, and make sure it is not combined with primary
* readPreference */
if (bson_iter_init_find_case(&iter, options, "maxstalenessms") && BSON_ITER_HOLDS_INT32(&iter)) {
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessMS");
mongoc_read_prefs_destroy(new_rp);

return false;
}

mongoc_read_prefs_set_max_staleness_ms(new_rp, bson_iter_int32(&iter));
}

/* This may be redundant in light of the last check (primary with tags), but
* we'll check anyway in case additional validation is implemented. */
if (!mongoc_read_prefs_is_valid(new_rp)) {
Expand Down Expand Up @@ -1390,8 +1407,6 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
const char *mongoc_version, *bson_version;
mongoc_client_t *client;

ENTRY;

#if PHP_VERSION_ID >= 70000
if (driverOptions && (zdebug = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "debug", sizeof("debug")-1)) != NULL) {
zend_string *key = zend_string_init(PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI)-1, 0);
Expand Down Expand Up @@ -1432,17 +1447,17 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
client = mongoc_client_new_from_uri(uri);

if (!client) {
RETURN(NULL);
return NULL;
}

if (mongoc_uri_get_ssl(uri) && driverOptions) {
if (!php_phongo_apply_ssl_opts(client, driverOptions TSRMLS_CC)) {
mongoc_client_destroy(client);
RETURN(NULL);
return NULL;
}
}

RETURN(client);
return client;
} /* }}} */

bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string, bson_t *bson_options, zval *driverOptions TSRMLS_DC) /* {{{ */
Expand Down Expand Up @@ -1880,11 +1895,17 @@ PHP_MINIT_FUNCTION(mongodb)
{
(void)type; /* We don't care if we are loaded via dl() or extension= */

char *php_version_string = malloc(4 + sizeof(MONGODB_VERSION_S) + 1);

REGISTER_INI_ENTRIES();

/* Initialize libmongoc */
mongoc_init();

/* Set handshake options */
snprintf(php_version_string, 4 + sizeof(MONGODB_VERSION_S) + 1, "PHP %s", PHP_VERSION);
mongoc_handshake_data_append("ext-mongodb:PHP", MONGODB_VERSION_S, php_version_string);

/* Initialize libbson */
bson_mem_set_vtable(&MONGODB_G(bsonMemVTable));

Expand Down
1 change: 1 addition & 0 deletions src/MongoDB/ReadConcern.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ PHP_MINIT_FUNCTION(ReadConcern)

zend_declare_class_constant_stringl(php_phongo_readconcern_ce, ZEND_STRL("LOCAL"), ZEND_STRL(MONGOC_READ_CONCERN_LEVEL_LOCAL) TSRMLS_CC);
zend_declare_class_constant_stringl(php_phongo_readconcern_ce, ZEND_STRL("MAJORITY"), ZEND_STRL(MONGOC_READ_CONCERN_LEVEL_MAJORITY) TSRMLS_CC);
zend_declare_class_constant_stringl(php_phongo_readconcern_ce, ZEND_STRL("LINEARIZABLE"), ZEND_STRL(MONGOC_READ_CONCERN_LEVEL_LINEARIZABLE) TSRMLS_CC);

return SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libmongoc
Submodule libmongoc updated 250 files
2 changes: 0 additions & 2 deletions tests/bson/bug0544.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
PHPC-544: Consult SIZEOF_ZEND_LONG for 64-bit integer support
--SKIPIF--
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
--INI--
mongodb.debug=stderr
--FILE--
<?php

Expand Down
2 changes: 0 additions & 2 deletions tests/bson/bug0623.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
PHPC-623: Numeric keys limited to unsigned 32-bit integer
--SKIPIF--
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
--INI--
mongodb.debug=stderr
--FILE--
<?php

Expand Down
26 changes: 0 additions & 26 deletions tests/manager/manager-debug-003.phpt

This file was deleted.

13 changes: 13 additions & 0 deletions tests/manager/manager-maxstalenessms-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
MongoDB\Driver\Manager: maxStalenessMS
--FILE--
<?php
// They both should work, and produce no output. We're not testing whether the flag actually does something
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxStalenessMS=1231");
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxStalenessMS' => 1231 ] );
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxstalenessms=1231");
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxstalenessms' => 1231 ] );
?>
==DONE==
--EXPECTF--
==DONE==
33 changes: 33 additions & 0 deletions tests/manager/manager-maxstalenessms_error-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
MongoDB\Driver\Manager: maxStalenessMS
--FILE--
<?php
try {
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxstalenessms=1231");
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
echo $e->getMessage(), "\n";
}

try {
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxStalenessMS=1231");
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
echo $e->getMessage(), "\n";
}

try {
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxstalenessms' => 1231 ] );
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
echo $e->getMessage(), "\n";
}

try {
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxStalenessMS' => 1231 ] );
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
Failed to parse MongoDB URI: 'mongodb://localhost/?maxstalenessms=1231'
Failed to parse MongoDB URI: 'mongodb://localhost/?maxStalenessMS=1231'
Primary read preference mode conflicts with maxStalenessMS
Primary read preference mode conflicts with maxStalenessMS
3 changes: 2 additions & 1 deletion tests/manager/manager-var-dump-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object(MongoDB\Driver\Manager)#%d (%d) {
["is_passive"]=>
bool(false)
["last_is_master"]=>
array(8) {
array(%d) {
["ismaster"]=>
bool(true)
["maxBsonObjectSize"]=>
Expand All @@ -68,6 +68,7 @@ object(MongoDB\Driver\Manager)#%d (%d) {
int(%d)
["minWireVersion"]=>
int(0)
%a
["ok"]=>
float(1)
}
Expand Down
3 changes: 2 additions & 1 deletion tests/server/server-debug.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object(MongoDB\Driver\Server)#%d (%d) {
["is_passive"]=>
bool(false)
["last_is_master"]=>
array(8) {
array(%d) {
["ismaster"]=>
bool(true)
["maxBsonObjectSize"]=>
Expand All @@ -51,6 +51,7 @@ object(MongoDB\Driver\Server)#%d (%d) {
int(%d)
["minWireVersion"]=>
int(0)
%a
["ok"]=>
float(1)
}
Expand Down