Skip to content

Commit

Permalink
Merged pull request mongodb#466
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Mar 31, 2017
2 parents 53ec3a6 + 0883a5d commit 075e6c7
Show file tree
Hide file tree
Showing 28 changed files with 2,259 additions and 6 deletions.
19 changes: 19 additions & 0 deletions .travis.scripts/debug-script.sh
@@ -0,0 +1,19 @@
#!/bin/sh

EXP=`echo $1 | sed 's/diff/exp/'`
OUT=`echo $1 | sed 's/diff/out/'`

echo "FILE $1"

echo $EXP
echo "======"
cat $EXP
echo "======"

echo $OUT
echo "======"
cat $OUT
echo "======"

echo

2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -41,4 +41,4 @@ after_success:

after_failure:
- find . -name \*core\* -exec ./.travis.scripts/debug-core.sh {} \;

- find . -name \*diff -exec ./.travis.scripts/debug-script.sh {} \;
6 changes: 6 additions & 0 deletions config.m4
Expand Up @@ -192,6 +192,12 @@ if test "$MONGODB" != "no"; then
src/MongoDB/Exception/SSLConnectionException.c \
src/MongoDB/Exception/UnexpectedValueException.c \
src/MongoDB/Exception/WriteException.c \
src/MongoDB/Monitoring.c \
src/MongoDB/Monitoring/CommandFailedEvent.c \
src/MongoDB/Monitoring/CommandStartedEvent.c \
src/MongoDB/Monitoring/CommandSubscriber.c \
src/MongoDB/Monitoring/CommandSucceededEvent.c \
src/MongoDB/Monitoring/Subscriber.c
"

PHP_ARG_WITH(libbson, whether to use system libbson,
Expand Down
3 changes: 2 additions & 1 deletion config.w32
Expand Up @@ -85,8 +85,9 @@ if (PHP_MONGODB != "no") {
EXTENSION("mongodb", "php_phongo.c phongo_compat.c", null, PHP_MONGODB_CFLAGS);
ADD_SOURCES(configure_module_dirname + "/src", "bson.c bson-encode.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/BSON", "Binary.c Decimal128.c Javascript.c MaxKey.c MinKey.c ObjectID.c Persistable.c Regex.c Serializable.c Timestamp.c Type.c Unserializable.c UTCDateTime.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/MongoDB", "BulkWrite.c Command.c Cursor.c CursorId.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/MongoDB", "BulkWrite.c Command.c Cursor.c CursorId.c Manager.c Monitoring.c Query.c ReadConcern.c ReadPreference.c Server.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/MongoDB/Exception", "AuthenticationException.c BulkWriteException.c ConnectionException.c ConnectionTimeoutException.c Exception.c ExecutionTimeoutException.c InvalidArgumentException.c LogicException.c RuntimeException.c SSLConnectionException.c UnexpectedValueException.c WriteException.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c Subscriber.c", "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/libbson/src/bson", PHP_MONGODB_BSON_SOURCES, "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/libbson/src/jsonsl", PHP_MONGODB_JSONSL_SOURCES, "mongodb");
ADD_SOURCES(configure_module_dirname + "/src/libmongoc/src/mongoc", PHP_MONGODB_MONGOC_SOURCES, "mongodb");
Expand Down
237 changes: 234 additions & 3 deletions php_phongo.c
Expand Up @@ -67,6 +67,7 @@
/* Our stuffz */
#include "php_phongo.h"
#include "php_bson.h"
#include "src/MongoDB/Monitoring.h"

#undef MONGOC_LOG_DOMAIN
#define MONGOC_LOG_DOMAIN "PHONGO"
Expand Down Expand Up @@ -413,7 +414,7 @@ zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC) /*
return true;
} /* }}} */

php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, bson_t *reply, mongoc_client_t *client, int server_id TSRMLS_DC) /* {{{ */
static php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, bson_t *reply, mongoc_client_t *client, int server_id TSRMLS_DC) /* {{{ */
{
php_phongo_writeresult_t *writeresult;

Expand Down Expand Up @@ -1357,6 +1358,198 @@ static void php_phongo_free_ssl_opt(mongoc_ssl_opt_t *ssl_opt)
}
#endif

/* APM callbacks */
static void php_phongo_dispatch_handlers(const char *name, zval *z_event)
{
#if PHP_VERSION_ID >= 70000
zval *value;

ZEND_HASH_FOREACH_VAL(&MONGODB_G(subscribers), value) {
/* We can't use the zend_call_method_with_1_params macro here, as it
* does a sizeof() on the name argument, which does only work with
* constant names, but not with parameterized ones as it does
* "sizeof(char*)" in that case. */
zend_call_method(value, NULL, NULL, name, strlen(name), NULL, 1, z_event, NULL TSRMLS_CC);
} ZEND_HASH_FOREACH_END();
#else
HashPosition pos;
TSRMLS_FETCH();

zend_hash_internal_pointer_reset_ex(&MONGODB_G(subscribers), &pos);
for (;; zend_hash_move_forward_ex(&MONGODB_G(subscribers), &pos)) {
zval **value;

if (zend_hash_get_current_data_ex(&MONGODB_G(subscribers), (void **) &value, &pos) == FAILURE) {
break;
}

/* We can't use the zend_call_method_with_1_params macro here, as it
* does a sizeof() on the name argument, which does only work with
* constant names, but not with parameterized ones as it does
* "sizeof(char*)" in that case. */
zend_call_method(value, NULL, NULL, name, strlen(name), NULL, 1, z_event, NULL TSRMLS_CC);
}
#endif
}

static void php_phongo_command_started(const mongoc_apm_command_started_t *event)
{
php_phongo_commandstartedevent_t *p_event;
#if PHP_VERSION_ID >= 70000
zval z_event;
#else
zval *z_event = NULL;
#endif
TSRMLS_FETCH();

/* Check for subscriber size */
if (zend_hash_num_elements(&MONGODB_G(subscribers)) == 0) {
return;
}

#if PHP_VERSION_ID >= 70000
object_init_ex(&z_event, php_phongo_commandstartedevent_ce);
p_event = Z_COMMANDSTARTEDEVENT_OBJ_P(&z_event);
#else
MAKE_STD_ZVAL(z_event);
object_init_ex(z_event, php_phongo_commandstartedevent_ce);
p_event = Z_COMMANDSTARTEDEVENT_OBJ_P(z_event);
#endif

p_event->client = mongoc_apm_command_started_get_context(event);
p_event->command_name = estrdup(mongoc_apm_command_started_get_command_name(event));
p_event->server_id = mongoc_apm_command_started_get_server_id(event);
p_event->operation_id = mongoc_apm_command_started_get_operation_id(event);
p_event->request_id = mongoc_apm_command_started_get_request_id(event);
p_event->command = bson_copy(mongoc_apm_command_started_get_command(event));
p_event->database_name = estrdup(mongoc_apm_command_started_get_database_name(event));

#if PHP_VERSION_ID >= 70000
php_phongo_dispatch_handlers("commandStarted", &z_event);
#else
php_phongo_dispatch_handlers("commandStarted", z_event);
#endif
zval_ptr_dtor(&z_event);
}

static void php_phongo_command_succeeded(const mongoc_apm_command_succeeded_t *event)
{
php_phongo_commandsucceededevent_t *p_event;
#if PHP_VERSION_ID >= 70000
zval z_event;
#else
zval *z_event = NULL;
#endif
TSRMLS_FETCH();

/* Check for subscriber size */
if (zend_hash_num_elements(&MONGODB_G(subscribers)) == 0) {
return;
}

#if PHP_VERSION_ID >= 70000
object_init_ex(&z_event, php_phongo_commandsucceededevent_ce);
p_event = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(&z_event);
#else
MAKE_STD_ZVAL(z_event);
object_init_ex(z_event, php_phongo_commandsucceededevent_ce);
p_event = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(z_event);
#endif

p_event->client = mongoc_apm_command_succeeded_get_context(event);
p_event->command_name = estrdup(mongoc_apm_command_succeeded_get_command_name(event));
p_event->server_id = mongoc_apm_command_succeeded_get_server_id(event);
p_event->operation_id = mongoc_apm_command_succeeded_get_operation_id(event);
p_event->request_id = mongoc_apm_command_succeeded_get_request_id(event);
p_event->duration_micros = mongoc_apm_command_succeeded_get_duration(event);
p_event->reply = bson_copy(mongoc_apm_command_succeeded_get_reply(event));

#if PHP_VERSION_ID >= 70000
php_phongo_dispatch_handlers("commandSucceeded", &z_event);
#else
php_phongo_dispatch_handlers("commandSucceeded", z_event);
#endif
zval_ptr_dtor(&z_event);
}

static void php_phongo_command_failed(const mongoc_apm_command_failed_t *event)
{
php_phongo_commandfailedevent_t *p_event;
#if PHP_VERSION_ID >= 70000
zval z_event;
#else
zval *z_event = NULL;
#endif
bson_error_t tmp_error;
zend_class_entry *default_exception_ce;
TSRMLS_FETCH();

default_exception_ce = zend_exception_get_default(TSRMLS_C);

/* Check for subscriber size */
if (zend_hash_num_elements(&MONGODB_G(subscribers)) == 0) {
return;
}

#if PHP_VERSION_ID >= 70000
object_init_ex(&z_event, php_phongo_commandfailedevent_ce);
p_event = Z_COMMANDFAILEDEVENT_OBJ_P(&z_event);
#else
MAKE_STD_ZVAL(z_event);
object_init_ex(z_event, php_phongo_commandfailedevent_ce);
p_event = Z_COMMANDFAILEDEVENT_OBJ_P(z_event);
#endif

p_event->client = mongoc_apm_command_failed_get_context(event);
p_event->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event));
p_event->server_id = mongoc_apm_command_failed_get_server_id(event);
p_event->operation_id = mongoc_apm_command_failed_get_operation_id(event);
p_event->request_id = mongoc_apm_command_failed_get_request_id(event);
p_event->duration_micros = mongoc_apm_command_failed_get_duration(event);

/* We need to process and convert the error right here, otherwise
* debug_info will turn into a recursive loop, and with the wrong trace
* locations */
mongoc_apm_command_failed_get_error(event, &tmp_error);
{
#if PHP_VERSION_ID < 70000
MAKE_STD_ZVAL(p_event->z_error);
object_init_ex(p_event->z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code));
zend_update_property_string(default_exception_ce, p_event->z_error, ZEND_STRL("message"), tmp_error.message TSRMLS_CC);
zend_update_property_long(default_exception_ce, p_event->z_error, ZEND_STRL("code"), tmp_error.code TSRMLS_CC);
#else
object_init_ex(&p_event->z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code));
zend_update_property_string(default_exception_ce, &p_event->z_error, ZEND_STRL("message"), tmp_error.message TSRMLS_CC);
zend_update_property_long(default_exception_ce, &p_event->z_error, ZEND_STRL("code"), tmp_error.code TSRMLS_CC);
#endif
}

#if PHP_VERSION_ID >= 70000
php_phongo_dispatch_handlers("commandFailed", &z_event);
#else
php_phongo_dispatch_handlers("commandFailed", z_event);
#endif
zval_ptr_dtor(&z_event);
}


/* Sets the callbacks for APM */
int php_phongo_set_monitoring_callbacks(mongoc_client_t *client)
{
int retval;

mongoc_apm_callbacks_t *callbacks = mongoc_apm_callbacks_new();

mongoc_apm_set_command_started_cb(callbacks, php_phongo_command_started);
mongoc_apm_set_command_succeeded_cb(callbacks, php_phongo_command_succeeded);
mongoc_apm_set_command_failed_cb(callbacks, php_phongo_command_failed);
retval = mongoc_client_set_apm_callbacks(client, callbacks, client);

mongoc_apm_callbacks_destroy(callbacks);

return retval;
}

/* Creates a hash for a client by concatenating the URI string with serialized
* options arrays. On success, a persistent string is returned (i.e. pefree()
* should be used to free it) and hash_len will be set to the string's length.
Expand Down Expand Up @@ -1827,6 +2020,16 @@ static void php_phongo_pclient_dtor(void *pp)
}
#endif

/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(mongodb)
{
/* Initialize HashTable for APM subscribers */
zend_hash_init(&MONGODB_G(subscribers), 0, NULL, ZVAL_PTR_DTOR, 0);

return SUCCESS;
}
/* }}} */

/* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(mongodb)
{
Expand All @@ -1843,6 +2046,7 @@ PHP_GINIT_FUNCTION(mongodb)
#endif
memset(mongodb_globals, 0, sizeof(zend_mongodb_globals));
mongodb_globals->bsonMemVTable = bsonMemVTable;

/* Initialize HashTable for persistent clients */
zend_hash_init_ex(&mongodb_globals->pclients, 0, NULL, php_phongo_pclient_dtor, 1, 0);
}
Expand Down Expand Up @@ -1963,6 +2167,13 @@ PHP_MINIT_FUNCTION(mongodb)
php_phongo_sslconnectionexception_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_unexpectedvalueexception_init_ce(INIT_FUNC_ARGS_PASSTHRU);

/* Register base APM classes first */
php_phongo_subscriber_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandsubscriber_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandstartedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);

REGISTER_STRING_CONSTANT("MONGODB_VERSION", (char *)PHP_MONGODB_VERSION, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("MONGODB_STABILITY", (char *)PHP_MONGODB_STABILITY, CONST_CS | CONST_PERSISTENT);

Expand All @@ -1989,6 +2200,15 @@ PHP_MSHUTDOWN_FUNCTION(mongodb)
}
/* }}} */

/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(mongodb)
{
zend_hash_destroy(&MONGODB_G(subscribers));

return SUCCESS;
}
/* }}} */

/* {{{ PHP_GSHUTDOWN_FUNCTION */
PHP_GSHUTDOWN_FUNCTION(mongodb)
{
Expand Down Expand Up @@ -2107,11 +2327,22 @@ ZEND_BEGIN_ARG_INFO_EX(ai_bson_fromJSON, 0, 0, 1)
ZEND_ARG_INFO(0, json)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_Monitoring_addSubscriber, 0, 0, 1)
ZEND_ARG_INFO(0, subscriber)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(ai_Monitoring_removeSubscriber, 0, 0, 1)
ZEND_ARG_INFO(0, subscriber)
ZEND_END_ARG_INFO()


static const zend_function_entry mongodb_functions[] = {
ZEND_NS_NAMED_FE("MongoDB\\BSON", fromPHP, PHP_FN(MongoDB_BSON_fromPHP), ai_bson_fromPHP)
ZEND_NS_NAMED_FE("MongoDB\\BSON", toPHP, PHP_FN(MongoDB_BSON_toPHP), ai_bson_toPHP)
ZEND_NS_NAMED_FE("MongoDB\\BSON", toJSON, PHP_FN(MongoDB_BSON_toJSON), ai_bson_toJSON)
ZEND_NS_NAMED_FE("MongoDB\\BSON", fromJSON, PHP_FN(MongoDB_BSON_fromJSON), ai_bson_fromJSON)
ZEND_NS_NAMED_FE("MongoDB\\Monitoring", addSubscriber, PHP_FN(MongoDB_Monitoring_addSubscriber), ai_Monitoring_addSubscriber)
ZEND_NS_NAMED_FE("MongoDB\\Monitoring", removeSubscriber, PHP_FN(MongoDB_Monitoring_removeSubscriber), ai_Monitoring_removeSubscriber)
PHP_FE_END
};
/* }}} */
Expand All @@ -2134,8 +2365,8 @@ zend_module_entry mongodb_module_entry = {
mongodb_functions,
PHP_MINIT(mongodb),
PHP_MSHUTDOWN(mongodb),
NULL /* PHP_RINIT(mongodb)*/,
NULL /* PHP_RSHUTDOWN(mongodb)*/,
PHP_RINIT(mongodb),
PHP_RSHUTDOWN(mongodb),
PHP_MINFO(mongodb),
PHP_MONGODB_VERSION,
PHP_MODULE_GLOBALS(mongodb),
Expand Down
2 changes: 2 additions & 0 deletions php_phongo.h
Expand Up @@ -41,6 +41,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mongodb)
FILE *debug_fd;
bson_mem_vtable_t bsonMemVTable;
HashTable pclients;
HashTable subscribers;
ZEND_END_MODULE_GLOBALS(mongodb)

#if PHP_VERSION_ID >= 70000
Expand Down Expand Up @@ -137,6 +138,7 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
void php_phongo_cursor_to_zval(zval *retval, const mongoc_cursor_t *cursor);

void phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string, zval *options, zval *driverOptions TSRMLS_DC);
int php_phongo_set_monitoring_callbacks(mongoc_client_t *client);
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);
void php_phongo_cursor_id_new_from_id(zval *object, int64_t cursorid TSRMLS_DC);
void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoch TSRMLS_DC);
Expand Down

0 comments on commit 075e6c7

Please sign in to comment.