Skip to content

Commit 7cd3737

Browse files
Merge branch 'main' into JAVA-5224
2 parents 0802829 + 1090b3f commit 7cd3737

File tree

14 files changed

+288
-33
lines changed

14 files changed

+288
-33
lines changed

driver-core/src/main/com/mongodb/ReadPreference.java

+3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public abstract class ReadPreference {
9898
* @return a new ReadPreference instance with hedge options
9999
* @since 4.1
100100
* @mongodb.server.release 4.4
101+
* @deprecated As of MongoDB 8.1, the server ignores the option and periodically logs a warning
101102
*/
103+
@Deprecated
102104
public abstract ReadPreference withHedgeOptions(ReadPreferenceHedgeOptions hedgeOptions);
103105

104106
/**
@@ -682,6 +684,7 @@ public TaggableReadPreference withMaxStalenessMS(final Long maxStalenessMS, fina
682684
throw new UnsupportedOperationException("Primary read preference can not also specify max staleness");
683685
}
684686

687+
@Deprecated
685688
@Override
686689
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
687690
throw new UnsupportedOperationException("Primary read preference can not also specify hedge");

driver-core/src/main/com/mongodb/ReadPreferenceHedgeOptions.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
*
2626
* @since 4.1
2727
* @mongodb.server.release 4.4
28+
* @deprecated As of MongoDB 8.1, the server ignores the option and periodically logs a warning
2829
*/
30+
@Deprecated
2931
@Immutable
3032
public final class ReadPreferenceHedgeOptions {
3133
private final boolean enabled;

driver-core/src/main/com/mongodb/TaggableReadPreference.java

+13
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ public abstract class TaggableReadPreference extends ReadPreference {
5454

5555
private final List<TagSet> tagSetList = new ArrayList<>();
5656
private final Long maxStalenessMS;
57+
@SuppressWarnings("deprecation")
5758
private final ReadPreferenceHedgeOptions hedgeOptions;
5859

5960
TaggableReadPreference() {
6061
this.maxStalenessMS = null;
6162
this.hedgeOptions = null;
6263
}
6364

65+
@SuppressWarnings("deprecation")
6466
TaggableReadPreference(final List<TagSet> tagSetList, @Nullable final Long maxStaleness, final TimeUnit timeUnit,
6567
@Nullable final ReadPreferenceHedgeOptions hedgeOptions) {
6668
notNull("tagSetList", tagSetList);
@@ -80,6 +82,7 @@ public abstract class TaggableReadPreference extends ReadPreference {
8082
@Override
8183
public abstract TaggableReadPreference withMaxStalenessMS(Long maxStalenessMS, TimeUnit timeUnit);
8284

85+
@Deprecated
8386
@Override
8487
public abstract TaggableReadPreference withHedgeOptions(ReadPreferenceHedgeOptions hedgeOptions);
8588

@@ -146,7 +149,9 @@ public Long getMaxStaleness(final TimeUnit timeUnit) {
146149
* @return the hedge options
147150
* @mongodb.server.release 4.4
148151
* @since 4.1
152+
* @deprecated As of MongoDB 8.1, the server ignores the option and periodically logs a warning
149153
*/
154+
@Deprecated
150155
@Nullable
151156
public ReadPreferenceHedgeOptions getHedgeOptions() {
152157
return hedgeOptions;
@@ -327,6 +332,7 @@ static class SecondaryReadPreference extends TaggableReadPreference {
327332
this(tagSetList, maxStaleness, timeUnit, null);
328333
}
329334

335+
@SuppressWarnings("deprecation")
330336
SecondaryReadPreference(final List<TagSet> tagSetList, @Nullable final Long maxStaleness, final TimeUnit timeUnit,
331337
@Nullable final ReadPreferenceHedgeOptions hedgeOptions) {
332338
super(tagSetList, maxStaleness, timeUnit, hedgeOptions);
@@ -349,6 +355,7 @@ public TaggableReadPreference withMaxStalenessMS(@Nullable final Long maxStalene
349355
return new SecondaryReadPreference(getTagSetList(), maxStaleness, timeUnit, getHedgeOptions());
350356
}
351357

358+
@Deprecated
352359
@Override
353360
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
354361
return new SecondaryReadPreference(getTagSetList(), getMaxStaleness(MILLISECONDS), MILLISECONDS, hedgeOptions);
@@ -388,6 +395,7 @@ static class SecondaryPreferredReadPreference extends SecondaryReadPreference {
388395
this(tagSetList, maxStaleness, timeUnit, null);
389396
}
390397

398+
@SuppressWarnings("deprecation")
391399
SecondaryPreferredReadPreference(final List<TagSet> tagSetList, @Nullable final Long maxStaleness, final TimeUnit timeUnit,
392400
@Nullable final ReadPreferenceHedgeOptions hedgeOptions) {
393401
super(tagSetList, maxStaleness, timeUnit, hedgeOptions);
@@ -410,6 +418,7 @@ public TaggableReadPreference withMaxStalenessMS(@Nullable final Long maxStalene
410418
return new SecondaryPreferredReadPreference(getTagSetList(), maxStaleness, timeUnit, getHedgeOptions());
411419
}
412420

421+
@Deprecated
413422
@Override
414423
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
415424
return new SecondaryPreferredReadPreference(getTagSetList(), getMaxStaleness(MILLISECONDS), MILLISECONDS, hedgeOptions);
@@ -441,6 +450,7 @@ static class NearestReadPreference extends TaggableReadPreference {
441450
this(tagSetList, maxStaleness, timeUnit, null);
442451
}
443452

453+
@SuppressWarnings("deprecation")
444454
NearestReadPreference(final List<TagSet> tagSetList, @Nullable final Long maxStaleness, final TimeUnit timeUnit,
445455
@Nullable final ReadPreferenceHedgeOptions hedgeOptions) {
446456
super(tagSetList, maxStaleness, timeUnit, hedgeOptions);
@@ -463,6 +473,7 @@ public TaggableReadPreference withMaxStalenessMS(@Nullable final Long maxStalene
463473
return new NearestReadPreference(getTagSetList(), maxStaleness, timeUnit, getHedgeOptions());
464474
}
465475

476+
@Deprecated
466477
@Override
467478
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
468479
return new NearestReadPreference(getTagSetList(), getMaxStaleness(MILLISECONDS), MILLISECONDS, hedgeOptions);
@@ -503,6 +514,7 @@ static class PrimaryPreferredReadPreference extends SecondaryReadPreference {
503514
this(tagSetList, maxStaleness, timeUnit, null);
504515
}
505516

517+
@SuppressWarnings("deprecation")
506518
PrimaryPreferredReadPreference(final List<TagSet> tagSetList, @Nullable final Long maxStaleness, final TimeUnit timeUnit,
507519
@Nullable final ReadPreferenceHedgeOptions hedgeOptions) {
508520
super(tagSetList, maxStaleness, timeUnit, hedgeOptions);
@@ -525,6 +537,7 @@ public TaggableReadPreference withMaxStalenessMS(@Nullable final Long maxStalene
525537
return new PrimaryPreferredReadPreference(getTagSetList(), maxStaleness, timeUnit, getHedgeOptions());
526538
}
527539

540+
@Deprecated
528541
@Override
529542
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
530543
return new PrimaryPreferredReadPreference(getTagSetList(), getMaxStaleness(MILLISECONDS), MILLISECONDS, hedgeOptions);

driver-core/src/main/com/mongodb/internal/connection/StreamFactoryHelper.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import com.mongodb.connection.AsyncTransportSettings;
2222
import com.mongodb.connection.NettyTransportSettings;
2323
import com.mongodb.connection.SocketSettings;
24+
import com.mongodb.connection.SslSettings;
2425
import com.mongodb.connection.TransportSettings;
2526
import com.mongodb.internal.connection.netty.NettyStreamFactoryFactory;
27+
import com.mongodb.lang.Nullable;
2628
import com.mongodb.spi.dns.InetAddressResolver;
2729

2830
import java.io.IOException;
@@ -34,16 +36,26 @@
3436
*/
3537
public final class StreamFactoryHelper {
3638

37-
public static StreamFactory getSyncStreamFactory(final MongoClientSettings settings,
38-
final InetAddressResolver inetAddressResolver, final SocketSettings socketSettings) {
39-
TransportSettings transportSettings = settings.getTransportSettings();
39+
public static StreamFactoryFactory getSyncStreamFactoryFactory(
40+
@Nullable final TransportSettings transportSettings,
41+
final InetAddressResolver inetAddressResolver) {
42+
4043
if (transportSettings == null) {
41-
return new SocketStreamFactory(inetAddressResolver, socketSettings, settings.getSslSettings());
44+
return new StreamFactoryFactory() {
45+
@Override
46+
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
47+
return new SocketStreamFactory(inetAddressResolver, socketSettings, sslSettings);
48+
}
49+
50+
@Override
51+
public void close() {
52+
//NOP
53+
}
54+
};
4255
} else if (transportSettings instanceof AsyncTransportSettings) {
4356
throw new MongoClientException("Unsupported transport settings in sync: " + transportSettings.getClass().getName());
4457
} else if (transportSettings instanceof NettyTransportSettings) {
45-
return getNettyStreamFactoryFactory(inetAddressResolver, (NettyTransportSettings) transportSettings)
46-
.create(socketSettings, settings.getSslSettings());
58+
return getNettyStreamFactoryFactory(inetAddressResolver, (NettyTransportSettings) transportSettings);
4759
} else {
4860
throw new MongoClientException("Unsupported transport settings: " + transportSettings.getClass().getName());
4961
}

driver-core/src/main/com/mongodb/internal/operation/ChangeStreamBatchCursorHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
final class ChangeStreamBatchCursorHelper {
3636
@VisibleForTesting(otherwise = PRIVATE)
3737
static final List<Integer> RETRYABLE_SERVER_ERROR_CODES =
38-
asList(6, 7, 63, 89, 91, 133, 134, 150, 189, 234, 262, 9001, 10107, 11600, 11602, 13388, 13435, 13436);
38+
asList(6, 7, 63, 89, 91, 133, 150, 189, 234, 262, 9001, 10107, 11600, 11602, 13388, 13435, 13436);
3939
@VisibleForTesting(otherwise = PRIVATE)
4040
static final String RESUMABLE_CHANGE_STREAM_ERROR_LABEL = "ResumableChangeStreamError";
4141

driver-core/src/main/com/mongodb/internal/operation/FindOperation.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,12 @@ private BsonDocument getCommand(final OperationContext operationContext, final i
390390
if (batchSize < 0 && Math.abs(batchSize) < limit) {
391391
commandDocument.put("limit", new BsonInt32(Math.abs(batchSize)));
392392
} else if (batchSize != 0) {
393-
commandDocument.put("batchSize", new BsonInt32(Math.abs(batchSize)));
393+
int effectiveBatchSize = Math.abs(batchSize);
394+
if (effectiveBatchSize == limit) {
395+
// avoid an open cursor on server side when batchSize and limit are equal
396+
effectiveBatchSize++;
397+
}
398+
commandDocument.put("batchSize", new BsonInt32(effectiveBatchSize));
394399
}
395400
}
396401
if (limit < 0 || batchSize < 0) {

driver-core/src/test/resources/unified-test-format/crud/find.json

+62
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,68 @@
237237
]
238238
}
239239
]
240+
},
241+
{
242+
"description": "Find with batchSize equal to limit",
243+
"operations": [
244+
{
245+
"object": "collection0",
246+
"name": "find",
247+
"arguments": {
248+
"filter": {
249+
"_id": {
250+
"$gt": 1
251+
}
252+
},
253+
"sort": {
254+
"_id": 1
255+
},
256+
"limit": 4,
257+
"batchSize": 4
258+
},
259+
"expectResult": [
260+
{
261+
"_id": 2,
262+
"x": 22
263+
},
264+
{
265+
"_id": 3,
266+
"x": 33
267+
},
268+
{
269+
"_id": 4,
270+
"x": 44
271+
},
272+
{
273+
"_id": 5,
274+
"x": 55
275+
}
276+
]
277+
}
278+
],
279+
"expectEvents": [
280+
{
281+
"client": "client0",
282+
"events": [
283+
{
284+
"commandStartedEvent": {
285+
"command": {
286+
"find": "coll0",
287+
"filter": {
288+
"_id": {
289+
"$gt": 1
290+
}
291+
},
292+
"limit": 4,
293+
"batchSize": 5
294+
},
295+
"commandName": "find",
296+
"databaseName": "find-tests"
297+
}
298+
}
299+
]
300+
}
301+
]
240302
}
241303
]
242304
}

driver-legacy/src/main/com/mongodb/MongoClient.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.mongodb.client.ListDatabasesIterable;
2222
import com.mongodb.client.MongoDatabase;
2323
import com.mongodb.client.MongoIterable;
24+
import com.mongodb.client.internal.Clusters;
2425
import com.mongodb.client.internal.MongoClientImpl;
2526
import com.mongodb.client.internal.OperationExecutor;
2627
import com.mongodb.connection.ClusterConnectionMode;
@@ -37,6 +38,7 @@
3738
import com.mongodb.internal.connection.Connection;
3839
import com.mongodb.internal.connection.NoOpSessionContext;
3940
import com.mongodb.internal.connection.OperationContext;
41+
import com.mongodb.internal.connection.StreamFactoryFactory;
4042
import com.mongodb.internal.diagnostics.logging.Logger;
4143
import com.mongodb.internal.diagnostics.logging.Loggers;
4244
import com.mongodb.internal.session.ServerSessionPool;
@@ -63,8 +65,11 @@
6365
import java.util.concurrent.atomic.AtomicBoolean;
6466
import java.util.stream.Collectors;
6567

68+
import static com.mongodb.assertions.Assertions.notNull;
6669
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
6770
import static com.mongodb.internal.connection.ServerAddressHelper.createServerAddress;
71+
import static com.mongodb.internal.connection.ServerAddressHelper.getInetAddressResolver;
72+
import static com.mongodb.internal.connection.StreamFactoryHelper.getSyncStreamFactoryFactory;
6873
import static java.lang.String.format;
6974
import static java.util.Collections.singletonList;
7075
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -244,8 +249,20 @@ public MongoClient(final MongoClientSettings settings, @Nullable final MongoDriv
244249
private MongoClient(final MongoClientSettings settings,
245250
@Nullable final MongoClientOptions options,
246251
@Nullable final MongoDriverInformation mongoDriverInformation) {
252+
notNull("settings", settings);
253+
247254
MongoDriverInformation wrappedMongoDriverInformation = wrapMongoDriverInformation(mongoDriverInformation);
248-
delegate = new MongoClientImpl(settings, wrappedMongoDriverInformation);
255+
256+
StreamFactoryFactory syncStreamFactoryFactory = getSyncStreamFactoryFactory(
257+
settings.getTransportSettings(),
258+
getInetAddressResolver(settings));
259+
260+
Cluster cluster = Clusters.createCluster(
261+
settings,
262+
wrappedMongoDriverInformation,
263+
syncStreamFactoryFactory);
264+
265+
delegate = new MongoClientImpl(cluster, settings, wrappedMongoDriverInformation, syncStreamFactoryFactory);
249266
this.options = options != null ? options : MongoClientOptions.builder(settings).build();
250267
cursorCleaningService = this.options.isCursorFinalizerEnabled() ? createCursorCleaningService() : null;
251268
this.closed = new AtomicBoolean();

driver-legacy/src/test/unit/com/mongodb/MongoClientSpecification.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class MongoClientSpecification extends Specification {
309309
def 'should validate the ChangeStreamIterable pipeline data correctly'() {
310310
given:
311311
def executor = new TestOperationExecutor([])
312-
def client = new MongoClientImpl(Stub(Cluster), null, MongoClientSettings.builder().build(), executor)
312+
def client = new MongoClientImpl(Stub(Cluster), null, MongoClientSettings.builder().build(), null, executor)
313313

314314
when:
315315
client.watch((Class) null)

driver-sync/src/main/com/mongodb/client/MongoClients.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
import com.mongodb.ConnectionString;
2020
import com.mongodb.MongoClientSettings;
2121
import com.mongodb.MongoDriverInformation;
22+
import com.mongodb.client.internal.Clusters;
2223
import com.mongodb.client.internal.MongoClientImpl;
24+
import com.mongodb.internal.connection.Cluster;
25+
import com.mongodb.internal.connection.StreamFactoryFactory;
2326
import com.mongodb.lang.Nullable;
2427

28+
import static com.mongodb.assertions.Assertions.notNull;
29+
import static com.mongodb.internal.connection.ServerAddressHelper.getInetAddressResolver;
30+
import static com.mongodb.internal.connection.StreamFactoryHelper.getSyncStreamFactoryFactory;
31+
2532

2633
/**
2734
* A factory for {@link MongoClient} instances. Use of this class is now the recommended way to connect to MongoDB via the Java driver.
@@ -103,9 +110,23 @@ public static MongoClient create(final ConnectionString connectionString,
103110
* @return the client
104111
*/
105112
public static MongoClient create(final MongoClientSettings settings, @Nullable final MongoDriverInformation mongoDriverInformation) {
113+
notNull("settings", settings);
114+
106115
MongoDriverInformation.Builder builder = mongoDriverInformation == null ? MongoDriverInformation.builder()
107116
: MongoDriverInformation.builder(mongoDriverInformation);
108-
return new MongoClientImpl(settings, builder.driverName("sync").build());
117+
118+
MongoDriverInformation driverInfo = builder.driverName("sync").build();
119+
120+
StreamFactoryFactory syncStreamFactoryFactory = getSyncStreamFactoryFactory(
121+
settings.getTransportSettings(),
122+
getInetAddressResolver(settings));
123+
124+
Cluster cluster = Clusters.createCluster(
125+
settings,
126+
driverInfo,
127+
syncStreamFactoryFactory);
128+
129+
return new MongoClientImpl(cluster, settings, driverInfo, syncStreamFactoryFactory);
109130
}
110131

111132
private MongoClients() {

0 commit comments

Comments
 (0)