-
Notifications
You must be signed in to change notification settings - Fork 208
[WAIT] PHPC-1076: Expose result document for failed commands via CommandFailedEvent #818
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2214,11 +2214,13 @@ static void php_phongo_command_failed(const mongoc_apm_command_failed_t* 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); | ||
p_event->reply = bson_copy(mongoc_apm_command_failed_get_reply(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); | ||
|
@@ -2229,6 +2231,7 @@ static void php_phongo_command_failed(const mongoc_apm_command_failed_t* event) | |
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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you should add this extra white space line here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this in #840. |
||
#endif | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,29 @@ PHP_METHOD(CommandFailedEvent, getOperationId) | |
PHONGO_RETVAL_STRING(int_as_string); | ||
} /* }}} */ | ||
|
||
/* {{{ proto stdClass CommandFailedEvent::getReply() | ||
Returns the reply document associated with the event */ | ||
PHP_METHOD(CommandFailedEvent, getReply) | ||
{ | ||
php_phongo_commandfailedevent_t* intern; | ||
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER; | ||
SUPPRESS_UNUSED_WARNING(return_value_ptr) | ||
SUPPRESS_UNUSED_WARNING(return_value_used) | ||
|
||
intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis()); | ||
|
||
if (zend_parse_parameters_none() == FAILURE) { | ||
return; | ||
} | ||
|
||
php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state); | ||
#if PHP_VERSION_ID >= 70000 | ||
RETURN_ZVAL(&state.zchild, 0, 1); | ||
#else | ||
RETURN_ZVAL(state.zchild, 0, 1); | ||
#endif | ||
} /* }}} */ | ||
|
||
/* {{{ proto string CommandFailedEvent::getRequestId() | ||
Returns the event's request ID */ | ||
PHP_METHOD(CommandFailedEvent, getRequestId) | ||
|
@@ -152,6 +175,7 @@ static zend_function_entry php_phongo_commandfailedevent_me[] = { | |
PHP_ME(CommandFailedEvent, getError, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(CommandFailedEvent, getDurationMicros, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(CommandFailedEvent, getOperationId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(CommandFailedEvent, getReply, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you did here, indenting it nicely in between the one before and after — however, I don't think our intention was ever to stair-case-indent these in the first place, and it might just have slipped by our review radar when we introduced clang-format. This can be addressed by using the following patch:
I will make a patch to do that for all places — but that might mean a conflict here. For now, I think you can keep it and I'll deal with it in my formatting patch. |
||
PHP_ME(CommandFailedEvent, getRequestId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(CommandFailedEvent, getServer, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
|
@@ -172,6 +196,10 @@ static void php_phongo_commandfailedevent_free_object(phongo_free_object_arg* ob | |
zval_ptr_dtor(&intern->z_error); | ||
} | ||
|
||
if (intern->reply) { | ||
bson_destroy(intern->reply); | ||
} | ||
|
||
if (intern->command_name) { | ||
efree(intern->command_name); | ||
} | ||
|
@@ -210,6 +238,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(zval* object, int | |
php_phongo_commandfailedevent_t* intern; | ||
zval retval = ZVAL_STATIC_INIT; | ||
char operation_id[20], request_id[20]; | ||
php_phongo_bson_state reply_state = PHONGO_BSON_STATE_INITIALIZER; | ||
|
||
intern = Z_COMMANDFAILEDEVENT_OBJ_P(object); | ||
*is_temp = 1; | ||
|
@@ -229,6 +258,13 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(zval* object, int | |
sprintf(operation_id, "%" PRIu64, intern->operation_id); | ||
ADD_ASSOC_STRING(&retval, "operationId", operation_id); | ||
|
||
php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state); | ||
#if PHP_VERSION_ID >= 70000 | ||
ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild); | ||
#else | ||
ADD_ASSOC_ZVAL(&retval, "reply", reply_state.zchild); | ||
#endif | ||
|
||
sprintf(request_id, "%" PRIu64, intern->request_id); | ||
ADD_ASSOC_STRING(&retval, "requestId", request_id); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--TEST-- | ||
MongoDB\Driver\Monitoring\CommandFailedEvent | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.4"); ?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$manager = new MongoDB\Driver\Manager(STANDALONE); | ||
|
||
class MySubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber | ||
{ | ||
public function commandStarted( \MongoDB\Driver\Monitoring\CommandStartedEvent $event ) | ||
{ | ||
echo "started: ", $event->getCommandName(), "\n"; | ||
} | ||
|
||
public function commandSucceeded( \MongoDB\Driver\Monitoring\CommandSucceededEvent $event ) | ||
{ | ||
var_dump($event); | ||
} | ||
|
||
public function commandFailed( \MongoDB\Driver\Monitoring\CommandFailedEvent $event ) | ||
{ | ||
echo "failed: ", $event->getCommandName(), "\n"; | ||
var_dump($event->getReply()); | ||
} | ||
} | ||
|
||
$subscriber = new MySubscriber; | ||
|
||
MongoDB\Driver\Monitoring\addSubscriber( $subscriber ); | ||
|
||
$command = new MongoDB\Driver\Command([ | ||
'findAndModify' => COLLECTION_NAME, | ||
'query' => ['_id' => 'foo'], | ||
'upsert' => true, | ||
'new' => true, | ||
]); | ||
|
||
try { | ||
$manager->executeWriteCommand(DATABASE_NAME, $command); | ||
} catch (MongoDB\Driver\Exception\CommandException $e) {} | ||
|
||
?> | ||
--EXPECTF-- | ||
started: findAndModify | ||
failed: findAndModify | ||
object(stdClass)#%d (4) { | ||
["ok"]=> | ||
float(0) | ||
["errmsg"]=> | ||
string(49) "Either an update or remove=true must be specified" | ||
["code"]=> | ||
int(9) | ||
["codeName"]=> | ||
string(13) "FailedToParse" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The build currently fails because
mongoc_apm_command_failed_get_reply()
does not exist in the libmongoc submodule. You'll need to do the following to address this:config.m4
, you'll also need to bump lines that check the version of libmongoc when used as a system library. See:mongo-php-driver/config.m4
Line 289 in 94c7487
Note that libmongoc's master branch (1.10-dev at the moment) incorporates libbson, so I'm not suggesting you bump the libbson submodule since there is nothing to bump to. I assume libmongoc 1.10-dev will still play nice with libbson 1.9 (for now), but if only updating libmongoc causes issues, we can work through a solution in more detail.