Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ dependencies {
implementation 'io.netty:netty-codec:latest.release'
implementation 'io.netty:netty-handler:latest.release'

testImplementation 'org.mongodb:mongo-java-driver:latest.release'
testImplementation 'org.mongodb:bson:latest.release'
testImplementation 'org.awaitility:awaitility:latest.release'
testImplementation "org.mockito:mockito-core:latest.release"
testImplementation "org.mockito:mockito-junit-jupiter:latest.release"
testImplementation "nl.jqno.equalsverifier:equalsverifier:latest.release"
Expand Down
4 changes: 3 additions & 1 deletion core/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ net.bytebuddy:byte-buddy:1.18.5=testCompileClasspath,testRuntimeClasspath
nl.jqno.equalsverifier:equalsverifier:4.5.2=testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.assertj:assertj-core:3.27.7=testCompileClasspath,testRuntimeClasspath
org.awaitility:awaitility:4.3.0=testCompileClasspath,testRuntimeClasspath
org.hamcrest:hamcrest:2.1=testCompileClasspath,testRuntimeClasspath
org.jacoco:org.jacoco.agent:0.8.15=jacocoAgent,jacocoAnt
org.jacoco:org.jacoco.ant:0.8.15=jacocoAnt
org.jacoco:org.jacoco.core:0.8.15=jacocoAnt
Expand All @@ -34,7 +36,7 @@ org.junit.platform:junit-platform-launcher:6.1.3=testRuntimeClasspath
org.junit:junit-bom:6.1.3=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-core:5.24.0=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-junit-jupiter:5.24.0=testCompileClasspath,testRuntimeClasspath
org.mongodb:mongo-java-driver:3.12.14=testCompileClasspath,testRuntimeClasspath
org.mongodb:bson:5.14.0=testCompileClasspath,testRuntimeClasspath
org.objenesis:objenesis:3.6=testCompileClasspath,testRuntimeClasspath
org.opentest4j:opentest4j:1.3.0=testCompileClasspath,testRuntimeClasspath
org.ow2.asm:asm-commons:9.10.1=jacocoAnt
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/de/bwaldvogel/mongo/MongoBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.wire.message.MongoMessage;
import de.bwaldvogel.mongo.wire.message.MongoQuery;
import io.netty.channel.Channel;

public interface MongoBackend {

void handleClose(Channel channel);
void handleClose();

Document handleCommand(Channel channel, String database, DatabaseCommand command, Document query);
Document handleCommand(String database, DatabaseCommand command, Document query);

QueryResult handleQuery(MongoQuery query);

Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/de/bwaldvogel/mongo/MongoDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.oplog.Oplog;
import de.bwaldvogel.mongo.wire.message.MongoQuery;
import io.netty.channel.Channel;

public interface MongoDatabase {

String getDatabaseName();

void handleClose(Channel channel);

Document handleCommand(Channel channel, DatabaseCommand command, Document query, DatabaseResolver databaseResolver, Oplog oplog);
Document handleCommand(DatabaseCommand command, Document query, DatabaseResolver databaseResolver, Oplog oplog);

QueryResult handleQuery(MongoQuery query);

Expand Down
20 changes: 12 additions & 8 deletions core/src/main/java/de/bwaldvogel/mongo/MongoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,7 @@ public void bind(SocketAddress socketAddress, int numberOfBossThreads, int numbe
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (sslContext != null) {
ch.pipeline().addLast(sslContext.newHandler(ch.alloc()));
}
ch.pipeline().addLast(new MongoWireReplyEncoder());
ch.pipeline().addLast(new MongoWireMessageEncoder());
ch.pipeline().addLast(new MongoWireProtocolHandler());
ch.pipeline().addLast(new MongoDatabaseHandler(backend, channelGroup));
ch.pipeline().addLast(new MongoExceptionHandler());
MongoServer.this.initChannel(ch);
}
});

Expand All @@ -113,6 +106,17 @@ public void initChannel(SocketChannel ch) throws Exception {
}
}

void initChannel(SocketChannel channel) {
if (sslContext != null) {
channel.pipeline().addLast(sslContext.newHandler(channel.alloc()));
}
channel.pipeline().addLast(new MongoWireReplyEncoder());
channel.pipeline().addLast(new MongoWireMessageEncoder());
channel.pipeline().addLast(new MongoWireProtocolHandler());
channel.pipeline().addLast(new MongoDatabaseHandler(backend, channelGroup));
channel.pipeline().addLast(new MongoExceptionHandler());
}

/**
* Starts and binds the server on a local random port
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.time.Clock;
import java.time.Duration;
Expand All @@ -24,8 +23,9 @@
import de.bwaldvogel.mongo.MongoVersion;
import de.bwaldvogel.mongo.ServerVersion;
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.ErrorCode;
import de.bwaldvogel.mongo.exception.MongoServerError;
import de.bwaldvogel.mongo.exception.MongoServerException;
import de.bwaldvogel.mongo.exception.MongoSilentServerException;
import de.bwaldvogel.mongo.exception.NamespaceExistsException;
import de.bwaldvogel.mongo.exception.NoReplicationEnabledException;
import de.bwaldvogel.mongo.exception.NoSuchCommandException;
Expand All @@ -37,7 +37,6 @@
import de.bwaldvogel.mongo.wire.message.Message;
import de.bwaldvogel.mongo.wire.message.MongoMessage;
import de.bwaldvogel.mongo.wire.message.MongoQuery;
import io.netty.channel.Channel;

public abstract class AbstractMongoBackend implements MongoBackend {

Expand Down Expand Up @@ -146,7 +145,7 @@ private Document getLog(String argument) {
Utils.markOkay(response);
break;
default:
throw new MongoSilentServerException("no RamLog named: " + argument);
throw new MongoServerError(ErrorCode.OperationFailed, "No log named '" + argument + "'");
}
return response;
}
Expand All @@ -161,14 +160,9 @@ private Document handleAdminCommand(DatabaseCommand command, Document query) {
yield getLog(argument == null ? null : argument.toString());
}
case RENAME_COLLECTION -> handleRenameCollection(command.getQueryValue(), query);
case GET_LAST_ERROR -> {
log.debug("getLastError on admin database");
yield successResponse();
}
case CONNECTION_STATUS -> handleConnectionStatus();
case HOST_INFO -> handleHostInfo();
case GET_CMD_LINE_OPTS -> handleGetCmdLineOpts();
case GET_FREE_MONITORING_STATUS -> handleGetFreeMonitoringStatus();
case END_SESSIONS -> {
log.debug("endSessions on admin database");
yield successResponse();
Expand Down Expand Up @@ -247,15 +241,6 @@ private Document handleGetCmdLineOpts() {
return response;
}

private Document handleGetFreeMonitoringStatus() {
Document response = new Document();
response.append("state", "disabled");
response.append("debug", Map.of("state", "undecided"));
response.append("message", "Free monitoring is deprecated, refer to 'debug' field for actual status");
Utils.markOkay(response);
return response;
}

@VisibleForExternalBackends
protected Set<String> listDatabaseNames() {
return databases.keySet();
Expand Down Expand Up @@ -307,9 +292,8 @@ private MongoCollection<?> resolveCollection(String namespace) {
protected abstract MongoDatabase openOrCreateDatabase(String databaseName);

@Override
public Document handleCommand(Channel channel, String databaseName, DatabaseCommand command, Document query) {
public Document handleCommand(String databaseName, DatabaseCommand command, Document query) {
return switch (command.getCommand()) {
case WHATS_MY_URI -> handleWhatsMyUri(channel);
case IS_MASTER -> handleIsMaster();
case BUILD_INFO -> handleBuildInfo();
case DROP_DATABASE -> handleDropDatabase(databaseName);
Expand All @@ -322,7 +306,7 @@ public Document handleCommand(Channel channel, String databaseName, DatabaseComm
yield handleAdminCommand(command, query);
}
MongoDatabase mongoDatabase = resolveDatabase(databaseName);
yield mongoDatabase.handleCommand(channel, command, query, this::resolveDatabase, oplog);
yield mongoDatabase.handleCommand(command, query, this::resolveDatabase, oplog);
}
};
}
Expand All @@ -343,14 +327,6 @@ public void closeCursors(List<Long> cursorIds) {
cursorIds.forEach(cursorRegistry::remove);
}

private static Document handleWhatsMyUri(Channel channel) {
Document response = new Document();
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
response.put("you", remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort());
Utils.markOkay(response);
return response;
}

private Document handleIsMaster() {
Document response = new Document("ismaster", Boolean.TRUE);
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
Expand Down Expand Up @@ -422,11 +398,10 @@ protected Document handleDropDatabase(String databaseName) {

@Override
public Document handleMessage(MongoMessage message) {
Channel channel = message.getChannel();
String databaseName = message.getDatabaseName();
Document query = message.getDocument();
DatabaseCommand command = DatabaseCommand.of(query.keySet().iterator().next());
return handleCommand(channel, databaseName, command, query);
return handleCommand(databaseName, command, query);
}

@Override
Expand All @@ -438,10 +413,7 @@ public void dropDatabase(String databaseName) {
}

@Override
public void handleClose(Channel channel) {
for (MongoDatabase db : databases.values()) {
db.handleClose(channel);
}
public void handleClose() {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.bwaldvogel.mongo.backend.Constants.ID_FIELD;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -462,6 +463,7 @@ public Document findAndModify(Document query) {

public static class FindAndModifyPlanExecutorError extends MongoServerError {

@Serial
private static final long serialVersionUID = 1L;

private static final String PREFIX = "Plan executor error during findAndModify :: caused by :: ";
Expand Down
Loading
Loading