forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClusterFixture.java
758 lines (655 loc) · 32.6 KB
/
ClusterFixture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import com.mongodb.async.FutureResultCallback;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterDescription;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ClusterType;
import com.mongodb.connection.ConnectionPoolSettings;
import com.mongodb.connection.NettyTransportSettings;
import com.mongodb.connection.ServerDescription;
import com.mongodb.connection.ServerSettings;
import com.mongodb.connection.ServerVersion;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.connection.TransportSettings;
import com.mongodb.internal.IgnorableRequestContext;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.binding.AsyncClusterBinding;
import com.mongodb.internal.binding.AsyncConnectionSource;
import com.mongodb.internal.binding.AsyncReadBinding;
import com.mongodb.internal.binding.AsyncReadWriteBinding;
import com.mongodb.internal.binding.AsyncSessionBinding;
import com.mongodb.internal.binding.AsyncSingleConnectionBinding;
import com.mongodb.internal.binding.AsyncWriteBinding;
import com.mongodb.internal.binding.ClusterBinding;
import com.mongodb.internal.binding.ReadWriteBinding;
import com.mongodb.internal.binding.ReferenceCounted;
import com.mongodb.internal.binding.SessionBinding;
import com.mongodb.internal.binding.SingleConnectionBinding;
import com.mongodb.internal.connection.AsyncConnection;
import com.mongodb.internal.connection.AsynchronousSocketChannelStreamFactory;
import com.mongodb.internal.connection.Cluster;
import com.mongodb.internal.connection.DefaultClusterFactory;
import com.mongodb.internal.connection.DefaultInetAddressResolver;
import com.mongodb.internal.connection.InternalConnectionPoolSettings;
import com.mongodb.internal.connection.MongoCredentialWithCache;
import com.mongodb.internal.connection.SocketStreamFactory;
import com.mongodb.internal.connection.StreamFactory;
import com.mongodb.internal.connection.StreamFactoryFactory;
import com.mongodb.internal.connection.TlsChannelStreamFactoryFactory;
import com.mongodb.internal.connection.netty.NettyStreamFactoryFactory;
import com.mongodb.internal.operation.AsyncReadOperation;
import com.mongodb.internal.operation.AsyncWriteOperation;
import com.mongodb.internal.operation.BatchCursor;
import com.mongodb.internal.operation.CommandReadOperation;
import com.mongodb.internal.operation.DropDatabaseOperation;
import com.mongodb.internal.operation.ReadOperation;
import com.mongodb.internal.operation.WriteOperation;
import com.mongodb.lang.Nullable;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.DocumentCodec;
import javax.net.ssl.SSLException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.connection.ClusterConnectionMode.LOAD_BALANCED;
import static com.mongodb.connection.ClusterConnectionMode.MULTIPLE;
import static com.mongodb.connection.ClusterType.REPLICA_SET;
import static com.mongodb.connection.ClusterType.SHARDED;
import static com.mongodb.connection.ClusterType.STANDALONE;
import static com.mongodb.connection.ClusterType.UNKNOWN;
import static com.mongodb.internal.connection.ClusterDescriptionHelper.getPrimaries;
import static com.mongodb.internal.connection.ClusterDescriptionHelper.getSecondaries;
import static com.mongodb.internal.thread.InterruptionUtil.interruptAndCreateMongoInterruptedException;
import static java.lang.String.format;
import static java.lang.Thread.sleep;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
/**
* Helper class for the acceptance tests. Used primarily by DatabaseTestCase and FunctionalSpecification. This fixture allows Test
* super-classes to share functionality whilst minimising duplication.
*/
public final class ClusterFixture {
public static final String DEFAULT_URI = "mongodb://localhost:27017";
public static final String MONGODB_URI_SYSTEM_PROPERTY_NAME = "org.mongodb.test.uri";
public static final String MONGODB_API_VERSION = "org.mongodb.test.api.version";
public static final String MONGODB_MULTI_MONGOS_URI_SYSTEM_PROPERTY_NAME = "org.mongodb.test.multi.mongos.uri";
public static final String SERVERLESS_TEST_SYSTEM_PROPERTY_NAME = "org.mongodb.test.serverless";
public static final String DATA_LAKE_TEST_SYSTEM_PROPERTY_NAME = "org.mongodb.test.data.lake";
public static final String ATLAS_SEARCH_TEST_SYSTEM_PROPERTY_NAME = "org.mongodb.test.atlas.search";
private static final String MONGODB_OCSP_SHOULD_SUCCEED = "org.mongodb.test.ocsp.tls.should.succeed";
private static final String DEFAULT_DATABASE_NAME = "JavaDriverTest";
private static final int COMMAND_NOT_FOUND_ERROR_CODE = 59;
public static final long TIMEOUT = 60L;
public static final Duration TIMEOUT_DURATION = Duration.ofMinutes(1);
public static final String LEGACY_HELLO = "isMaster";
private static ConnectionString connectionString;
private static Cluster cluster;
private static Cluster asyncCluster;
private static final Map<ReadPreference, ReadWriteBinding> BINDING_MAP = new HashMap<>();
private static final Map<ReadPreference, AsyncReadWriteBinding> ASYNC_BINDING_MAP = new HashMap<>();
private static ServerVersion serverVersion;
private static BsonDocument serverParameters;
private static NettyTransportSettings nettyTransportSettings;
static {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
}
private ClusterFixture() {
}
public static String getDefaultDatabaseName() {
return DEFAULT_DATABASE_NAME;
}
public static boolean clusterIsType(final ClusterType clusterType) {
return getClusterDescription(getCluster()).getType() == clusterType;
}
public static ClusterDescription getClusterDescription(final Cluster cluster) {
try {
ClusterDescription clusterDescription = cluster.getCurrentDescription();
while (clusterDescription.getType() == UNKNOWN) {
Thread.sleep(10);
clusterDescription = cluster.getCurrentDescription();
}
return clusterDescription;
} catch (InterruptedException e) {
throw interruptAndCreateMongoInterruptedException("Interrupted", e);
}
}
public static ServerVersion getServerVersion() {
if (serverVersion == null) {
serverVersion = getVersion(new CommandReadOperation<>("admin",
new BsonDocument("buildInfo", new BsonInt32(1)), new BsonDocumentCodec())
.execute(new ClusterBinding(getCluster(), ReadPreference.nearest(), ReadConcern.DEFAULT, getServerApi(),
IgnorableRequestContext.INSTANCE)));
}
return serverVersion;
}
private static ServerVersion getVersion(final BsonDocument buildInfoResult) {
List<BsonValue> versionArray = buildInfoResult.getArray("versionArray").subList(0, 3);
return new ServerVersion(asList(versionArray.get(0).asInt32().getValue(),
versionArray.get(1).asInt32().getValue(),
versionArray.get(2).asInt32().getValue()));
}
public static boolean serverVersionAtLeast(final int majorVersion, final int minorVersion) {
return getServerVersion().compareTo(new ServerVersion(asList(majorVersion, minorVersion, 0))) >= 0;
}
public static boolean serverVersionLessThan(final int majorVersion, final int minorVersion) {
return getServerVersion().compareTo(new ServerVersion(asList(majorVersion, minorVersion, 0))) < 0;
}
public static List<Integer> getVersionList(final String versionString) {
List<Integer> versionList = new ArrayList<>();
for (String s : versionString.split("\\.")) {
versionList.add(Integer.valueOf(s));
}
while (versionList.size() < 3) {
versionList.add(0);
}
return versionList;
}
public static boolean hasEncryptionTestsEnabled() {
List<String> requiredSystemProperties = asList("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AZURE_TENANT_ID", "AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET", "GCP_EMAIL", "GCP_PRIVATE_KEY", "AWS_TEMP_ACCESS_KEY_ID", "AWS_TEMP_SECRET_ACCESS_KEY",
"AWS_TEMP_SESSION_TOKEN");
return requiredSystemProperties.stream()
.map(name -> getEnv(name, ""))
.filter(s -> !s.isEmpty())
.count() == requiredSystemProperties.size();
}
public static Document getServerStatus() {
return new CommandReadOperation<>("admin", new BsonDocument("serverStatus", new BsonInt32(1)), new DocumentCodec())
.execute(getBinding());
}
public static boolean supportsFsync() {
Document serverStatus = getServerStatus();
Document storageEngine = (Document) serverStatus.get("storageEngine");
return storageEngine != null && !storageEngine.get("name").equals("inMemory");
}
static class ShutdownHook extends Thread {
@Override
public void run() {
if (cluster != null) {
new DropDatabaseOperation(getDefaultDatabaseName(), WriteConcern.ACKNOWLEDGED).execute(getBinding());
cluster.close();
}
}
}
public static String getEnv(final String name, final String defaultValue) {
String value = getEnv(name);
return value == null ? defaultValue : value;
}
@Nullable
public static String getEnv(final String name) {
return System.getenv(name);
}
public static boolean getOcspShouldSucceed() {
return Integer.parseInt(System.getProperty(MONGODB_OCSP_SHOULD_SUCCEED)) == 1;
}
@Nullable
public static synchronized ConnectionString getMultiMongosConnectionString() {
return getConnectionStringFromSystemProperty(MONGODB_MULTI_MONGOS_URI_SYSTEM_PROPERTY_NAME);
}
public static boolean isServerlessTest() {
return System.getProperty(SERVERLESS_TEST_SYSTEM_PROPERTY_NAME, "").equals("true");
}
public static synchronized boolean isDataLakeTest() {
String isDataLakeSystemProperty = System.getProperty(DATA_LAKE_TEST_SYSTEM_PROPERTY_NAME);
return "true".equals(isDataLakeSystemProperty);
}
public static synchronized ConnectionString getConnectionString() {
if (connectionString != null) {
return connectionString;
}
ConnectionString mongoURIProperty = getConnectionStringFromSystemProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME);
if (mongoURIProperty != null) {
return mongoURIProperty;
}
// Figure out what the connection string should be
Cluster cluster = createCluster(new ConnectionString(DEFAULT_URI),
new SocketStreamFactory(new DefaultInetAddressResolver(), SocketSettings.builder().build(), SslSettings.builder().build()));
try {
BsonDocument helloResult = new CommandReadOperation<>("admin",
new BsonDocument(LEGACY_HELLO, new BsonInt32(1)), new BsonDocumentCodec()).execute(new ClusterBinding(cluster,
ReadPreference.nearest(), ReadConcern.DEFAULT, getServerApi(), IgnorableRequestContext.INSTANCE));
if (helloResult.containsKey("setName")) {
connectionString = new ConnectionString(DEFAULT_URI + "/?replicaSet="
+ helloResult.getString("setName").getValue());
} else {
connectionString = new ConnectionString(DEFAULT_URI);
ClusterFixture.cluster = cluster;
}
return connectionString;
} finally {
if (ClusterFixture.cluster == null) {
cluster.close();
}
}
}
public static ClusterConnectionMode getClusterConnectionMode() {
return getCluster().getCurrentDescription().getConnectionMode();
}
@Nullable
public static ServerApi getServerApi() {
if (System.getProperty(MONGODB_API_VERSION) == null) {
return null;
} else {
return ServerApi.builder().version(ServerApiVersion.findByValue(System.getProperty(MONGODB_API_VERSION))).build();
}
}
public static String getConnectionStringSystemPropertyOrDefault() {
return System.getProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME, DEFAULT_URI);
}
@Nullable
private static ConnectionString getConnectionStringFromSystemProperty(final String property) {
String mongoURIProperty = System.getProperty(property);
if (mongoURIProperty != null && !mongoURIProperty.isEmpty()) {
return new ConnectionString(mongoURIProperty);
}
return null;
}
public static ReadWriteBinding getBinding(final Cluster cluster) {
return new ClusterBinding(cluster, ReadPreference.primary(), ReadConcern.DEFAULT, getServerApi(), IgnorableRequestContext.INSTANCE);
}
public static ReadWriteBinding getBinding() {
return getBinding(getCluster(), ReadPreference.primary());
}
public static ReadWriteBinding getBinding(final ReadPreference readPreference) {
return getBinding(getCluster(), readPreference);
}
private static ReadWriteBinding getBinding(final Cluster cluster, final ReadPreference readPreference) {
if (!BINDING_MAP.containsKey(readPreference)) {
ReadWriteBinding binding = new SessionBinding(new ClusterBinding(cluster, readPreference, ReadConcern.DEFAULT, getServerApi(),
IgnorableRequestContext.INSTANCE));
BINDING_MAP.put(readPreference, binding);
}
return BINDING_MAP.get(readPreference);
}
public static SingleConnectionBinding getSingleConnectionBinding() {
return new SingleConnectionBinding(getCluster(), ReadPreference.primary(), getServerApi());
}
public static AsyncSingleConnectionBinding getAsyncSingleConnectionBinding() {
return getAsyncSingleConnectionBinding(getAsyncCluster());
}
public static AsyncSingleConnectionBinding getAsyncSingleConnectionBinding(final Cluster cluster) {
return new AsyncSingleConnectionBinding(cluster, 20, SECONDS, getServerApi());
}
public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster) {
return new AsyncClusterBinding(cluster, ReadPreference.primary(), ReadConcern.DEFAULT, getServerApi(),
IgnorableRequestContext.INSTANCE);
}
public static AsyncReadWriteBinding getAsyncBinding() {
return getAsyncBinding(getAsyncCluster(), ReadPreference.primary());
}
public static AsyncReadWriteBinding getAsyncBinding(final ReadPreference readPreference) {
return getAsyncBinding(getAsyncCluster(), readPreference);
}
public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster, final ReadPreference readPreference) {
if (!ASYNC_BINDING_MAP.containsKey(readPreference)) {
AsyncReadWriteBinding binding = new AsyncSessionBinding(new AsyncClusterBinding(cluster, readPreference, ReadConcern.DEFAULT,
getServerApi(), IgnorableRequestContext.INSTANCE));
ASYNC_BINDING_MAP.put(readPreference, binding);
}
return ASYNC_BINDING_MAP.get(readPreference);
}
public static synchronized Cluster getCluster() {
if (cluster == null) {
cluster = createCluster(new SocketStreamFactory(new DefaultInetAddressResolver(), getSocketSettings(), getSslSettings()));
}
return cluster;
}
public static synchronized Cluster getAsyncCluster() {
if (asyncCluster == null) {
asyncCluster = createCluster(getAsyncStreamFactory());
}
return asyncCluster;
}
public static Cluster createCluster(final StreamFactory streamFactory) {
return createCluster(getConnectionString(), streamFactory);
}
public static Cluster createCluster(final MongoCredential credential) {
return createCluster(credential, getStreamFactory());
}
public static Cluster createAsyncCluster(final MongoCredential credential) {
return createCluster(credential, getAsyncStreamFactory());
}
private static Cluster createCluster(final MongoCredential credential, final StreamFactory streamFactory) {
return new DefaultClusterFactory().createCluster(ClusterSettings.builder().hosts(asList(getPrimary())).build(),
ServerSettings.builder().build(),
ConnectionPoolSettings.builder().maxSize(1).build(), InternalConnectionPoolSettings.builder().build(),
streamFactory, streamFactory, credential, LoggerSettings.builder().build(), null, null, null,
Collections.emptyList(), getServerApi(), null);
}
private static Cluster createCluster(final ConnectionString connectionString, final StreamFactory streamFactory) {
return new DefaultClusterFactory().createCluster(ClusterSettings.builder().applyConnectionString(connectionString).build(),
ServerSettings.builder().build(),
ConnectionPoolSettings.builder().applyConnectionString(connectionString).build(),
InternalConnectionPoolSettings.builder().build(),
streamFactory,
new SocketStreamFactory(new DefaultInetAddressResolver(), SocketSettings.builder().readTimeout(5, SECONDS).build(),
getSslSettings(connectionString)),
connectionString.getCredential(),
LoggerSettings.builder().build(), null, null, null,
connectionString.getCompressorList(), getServerApi(), null);
}
public static StreamFactory getStreamFactory() {
return new SocketStreamFactory(new DefaultInetAddressResolver(), SocketSettings.builder().build(), getSslSettings());
}
public static StreamFactory getAsyncStreamFactory() {
TransportSettings transportSettings = getOverriddenTransportSettings();
if (transportSettings == null) { // use NIO2
if (getSslSettings().isEnabled()) {
return new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver()).create(getSocketSettings(), getSslSettings());
} else {
return new AsynchronousSocketChannelStreamFactory(new DefaultInetAddressResolver(), getSocketSettings(), getSslSettings());
}
} else {
StreamFactoryFactory overriddenStreamFactoryFactory = NettyStreamFactoryFactory.builder()
.applySettings((NettyTransportSettings) transportSettings)
.inetAddressResolver(new DefaultInetAddressResolver())
.build();
return assertNotNull(overriddenStreamFactoryFactory).create(getSocketSettings(), getSslSettings());
}
}
@Nullable
public static TransportSettings getOverriddenTransportSettings() {
String streamType = System.getProperty("org.mongodb.test.async.type", "nio2");
if (nettyTransportSettings == null && streamType.equals("netty")) {
NettyTransportSettings.Builder builder = TransportSettings.nettyBuilder();
String sslProvider = System.getProperty("org.mongodb.test.netty.ssl.provider");
if (sslProvider != null) {
SslContext sslContext;
try {
sslContext = SslContextBuilder.forClient()
.sslProvider(SslProvider.valueOf(sslProvider))
.build();
} catch (SSLException e) {
throw new MongoClientException("Unable to create Netty SslContext", e);
}
builder.sslContext(sslContext);
}
nettyTransportSettings = builder.build();
}
return nettyTransportSettings;
}
private static SocketSettings getSocketSettings() {
return SocketSettings.builder().applyConnectionString(getConnectionString()).build();
}
public static SslSettings getSslSettings() {
return getSslSettings(getConnectionString());
}
public static SslSettings getSslSettings(final ConnectionString connectionString) {
return SslSettings.builder().applyConnectionString(connectionString).build();
}
public static ServerAddress getPrimary() {
List<ServerDescription> serverDescriptions = getPrimaries(getClusterDescription(getCluster()));
while (serverDescriptions.isEmpty()) {
try {
sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
serverDescriptions = getPrimaries(getClusterDescription(getCluster()));
}
return serverDescriptions.get(0).getAddress();
}
public static ServerAddress getSecondary() {
List<ServerDescription> serverDescriptions = getSecondaries(getClusterDescription(getCluster()));
while (serverDescriptions.isEmpty()) {
try {
sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
serverDescriptions = getSecondaries(getClusterDescription(getCluster()));
}
return serverDescriptions.get(0).getAddress();
}
@Nullable
public static MongoCredential getCredential() {
return getConnectionString().getCredential();
}
public static String getLoginContextName() {
return System.getProperty("org.mongodb.test.gssapi.login.context.name", "com.sun.security.jgss.krb5.initiate");
}
@Nullable
public static MongoCredentialWithCache getCredentialWithCache() {
return getConnectionString().getCredential() == null ? null : new MongoCredentialWithCache(getConnectionString().getCredential());
}
public static BsonDocument getServerParameters() {
if (serverParameters == null) {
serverParameters = new CommandReadOperation<>("admin",
new BsonDocument("getParameter", new BsonString("*")),
new BsonDocumentCodec())
.execute(getBinding());
}
return serverParameters;
}
public static boolean isDiscoverableReplicaSet() {
return clusterIsType(REPLICA_SET) && getClusterConnectionMode() == MULTIPLE;
}
public static boolean isSharded() {
return clusterIsType(SHARDED);
}
public static boolean isStandalone() {
return clusterIsType(STANDALONE);
}
public static boolean isLoadBalanced() {
return getClusterConnectionMode() == LOAD_BALANCED;
}
public static boolean isAuthenticated() {
return getConnectionString().getCredential() != null;
}
public static boolean isClientSideEncryptionTest() {
return !getEnv("AWS_ACCESS_KEY_ID", "").isEmpty();
}
public static boolean isAtlasSearchTest() {
return System.getProperty(ATLAS_SEARCH_TEST_SYSTEM_PROPERTY_NAME) != null;
}
public static void enableMaxTimeFailPoint() {
configureFailPoint(BsonDocument.parse("{configureFailPoint: 'maxTimeAlwaysTimeOut', mode: 'alwaysOn'}"));
}
public static void disableMaxTimeFailPoint() {
disableFailPoint("maxTimeAlwaysTimeOut");
}
public static void enableOnPrimaryTransactionalWriteFailPoint(final BsonValue failPointData) {
BsonDocument command = BsonDocument.parse("{ configureFailPoint: 'onPrimaryTransactionalWrite'}");
if (failPointData.isDocument() && failPointData.asDocument().containsKey("mode")) {
for (Map.Entry<String, BsonValue> keyValue : failPointData.asDocument().entrySet()) {
command.append(keyValue.getKey(), keyValue.getValue());
}
} else {
command.append("mode", failPointData);
}
configureFailPoint(command);
}
public static void disableOnPrimaryTransactionalWriteFailPoint() {
disableFailPoint("onPrimaryTransactionalWrite");
}
public static void configureFailPoint(final BsonDocument failPointDocument) {
assumeThat(isSharded(), is(false));
boolean failsPointsSupported = true;
if (!isSharded()) {
try {
new CommandReadOperation<>("admin", failPointDocument, new BsonDocumentCodec())
.execute(getBinding());
} catch (MongoCommandException e) {
if (e.getErrorCode() == COMMAND_NOT_FOUND_ERROR_CODE) {
failsPointsSupported = false;
}
}
assumeTrue("configureFailPoint is not enabled", failsPointsSupported);
}
}
public static void disableFailPoint(final String failPoint) {
if (!isSharded()) {
BsonDocument failPointDocument = new BsonDocument("configureFailPoint", new BsonString(failPoint))
.append("mode", new BsonString("off"));
try {
new CommandReadOperation<>("admin", failPointDocument, new BsonDocumentCodec()).execute(getBinding());
} catch (MongoCommandException e) {
// ignore
}
}
}
@SuppressWarnings("overloads")
public static <T> T executeSync(final WriteOperation<T> op) {
return executeSync(op, getBinding());
}
@SuppressWarnings("overloads")
public static <T> T executeSync(final WriteOperation<T> op, final ReadWriteBinding binding) {
return op.execute(binding);
}
@SuppressWarnings("overloads")
public static <T> T executeSync(final ReadOperation<T> op) {
return executeSync(op, getBinding());
}
@SuppressWarnings("overloads")
public static <T> T executeSync(final ReadOperation<T> op, final ReadWriteBinding binding) {
return op.execute(binding);
}
@SuppressWarnings("overloads")
public static <T> T executeAsync(final AsyncWriteOperation<T> op) throws Throwable {
return executeAsync(op, getAsyncBinding());
}
@SuppressWarnings("overloads")
public static <T> T executeAsync(final AsyncWriteOperation<T> op, final AsyncWriteBinding binding) throws Throwable {
FutureResultCallback<T> futureResultCallback = new FutureResultCallback<>();
op.executeAsync(binding, futureResultCallback);
return futureResultCallback.get(TIMEOUT, SECONDS);
}
@SuppressWarnings("overloads")
public static <T> T executeAsync(final AsyncReadOperation<T> op) throws Throwable {
return executeAsync(op, getAsyncBinding());
}
@SuppressWarnings("overloads")
public static <T> T executeAsync(final AsyncReadOperation<T> op, final AsyncReadBinding binding) throws Throwable {
FutureResultCallback<T> futureResultCallback = new FutureResultCallback<>();
op.executeAsync(binding, futureResultCallback);
return futureResultCallback.get(TIMEOUT, SECONDS);
}
public static <T> void loopCursor(final List<AsyncBatchCursor<T>> batchCursors, final Block<T> block) throws Throwable {
List<FutureResultCallback<Void>> futures = new ArrayList<>();
for (AsyncBatchCursor<T> batchCursor : batchCursors) {
FutureResultCallback<Void> futureResultCallback = new FutureResultCallback<>();
futures.add(futureResultCallback);
loopCursor(batchCursor, block, futureResultCallback);
}
for (int i = 0; i < batchCursors.size(); i++) {
futures.get(i).get(TIMEOUT, SECONDS);
}
}
public static <T> void loopCursor(final AsyncReadOperation<AsyncBatchCursor<T>> op, final Block<T> block) throws Throwable {
FutureResultCallback<Void> futureResultCallback = new FutureResultCallback<>();
loopCursor(executeAsync(op), block, futureResultCallback);
futureResultCallback.get(TIMEOUT, SECONDS);
}
public static <T> void loopCursor(final AsyncBatchCursor<T> batchCursor, final Block<T> block,
final SingleResultCallback<Void> callback) {
if (batchCursor.isClosed()) {
callback.onResult(null, null);
return;
}
batchCursor.next((results, t) -> {
if (t != null || results == null) {
batchCursor.close();
callback.onResult(null, t);
} else {
try {
for (T result : results) {
block.apply(result);
}
loopCursor(batchCursor, block, callback);
} catch (Throwable tr) {
batchCursor.close();
callback.onResult(null, tr);
}
}
});
}
public static <T> List<T> collectCursorResults(final AsyncBatchCursor<T> batchCursor) throws Throwable {
List<T> results = new ArrayList<>();
FutureResultCallback<Void> futureResultCallback = new FutureResultCallback<>();
loopCursor(batchCursor, t -> results.add(t), futureResultCallback);
futureResultCallback.get(TIMEOUT, SECONDS);
return results;
}
public static <T> List<T> collectCursorResults(final BatchCursor<T> batchCursor) {
List<T> results = new ArrayList<>();
while (batchCursor.hasNext()) {
results.addAll(batchCursor.next());
}
return results;
}
public static AsyncConnectionSource getWriteConnectionSource(final AsyncReadWriteBinding binding) throws Throwable {
FutureResultCallback<AsyncConnectionSource> futureResultCallback = new FutureResultCallback<>();
binding.getWriteConnectionSource(futureResultCallback);
return futureResultCallback.get(TIMEOUT, SECONDS);
}
public static AsyncConnectionSource getReadConnectionSource(final AsyncReadWriteBinding binding) throws Throwable {
FutureResultCallback<AsyncConnectionSource> futureResultCallback = new FutureResultCallback<>();
binding.getReadConnectionSource(futureResultCallback);
return futureResultCallback.get(TIMEOUT, SECONDS);
}
public static AsyncConnection getConnection(final AsyncConnectionSource source) throws Throwable {
FutureResultCallback<AsyncConnection> futureResultCallback = new FutureResultCallback<>();
source.getConnection(futureResultCallback);
return futureResultCallback.get(TIMEOUT, SECONDS);
}
public static synchronized void checkReferenceCountReachesTarget(final ReferenceCounted referenceCounted, final int target) {
int count = getReferenceCountAfterTimeout(referenceCounted, target);
if (count != target) {
throw new MongoTimeoutException(
format("Timed out waiting for reference count to drop to %d. Now at %d for %s", target, count,
referenceCounted));
}
}
public static int getReferenceCountAfterTimeout(final ReferenceCounted referenceCounted, final int target) {
long startTime = System.currentTimeMillis();
int count = referenceCounted.getCount();
while (count > target) {
try {
if (System.currentTimeMillis() > startTime + TIMEOUT_DURATION.toMillis()) {
return count;
}
sleep(10);
count = referenceCounted.getCount();
} catch (InterruptedException e) {
throw interruptAndCreateMongoInterruptedException("Interrupted", e);
}
}
return count;
}
public static ClusterSettings.Builder setDirectConnection(final ClusterSettings.Builder builder) {
return builder.mode(ClusterConnectionMode.SINGLE).hosts(singletonList(getPrimary()));
}
}