-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-1438: Expose session state #1061
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,12 @@ | |
|
||
zend_class_entry* php_phongo_session_ce; | ||
|
||
#define PHONGO_TRANSACTION_NONE "none" | ||
#define PHONGO_TRANSACTION_STARTING "starting" | ||
#define PHONGO_TRANSACTION_IN_PROGRESS "in_progress" | ||
#define PHONGO_TRANSACTION_COMMITTED "committed" | ||
#define PHONGO_TRANSACTION_ABORTED "aborted" | ||
|
||
#define SESSION_CHECK_LIVELINESS(i, m) \ | ||
if (!(i)->client_session) { \ | ||
phongo_throw_exception( \ | ||
|
@@ -93,6 +99,25 @@ static bool php_phongo_session_get_timestamp_parts(zval* obj, uint32_t* timestam | |
return retval; | ||
} | ||
|
||
static const char* php_phongo_get_transaction_state_string(mongoc_transaction_state_t state TSRMLS_DC) | ||
{ | ||
switch (state) { | ||
case MONGOC_TRANSACTION_NONE: | ||
return PHONGO_TRANSACTION_NONE; | ||
case MONGOC_TRANSACTION_STARTING: | ||
return PHONGO_TRANSACTION_STARTING; | ||
case MONGOC_TRANSACTION_IN_PROGRESS: | ||
return PHONGO_TRANSACTION_IN_PROGRESS; | ||
case MONGOC_TRANSACTION_COMMITTED: | ||
return PHONGO_TRANSACTION_COMMITTED; | ||
case MONGOC_TRANSACTION_ABORTED: | ||
return PHONGO_TRANSACTION_ABORTED; | ||
default: | ||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid transaction state %d given", (int) state); | ||
return NULL; | ||
} | ||
} | ||
|
||
/* {{{ proto void MongoDB\Driver\Session::advanceClusterTime(array|object $clusterTime) | ||
Advances the cluster time for this Session */ | ||
static PHP_METHOD(Session, advanceClusterTime) | ||
|
@@ -343,6 +368,30 @@ static PHP_METHOD(Session, getTransactionOptions) | |
} | ||
} /* }}} */ | ||
|
||
/* {{{ proto string MongoDB\Driver\Session::getTransactionState() | ||
Returns the current transaction state for this session */ | ||
static PHP_METHOD(Session, getTransactionState) | ||
{ | ||
php_phongo_session_t* intern; | ||
const char* state; | ||
|
||
intern = Z_SESSION_OBJ_P(getThis()); | ||
SESSION_CHECK_LIVELINESS(intern, "getTransactionState") | ||
|
||
if (zend_parse_parameters_none() == FAILURE) { | ||
return; | ||
} | ||
|
||
state = php_phongo_get_transaction_state_string(mongoc_client_session_get_transaction_state(intern->client_session) TSRMLS_CC); | ||
if (!state) { | ||
/* Exception already thrown */ | ||
return; | ||
} | ||
|
||
PHONGO_RETURN_STRING(state); | ||
} /* }}} */ | ||
|
||
|
||
/* Creates a opts structure from an array optionally containing an RP, RC, | ||
* WC object, and/or maxCommitTimeMS int. Returns NULL if no options were found, | ||
* or there was an invalid option. If there was an invalid option or structure, | ||
|
@@ -572,6 +621,7 @@ static zend_function_entry php_phongo_session_me[] = { | |
PHP_ME(Session, getOperationTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(Session, getServer, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(Session, getTransactionOptions, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(Session, getTransactionState, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(Session, isInTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
PHP_ME(Session, startTransaction, ai_Session_startTransaction, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) | ||
ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Session_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL) | ||
|
@@ -760,6 +810,12 @@ void php_phongo_session_init_ce(INIT_FUNC_ARGS) /* {{{ */ | |
php_phongo_handler_session.free_obj = php_phongo_session_free_object; | ||
php_phongo_handler_session.offset = XtOffsetOf(php_phongo_session_t, std); | ||
#endif | ||
|
||
zend_declare_class_constant_string(php_phongo_session_ce, ZEND_STRL("TRANSACTION_NONE"), PHONGO_TRANSACTION_NONE TSRMLS_CC); | ||
zend_declare_class_constant_string(php_phongo_session_ce, ZEND_STRL("TRANSACTION_STARTING"), PHONGO_TRANSACTION_STARTING TSRMLS_CC); | ||
zend_declare_class_constant_string(php_phongo_session_ce, ZEND_STRL("TRANSACTION_IN_PROGRESS"), PHONGO_TRANSACTION_IN_PROGRESS TSRMLS_CC); | ||
zend_declare_class_constant_string(php_phongo_session_ce, ZEND_STRL("TRANSACTION_COMMITTED"), PHONGO_TRANSACTION_COMMITTED TSRMLS_CC); | ||
zend_declare_class_constant_string(php_phongo_session_ce, ZEND_STRL("TRANSACTION_ABORTED"), PHONGO_TRANSACTION_ABORTED 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 suggest adding one more test for these constants, similar to https://github.com/mongodb/mongo-php-driver/blob/master/tests/readConcern/readconcern-constants.phpt. |
||
} /* }}} */ | ||
|
||
/* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
MongoDB\Driver\Session constants | ||
--FILE-- | ||
<?php | ||
|
||
var_dump(MongoDB\Driver\Session::TRANSACTION_NONE); | ||
var_dump(MongoDB\Driver\Session::TRANSACTION_STARTING); | ||
var_dump(MongoDB\Driver\Session::TRANSACTION_IN_PROGRESS); | ||
var_dump(MongoDB\Driver\Session::TRANSACTION_COMMITTED); | ||
var_dump(MongoDB\Driver\Session::TRANSACTION_ABORTED); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
string(4) "none" | ||
string(8) "starting" | ||
string(11) "in_progress" | ||
string(9) "committed" | ||
string(7) "aborted" | ||
===DONE=== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--TEST-- | ||
MongoDB\Driver\Session::getTransactionState() | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
<?php skip_if_not_libmongoc_crypto() ?> | ||
<?php skip_if_no_transactions(); ?> | ||
<?php skip_if_not_clean(); ?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$manager = new MongoDB\Driver\Manager(URI); | ||
|
||
/* Create collections as that can't be (automatically) done in a transaction */ | ||
$cmd = new \MongoDB\Driver\Command([ | ||
'create' => COLLECTION_NAME, | ||
]); | ||
$manager->executeCommand(DATABASE_NAME, $cmd); | ||
|
||
/* Start a session */ | ||
$session = $manager->startSession(); | ||
|
||
echo "Test case: Empty transaction, and aborted empty transaction\n"; | ||
var_dump($session->getTransactionState()); | ||
$session->startTransaction(); | ||
var_dump($session->getTransactionState()); | ||
$session->abortTransaction(); | ||
var_dump($session->getTransactionState()); | ||
echo "\n"; | ||
|
||
echo "Test case: Empty transaction, and committed empty transaction\n"; | ||
$session->startTransaction(); | ||
var_dump($session->getTransactionState()); | ||
$session->commitTransaction(); | ||
var_dump($session->getTransactionState()); | ||
echo "\n"; | ||
|
||
echo "Test case: Aborted transaction with one operation\n"; | ||
$session->startTransaction(); | ||
var_dump($session->getTransactionState()); | ||
$bw = new \MongoDB\Driver\BulkWrite(); | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] ); | ||
$manager->executeBulkWrite(NS, $bw, ['session' => $session]); | ||
var_dump($session->getTransactionState()); | ||
$session->abortTransaction(); | ||
var_dump($session->getTransactionState()); | ||
echo "\n"; | ||
|
||
echo "Test case: Committed transaction with one operation\n"; | ||
$session->startTransaction(); | ||
var_dump($session->getTransactionState()); | ||
$bw = new \MongoDB\Driver\BulkWrite(); | ||
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] ); | ||
$manager->executeBulkWrite(NS, $bw, ['session' => $session]); | ||
var_dump($session->getTransactionState()); | ||
$session->commitTransaction(); | ||
var_dump($session->getTransactionState()); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
Test case: Empty transaction, and aborted empty transaction | ||
string(4) "none" | ||
string(8) "starting" | ||
string(7) "aborted" | ||
|
||
Test case: Empty transaction, and committed empty transaction | ||
string(8) "starting" | ||
string(9) "committed" | ||
|
||
Test case: Aborted transaction with one operation | ||
string(8) "starting" | ||
string(11) "in_progress" | ||
string(7) "aborted" | ||
|
||
Test case: Committed transaction with one operation | ||
string(8) "starting" | ||
string(11) "in_progress" | ||
string(9) "committed" | ||
===DONE=== |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Considering that libmongoc exposes transaction states as defined in the spec, I've decided to throw an exception in this case. While this requires us to pay close attention to any new transaction states, I'm not sure what other alternative would be feasible (besides returning the integer value)
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.
I think an exception is fine here. Based on how CDRIVER-3364 was implemented, we know that libmongoc would abort before we ever reach this point. Also, any new transaction state would likely entail a spec change, which would trickle down as CDRIVER and PHPC tickets, so I'm not worried about missing such a change.