Skip to content

Commit 6b4eb7c

Browse files
committed
PHPC-1889: Session snapshot option
1 parent aceae37 commit 6b4eb7c

10 files changed

+105
-0
lines changed

src/MongoDB/Manager.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,18 @@ static PHP_METHOD(Manager, startSession)
805805
}
806806
}
807807

808+
if (options && php_array_existsc(options, "snapshot")) {
809+
if (!cs_opts) {
810+
cs_opts = mongoc_session_opts_new();
811+
}
812+
mongoc_session_opts_set_snapshot(cs_opts, php_array_fetchc_bool(options, "snapshot"));
813+
}
814+
815+
if (cs_opts && mongoc_session_opts_get_causal_consistency(cs_opts) && mongoc_session_opts_get_snapshot(cs_opts)) {
816+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Only one of \"causalConsistency\" and \"snapshot\" can be enabled");
817+
goto cleanup;
818+
}
819+
808820
/* If the Manager was created in a different process, reset the client so
809821
* that its session pool is cleared. This will ensure that we do not re-use
810822
* a server session (i.e. LSID) created by a parent process. */

src/MongoDB/Session.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,10 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
740740
if (intern->client_session) {
741741
const mongoc_session_opt_t* cs_opts = mongoc_client_session_get_opts(intern->client_session);
742742
ADD_ASSOC_BOOL_EX(&retval, "causalConsistency", mongoc_session_opts_get_causal_consistency(cs_opts));
743+
ADD_ASSOC_BOOL_EX(&retval, "snapshot", mongoc_session_opts_get_snapshot(cs_opts));
743744
} else {
744745
ADD_ASSOC_NULL_EX(&retval, "causalConsistency");
746+
ADD_ASSOC_NULL_EX(&retval, "snapshot");
745747
}
746748

747749
if (intern->client_session) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::startSession() snapshot and causalConsistency cannot both be true
3+
--DESCRIPTION--
4+
Session spec prose test #1
5+
https://github.com/mongodb/specifications/blob/master/source/sessions/tests/README.rst#prose-tests
6+
--SKIPIF--
7+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
8+
<?php skip_if_not_libmongoc_crypto(); ?>
9+
<?php skip_if_not_live(); ?>
10+
<?php skip_if_server_version('<', '3.6'); ?>
11+
--FILE--
12+
<?php
13+
require_once __DIR__ . "/../utils/basic.inc";
14+
15+
$manager = create_test_manager();
16+
17+
echo throws(function() use ($manager) {
18+
$manager->startSession([
19+
'causalConsistency' => true,
20+
'snapshot' => true,
21+
]);
22+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Only one of "causalConsistency" and "snapshot" can be enabled
30+
===DONE===

tests/session/session-debug-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3333
NULL
3434
["causalConsistency"]=>
3535
bool(true)
36+
["snapshot"]=>
37+
bool(false)
3638
["operationTime"]=>
3739
NULL
3840
["server"]=>

tests/session/session-debug-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
4646
}
4747
["causalConsistency"]=>
4848
bool(true)
49+
["snapshot"]=>
50+
bool(false)
4951
["operationTime"]=>
5052
object(MongoDB\BSON\Timestamp)#%d (%d) {
5153
["increment"]=>

tests/session/session-debug-003.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3333
NULL
3434
["causalConsistency"]=>
3535
bool(false)
36+
["snapshot"]=>
37+
bool(false)
3638
["operationTime"]=>
3739
NULL
3840
["server"]=>

tests/session/session-debug-004.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3030
NULL
3131
["causalConsistency"]=>
3232
NULL
33+
["snapshot"]=>
34+
NULL
3335
["operationTime"]=>
3436
NULL
3537
["server"]=>

tests/session/session-debug-005.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
5353
}
5454
["causalConsistency"]=>
5555
bool(true)
56+
["snapshot"]=>
57+
bool(false)
5658
["operationTime"]=>
5759
object(MongoDB\BSON\Timestamp)#%d (%d) {
5860
["increment"]=>

tests/session/session-debug-006.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
4141
NULL
4242
["causalConsistency"]=>
4343
bool(true)
44+
["snapshot"]=>
45+
bool(false)
4446
["operationTime"]=>
4547
NULL
4648
["server"]=>

tests/session/session-debug-007.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
MongoDB\Driver\Session debug output (snapshot=true)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_not_live(); ?>
7+
<?php skip_if_server_version('<', '3.6'); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = create_test_manager();
13+
$session = $manager->startSession(['snapshot' => true]);
14+
15+
var_dump($session);
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECTF--
21+
object(MongoDB\Driver\Session)#%d (%d) {
22+
["logicalSessionId"]=>
23+
array(1) {
24+
["id"]=>
25+
object(MongoDB\BSON\Binary)#%d (%d) {
26+
["data"]=>
27+
string(16) "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
28+
["type"]=>
29+
int(4)
30+
}
31+
}
32+
["clusterTime"]=>
33+
NULL
34+
["causalConsistency"]=>
35+
bool(false)
36+
["snapshot"]=>
37+
bool(true)
38+
["operationTime"]=>
39+
NULL
40+
["server"]=>
41+
NULL
42+
["inTransaction"]=>
43+
bool(false)
44+
["transactionState"]=>
45+
string(4) "none"
46+
["transactionOptions"]=>
47+
NULL
48+
}
49+
===DONE===

0 commit comments

Comments
 (0)