Skip to content

Commit 9890e61

Browse files
committed
PHPC-1474: Expose transaction information in debug info for session
1 parent 2ab8d6b commit 9890e61

File tree

7 files changed

+196
-47
lines changed

7 files changed

+196
-47
lines changed

src/MongoDB/Session.c

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,51 @@ static const char* php_phongo_get_transaction_state_string(mongoc_transaction_st
9898
}
9999
}
100100

101+
static void php_phongo_transaction_options_to_zval(mongoc_transaction_opt_t* opts, zval* retval)
102+
{
103+
int64_t max_commit_time_ms;
104+
const mongoc_read_concern_t* read_concern;
105+
const mongoc_read_prefs_t* read_preference;
106+
const mongoc_write_concern_t* write_concern;
107+
108+
if (!opts) {
109+
ZVAL_NULL(retval);
110+
return;
111+
}
112+
113+
max_commit_time_ms = mongoc_transaction_opts_get_max_commit_time_ms(opts);
114+
read_concern = mongoc_transaction_opts_get_read_concern(opts);
115+
read_preference = mongoc_transaction_opts_get_read_prefs(opts);
116+
write_concern = mongoc_transaction_opts_get_write_concern(opts);
117+
118+
array_init_size(retval, 4);
119+
120+
if (max_commit_time_ms) {
121+
ADD_ASSOC_LONG_EX(retval, "maxCommitTimeMS", max_commit_time_ms);
122+
}
123+
124+
if (!mongoc_read_concern_is_default(read_concern)) {
125+
zval zread_concern;
126+
127+
phongo_readconcern_init(&zread_concern, read_concern);
128+
ADD_ASSOC_ZVAL_EX(retval, "readConcern", &zread_concern);
129+
}
130+
131+
if (read_preference) {
132+
zval zread_preference;
133+
134+
phongo_readpreference_init(&zread_preference, read_preference);
135+
ADD_ASSOC_ZVAL_EX(retval, "readPreference", &zread_preference);
136+
}
137+
138+
if (!mongoc_write_concern_is_default(write_concern)) {
139+
zval zwrite_concern;
140+
141+
phongo_writeconcern_init(&zwrite_concern, write_concern);
142+
ADD_ASSOC_ZVAL_EX(retval, "writeConcern", &zwrite_concern);
143+
}
144+
}
145+
101146
/* {{{ proto void MongoDB\Driver\Session::advanceClusterTime(array|object $clusterTime)
102147
Advances the cluster time for this Session */
103148
static PHP_METHOD(Session, advanceClusterTime)
@@ -287,13 +332,8 @@ static PHP_METHOD(Session, getServer)
287332
Returns options for the currently running transaction */
288333
static PHP_METHOD(Session, getTransactionOptions)
289334
{
290-
zend_error_handling error_handling;
291-
php_phongo_session_t* intern;
292-
mongoc_transaction_opt_t* opts;
293-
int64_t max_commit_time_ms;
294-
const mongoc_read_concern_t* read_concern;
295-
const mongoc_read_prefs_t* read_preference;
296-
const mongoc_write_concern_t* write_concern;
335+
zend_error_handling error_handling;
336+
php_phongo_session_t* intern;
297337

298338
intern = Z_SESSION_OBJ_P(getThis());
299339
SESSION_CHECK_LIVELINESS(intern, "getTransactionOptions")
@@ -305,43 +345,7 @@ static PHP_METHOD(Session, getTransactionOptions)
305345
}
306346
zend_restore_error_handling(&error_handling);
307347

308-
opts = mongoc_session_opts_get_transaction_opts(intern->client_session);
309-
310-
if (!opts) {
311-
return;
312-
}
313-
314-
max_commit_time_ms = mongoc_transaction_opts_get_max_commit_time_ms(opts);
315-
read_concern = mongoc_transaction_opts_get_read_concern(opts);
316-
read_preference = mongoc_transaction_opts_get_read_prefs(opts);
317-
write_concern = mongoc_transaction_opts_get_write_concern(opts);
318-
319-
array_init_size(return_value, 4);
320-
321-
if (max_commit_time_ms) {
322-
ADD_ASSOC_LONG_EX(return_value, "maxCommitTimeMS", max_commit_time_ms);
323-
}
324-
325-
if (!mongoc_read_concern_is_default(read_concern)) {
326-
zval zread_concern;
327-
328-
phongo_readconcern_init(&zread_concern, read_concern);
329-
ADD_ASSOC_ZVAL_EX(return_value, "readConcern", &zread_concern);
330-
}
331-
332-
if (read_preference) {
333-
zval zread_preference;
334-
335-
phongo_readpreference_init(&zread_preference, read_preference);
336-
ADD_ASSOC_ZVAL_EX(return_value, "readPreference", &zread_preference);
337-
}
338-
339-
if (!mongoc_write_concern_is_default(write_concern)) {
340-
zval zwrite_concern;
341-
342-
phongo_writeconcern_init(&zwrite_concern, write_concern);
343-
ADD_ASSOC_ZVAL_EX(return_value, "writeConcern", &zwrite_concern);
344-
}
348+
php_phongo_transaction_options_to_zval(mongoc_session_opts_get_transaction_opts(intern->client_session), return_value);
345349
} /* }}} */
346350

347351
/* {{{ proto string MongoDB\Driver\Session::getTransactionState()
@@ -674,9 +678,8 @@ static zend_object* php_phongo_session_create_object(zend_class_entry* class_typ
674678

675679
static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
676680
{
677-
php_phongo_session_t* intern = NULL;
678-
const mongoc_session_opt_t* cs_opts;
679-
zval retval = ZVAL_STATIC_INIT;
681+
php_phongo_session_t* intern = NULL;
682+
zval retval = ZVAL_STATIC_INIT;
680683

681684
*is_temp = 1;
682685
intern = Z_OBJ_SESSION(PHONGO_COMPAT_GET_OBJ(object));
@@ -725,6 +728,7 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
725728
}
726729

727730
if (intern->client_session) {
731+
const mongoc_session_opt_t* cs_opts;
728732
cs_opts = mongoc_client_session_get_opts(intern->client_session);
729733
ADD_ASSOC_BOOL_EX(&retval, "causalConsistency", mongoc_session_opts_get_causal_consistency(cs_opts));
730734
} else {
@@ -764,6 +768,41 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
764768
ADD_ASSOC_NULL_EX(&retval, "server");
765769
}
766770

771+
if (intern->client_session) {
772+
ADD_ASSOC_BOOL_EX(&retval, "inTransaction", mongoc_client_session_in_transaction(intern->client_session));
773+
} else {
774+
ADD_ASSOC_NULL_EX(&retval, "inTransaction");
775+
}
776+
777+
if (intern->client_session) {
778+
const char* state = php_phongo_get_transaction_state_string(mongoc_client_session_get_transaction_state(intern->client_session));
779+
780+
if (!state) {
781+
/* Exception should already have been thrown */
782+
goto done;
783+
}
784+
785+
ADD_ASSOC_STRING(&retval, "transactionState", state);
786+
} else {
787+
ADD_ASSOC_NULL_EX(&retval, "transactionState");
788+
}
789+
790+
if (intern->client_session) {
791+
mongoc_transaction_opt_t* txn_opts = mongoc_session_opts_get_transaction_opts(intern->client_session);
792+
793+
if (txn_opts) {
794+
795+
zval transaction;
796+
797+
php_phongo_transaction_options_to_zval(txn_opts, &transaction);
798+
ADD_ASSOC_ZVAL_EX(&retval, "transactionOptions", &transaction);
799+
} else {
800+
ADD_ASSOC_NULL_EX(&retval, "transactionOptions");
801+
}
802+
} else {
803+
ADD_ASSOC_NULL_EX(&retval, "transactionOptions");
804+
}
805+
767806
done:
768807
return Z_ARRVAL(retval);
769808
} /* }}} */

tests/session/session-debug-001.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ object(MongoDB\Driver\Session)#%d (%d) {
3737
NULL
3838
["server"]=>
3939
NULL
40+
["inTransaction"]=>
41+
bool(false)
42+
["transactionState"]=>
43+
string(4) "none"
44+
["transactionOptions"]=>
45+
NULL
4046
}
4147
===DONE===

tests/session/session-debug-002.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ object(MongoDB\Driver\Session)#%d (%d) {
5555
}
5656
["server"]=>
5757
NULL
58+
["inTransaction"]=>
59+
bool(false)
60+
["transactionState"]=>
61+
string(4) "none"
62+
["transactionOptions"]=>
63+
NULL
5864
}
5965
===DONE===

tests/session/session-debug-003.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ object(MongoDB\Driver\Session)#%d (%d) {
3737
NULL
3838
["server"]=>
3939
NULL
40+
["inTransaction"]=>
41+
bool(false)
42+
["transactionState"]=>
43+
string(4) "none"
44+
["transactionOptions"]=>
45+
NULL
4046
}
4147
===DONE===

tests/session/session-debug-004.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ object(MongoDB\Driver\Session)#%d (%d) {
3434
NULL
3535
["server"]=>
3636
NULL
37+
["inTransaction"]=>
38+
NULL
39+
["transactionState"]=>
40+
NULL
41+
["transactionOptions"]=>
42+
NULL
3743
}
3844
===DONE===

tests/session/session-debug-005.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MongoDB\Driver\Session debug output (during a pinned transaction)
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
56
<?php skip_if_not_mongos_with_replica_set(); ?>
67
<?php skip_if_server_version('<', '4.1.6'); ?>
78
<?php skip_if_not_clean(); ?>
@@ -63,5 +64,17 @@ object(MongoDB\Driver\Session)#%d (%d) {
6364
object(MongoDB\Driver\Server)#%d (%d) {
6465
%a
6566
}
67+
["inTransaction"]=>
68+
bool(true)
69+
["transactionState"]=>
70+
string(11) "in_progress"
71+
["transactionOptions"]=>
72+
array(1) {
73+
["readPreference"]=>
74+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
75+
["mode"]=>
76+
string(7) "primary"
77+
}
78+
}
6679
}
6780
===DONE===
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
MongoDB\Driver\Session debug output (with transaction options)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_no_transactions(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$session = $manager->startSession();
13+
14+
$options = [
15+
'maxCommitTimeMS' => 1,
16+
'readConcern' => new \MongoDB\Driver\ReadConcern('majority'),
17+
'readPreference' => new \MongoDB\Driver\ReadPreference('primaryPreferred'),
18+
'writeConcern' => new \MongoDB\Driver\WriteConcern('majority'),
19+
];
20+
21+
$session->startTransaction($options);
22+
23+
var_dump($session);
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
object(MongoDB\Driver\Session)#%d (%d) {
30+
["logicalSessionId"]=>
31+
array(1) {
32+
["id"]=>
33+
object(MongoDB\BSON\Binary)#%d (%d) {
34+
["data"]=>
35+
string(16) "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
36+
["type"]=>
37+
int(4)
38+
}
39+
}
40+
["clusterTime"]=>
41+
NULL
42+
["causalConsistency"]=>
43+
bool(true)
44+
["operationTime"]=>
45+
NULL
46+
["server"]=>
47+
NULL
48+
["inTransaction"]=>
49+
bool(true)
50+
["transactionState"]=>
51+
string(8) "starting"
52+
["transactionOptions"]=>
53+
array(4) {
54+
["maxCommitTimeMS"]=>
55+
int(1)
56+
["readConcern"]=>
57+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
58+
["level"]=>
59+
string(8) "majority"
60+
}
61+
["readPreference"]=>
62+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
63+
["mode"]=>
64+
string(16) "primaryPreferred"
65+
}
66+
["writeConcern"]=>
67+
object(MongoDB\Driver\WriteConcern)#%d (%d) {
68+
["w"]=>
69+
string(8) "majority"
70+
}
71+
}
72+
}
73+
===DONE===

0 commit comments

Comments
 (0)