Skip to content

PHPC-1359: Prohibit startTransaction() on sharded clusters #984

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
Jun 3, 2019
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
12 changes: 12 additions & 0 deletions src/MongoDB/Session.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ zend_class_entry* php_phongo_session_ce;
return; \
}

static bool php_phongo_topology_is_sharded_cluster(mongoc_client_t* client)
{
mongoc_server_description_t* sd = mongoc_client_select_server(client, true, NULL, NULL);

return (sd && !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_MONGOS].name));
}

static bool php_phongo_session_get_timestamp_parts(zval* obj, uint32_t* timestamp, uint32_t* increment TSRMLS_DC)
{
bool retval = false;
Expand Down Expand Up @@ -334,6 +341,11 @@ static PHP_METHOD(Session, startTransaction)
return;
}

if (php_phongo_topology_is_sharded_cluster(mongoc_client_session_get_client(intern->client_session))) {
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "PHP MongoDB driver %s does not support running multi-document transactions on sharded clusters", PHP_MONGODB_VERSION);
return;
}

if (!mongoc_client_session_start_transaction(intern->client_session, txn_options, &error)) {
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/session/session-startTransaction_error-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
MongoDB\Driver\Session::startTransaction() does not support sharded clusters
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_libmongoc_crypto() ?>
<?php skip_if_not_mongos(); ?>
<?php skip_if_server_version('<', '4.0'); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(URI);
$session = $manager->startSession();

echo throws(function() use ($session) {
$session->startTransaction();
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\RuntimeException
PHP MongoDB driver %s does not support running multi-document transactions on sharded clusters
===DONE===