Skip to content

Commit 04996f0

Browse files
authored
Remove internal use of MongoNamespace.COMMAND_COLLECTION_NAME (#1782)
This is a remnant of the pre-OP_MSG wire protocol, which required a synthetic collection name for most commands. It's no longer needed, except for the last remaining bit where the driver uses OP_QUERY for the first message in the connection handshake. As part of this, RequestMessage and CommandMessage have been simplified. JAVA-5939
1 parent 7fa05cd commit 04996f0

File tree

20 files changed

+101
-125
lines changed

20 files changed

+101
-125
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.mongodb.internal.connection;
1818

19-
import com.mongodb.MongoNamespace;
2019
import com.mongodb.MongoServerException;
2120
import com.mongodb.ServerApi;
2221
import com.mongodb.connection.ClusterConnectionMode;
@@ -31,7 +30,6 @@
3130

3231
import java.util.Locale;
3332

34-
import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;
3533
import static com.mongodb.ReadPreference.primary;
3634
import static com.mongodb.assertions.Assertions.assertNotNull;
3735

@@ -107,7 +105,7 @@ private static CommandMessage getCommandMessage(final String database, final Bso
107105
final InternalConnection internalConnection,
108106
final ClusterConnectionMode clusterConnectionMode,
109107
@Nullable final ServerApi serverApi) {
110-
return new CommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME), command, NoOpFieldNameValidator.INSTANCE, primary(),
108+
return new CommandMessage(database, command, NoOpFieldNameValidator.INSTANCE, primary(),
111109
MessageSettings
112110
.builder()
113111
// Note: server version will be 0.0 at this point when called from InternalConnectionInitializer,

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ public final class CommandMessage extends RequestMessage {
7777
*/
7878
private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1;
7979

80-
private final MongoNamespace namespace;
8180
private final BsonDocument command;
8281
private final FieldNameValidator commandFieldNameValidator;
8382
private final ReadPreference readPreference;
8483
private final boolean exhaustAllowed;
8584
private final MessageSequences sequences;
8685
private final boolean responseExpected;
86+
private final String database;
8787
/**
8888
* {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
8989
* or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata(ByteBufferBsonOutput, OperationContext) encoded}.
@@ -93,35 +93,35 @@ public final class CommandMessage extends RequestMessage {
9393
private final ClusterConnectionMode clusterConnectionMode;
9494
private final ServerApi serverApi;
9595

96-
CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
96+
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
9797
final ReadPreference readPreference, final MessageSettings settings, final ClusterConnectionMode clusterConnectionMode,
9898
@Nullable final ServerApi serverApi) {
99-
this(namespace, command, commandFieldNameValidator, readPreference, settings, true, EmptyMessageSequences.INSTANCE,
99+
this(database, command, commandFieldNameValidator, readPreference, settings, true, EmptyMessageSequences.INSTANCE,
100100
clusterConnectionMode, serverApi);
101101
}
102102

103-
CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
103+
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
104104
final ReadPreference readPreference, final MessageSettings settings, final boolean exhaustAllowed,
105105
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
106-
this(namespace, command, commandFieldNameValidator, readPreference, settings, true, exhaustAllowed, EmptyMessageSequences.INSTANCE,
106+
this(database, command, commandFieldNameValidator, readPreference, settings, true, exhaustAllowed, EmptyMessageSequences.INSTANCE,
107107
clusterConnectionMode, serverApi);
108108
}
109109

110-
CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
110+
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
111111
final ReadPreference readPreference, final MessageSettings settings, final boolean responseExpected,
112112
final MessageSequences sequences,
113113
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
114-
this(namespace, command, commandFieldNameValidator, readPreference, settings, responseExpected, false,
114+
this(database, command, commandFieldNameValidator, readPreference, settings, responseExpected, false,
115115
sequences, clusterConnectionMode, serverApi);
116116
}
117117

118-
CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
118+
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
119119
final ReadPreference readPreference, final MessageSettings settings,
120120
final boolean responseExpected, final boolean exhaustAllowed,
121121
final MessageSequences sequences,
122122
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
123-
super(namespace.getFullName(), getOpCode(settings, clusterConnectionMode, serverApi), settings);
124-
this.namespace = namespace;
123+
super(getOpCode(settings, clusterConnectionMode, serverApi), settings);
124+
this.database = database;
125125
this.command = command;
126126
this.commandFieldNameValidator = commandFieldNameValidator;
127127
this.readPreference = readPreference;
@@ -222,10 +222,6 @@ boolean isResponseExpected() {
222222
}
223223
}
224224

225-
MongoNamespace getNamespace() {
226-
return namespace;
227-
}
228-
229225
@Override
230226
protected EncodingMetadata encodeMessageBodyWithMetadata(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
231227
int commandStartPosition = useOpMsg() ? writeOpMsg(bsonOutput, operationContext) : writeOpQuery(bsonOutput);
@@ -281,7 +277,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
281277

282278
private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
283279
bsonOutput.writeInt32(0);
284-
bsonOutput.writeCString(namespace.getFullName());
280+
bsonOutput.writeCString(new MongoNamespace(getDatabase(), "$cmd").getFullName());
285281
bsonOutput.writeInt32(0);
286282
bsonOutput.writeInt32(-1);
287283

@@ -328,7 +324,7 @@ private List<BsonElement> getExtraElements(final OperationContext operationConte
328324
extraElements.add(new BsonElement("maxTimeMS", new BsonInt64(maxTimeMS)))
329325
);
330326
}
331-
extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName())));
327+
extraElements.add(new BsonElement("$db", new BsonString(getDatabase())));
332328
if (sessionContext.getClusterTime() != null) {
333329
extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime()));
334330
}
@@ -411,6 +407,15 @@ private static boolean isServerVersionKnown(final MessageSettings settings) {
411407
return settings.getMaxWireVersion() != UNKNOWN_WIRE_VERSION;
412408
}
413409

410+
/**
411+
* Gets the database name
412+
*
413+
* @return the database name
414+
*/
415+
public String getDatabase() {
416+
return database;
417+
}
418+
414419
@FunctionalInterface
415420
private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
416421
void close();

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.mongodb.internal.connection;
1818

19-
import com.mongodb.MongoNamespace;
2019
import com.mongodb.ReadPreference;
2120
import com.mongodb.connection.ClusterConnectionMode;
2221
import com.mongodb.internal.async.SingleResultCallback;
@@ -30,7 +29,7 @@
3029
import static com.mongodb.internal.connection.ProtocolHelper.getMessageSettings;
3130

3231
class CommandProtocolImpl<T> implements CommandProtocol<T> {
33-
private final MongoNamespace namespace;
32+
private final String database;
3433
private final BsonDocument command;
3534
private final MessageSequences sequences;
3635
private final ReadPreference readPreference;
@@ -44,7 +43,7 @@ class CommandProtocolImpl<T> implements CommandProtocol<T> {
4443
@Nullable final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final boolean responseExpected,
4544
final MessageSequences sequences, final ClusterConnectionMode clusterConnectionMode, final OperationContext operationContext) {
4645
notNull("database", database);
47-
this.namespace = new MongoNamespace(notNull("database", database), MongoNamespace.COMMAND_COLLECTION_NAME);
46+
this.database = notNull("database", database);
4847
this.command = notNull("command", command);
4948
this.commandFieldNameValidator = notNull("commandFieldNameValidator", commandFieldNameValidator);
5049
this.readPreference = readPreference;
@@ -79,13 +78,13 @@ public void executeAsync(final InternalConnection connection, final SingleResult
7978

8079
@Override
8180
public CommandProtocolImpl<T> withSessionContext(final SessionContext sessionContext) {
82-
return new CommandProtocolImpl<>(namespace.getDatabaseName(), command, commandFieldNameValidator, readPreference,
81+
return new CommandProtocolImpl<>(database, command, commandFieldNameValidator, readPreference,
8382
commandResultDecoder, responseExpected, sequences, clusterConnectionMode,
8483
operationContext.withSessionContext(sessionContext));
8584
}
8685

8786
private CommandMessage getCommandMessage(final InternalConnection connection) {
88-
return new CommandMessage(namespace, command, commandFieldNameValidator, readPreference,
87+
return new CommandMessage(database, command, commandFieldNameValidator, readPreference,
8988
getMessageSettings(connection.getDescription(), connection.getInitialServerDescription()), responseExpected,
9089
sequences, clusterConnectionMode, operationContext.getServerApi());
9190
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.mongodb.internal.connection;
1818

1919
import com.mongodb.MongoInterruptedException;
20-
import com.mongodb.MongoNamespace;
2120
import com.mongodb.MongoSocketException;
2221
import com.mongodb.ServerApi;
2322
import com.mongodb.annotations.ThreadSafe;
@@ -51,7 +50,6 @@
5150
import java.util.concurrent.locks.Lock;
5251
import java.util.concurrent.locks.ReentrantLock;
5352

54-
import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;
5553
import static com.mongodb.ReadPreference.primary;
5654
import static com.mongodb.assertions.Assertions.assertNotNull;
5755
import static com.mongodb.assertions.Assertions.fail;
@@ -381,7 +379,7 @@ private boolean shouldStreamResponses(final ServerDescription currentServerDescr
381379

382380
private CommandMessage createCommandMessage(final BsonDocument command, final InternalConnection connection,
383381
final ServerDescription currentServerDescription) {
384-
return new CommandMessage(new MongoNamespace("admin", COMMAND_COLLECTION_NAME), command,
382+
return new CommandMessage("admin", command,
385383
NoOpFieldNameValidator.INSTANCE, primary(),
386384
MessageSettings.builder()
387385
.maxWireVersion(connection.getDescription().getMaxWireVersion())

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void sendStartedEvent() {
102102

103103
logEventMessage(messagePrefix, "Command started", null, entries -> {
104104
entries.add(new Entry(COMMAND_NAME, commandName));
105-
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
105+
entries.add(new Entry(DATABASE_NAME, message.getDatabase()));
106106
},
107107
entries -> entries.add(new Entry(COMMAND_CONTENT, command)));
108108
}
@@ -111,7 +111,7 @@ public void sendStartedEvent() {
111111
BsonDocument commandDocumentForEvent = redactionRequired
112112
? new BsonDocument() : commandDocument;
113113

114-
sendCommandStartedEvent(message, message.getNamespace().getDatabaseName(), commandName, commandDocumentForEvent, description,
114+
sendCommandStartedEvent(message, message.getDatabase(), commandName, commandDocumentForEvent, description,
115115
assertNotNull(commandListener), operationContext);
116116
}
117117
// the buffer underlying the command document may be released after the started event, so set to null to ensure it's not used
@@ -134,14 +134,14 @@ public void sendFailedEvent(final Throwable t) {
134134
logEventMessage(messagePrefix, "Command failed", commandEventException,
135135
entries -> {
136136
entries.add(new Entry(COMMAND_NAME, commandName));
137-
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
137+
entries.add(new Entry(DATABASE_NAME, message.getDatabase()));
138138
entries.add(new Entry(DURATION_MS, elapsedTimeNanos / NANOS_PER_MILLI));
139139
},
140140
entries -> entries.add(new Entry(COMMAND_CONTENT, null)));
141141
}
142142

143143
if (eventRequired()) {
144-
sendCommandFailedEvent(message, commandName, message.getNamespace().getDatabaseName(), description, elapsedTimeNanos,
144+
sendCommandFailedEvent(message, commandName, message.getDatabase(), description, elapsedTimeNanos,
145145
commandEventException, commandListener, operationContext);
146146
}
147147
}
@@ -170,15 +170,15 @@ private void sendSucceededEvent(final BsonDocument reply) {
170170
logEventMessage("Command succeeded", null,
171171
entries -> {
172172
entries.add(new Entry(COMMAND_NAME, commandName));
173-
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
173+
entries.add(new Entry(DATABASE_NAME, message.getDatabase()));
174174
entries.add(new Entry(DURATION_MS, elapsedTimeNanos / NANOS_PER_MILLI));
175175
},
176176
entries -> entries.add(new Entry(REPLY, replyString)), format);
177177
}
178178

179179
if (eventRequired()) {
180180
BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : reply;
181-
sendCommandSucceededEvent(message, commandName, message.getNamespace().getDatabaseName(), responseDocumentForEvent,
181+
sendCommandSucceededEvent(message, commandName, message.getDatabase(), responseDocumentForEvent,
182182
description, elapsedTimeNanos, commandListener, operationContext);
183183
}
184184
}

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.mongodb.internal.connection;
1818

19-
import com.mongodb.lang.Nullable;
2019
import org.bson.BsonBinaryWriter;
2120
import org.bson.BsonDocument;
2221
import org.bson.FieldNameValidator;
@@ -38,7 +37,6 @@ abstract class RequestMessage {
3837

3938
static final int MESSAGE_PROLOGUE_LENGTH = 16;
4039

41-
private final String collectionName;
4240
private final MessageSettings settings;
4341
private final int id;
4442
private final OpCode opCode;
@@ -64,18 +62,11 @@ public static int getCurrentGlobalId() {
6462
return REQUEST_ID.get();
6563
}
6664

67-
RequestMessage(final OpCode opCode, final int requestId, final MessageSettings settings) {
68-
this(null, opCode, requestId, settings);
65+
RequestMessage(final OpCode opCode, final MessageSettings settings) {
66+
this(opCode, REQUEST_ID.getAndIncrement(), settings);
6967
}
7068

71-
72-
RequestMessage(final String collectionName, final OpCode opCode, final MessageSettings settings) {
73-
this(collectionName, opCode, REQUEST_ID.getAndIncrement(), settings);
74-
}
75-
76-
private RequestMessage(@Nullable final String collectionName, final OpCode opCode, final int requestId,
77-
final MessageSettings settings) {
78-
this.collectionName = collectionName;
69+
RequestMessage(final OpCode opCode, final int requestId, final MessageSettings settings) {
7970
this.settings = settings;
8071
id = requestId;
8172
this.opCode = opCode;
@@ -159,13 +150,4 @@ protected int writeDocument(final BsonDocument document, final BsonOutput bsonOu
159150
encodeUsingRegistry(writer, document);
160151
return bsonOutput.getPosition() - documentStart;
161152
}
162-
163-
/**
164-
* Gets the collection name, which may be null for some message types
165-
*
166-
* @return the collection name
167-
*/
168-
protected String getCollectionName() {
169-
return collectionName;
170-
}
171153
}

0 commit comments

Comments
 (0)