19
19
import com .mongodb .MongoClientSettings ;
20
20
import com .mongodb .MongoCommandException ;
21
21
import com .mongodb .MongoNamespace ;
22
+ import com .mongodb .MongoWriteConcernException ;
22
23
import com .mongodb .ReadPreference ;
23
24
import com .mongodb .ServerCursor ;
24
25
import com .mongodb .WriteConcern ;
34
35
import com .mongodb .internal .bulk .UpdateRequest ;
35
36
import com .mongodb .internal .bulk .WriteRequest ;
36
37
import com .mongodb .internal .client .model .AggregationLevel ;
38
+ import com .mongodb .internal .diagnostics .logging .Logger ;
39
+ import com .mongodb .internal .diagnostics .logging .Loggers ;
37
40
import com .mongodb .internal .operation .AggregateOperation ;
38
41
import com .mongodb .internal .operation .BatchCursor ;
39
42
import com .mongodb .internal .operation .CommandReadOperation ;
70
73
import static java .util .Collections .singletonList ;
71
74
72
75
public final class CollectionHelper <T > {
76
+ private static final Logger LOGGER = Loggers .getLogger ("test" );
73
77
74
78
private final Codec <T > codec ;
75
79
private final CodecRegistry registry = MongoClientSettings .getDefaultCodecRegistry ();
@@ -89,7 +93,18 @@ public static void drop(final MongoNamespace namespace) {
89
93
}
90
94
91
95
public static void drop (final MongoNamespace namespace , final WriteConcern writeConcern ) {
92
- new DropCollectionOperation (namespace , writeConcern ).execute (getBinding ());
96
+ // This loop is a workaround for unanticipated failures of the drop command when run on a sharded cluster < 4.2.
97
+ // In practice the command tends to succeed on the first attempt after a failure
98
+ boolean success = false ;
99
+ while (!success ) {
100
+ try {
101
+ new DropCollectionOperation (namespace , writeConcern ).execute (getBinding ());
102
+ success = true ;
103
+ } catch (MongoWriteConcernException e ) {
104
+ LOGGER .info ("Retrying drop collection after a write concern error: " + e );
105
+ // repeat until success!
106
+ }
107
+ }
93
108
}
94
109
95
110
public static void dropDatabase (final String name ) {
@@ -159,7 +174,23 @@ public void create(final String collectionName, final CreateCollectionOptions op
159
174
if (validationOptions .getValidationAction () != null ) {
160
175
operation .validationAction (validationOptions .getValidationAction ());
161
176
}
162
- operation .execute (getBinding ());
177
+
178
+ // This loop is a workaround for unanticipated failures of the create command when run on a sharded cluster < 4.2
179
+ // In practice the command tends to succeed on the first attempt after a failure
180
+ boolean success = false ;
181
+ while (!success ) {
182
+ try {
183
+ operation .execute (getBinding ());
184
+ success = true ;
185
+ } catch (MongoCommandException e ) {
186
+ if ("Interrupted" .equals (e .getErrorCodeName ())) {
187
+ LOGGER .info ("Retrying create collection after a write concern error: " + e );
188
+ // repeat until success!
189
+ } else {
190
+ throw e ;
191
+ }
192
+ }
193
+ }
163
194
}
164
195
165
196
public void killCursor (final MongoNamespace namespace , final ServerCursor serverCursor ) {
@@ -399,7 +430,7 @@ public List<BsonDocument> listIndexes(){
399
430
return indexes ;
400
431
}
401
432
402
- public void killAllSessions () {
433
+ public static void killAllSessions () {
403
434
try {
404
435
new CommandReadOperation <>("admin" , new BsonDocument ("killAllSessions" , new BsonArray ()),
405
436
new BsonDocumentCodec ()).execute (getBinding ());
0 commit comments