Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.mongodb.internal.connection;

import com.mongodb.MongoNamespace;
import com.mongodb.MongoServerException;
import com.mongodb.ServerApi;
import com.mongodb.connection.ClusterConnectionMode;
Expand All @@ -31,7 +30,6 @@

import java.util.Locale;

import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;
import static com.mongodb.ReadPreference.primary;
import static com.mongodb.assertions.Assertions.assertNotNull;

Expand Down Expand Up @@ -107,7 +105,7 @@ private static CommandMessage getCommandMessage(final String database, final Bso
final InternalConnection internalConnection,
final ClusterConnectionMode clusterConnectionMode,
@Nullable final ServerApi serverApi) {
return new CommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME), command, NoOpFieldNameValidator.INSTANCE, primary(),
return new CommandMessage(database, command, NoOpFieldNameValidator.INSTANCE, primary(),
MessageSettings
.builder()
// Note: server version will be 0.0 at this point when called from InternalConnectionInitializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public final class CommandMessage extends RequestMessage {
*/
private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1;

private final MongoNamespace namespace;
private final BsonDocument command;
private final FieldNameValidator commandFieldNameValidator;
private final ReadPreference readPreference;
private final boolean exhaustAllowed;
private final MessageSequences sequences;
private final boolean responseExpected;
private final String database;
/**
* {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
* or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata(ByteBufferBsonOutput, OperationContext) encoded}.
Expand All @@ -93,35 +93,35 @@ public final class CommandMessage extends RequestMessage {
private final ClusterConnectionMode clusterConnectionMode;
private final ServerApi serverApi;

CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
final ReadPreference readPreference, final MessageSettings settings, final ClusterConnectionMode clusterConnectionMode,
@Nullable final ServerApi serverApi) {
this(namespace, command, commandFieldNameValidator, readPreference, settings, true, EmptyMessageSequences.INSTANCE,
this(database, command, commandFieldNameValidator, readPreference, settings, true, EmptyMessageSequences.INSTANCE,
clusterConnectionMode, serverApi);
}

CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
final ReadPreference readPreference, final MessageSettings settings, final boolean exhaustAllowed,
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
this(namespace, command, commandFieldNameValidator, readPreference, settings, true, exhaustAllowed, EmptyMessageSequences.INSTANCE,
this(database, command, commandFieldNameValidator, readPreference, settings, true, exhaustAllowed, EmptyMessageSequences.INSTANCE,
clusterConnectionMode, serverApi);
}

CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
final ReadPreference readPreference, final MessageSettings settings, final boolean responseExpected,
final MessageSequences sequences,
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
this(namespace, command, commandFieldNameValidator, readPreference, settings, responseExpected, false,
this(database, command, commandFieldNameValidator, readPreference, settings, responseExpected, false,
sequences, clusterConnectionMode, serverApi);
}

CommandMessage(final MongoNamespace namespace, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
CommandMessage(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
final ReadPreference readPreference, final MessageSettings settings,
final boolean responseExpected, final boolean exhaustAllowed,
final MessageSequences sequences,
final ClusterConnectionMode clusterConnectionMode, @Nullable final ServerApi serverApi) {
super(namespace.getFullName(), getOpCode(settings, clusterConnectionMode, serverApi), settings);
this.namespace = namespace;
super(getOpCode(settings, clusterConnectionMode, serverApi), settings);
this.database = database;
this.command = command;
this.commandFieldNameValidator = commandFieldNameValidator;
this.readPreference = readPreference;
Expand Down Expand Up @@ -222,10 +222,6 @@ boolean isResponseExpected() {
}
}

MongoNamespace getNamespace() {
return namespace;
}

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
int commandStartPosition = useOpMsg() ? writeOpMsg(bsonOutput, operationContext) : writeOpQuery(bsonOutput);
Expand Down Expand Up @@ -281,7 +277,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon

private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
bsonOutput.writeInt32(0);
bsonOutput.writeCString(namespace.getFullName());
bsonOutput.writeCString(new MongoNamespace(getDatabase(), "$cmd").getFullName());
bsonOutput.writeInt32(0);
bsonOutput.writeInt32(-1);

Expand Down Expand Up @@ -328,7 +324,7 @@ private List<BsonElement> getExtraElements(final OperationContext operationConte
extraElements.add(new BsonElement("maxTimeMS", new BsonInt64(maxTimeMS)))
);
}
extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName())));
extraElements.add(new BsonElement("$db", new BsonString(getDatabase())));
if (sessionContext.getClusterTime() != null) {
extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime()));
}
Expand Down Expand Up @@ -411,6 +407,15 @@ private static boolean isServerVersionKnown(final MessageSettings settings) {
return settings.getMaxWireVersion() != UNKNOWN_WIRE_VERSION;
}

/**
* Gets the collection name, which may be null for some message types
*
* @return the collection name
*/
public String getDatabase() {
return database;
}

@FunctionalInterface
private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
void close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.mongodb.internal.connection;

import com.mongodb.MongoNamespace;
import com.mongodb.ReadPreference;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.internal.async.SingleResultCallback;
Expand All @@ -30,7 +29,7 @@
import static com.mongodb.internal.connection.ProtocolHelper.getMessageSettings;

class CommandProtocolImpl<T> implements CommandProtocol<T> {
private final MongoNamespace namespace;
private final String database;
private final BsonDocument command;
private final MessageSequences sequences;
private final ReadPreference readPreference;
Expand All @@ -44,7 +43,7 @@ class CommandProtocolImpl<T> implements CommandProtocol<T> {
@Nullable final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final boolean responseExpected,
final MessageSequences sequences, final ClusterConnectionMode clusterConnectionMode, final OperationContext operationContext) {
notNull("database", database);
this.namespace = new MongoNamespace(notNull("database", database), MongoNamespace.COMMAND_COLLECTION_NAME);
this.database = notNull("database", database);
this.command = notNull("command", command);
this.commandFieldNameValidator = notNull("commandFieldNameValidator", commandFieldNameValidator);
this.readPreference = readPreference;
Expand Down Expand Up @@ -79,13 +78,13 @@ public void executeAsync(final InternalConnection connection, final SingleResult

@Override
public CommandProtocolImpl<T> withSessionContext(final SessionContext sessionContext) {
return new CommandProtocolImpl<>(namespace.getDatabaseName(), command, commandFieldNameValidator, readPreference,
return new CommandProtocolImpl<>(database, command, commandFieldNameValidator, readPreference,
commandResultDecoder, responseExpected, sequences, clusterConnectionMode,
operationContext.withSessionContext(sessionContext));
}

private CommandMessage getCommandMessage(final InternalConnection connection) {
return new CommandMessage(namespace, command, commandFieldNameValidator, readPreference,
return new CommandMessage(database, command, commandFieldNameValidator, readPreference,
getMessageSettings(connection.getDescription(), connection.getInitialServerDescription()), responseExpected,
sequences, clusterConnectionMode, operationContext.getServerApi());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.mongodb.internal.connection;

import com.mongodb.MongoInterruptedException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoSocketException;
import com.mongodb.ServerApi;
import com.mongodb.annotations.ThreadSafe;
Expand Down Expand Up @@ -51,7 +50,6 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;
import static com.mongodb.ReadPreference.primary;
import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.assertions.Assertions.fail;
Expand Down Expand Up @@ -381,7 +379,7 @@ private boolean shouldStreamResponses(final ServerDescription currentServerDescr

private CommandMessage createCommandMessage(final BsonDocument command, final InternalConnection connection,
final ServerDescription currentServerDescription) {
return new CommandMessage(new MongoNamespace("admin", COMMAND_COLLECTION_NAME), command,
return new CommandMessage("admin", command,
NoOpFieldNameValidator.INSTANCE, primary(),
MessageSettings.builder()
.maxWireVersion(connection.getDescription().getMaxWireVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void sendStartedEvent() {

logEventMessage(messagePrefix, "Command started", null, entries -> {
entries.add(new Entry(COMMAND_NAME, commandName));
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
entries.add(new Entry(DATABASE_NAME, message.getDatabase()));
},
entries -> entries.add(new Entry(COMMAND_CONTENT, command)));
}
Expand All @@ -111,7 +111,7 @@ public void sendStartedEvent() {
BsonDocument commandDocumentForEvent = redactionRequired
? new BsonDocument() : commandDocument;

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

if (eventRequired()) {
sendCommandFailedEvent(message, commandName, message.getNamespace().getDatabaseName(), description, elapsedTimeNanos,
sendCommandFailedEvent(message, commandName, message.getDatabase(), description, elapsedTimeNanos,
commandEventException, commandListener, operationContext);
}
}
Expand Down Expand Up @@ -170,15 +170,15 @@ private void sendSucceededEvent(final BsonDocument reply) {
logEventMessage("Command succeeded", null,
entries -> {
entries.add(new Entry(COMMAND_NAME, commandName));
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
entries.add(new Entry(DATABASE_NAME, message.getDatabase()));
entries.add(new Entry(DURATION_MS, elapsedTimeNanos / NANOS_PER_MILLI));
},
entries -> entries.add(new Entry(REPLY, replyString)), format);
}

if (eventRequired()) {
BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : reply;
sendCommandSucceededEvent(message, commandName, message.getNamespace().getDatabaseName(), responseDocumentForEvent,
sendCommandSucceededEvent(message, commandName, message.getDatabase(), responseDocumentForEvent,
description, elapsedTimeNanos, commandListener, operationContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.mongodb.internal.connection;

import com.mongodb.lang.Nullable;
import org.bson.BsonBinaryWriter;
import org.bson.BsonDocument;
import org.bson.FieldNameValidator;
Expand All @@ -38,7 +37,6 @@ abstract class RequestMessage {

static final int MESSAGE_PROLOGUE_LENGTH = 16;

private final String collectionName;
private final MessageSettings settings;
private final int id;
private final OpCode opCode;
Expand All @@ -64,18 +62,11 @@ public static int getCurrentGlobalId() {
return REQUEST_ID.get();
}

RequestMessage(final OpCode opCode, final int requestId, final MessageSettings settings) {
this(null, opCode, requestId, settings);
RequestMessage(final OpCode opCode, final MessageSettings settings) {
this(opCode, REQUEST_ID.getAndIncrement(), settings);
}


RequestMessage(final String collectionName, final OpCode opCode, final MessageSettings settings) {
this(collectionName, opCode, REQUEST_ID.getAndIncrement(), settings);
}

private RequestMessage(@Nullable final String collectionName, final OpCode opCode, final int requestId,
final MessageSettings settings) {
this.collectionName = collectionName;
RequestMessage(final OpCode opCode, final int requestId, final MessageSettings settings) {
this.settings = settings;
id = requestId;
this.opCode = opCode;
Expand Down Expand Up @@ -159,13 +150,4 @@ protected int writeDocument(final BsonDocument document, final BsonOutput bsonOu
encodeUsingRegistry(writer, document);
return bsonOutput.getPosition() - documentStart;
}

/**
* Gets the collection name, which may be null for some message types
*
* @return the collection name
*/
protected String getCollectionName() {
return collectionName;
}
}
Loading