16
16
using System ;
17
17
using System . Collections . Generic ;
18
18
using MongoDB . Bson ;
19
+ using MongoDB . Driver . Core . Connections ;
19
20
using MongoDB . Driver . Core . Misc ;
21
+ using MongoDB . Driver . Core . Servers ;
20
22
21
23
namespace MongoDB . Driver . Core . Operations
22
24
{
@@ -90,9 +92,9 @@ static RetryabilityHelper()
90
92
}
91
93
92
94
// public static methods
93
- public static void AddRetryableWriteErrorLabelIfRequired ( MongoException exception , int maxWireVersion )
95
+ public static void AddRetryableWriteErrorLabelIfRequired ( MongoException exception , ConnectionDescription connectionDescription )
94
96
{
95
- if ( ShouldRetryableWriteExceptionLabelBeAdded ( exception , maxWireVersion ) )
97
+ if ( ShouldRetryableWriteExceptionLabelBeAdded ( exception , connectionDescription ) )
96
98
{
97
99
exception . AddErrorLabel ( RetryableWriteErrorLabel ) ;
98
100
}
@@ -172,18 +174,22 @@ private static bool IsNetworkException(Exception exception)
172
174
return exception is MongoConnectionException mongoConnectionException && mongoConnectionException . IsNetworkException ;
173
175
}
174
176
175
- private static bool ShouldRetryableWriteExceptionLabelBeAdded ( Exception exception , int maxWireVersion )
177
+ private static bool ShouldRetryableWriteExceptionLabelBeAdded ( Exception exception , ConnectionDescription connectionDescription )
176
178
{
177
179
if ( IsNetworkException ( exception ) )
178
180
{
179
181
return true ;
180
182
}
181
183
184
+ var maxWireVersion = connectionDescription . MaxWireVersion ;
182
185
if ( Feature . ServerReturnsRetryableWriteErrorLabel . IsSupported ( maxWireVersion ) )
183
186
{
184
187
return false ;
185
188
}
186
189
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
+
187
193
if ( __retryableWriteExceptions . Contains ( exception . GetType ( ) ) )
188
194
{
189
195
return true ;
@@ -199,29 +205,33 @@ private static bool ShouldRetryableWriteExceptionLabelBeAdded(Exception exceptio
199
205
}
200
206
}
201
207
202
- var writeConcernException = exception as MongoWriteConcernException ;
203
- if ( writeConcernException != null )
208
+ var serverType = connectionDescription . HelloResult . ServerType ;
209
+ if ( serverType != ServerType . ShardRouter )
204
210
{
205
- var writeConcernError = writeConcernException . WriteConcernResult . Response . GetValue ( "writeConcernError" , null ) ? . AsBsonDocument ;
206
- if ( writeConcernError != null )
211
+ var writeConcernException = exception as MongoWriteConcernException ;
212
+ if ( writeConcernException != null )
207
213
{
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 )
210
216
{
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
+ }
225
235
}
226
236
}
227
237
}
0 commit comments