Skip to content

PHPC-752: Add maxStalenessMS to ReadPreference class #400

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

Merged
merged 7 commits into from
Sep 19, 2016
Merged
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
20 changes: 18 additions & 2 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t
const bson_t *tags = mongoc_read_prefs_get_tags(read_prefs);
mongoc_read_mode_t mode = mongoc_read_prefs_get_mode(read_prefs);

array_init_size(retval, 2);
array_init_size(retval, 3);

switch (mode) {
case MONGOC_READ_PRIMARY: ADD_ASSOC_STRING(retval, "mode", "primary"); break;
Expand All @@ -987,6 +987,10 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t
ADD_ASSOC_ZVAL_EX(retval, "tags", state.zchild);
#endif
}

if (mongoc_read_prefs_get_max_staleness_ms(read_prefs) != 0) {
ADD_ASSOC_LONG_EX(retval, "maxStalenessMS", mongoc_read_prefs_get_max_staleness_ms(read_prefs));
}
} /* }}} */

void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t *write_concern) /* {{{ */
Expand Down Expand Up @@ -1197,14 +1201,26 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
/* 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)) {
int32_t max_staleness_ms = bson_iter_int32(&iter);

if (max_staleness_ms < 0) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be >= 0, %" PRId32 " given", max_staleness_ms);
mongoc_read_prefs_destroy(new_rp);

return false;
}

/* max_staleness_ms is fetched as an INT32, so there is no need to check
* if it exists INT32_MAX as we do in the ReadPreference constructor. */

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));
mongoc_read_prefs_set_max_staleness_ms(new_rp, max_staleness_ms);
}

/* This may be redundant in light of the last check (primary with tags), but
Expand Down
80 changes: 60 additions & 20 deletions src/MongoDB/ReadPreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <ext/standard/info.h>
#include <Zend/zend_interfaces.h>
#include <ext/spl/spl_iterators.h>
/* PHP array helpers */
#include "php_array_api.h"
/* Our Compatability header */
#include "phongo_compat.h"

Expand All @@ -46,21 +48,22 @@ PHONGO_API zend_class_entry *php_phongo_readpreference_ce;

zend_object_handlers php_phongo_handler_readpreference;

/* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()])
/* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()[, array $options = array()]])
Constructs a new ReadPreference */
PHP_METHOD(ReadPreference, __construct)
{
php_phongo_readpreference_t *intern;
zend_error_handling error_handling;
phongo_long mode;
zval *tagSets = NULL;
zval *options = NULL;
SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_used)


zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
intern = Z_READPREFERENCE_OBJ_P(getThis());

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &mode, &tagSets) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!a!", &mode, &tagSets, &options) == FAILURE) {
zend_restore_error_handling(&error_handling TSRMLS_CC);
return;
}
Expand All @@ -80,28 +83,46 @@ PHP_METHOD(ReadPreference, __construct)
return;
}

switch(ZEND_NUM_ARGS()) {
case 2:
if (tagSets) {
bson_t *tags = bson_new();
if (tagSets) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I removed the switch statement and am processing tagSets first, I added a test that tag set exceptions are thrown before maxStalenessMS.

bson_t *tags = bson_new();

phongo_zval_to_bson(tagSets, PHONGO_BSON_NONE, (bson_t *)tags, NULL TSRMLS_CC);
phongo_zval_to_bson(tagSets, PHONGO_BSON_NONE, (bson_t *)tags, NULL TSRMLS_CC);

if (!php_phongo_read_preference_tags_are_valid(tags)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "tagSets must be an array of zero or more documents");
bson_destroy(tags);
return;
}
if (!php_phongo_read_preference_tags_are_valid(tags)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "tagSets must be an array of zero or more documents");
bson_destroy(tags);
return;
}

if (!bson_empty(tags) && mode == MONGOC_READ_PRIMARY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "tagSets may not be used with primary mode");
bson_destroy(tags);
return;
}

mongoc_read_prefs_set_tags(intern->read_preference, tags);
bson_destroy(tags);
}

if (options && php_array_exists(options, "maxStalenessMS")) {
phongo_long maxStalenessMS = php_array_fetchc_long(options, "maxStalenessMS");

if (!bson_empty(tags) && mode == MONGOC_READ_PRIMARY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "tagSets may not be used with primary mode");
bson_destroy(tags);
return;
}
if (maxStalenessMS < 0) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be >= 0, %" PHONGO_LONG_FORMAT " given", maxStalenessMS);
return;
}

if (maxStalenessMS > INT32_MAX) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be <= %" PRId32 ", %" PHONGO_LONG_FORMAT " given", INT32_MAX, maxStalenessMS);
return;
}

mongoc_read_prefs_set_tags(intern->read_preference, tags);
bson_destroy(tags);
}
if (maxStalenessMS > 0 && mode == MONGOC_READ_PRIMARY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "maxStalenessMS may not be used with primary mode");
return;
}

mongoc_read_prefs_set_max_staleness_ms(intern->read_preference, maxStalenessMS);
}

if (!mongoc_read_prefs_is_valid(intern->read_preference)) {
Expand All @@ -111,6 +132,23 @@ PHP_METHOD(ReadPreference, __construct)
}
/* }}} */

/* {{{ proto integer ReadPreference::getMaxStalenessMS()
Returns the ReadPreference maxStalenessMS value */
PHP_METHOD(ReadPreference, getMaxStalenessMS)
{
php_phongo_readpreference_t *intern;
SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)

intern = Z_READPREFERENCE_OBJ_P(getThis());

if (zend_parse_parameters_none() == FAILURE) {
return;
}

RETURN_LONG(mongoc_read_prefs_get_max_staleness_ms(intern->read_preference));
}
/* }}} */

/* {{{ proto integer ReadPreference::getMode()
Returns the ReadPreference mode */
PHP_METHOD(ReadPreference, getMode)
Expand Down Expand Up @@ -184,13 +222,15 @@ PHP_METHOD(ReadPreference, bsonSerialize)
ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___construct, 0, 0, 1)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_ARRAY_INFO(0, tagSets, 1)
ZEND_ARG_ARRAY_INFO(0, options, 1)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_void, 0, 0, 0)
ZEND_END_ARG_INFO()

static zend_function_entry php_phongo_readpreference_me[] = {
PHP_ME(ReadPreference, __construct, ai_ReadPreference___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, getMaxStalenessMS, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, getMode, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, bsonSerialize, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
Expand Down
3 changes: 0 additions & 3 deletions tests/manager/manager-ctor-001.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
--TEST--
MongoDB\Driver\Manager::__construct() with default URI
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager();

Expand Down
3 changes: 0 additions & 3 deletions tests/manager/manager-ctor-003.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
--TEST--
MongoDB\Driver\Manager::__construct() URI defaults to "mongodb://127.0.0.1/"
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

ini_set('mongodb.debug', 'stderr');
$manager = new MongoDB\Driver\Manager();
Expand Down
76 changes: 76 additions & 0 deletions tests/manager/manager-ctor-read_preference-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
MongoDB\Driver\Manager::__construct(): read preference options
--FILE--
<?php

$tests = [
['mongodb://127.0.0.1/?readPreference=primary', []],
['mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=tag:one&readPreferenceTags=', []],
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', []],
[null, ['readPreference' => 'primary']],
[null, ['readPreference' => 'secondary', 'readPreferenceTags' => [['tag' => 'one'], []]]],
[null, ['readPreference' => 'secondary', 'maxStalenessMS' => 1000]],
];

foreach ($tests as $test) {
list($uri, $options) = $test;

$manager = new MongoDB\Driver\Manager($uri, $options);
var_dump($manager->getReadPreference());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(7) "primary"
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["tags"]=>
array(2) {
[0]=>
array(1) {
["tag"]=>
string(3) "one"
}
[1]=>
array(0) {
}
}
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1000)
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(7) "primary"
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["tags"]=>
array(2) {
[0]=>
array(1) {
["tag"]=>
string(3) "one"
}
[1]=>
array(0) {
}
}
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1000)
}
===DONE===
54 changes: 54 additions & 0 deletions tests/manager/manager-ctor-read_preference-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
MongoDB\Driver\Manager::__construct(): read preference options (maxStalenessMS)
--FILE--
<?php

$tests = [
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1231', []],
['mongodb://127.0.0.1/?readPreference=secondary', ['maxStalenessMS' => 1231]],
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', ['maxStalenessMS' => 2000]],
['mongodb://127.0.0.1/?readpreference=secondary&maxstalenessms=1231', []],
['mongodb://127.0.0.1/?readpreference=secondary', ['maxstalenessms' => 1231]],
];

foreach ($tests as $test) {
list($uri, $options) = $test;

$manager = new MongoDB\Driver\Manager($uri, $options);
var_dump($manager->getReadPreference());
}

?>
===DONE===
--EXPECTF--
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1231)
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1231)
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(2000)
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1231)
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1231)
}
===DONE===
44 changes: 44 additions & 0 deletions tests/manager/manager-ctor-read_preference-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
MongoDB\Driver\Manager::__construct(): read preference options of the wrong type are ignored
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to change this behavior in the future, but this is how processing of Manager options operates today. We can revise this test if we decide to change this in the future.

I'll note that mongoc-uri.c is also quite lax about type errors. For instance, options that are expected to be integers are run through strtol() with no error handling.

--FILE--
<?php

$tests = [
['mongodb://127.0.0.1/?readPreference=secondary', ['readPreference' => 1]],
['mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=tag:one', ['readPreferenceTags' => 'invalid']],
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', ['maxStalenessMS' => 'invalid']],
];

foreach ($tests as $test) {
list($uri, $options) = $test;

$manager = new MongoDB\Driver\Manager($uri, $options);
var_dump($manager->getReadPreference());
}

?>
===DONE===
--EXPECTF--
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["tags"]=>
array(1) {
[0]=>
array(1) {
["tag"]=>
string(3) "one"
}
}
}
object(MongoDB\Driver\ReadPreference)#%d (%d) {
["mode"]=>
string(9) "secondary"
["maxStalenessMS"]=>
int(1000)
}
===DONE===
Loading