Skip to content

Commit 969b0f8

Browse files
committed
PHPC-1290: Expose pinned server on session object
1 parent d03f362 commit 969b0f8

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

src/MongoDB/Session.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,30 @@ static PHP_METHOD(Session, getOperationTime)
234234
php_phongo_new_timestamp_from_increment_and_timestamp(return_value, increment, timestamp TSRMLS_CC);
235235
} /* }}} */
236236

237+
/* {{{ proto MongoDB\Driver\Server|null MongoDB\Driver\Session::getServer()
238+
Returns the server this session is pinned to */
239+
static PHP_METHOD(Session, getServer)
240+
{
241+
php_phongo_session_t* intern;
242+
uint32_t server_id = 0;
243+
244+
intern = Z_SESSION_OBJ_P(getThis());
245+
SESSION_CHECK_LIVELINESS(intern, "getServer")
246+
247+
if (zend_parse_parameters_none() == FAILURE) {
248+
return;
249+
}
250+
251+
server_id = mongoc_client_session_get_server_id(intern->client_session);
252+
253+
/* For sessions without a pinned server, 0 is returned. */
254+
if (!server_id) {
255+
RETURN_NULL();
256+
}
257+
258+
phongo_server_init(return_value, mongoc_client_session_get_client(intern->client_session), server_id TSRMLS_CC);
259+
} /* }}} */
260+
237261
/* Creates a opts structure from an array optionally containing an RP, RC,
238262
* WC object, and/or maxCommitTimeMS int. Returns NULL if no options were found,
239263
* or there was an invalid option. If there was an invalid option or structure,
@@ -461,6 +485,7 @@ static zend_function_entry php_phongo_session_me[] = {
461485
PHP_ME(Session, getClusterTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
462486
PHP_ME(Session, getLogicalSessionId, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
463487
PHP_ME(Session, getOperationTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
488+
PHP_ME(Session, getServer, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
464489
PHP_ME(Session, isInTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
465490
PHP_ME(Session, startTransaction, ai_Session_startTransaction, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
466491
ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Session_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
@@ -601,6 +626,30 @@ static HashTable* php_phongo_session_get_debug_info(zval* object, int* is_temp T
601626
ADD_ASSOC_NULL_EX(&retval, "operationTime");
602627
}
603628

629+
if (intern->client_session) {
630+
uint32_t server_id = mongoc_client_session_get_server_id(intern->client_session);
631+
632+
if (server_id) {
633+
634+
#if PHP_VERSION_ID >= 70000
635+
zval server;
636+
637+
phongo_server_init(&server, mongoc_client_session_get_client(intern->client_session), server_id TSRMLS_CC);
638+
ADD_ASSOC_ZVAL_EX(&retval, "server", &server);
639+
#else
640+
zval* server = NULL;
641+
642+
MAKE_STD_ZVAL(server);
643+
phongo_server_init(server, mongoc_client_session_get_client(intern->client_session), server_id TSRMLS_CC);
644+
ADD_ASSOC_ZVAL_EX(&retval, "server", server);
645+
#endif
646+
} else {
647+
ADD_ASSOC_NULL_EX(&retval, "server");
648+
}
649+
} else {
650+
ADD_ASSOC_NULL_EX(&retval, "server");
651+
}
652+
604653
return Z_ARRVAL(retval);
605654
} /* }}} */
606655
/* }}} */

tests/session/session-debug-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ object(MongoDB\Driver\Session)#%d (%d) {
3535
bool(true)
3636
["operationTime"]=>
3737
NULL
38+
["server"]=>
39+
NULL
3840
}
3941
===DONE===

tests/session/session-debug-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ object(MongoDB\Driver\Session)#%d (%d) {
5353
["timestamp"]=>
5454
string(%d) "%d"
5555
}
56+
["server"]=>
57+
NULL
5658
}
5759
===DONE===

tests/session/session-debug-003.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ object(MongoDB\Driver\Session)#%d (%d) {
3535
bool(false)
3636
["operationTime"]=>
3737
NULL
38+
["server"]=>
39+
NULL
3840
}
3941
===DONE===

tests/session/session-debug-004.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ object(MongoDB\Driver\Session)#%d (%d) {
3232
NULL
3333
["operationTime"]=>
3434
NULL
35+
["server"]=>
36+
NULL
3537
}
3638
===DONE===

tests/session/session-debug-005.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
MongoDB\Driver\Session debug output (during a pinned transaction)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
$server = $manager->selectServer(new \MongoDB\Driver\ReadPreference('primary'));
14+
15+
$session = $manager->startSession();
16+
$session->startTransaction();
17+
18+
$query = new MongoDB\Driver\Query([]);
19+
$server->executeQuery(NS, $query, ['session' => $session]);
20+
21+
var_dump($session);
22+
23+
$session->abortTransaction();
24+
$session->endSession();
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECTF--
30+
object(MongoDB\Driver\Session)#%d (%d) {
31+
["logicalSessionId"]=>
32+
array(1) {
33+
["id"]=>
34+
object(MongoDB\BSON\Binary)#%d (%d) {
35+
["data"]=>
36+
string(16) "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
37+
["type"]=>
38+
int(4)
39+
}
40+
}
41+
["clusterTime"]=>
42+
array(2) {
43+
["clusterTime"]=>
44+
object(MongoDB\BSON\Timestamp)#%d (%d) {
45+
["increment"]=>
46+
string(%d) "%d"
47+
["timestamp"]=>
48+
string(%d) "%d"
49+
}
50+
["signature"]=>
51+
%a
52+
}
53+
["causalConsistency"]=>
54+
bool(true)
55+
["operationTime"]=>
56+
object(MongoDB\BSON\Timestamp)#%d (%d) {
57+
["increment"]=>
58+
string(%d) "%d"
59+
["timestamp"]=>
60+
string(%d) "%d"
61+
}
62+
["server"]=>
63+
object(MongoDB\Driver\Server)#%d (%d) {
64+
%a
65+
}
66+
}
67+
===DONE===

0 commit comments

Comments
 (0)