1616using System ;
1717using System . Collections . Generic ;
1818using MongoDB . Bson ;
19+ using MongoDB . Driver . Core . Connections ;
1920using MongoDB . Driver . Core . Misc ;
21+ using MongoDB . Driver . Core . Servers ;
2022
2123namespace 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 }
0 commit comments