Skip to content

Commit da0cff5

Browse files
committed
CSHARP-2589: Writes in transactions should ignore collection level write concern setting.
1 parent 6612755 commit da0cff5

File tree

6 files changed

+1639
-51
lines changed

6 files changed

+1639
-51
lines changed

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public override MongoCollectionSettings Settings
173173
}
174174
options = options ?? new BulkWriteOptions();
175175

176-
var operation = CreateBulkWriteOperation(requests, options);
176+
var operation = CreateBulkWriteOperation(session, requests, options);
177177
try
178178
{
179179
var result = ExecuteWriteOperation(session, operation, cancellationToken);
@@ -200,7 +200,7 @@ public override MongoCollectionSettings Settings
200200
}
201201
options = options ?? new BulkWriteOptions();
202202

203-
var operation = CreateBulkWriteOperation(requests, options);
203+
var operation = CreateBulkWriteOperation(session, requests, options);
204204
try
205205
{
206206
var result = await ExecuteWriteOperationAsync(session, operation, cancellationToken).ConfigureAwait(false);
@@ -747,8 +747,10 @@ private AggregateToCollectionOperation CreateAggregateToCollectionOperation<TRes
747747
};
748748
}
749749

750-
private BulkMixedWriteOperation CreateBulkWriteOperation(IEnumerable<WriteModel<TDocument>> requests, BulkWriteOptions options)
750+
private BulkMixedWriteOperation CreateBulkWriteOperation(IClientSessionHandle session, IEnumerable<WriteModel<TDocument>> requests, BulkWriteOptions options)
751751
{
752+
var effectiveWriteConcern = session.IsInTransaction ? WriteConcern.Acknowledged : _settings.WriteConcern;
753+
752754
return new BulkMixedWriteOperation(
753755
_collectionNamespace,
754756
requests.Select(ConvertWriteModelToWriteRequest),
@@ -757,7 +759,7 @@ private BulkMixedWriteOperation CreateBulkWriteOperation(IEnumerable<WriteModel<
757759
BypassDocumentValidation = options.BypassDocumentValidation,
758760
IsOrdered = options.IsOrdered,
759761
RetryRequested = _database.Client.Settings.RetryWrites,
760-
WriteConcern = _settings.WriteConcern
762+
WriteConcern = effectiveWriteConcern
761763
};
762764
}
763765

tests/MongoDB.Driver.Tests/Specifications/transactions/tests/README.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Server Fail Point
2121
=================
2222

2323
failCommand
24-
-----------
24+
```````````
2525

2626
Some tests depend on a server fail point, expressed in the ``failPoint`` field.
2727
For example the ``failCommand`` fail point allows the client to force the
@@ -207,6 +207,9 @@ Then for each element in ``tests``:
207207
create it explicitly.)
208208
#. If the YAML file contains a ``data`` array, insert the documents in ``data``
209209
into the test collection, using writeConcern "majority".
210+
#. When testing against a sharded cluster run a ``distinct`` command on the
211+
newly created collection on all mongoses. For an explanation see,
212+
`Why do tests that run distinct sometimes fail with StaleDbVersion?`_
210213
#. If ``failPoint`` is specified, its value is a configureFailPoint command.
211214
Run the command on the admin database to enable the fail point.
212215
#. Create a **new** MongoClient ``client``, with Command Monitoring listeners
@@ -496,9 +499,32 @@ manually.
496499

497500
.. _SERVER-39349: https://jira.mongodb.org/browse/SERVER-39349
498501

502+
Why do tests that run distinct sometimes fail with StaleDbVersion?
503+
``````````````````````````````````````````````````````````````````
504+
505+
When a shard receives its first command that contains a dbVersion, the shard
506+
returns a StaleDbVersion error and the Mongos retries the operation. In a
507+
sharded transaction, Mongos does not retry these operations and instead returns
508+
the error to the client. For example::
509+
510+
Command distinct failed: Transaction aa09e296-472a-494f-8334-48d57ab530b6:1 was aborted on statement 0 due to: an error from cluster data placement change :: caused by :: got stale databaseVersion response from shard sh01 at host localhost:27217 :: caused by :: don't know dbVersion.
511+
512+
To workaround this limitation, a driver test runner MUST run a
513+
non-transactional ``distinct`` command on each Mongos before running any test
514+
that uses ``distinct``. To ease the implementation drivers can simply run
515+
``distinct`` before *every* test.
516+
517+
Note that drivers can remove this workaround once `SERVER-39704`_ is resolved
518+
so that mongos retries this operation transparently. The ``distinct`` command
519+
is the only command allowed in a sharded transaction that uses the
520+
``dbVersion`` concept so it is the only command affected.
521+
522+
.. _SERVER-39704: https://jira.mongodb.org/browse/SERVER-39704
523+
499524
Changelog
500525
=========
501526

527+
:2019-03-25: Add workaround for StaleDbVersion on distinct.
502528
:2019-03-01: Add top-level ``runOn`` field to denote server version and/or
503529
topology requirements requirements for the test file. Removes the
504530
``topology`` top-level field, which is now expressed within

0 commit comments

Comments
 (0)