Skip to content

Commit 230369c

Browse files
authored
CDRIVER-4110: Default for causal_consistency depends on snapshot (#838)
* Don't force casualConsistency=false when creating session with snapshot=true This was originally added in d5f73e1 * Relocate session snapshot prose test This prose test is defined in the sessions spec, not transactions. It also does not require the server to support transactions. * Default for causal_consistency depends on snapshot Use mongoc_optional_t for causal_consistency and snapshot session opts to track whether or not values have been set. This also removes explicitly setting causal_consistency to true in _mongoc_client_session_new, since mongoc_session_opts_get_causal_consistency now enforces the default value.
1 parent 08f9542 commit 230369c

File tree

5 files changed

+87
-67
lines changed

5 files changed

+87
-67
lines changed

src/libmongoc/src/mongoc/mongoc-client-session-private.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@ struct _mongoc_transaction_opt_t {
3838
int64_t max_commit_time_ms;
3939
};
4040

41-
typedef enum {
42-
MONGOC_SESSION_NO_OPTS = 0,
43-
MONGOC_SESSION_CAUSAL_CONSISTENCY = (1 << 0),
44-
MONGOC_SESSION_SNAPSHOT = (2 << 0),
45-
} mongoc_session_flag_t;
46-
4741
struct _mongoc_session_opt_t {
48-
mongoc_session_flag_t flags;
42+
mongoc_optional_t causal_consistency;
43+
mongoc_optional_t snapshot;
4944
mongoc_transaction_opt_t default_txn_opts;
5045
};
5146

src/libmongoc/src/mongoc/mongoc-client-session.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,14 @@ mongoc_session_opts_get_causal_consistency (const mongoc_session_opt_t *opts)
403403

404404
BSON_ASSERT (opts);
405405

406-
RETURN (!!(opts->flags & MONGOC_SESSION_CAUSAL_CONSISTENCY));
406+
/* Causal Consistency spec: If no value is provided for causalConsistency
407+
* and snapshot reads are not requested a value of true is implied. */
408+
if (!mongoc_optional_is_set (&opts->causal_consistency) &&
409+
!mongoc_optional_value (&opts->snapshot)) {
410+
RETURN (true);
411+
}
412+
413+
RETURN (mongoc_optional_value (&opts->causal_consistency));
407414
}
408415

409416
bool
@@ -413,7 +420,7 @@ mongoc_session_opts_get_snapshot (const mongoc_session_opt_t *opts)
413420

414421
BSON_ASSERT (opts);
415422

416-
RETURN (!!(opts->flags & MONGOC_SESSION_SNAPSHOT));
423+
RETURN (mongoc_optional_value (&opts->snapshot));
417424
}
418425

419426
void
@@ -424,11 +431,7 @@ mongoc_session_opts_set_causal_consistency (mongoc_session_opt_t *opts,
424431

425432
BSON_ASSERT (opts);
426433

427-
if (causal_consistency) {
428-
opts->flags |= MONGOC_SESSION_CAUSAL_CONSISTENCY;
429-
} else {
430-
opts->flags &= ~MONGOC_SESSION_CAUSAL_CONSISTENCY;
431-
}
434+
mongoc_optional_set_value (&opts->causal_consistency, causal_consistency);
432435

433436
EXIT;
434437
}
@@ -440,11 +443,7 @@ mongoc_session_opts_set_snapshot (mongoc_session_opt_t *opts, bool snapshot)
440443

441444
BSON_ASSERT (opts);
442445

443-
if (snapshot) {
444-
opts->flags |= MONGOC_SESSION_SNAPSHOT;
445-
} else {
446-
opts->flags &= ~MONGOC_SESSION_SNAPSHOT;
447-
}
446+
mongoc_optional_set_value (&opts->snapshot, snapshot);
448447

449448
EXIT;
450449
}
@@ -454,11 +453,8 @@ mongoc_session_opts_new (void)
454453
{
455454
mongoc_session_opt_t *opts = bson_malloc0 (sizeof (mongoc_session_opt_t));
456455

457-
/* Driver Sessions Spec: causal consistency is true by default */
458-
mongoc_session_opts_set_causal_consistency (opts, true);
459-
460-
/* Snapshot Reads Spec: snapshot is false by default */
461-
mongoc_session_opts_set_snapshot (opts, false);
456+
mongoc_optional_init (&opts->causal_consistency);
457+
mongoc_optional_init (&opts->snapshot);
462458

463459
return opts;
464460
}
@@ -513,7 +509,8 @@ static void
513509
_mongoc_session_opts_copy (const mongoc_session_opt_t *src,
514510
mongoc_session_opt_t *dst)
515511
{
516-
dst->flags = src->flags;
512+
mongoc_optional_copy (&src->causal_consistency, &dst->causal_consistency);
513+
mongoc_optional_copy (&src->snapshot, &dst->snapshot);
517514
txn_opts_copy (&src->default_txn_opts, &dst->default_txn_opts);
518515
}
519516

@@ -800,22 +797,22 @@ _mongoc_client_session_new (mongoc_client_t *client,
800797
session->client_session_id = client_session_id;
801798
bson_init (&session->cluster_time);
802799

800+
mongoc_optional_init (&session->opts.causal_consistency);
801+
mongoc_optional_init (&session->opts.snapshot);
803802
txn_opts_set (&session->opts.default_txn_opts,
804803
client->read_concern,
805804
client->write_concern,
806805
client->read_prefs,
807806
DEFAULT_MAX_COMMIT_TIME_MS);
808807

809808
if (opts) {
810-
session->opts.flags = opts->flags;
809+
mongoc_optional_copy (&opts->causal_consistency, &session->opts.causal_consistency);
810+
mongoc_optional_copy (&opts->snapshot, &session->opts.snapshot);
811811
txn_opts_set (&session->opts.default_txn_opts,
812812
opts->default_txn_opts.read_concern,
813813
opts->default_txn_opts.write_concern,
814814
opts->default_txn_opts.read_prefs,
815815
opts->default_txn_opts.max_commit_time_ms);
816-
} else {
817-
/* sessions are causally consistent by default */
818-
session->opts.flags = MONGOC_SESSION_CAUSAL_CONSISTENCY;
819816
}
820817

821818
/* snapshot_time_set is false by default */

src/libmongoc/tests/test-mongoc-client-session.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_session_opts_clone (void)
3434

3535
opts = mongoc_session_opts_new ();
3636
clone = mongoc_session_opts_clone (opts);
37-
/* causal is enabled by default */
37+
/* causalConsistency is enabled by default if snapshot is not enabled */
3838
BSON_ASSERT (mongoc_session_opts_get_causal_consistency (clone));
3939
mongoc_session_opts_destroy (clone);
4040

@@ -47,6 +47,31 @@ test_session_opts_clone (void)
4747
}
4848

4949

50+
static void
51+
test_session_opts_causal_consistency_and_snapshot (void)
52+
{
53+
mongoc_session_opt_t *opts;
54+
55+
opts = mongoc_session_opts_new ();
56+
/* causalConsistency is enabled by default if snapshot is not enabled */
57+
BSON_ASSERT (mongoc_session_opts_get_causal_consistency (opts));
58+
BSON_ASSERT (!mongoc_session_opts_get_snapshot (opts));
59+
60+
/* causalConsistency is disabled by default if snapshot is enabled */
61+
mongoc_session_opts_set_snapshot (opts, true);
62+
BSON_ASSERT (!mongoc_session_opts_get_causal_consistency (opts));
63+
BSON_ASSERT (mongoc_session_opts_get_snapshot (opts));
64+
65+
/* causalConsistency and snapshot can both be enabled, although this will
66+
* result in an error when starting the session. */
67+
mongoc_session_opts_set_causal_consistency (opts, true);
68+
BSON_ASSERT (mongoc_session_opts_get_causal_consistency (opts));
69+
BSON_ASSERT (mongoc_session_opts_get_snapshot (opts));
70+
71+
mongoc_session_opts_destroy (opts);
72+
}
73+
74+
5075
static void
5176
test_session_no_crypto (void *ctx)
5277
{
@@ -2707,12 +2732,44 @@ test_session_dirty (void *unused)
27072732
_test_session_dirty_helper (false /* retry succceeds */);
27082733
}
27092734

2735+
void
2736+
test_sessions_snapshot_prose_test_1 (void *ctx)
2737+
{
2738+
mongoc_client_t *client = NULL;
2739+
mongoc_session_opt_t *session_opts = NULL;
2740+
bson_error_t error;
2741+
bool r;
2742+
2743+
client = test_framework_new_default_client ();
2744+
BSON_ASSERT (client);
2745+
2746+
session_opts = mongoc_session_opts_new ();
2747+
mongoc_session_opts_set_causal_consistency (session_opts, true);
2748+
mongoc_session_opts_set_snapshot (session_opts, true);
2749+
2750+
/* assert that starting session with causal consistency and snapshot enabled
2751+
* results in an error. */
2752+
r = mongoc_client_start_session (client, session_opts, &error);
2753+
ASSERT (!r);
2754+
ASSERT_ERROR_CONTAINS (
2755+
error,
2756+
MONGOC_ERROR_CLIENT,
2757+
MONGOC_ERROR_CLIENT_SESSION_FAILURE,
2758+
"Only one of causal consistency and snapshot can be enabled.");
2759+
2760+
mongoc_session_opts_destroy (session_opts);
2761+
mongoc_client_destroy (client);
2762+
}
2763+
27102764
void
27112765
test_session_install (TestSuite *suite)
27122766
{
27132767
char resolved[PATH_MAX];
27142768

27152769
TestSuite_Add (suite, "/Session/opts/clone", test_session_opts_clone);
2770+
TestSuite_Add (suite,
2771+
"/Session/opts/causal_consistency_and_snapshot",
2772+
test_session_opts_causal_consistency_and_snapshot);
27162773
TestSuite_AddFull (suite,
27172774
"/Session/no_crypto",
27182775
test_session_no_crypto,
@@ -3079,4 +3136,12 @@ test_session_install (TestSuite *suite)
30793136
test_framework_skip_if_no_failpoint,
30803137
/* Tests with retryable writes, requires non-standalone. */
30813138
test_framework_skip_if_single);
3139+
3140+
TestSuite_AddFull (suite,
3141+
"/Session/snapshot/prose_test_1",
3142+
test_sessions_snapshot_prose_test_1,
3143+
NULL,
3144+
NULL,
3145+
test_framework_skip_if_no_sessions,
3146+
test_framework_skip_if_no_crypto);
30823147
}

src/libmongoc/tests/test-mongoc-transactions.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,35 +1166,6 @@ test_max_commit_time_ms_is_reset (void *ctx)
11661166
mock_rs_destroy (rs);
11671167
}
11681168

1169-
void
1170-
test_snapshot_session_prose_1 (void *ctx)
1171-
{
1172-
mongoc_client_t *client = NULL;
1173-
mongoc_session_opt_t *session_opts = NULL;
1174-
bson_error_t error;
1175-
bool r;
1176-
1177-
client = test_framework_new_default_client ();
1178-
BSON_ASSERT (client);
1179-
1180-
session_opts = mongoc_session_opts_new ();
1181-
mongoc_session_opts_set_causal_consistency (session_opts, true);
1182-
mongoc_session_opts_set_snapshot (session_opts, true);
1183-
1184-
/* assert that starting session with causal consistency and snapshot enabled
1185-
* results in an error. */
1186-
r = mongoc_client_start_session (client, session_opts, &error);
1187-
ASSERT (!r);
1188-
ASSERT_ERROR_CONTAINS (
1189-
error,
1190-
MONGOC_ERROR_CLIENT,
1191-
MONGOC_ERROR_CLIENT_SESSION_FAILURE,
1192-
"Only one of causal consistency and snapshot can be enabled.");
1193-
1194-
mongoc_session_opts_destroy (session_opts);
1195-
mongoc_client_destroy (client);
1196-
}
1197-
11981169
void
11991170
test_transactions_install (TestSuite *suite)
12001171
{
@@ -1286,10 +1257,4 @@ test_transactions_install (TestSuite *suite)
12861257
NULL,
12871258
NULL,
12881259
test_framework_skip_if_no_crypto);
1289-
TestSuite_AddFull (suite,
1290-
"/transactions/snapshot_sessions_prose_1",
1291-
test_snapshot_session_prose_1,
1292-
NULL,
1293-
NULL,
1294-
test_framework_skip_if_no_txns);
12951260
}

src/libmongoc/tests/unified/entity-map.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,6 @@ session_opts_new (bson_t *bson, bson_error_t *error)
597597
mongoc_session_opts_set_causal_consistency (opts, *causal_consistency);
598598
}
599599
if (snapshot) {
600-
/* Set causal consistency to false to allow snapshot */
601-
mongoc_session_opts_set_causal_consistency (opts, false);
602600
mongoc_session_opts_set_snapshot (opts, *snapshot);
603601
}
604602

0 commit comments

Comments
 (0)