Skip to content

Commit 830b83a

Browse files
committed
CSHARP-4827: Don't add RetryableWriteError label on pre 4.4 mongos.
1 parent 211d76d commit 830b83a

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

src/MongoDB.Driver.Core/Core/Operations/RetryabilityHelper.cs

+33-23
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using MongoDB.Bson;
19+
using MongoDB.Driver.Core.Connections;
1920
using MongoDB.Driver.Core.Misc;
21+
using MongoDB.Driver.Core.Servers;
2022

2123
namespace MongoDB.Driver.Core.Operations
2224
{
@@ -90,9 +92,9 @@ static RetryabilityHelper()
9092
}
9193

9294
// public static methods
93-
public static void AddRetryableWriteErrorLabelIfRequired(MongoException exception, int maxWireVersion)
95+
public static void AddRetryableWriteErrorLabelIfRequired(MongoException exception, ConnectionDescription connectionDescription)
9496
{
95-
if (ShouldRetryableWriteExceptionLabelBeAdded(exception, maxWireVersion))
97+
if (ShouldRetryableWriteExceptionLabelBeAdded(exception, connectionDescription))
9698
{
9799
exception.AddErrorLabel(RetryableWriteErrorLabel);
98100
}
@@ -172,18 +174,22 @@ private static bool IsNetworkException(Exception exception)
172174
return exception is MongoConnectionException mongoConnectionException && mongoConnectionException.IsNetworkException;
173175
}
174176

175-
private static bool ShouldRetryableWriteExceptionLabelBeAdded(Exception exception, int maxWireVersion)
177+
private static bool ShouldRetryableWriteExceptionLabelBeAdded(Exception exception, ConnectionDescription connectionDescription)
176178
{
177179
if (IsNetworkException(exception))
178180
{
179181
return true;
180182
}
181183

184+
var maxWireVersion = connectionDescription.MaxWireVersion;
182185
if (Feature.ServerReturnsRetryableWriteErrorLabel.IsSupported(maxWireVersion))
183186
{
184187
return false;
185188
}
186189

190+
// on all servers from 4.4 on we would have returned false in the previous if statement
191+
// so from this point on we know we are connected to a pre 4.4 server
192+
187193
if (__retryableWriteExceptions.Contains(exception.GetType()))
188194
{
189195
return true;
@@ -199,29 +205,33 @@ private static bool ShouldRetryableWriteExceptionLabelBeAdded(Exception exceptio
199205
}
200206
}
201207

202-
var writeConcernException = exception as MongoWriteConcernException;
203-
if (writeConcernException != null)
208+
var serverType = connectionDescription.HelloResult.ServerType;
209+
if (serverType != ServerType.ShardRouter)
204210
{
205-
var writeConcernError = writeConcernException.WriteConcernResult.Response.GetValue("writeConcernError", null)?.AsBsonDocument;
206-
if (writeConcernError != null)
211+
var writeConcernException = exception as MongoWriteConcernException;
212+
if (writeConcernException != null)
207213
{
208-
var code = (ServerErrorCode)writeConcernError.GetValue("code", -1).AsInt32;
209-
switch (code)
214+
var writeConcernError = writeConcernException.WriteConcernResult.Response.GetValue("writeConcernError", null)?.AsBsonDocument;
215+
if (writeConcernError != null)
210216
{
211-
case ServerErrorCode.InterruptedAtShutdown:
212-
case ServerErrorCode.InterruptedDueToReplStateChange:
213-
case ServerErrorCode.LegacyNotPrimary:
214-
case ServerErrorCode.NotWritablePrimary:
215-
case ServerErrorCode.NotPrimaryNoSecondaryOk:
216-
case ServerErrorCode.NotPrimaryOrSecondary:
217-
case ServerErrorCode.PrimarySteppedDown:
218-
case ServerErrorCode.ShutdownInProgress:
219-
case ServerErrorCode.HostNotFound:
220-
case ServerErrorCode.HostUnreachable:
221-
case ServerErrorCode.NetworkTimeout:
222-
case ServerErrorCode.SocketException:
223-
case ServerErrorCode.ExceededTimeLimit:
224-
return true;
217+
var code = (ServerErrorCode)writeConcernError.GetValue("code", -1).AsInt32;
218+
switch (code)
219+
{
220+
case ServerErrorCode.InterruptedAtShutdown:
221+
case ServerErrorCode.InterruptedDueToReplStateChange:
222+
case ServerErrorCode.LegacyNotPrimary:
223+
case ServerErrorCode.NotWritablePrimary:
224+
case ServerErrorCode.NotPrimaryNoSecondaryOk:
225+
case ServerErrorCode.NotPrimaryOrSecondary:
226+
case ServerErrorCode.PrimarySteppedDown:
227+
case ServerErrorCode.ShutdownInProgress:
228+
case ServerErrorCode.HostNotFound:
229+
case ServerErrorCode.HostUnreachable:
230+
case ServerErrorCode.NetworkTimeout:
231+
case ServerErrorCode.SocketException:
232+
case ServerErrorCode.ExceededTimeLimit:
233+
return true;
234+
}
225235
}
226236
}
227237
}

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public TCommandResult Execute(IConnection connection, CancellationToken cancella
147147
}
148148
catch (Exception exception)
149149
{
150-
AddErrorLabelIfRequired(exception, connection.Description?.MaxWireVersion);
150+
AddErrorLabelIfRequired(exception, connection.Description);
151151

152152
TransactionHelper.UnpinServerIfNeededOnCommandException(_session, exception);
153153
throw;
@@ -201,15 +201,15 @@ public async Task<TCommandResult> ExecuteAsync(IConnection connection, Cancellat
201201
}
202202
catch (Exception exception)
203203
{
204-
AddErrorLabelIfRequired(exception, connection.Description?.MaxWireVersion);
204+
AddErrorLabelIfRequired(exception, connection.Description);
205205

206206
TransactionHelper.UnpinServerIfNeededOnCommandException(_session, exception);
207207
throw;
208208
}
209209
}
210210

211211
// private methods
212-
private void AddErrorLabelIfRequired(Exception exception, int? maxWireVersion)
212+
private void AddErrorLabelIfRequired(Exception exception, ConnectionDescription connectionDescription)
213213
{
214214
if (exception is MongoException mongoException)
215215
{
@@ -218,9 +218,9 @@ private void AddErrorLabelIfRequired(Exception exception, int? maxWireVersion)
218218
mongoException.AddErrorLabel("TransientTransactionError");
219219
}
220220

221-
if (RetryabilityHelper.IsCommandRetryable(_command) && maxWireVersion.HasValue)
221+
if (RetryabilityHelper.IsCommandRetryable(_command) && connectionDescription != null)
222222
{
223-
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(mongoException, maxWireVersion.Value);
223+
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(mongoException, connectionDescription);
224224
}
225225
}
226226
}

tests/MongoDB.Driver.Core.Tests/Core/Operations/RetryabilityHelperTests.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using MongoDB.Driver.Core.Misc;
2222
using MongoDB.Driver.Core.TestHelpers;
2323
using Xunit;
24+
using MongoDB.Driver.Core.Connections;
2425

2526
namespace MongoDB.Driver.Core.Operations
2627
{
@@ -45,8 +46,10 @@ public class RetryabilityHelperTests
4546
public void AddRetryableWriteErrorLabelIfRequired_should_add_RetryableWriteError_for_MongoWriteConcernException_when_required(int errorCode, bool shouldAddErrorLabel)
4647
{
4748
var exception = CoreExceptionHelper.CreateMongoWriteConcernException(BsonDocument.Parse($"{{ writeConcernError : {{ code : {errorCode} }} }}"));
49+
var maxWireVersion = Feature.ServerReturnsRetryableWriteErrorLabel.LastNotSupportedWireVersion;
50+
var connectionDescription = OperationTestHelper.CreateConnectionDescription(maxWireVersion);
4851

49-
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, Feature.ServerReturnsRetryableWriteErrorLabel.LastNotSupportedWireVersion);
52+
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, connectionDescription);
5053

5154
var hasRetryableWriteErrorLabel = exception.HasErrorLabel("RetryableWriteError");
5255
hasRetryableWriteErrorLabel.Should().Be(shouldAddErrorLabel);
@@ -59,8 +62,9 @@ public void AddRetryableWriteErrorLabelIfRequired_should_add_RetryableWriteError
5962
var exception = (MongoException)CoreExceptionHelper.CreateException(typeof(MongoConnectionException));
6063
var feature = Feature.ServerReturnsRetryableWriteErrorLabel;
6164
var wireVersion = serverReturnsRetryableWriteErrorLabel ? feature.FirstSupportedWireVersion : feature.LastNotSupportedWireVersion;
65+
var connectionDescription = OperationTestHelper.CreateConnectionDescription(wireVersion);
6266

63-
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, wireVersion);
67+
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, connectionDescription);
6468

6569
var hasRetryableWriteErrorLabel = exception.HasErrorLabel("RetryableWriteError");
6670
hasRetryableWriteErrorLabel.Should().BeTrue();
@@ -89,8 +93,10 @@ public void AddRetryableWriteErrorLabelIfRequired_should_add_RetryableWriteError
8993
{
9094
exception = CoreExceptionHelper.CreateMongoCommandException((int)exceptionDescription);
9195
}
96+
var maxWireVersion = Feature.ServerReturnsRetryableWriteErrorLabel.LastNotSupportedWireVersion;
97+
var connectionDescription = OperationTestHelper.CreateConnectionDescription(maxWireVersion);
9298

93-
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, Feature.ServerReturnsRetryableWriteErrorLabel.LastNotSupportedWireVersion);
99+
RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, connectionDescription);
94100

95101
var hasRetryableWriteErrorLabel = exception.HasErrorLabel("RetryableWriteError");
96102
hasRetryableWriteErrorLabel.Should().Be(shouldAddErrorLabel);

0 commit comments

Comments
 (0)