Skip to content

PHPC-600: Use mongoc_client_get_server_descriptions() to get Manager's Servers #290

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 1 commit into from
Apr 14, 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
96 changes: 42 additions & 54 deletions src/MongoDB/Manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <mongoc.h>
#include <mongoc-client-private.h>
#include <mongoc-host-list-private.h>
#include <mongoc-server-description-private.h>

/* PHP Core stuff */
#include <php.h>
Expand Down Expand Up @@ -201,8 +200,8 @@ PHP_METHOD(Manager, getReadPreference)
PHP_METHOD(Manager, getServers)
{
php_phongo_manager_t *intern;
mongoc_set_t *set;
size_t i;
mongoc_server_description_t **sds;
size_t i, n = 0;
SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)


Expand All @@ -212,31 +211,25 @@ PHP_METHOD(Manager, getServers)
return;
}

array_init(return_value);
set = intern->client->topology->description.servers;
for(i=0; i<set->items_len; i++) {
sds = mongoc_client_get_server_descriptions(intern->client, &n);
array_init_size(return_value, n);

for (i = 0; i < n; i++) {
#if PHP_VERSION_ID >= 70000
zval obj;
#else
zval *obj = NULL;
#endif

mongoc_server_description_t *sd = (mongoc_server_description_t *) set->items[i].item;

if (sd->type == MONGOC_SERVER_UNKNOWN) {
continue;
}

#if PHP_VERSION_ID >= 70000
phongo_server_init(&obj, intern->client, ((mongoc_server_description_t *)set->items[i].item)->id TSRMLS_CC);
phongo_server_init(&obj, intern->client, mongoc_server_description_id(sds[i]) TSRMLS_CC);
add_next_index_zval(return_value, &obj);
#else
MAKE_STD_ZVAL(obj);
zval *obj = NULL;

phongo_server_init(obj, intern->client, sd->id TSRMLS_CC);
MAKE_STD_ZVAL(obj);
phongo_server_init(obj, intern->client, mongoc_server_description_id(sds[i]) TSRMLS_CC);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you really need the method call mongoc_server_description_id(sds[i]) here? I believe, sds[i]->id should be safe too? As far as I know, the server_description_t type is public. Otherwise php_phongo_server_to_zval would not work at all either.

Copy link
Member Author

Choose a reason for hiding this comment

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

mongoc-server-description.h only defines:

typedef struct _mongoc_server_description_t mongoc_server_description_t;

_mongoc_server_description_t is defined in mongoc-server-description-private.h. I noticed that Manager.c was still including mongoc-server-description-private.h, so I've removed it and amended the commit. Any headers I don't catch in these PRs will ultimately be removed in PHPC-665, which should be the final test that we're abiding the public API.

FYI: php_phongo_server_to_zval() is being migrated to the public API in #291 for PHPC-606.

add_next_index_zval(return_value, obj);
#endif
}

mongoc_server_descriptions_destroy_all(sds, n);
Copy link
Member Author

Choose a reason for hiding this comment

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

@derickr: I believe mongodb/mongo-hhvm-driver#78 was missing this. Can you confirm if you're leaking memory via the array returned from mongoc_client_get_server_descriptions()?

Copy link
Member Author

Choose a reason for hiding this comment

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

I opened HHVM-215 to track this.

Copy link
Contributor

Choose a reason for hiding this comment

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

}
/* }}} */
/* {{{ proto MongoDB\Driver\WriteConcern Manager::getWriteConcern()
Expand Down Expand Up @@ -416,62 +409,57 @@ phongo_create_object_retval php_phongo_manager_create_object(zend_class_entry *c
#endif
} /* }}} */

bool phongo_add_server_debug(void *item, void *ctx) /* {{{ */
{
mongoc_server_description_t *server = item;
zval *retval = ctx;
#if PHP_VERSION_ID >= 70000
zval entry;

php_phongo_server_to_zval(&entry, server);
add_next_index_zval(retval, &entry);
#else
zval *entry = NULL;

MAKE_STD_ZVAL(entry);
php_phongo_server_to_zval(entry, server);
add_next_index_zval(retval, entry);
#endif

return true;
} /* }}} */

HashTable *php_phongo_manager_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
php_phongo_manager_t *intern;
php_phongo_manager_t *intern;
mongoc_server_description_t **sds;
size_t i, n = 0;
#if PHP_VERSION_ID >= 70000
zval retval;
zval retval, cluster;
#else
zval retval = zval_used_for_init;
zval retval = zval_used_for_init;
zval *cluster = NULL;
#endif


*is_temp = 1;
intern = Z_MANAGER_OBJ_P(object);


array_init(&retval);
array_init_size(&retval, 2);

ADD_ASSOC_STRING(&retval, "uri", (char *)mongoc_uri_get_string(intern->client->uri));

sds = mongoc_client_get_server_descriptions(intern->client, &n);

{
#if PHP_VERSION_ID >= 70000
zval cluster;
array_init_size(&cluster, n);

for (i = 0; i < n; i++) {
zval obj;

array_init(&cluster);
mongoc_set_for_each(intern->client->topology->description.servers, phongo_add_server_debug, &cluster);
ADD_ASSOC_ZVAL_EX(&retval, "cluster", &cluster);
php_phongo_server_to_zval(&obj, sds[i]);
add_next_index_zval(&cluster, &obj);
}

ADD_ASSOC_ZVAL_EX(&retval, "cluster", &cluster);
#else
zval *cluster = NULL;
MAKE_STD_ZVAL(cluster);
array_init_size(cluster, n);

MAKE_STD_ZVAL(cluster);
array_init(cluster);
mongoc_set_for_each(intern->client->topology->description.servers, phongo_add_server_debug, cluster);
ADD_ASSOC_ZVAL_EX(&retval, "cluster", cluster);
#endif
for (i = 0; i < n; i++) {
zval *obj = NULL;

MAKE_STD_ZVAL(obj);
php_phongo_server_to_zval(obj, sds[i]);
add_next_index_zval(cluster, obj);
}

ADD_ASSOC_ZVAL_EX(&retval, "cluster", cluster);
#endif

mongoc_server_descriptions_destroy_all(sds, n);

return Z_ARRVAL(retval);
} /* }}} */
/* }}} */
Expand Down
29 changes: 1 addition & 28 deletions tests/manager/manager-var-dump-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,7 @@ object(MongoDB\Driver\Manager)#%d (%d) {
["uri"]=>
string(%d) "mongodb://%s"
["cluster"]=>
array(1) {
[0]=>
array(11) {
["host"]=>
string(%d) "%s"
["port"]=>
int(%d)
["type"]=>
int(0)
["is_primary"]=>
bool(false)
["is_secondary"]=>
bool(false)
["is_arbiter"]=>
bool(false)
["is_hidden"]=>
bool(false)
["is_passive"]=>
bool(false)
["tags"]=>
array(0) {
}
["last_is_master"]=>
array(0) {
}
["round_trip_time"]=>
int(-1)
}
array(0) {
}
}
object(MongoDB\Driver\Manager)#%d (%d) {
Expand Down