Skip to content

Commit afe3600

Browse files
authored
Remove code that won't execute on supported server releases (#1704)
Remove connection handshake code that executes the getlasterror command in order to get a connectionId. This code is no longer necessary on server releases >= 4.2, as connectionId is included in the hello command response. JAVA-5865
1 parent dcc0371 commit afe3600

File tree

2 files changed

+14
-96
lines changed

2 files changed

+14
-96
lines changed

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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import static com.mongodb.internal.connection.CommandHelper.LEGACY_HELLO;
4141
import static com.mongodb.internal.connection.CommandHelper.executeCommand;
4242
import static com.mongodb.internal.connection.CommandHelper.executeCommandAsync;
43-
import static com.mongodb.internal.connection.CommandHelper.executeCommandWithoutCheckingForFailure;
4443
import static com.mongodb.internal.connection.DefaultAuthenticator.USER_NOT_FOUND_CODE;
4544
import static com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription;
4645
import static com.mongodb.internal.connection.DescriptionHelper.createServerDescription;
@@ -88,7 +87,8 @@ public InternalConnectionInitializationDescription finishHandshake(final Interna
8887
if (Authenticator.shouldAuthenticate(authenticator, connectionDescription)) {
8988
authenticator.authenticate(internalConnection, connectionDescription, operationContext);
9089
}
91-
return completeConnectionDescriptionInitialization(internalConnection, description, operationContext);
90+
91+
return description;
9292
}
9393

9494
@Override
@@ -121,14 +121,14 @@ public void finishHandshakeAsync(final InternalConnection internalConnection,
121121
ConnectionDescription connectionDescription = description.getConnectionDescription();
122122

123123
if (!Authenticator.shouldAuthenticate(authenticator, connectionDescription)) {
124-
completeConnectionDescriptionInitializationAsync(internalConnection, description, operationContext, callback);
124+
callback.onResult(description, null);
125125
} else {
126126
authenticator.authenticateAsync(internalConnection, connectionDescription, operationContext,
127127
(result1, t1) -> {
128128
if (t1 != null) {
129129
callback.onResult(null, t1);
130130
} else {
131-
completeConnectionDescriptionInitializationAsync(internalConnection, description, operationContext, callback);
131+
callback.onResult(description, null);
132132
}
133133
});
134134
}
@@ -203,50 +203,13 @@ private BsonDocument createHelloCommand(final Authenticator authenticator, final
203203
return helloCommandDocument;
204204
}
205205

206-
private InternalConnectionInitializationDescription completeConnectionDescriptionInitialization(
207-
final InternalConnection internalConnection,
208-
final InternalConnectionInitializationDescription description,
209-
final OperationContext operationContext) {
210-
211-
if (description.getConnectionDescription().getConnectionId().getServerValue() != null) {
212-
return description;
213-
}
214-
215-
return applyGetLastErrorResult(executeCommandWithoutCheckingForFailure("admin",
216-
new BsonDocument("getlasterror", new BsonInt32(1)), clusterConnectionMode, serverApi,
217-
internalConnection, operationContext),
218-
description);
219-
}
220-
221206
private void setSpeculativeAuthenticateResponse(final BsonDocument helloResult) {
222207
if (authenticator instanceof SpeculativeAuthenticator) {
223208
((SpeculativeAuthenticator) authenticator).setSpeculativeAuthenticateResponse(
224209
helloResult.getDocument("speculativeAuthenticate", null));
225210
}
226211
}
227212

228-
private void completeConnectionDescriptionInitializationAsync(
229-
final InternalConnection internalConnection,
230-
final InternalConnectionInitializationDescription description,
231-
final OperationContext operationContext,
232-
final SingleResultCallback<InternalConnectionInitializationDescription> callback) {
233-
234-
if (description.getConnectionDescription().getConnectionId().getServerValue() != null) {
235-
callback.onResult(description, null);
236-
return;
237-
}
238-
239-
executeCommandAsync("admin", new BsonDocument("getlasterror", new BsonInt32(1)), clusterConnectionMode, serverApi,
240-
internalConnection, operationContext,
241-
(result, t) -> {
242-
if (t != null) {
243-
callback.onResult(description, null);
244-
} else {
245-
callback.onResult(applyGetLastErrorResult(result, description), null);
246-
}
247-
});
248-
}
249-
250213
private InternalConnectionInitializationDescription applyGetLastErrorResult(
251214
final BsonDocument getLastErrorResult,
252215
final InternalConnectionInitializationDescription description) {

driver-core/src/test/unit/com/mongodb/internal/connection/InternalStreamConnectionInitializerSpecification.groovy

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
6161
def initializer = new InternalStreamConnectionInitializer(SINGLE, null, null, [], null)
6262

6363
when:
64-
enqueueSuccessfulReplies(false, null)
64+
enqueueSuccessfulReplies(false, 123)
6565
def description = initializer.startHandshake(internalConnection, operationContext)
6666
description = initializer.finishHandshake(internalConnection, description, operationContext)
6767
def connectionDescription = description.connectionDescription
6868
def serverDescription = description.serverDescription
6969

7070
then:
71-
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, null)
71+
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
7272
serverDescription == getExpectedServerDescription(serverDescription)
7373
}
7474

@@ -77,7 +77,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
7777
def initializer = new InternalStreamConnectionInitializer(SINGLE, null, null, [], null)
7878

7979
when:
80-
enqueueSuccessfulReplies(false, null)
80+
enqueueSuccessfulReplies(false, 123)
8181
def futureCallback = new FutureResultCallback<InternalConnectionInitializationDescription>()
8282
initializer.startHandshakeAsync(internalConnection, operationContext, futureCallback)
8383
def description = futureCallback.get()
@@ -88,7 +88,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
8888
def serverDescription = description.serverDescription
8989

9090
then:
91-
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, null)
91+
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
9292
serverDescription == getExpectedServerDescription(serverDescription)
9393
}
9494

@@ -106,20 +106,6 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
106106
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
107107
}
108108

109-
def 'should create correct description with server connection id from hello'() {
110-
given:
111-
def initializer = new InternalStreamConnectionInitializer(SINGLE, null, null, [], null)
112-
113-
when:
114-
enqueueSuccessfulRepliesWithConnectionIdIsHelloResponse(false, 123)
115-
def internalDescription = initializer.startHandshake(internalConnection, operationContext)
116-
def connectionDescription = initializer.finishHandshake(internalConnection, internalDescription, operationContext)
117-
.connectionDescription
118-
119-
then:
120-
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
121-
}
122-
123109
def 'should create correct description with server connection id asynchronously'() {
124110
given:
125111
def initializer = new InternalStreamConnectionInitializer(SINGLE, null, null, [], null)
@@ -137,31 +123,13 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
137123
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
138124
}
139125

140-
def 'should create correct description with server connection id from hello asynchronously'() {
141-
given:
142-
def initializer = new InternalStreamConnectionInitializer(SINGLE, null, null, [], null)
143-
144-
when:
145-
enqueueSuccessfulRepliesWithConnectionIdIsHelloResponse(false, 123)
146-
def futureCallback = new FutureResultCallback<InternalConnectionInitializationDescription>()
147-
initializer.startHandshakeAsync(internalConnection, operationContext, futureCallback)
148-
def description = futureCallback.get()
149-
futureCallback = new FutureResultCallback<InternalConnectionInitializationDescription>()
150-
initializer.finishHandshakeAsync(internalConnection, description, operationContext, futureCallback)
151-
description = futureCallback.get()
152-
def connectionDescription = description.connectionDescription
153-
154-
then:
155-
connectionDescription == getExpectedConnectionDescription(connectionDescription.connectionId.localValue, 123)
156-
}
157-
158126
def 'should authenticate'() {
159127
given:
160128
def firstAuthenticator = Mock(Authenticator)
161129
def initializer = new InternalStreamConnectionInitializer(SINGLE, firstAuthenticator, null, [], null)
162130

163131
when:
164-
enqueueSuccessfulReplies(false, null)
132+
enqueueSuccessfulReplies(false, 123)
165133

166134
def internalDescription = initializer.startHandshake(internalConnection, operationContext)
167135
def connectionDescription = initializer.finishHandshake(internalConnection, internalDescription, operationContext)
@@ -178,7 +146,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
178146
def initializer = new InternalStreamConnectionInitializer(SINGLE, authenticator, null, [], null)
179147

180148
when:
181-
enqueueSuccessfulReplies(false, null)
149+
enqueueSuccessfulReplies(false, 123)
182150

183151
def futureCallback = new FutureResultCallback<InternalConnectionInitializationDescription>()
184152
initializer.startHandshakeAsync(internalConnection, operationContext, futureCallback)
@@ -198,7 +166,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
198166
def initializer = new InternalStreamConnectionInitializer(SINGLE, authenticator, null, [], null)
199167

200168
when:
201-
enqueueSuccessfulReplies(true, null)
169+
enqueueSuccessfulReplies(true, 123)
202170

203171
def internalDescription = initializer.startHandshake(internalConnection, operationContext)
204172
def connectionDescription = initializer.finishHandshake(internalConnection, internalDescription, operationContext)
@@ -215,7 +183,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
215183
def initializer = new InternalStreamConnectionInitializer(SINGLE, authenticator, null, [], null)
216184

217185
when:
218-
enqueueSuccessfulReplies(true, null)
186+
enqueueSuccessfulReplies(true, 123)
219187

220188
def futureCallback = new FutureResultCallback<InternalConnectionInitializationDescription>()
221189
initializer.startHandshakeAsync(internalConnection, operationContext, futureCallback)
@@ -240,7 +208,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
240208
}
241209

242210
when:
243-
enqueueSuccessfulReplies(false, null)
211+
enqueueSuccessfulReplies(false, 123)
244212
if (async) {
245213
def callback = new FutureResultCallback<InternalConnectionInitializationDescription>()
246214
initializer.startHandshakeAsync(internalConnection, operationContext, callback)
@@ -277,7 +245,7 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
277245
}
278246

279247
when:
280-
enqueueSuccessfulReplies(false, null)
248+
enqueueSuccessfulReplies(false, 123)
281249
if (async) {
282250
def callback = new FutureResultCallback<InternalConnectionInitializationDescription>()
283251
initializer.startHandshakeAsync(internalConnection, operationContext, callback)
@@ -477,25 +445,12 @@ class InternalStreamConnectionInitializerSpecification extends Specification {
477445
}
478446

479447
def enqueueSuccessfulReplies(final boolean isArbiter, final Integer serverConnectionId) {
480-
internalConnection.enqueueReply(buildSuccessfulReply(
481-
'{ok: 1, ' +
482-
'maxWireVersion: 3' +
483-
(isArbiter ? ', isreplicaset: true, arbiterOnly: true' : '') +
484-
'}'))
485-
internalConnection.enqueueReply(buildSuccessfulReply(
486-
'{ok: 1 ' +
487-
(serverConnectionId == null ? '' : ', connectionId: ' + serverConnectionId) +
488-
'}'))
489-
}
490-
491-
def enqueueSuccessfulRepliesWithConnectionIdIsHelloResponse(final boolean isArbiter, final Integer serverConnectionId) {
492448
internalConnection.enqueueReply(buildSuccessfulReply(
493449
'{ok: 1, ' +
494450
'maxWireVersion: 3,' +
495451
'connectionId: ' + serverConnectionId +
496452
(isArbiter ? ', isreplicaset: true, arbiterOnly: true' : '') +
497453
'}'))
498-
internalConnection.enqueueReply(buildSuccessfulReply('{ok: 1, versionArray : [3, 0, 0]}'))
499454
}
500455

501456
def enqueueSpeculativeAuthenticationResponsesForScramSha256() {

0 commit comments

Comments
 (0)