Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import de.bwaldvogel.mongo.MongoVersion;
import de.bwaldvogel.mongo.ServerVersion;
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.bson.ObjectId;
import de.bwaldvogel.mongo.exception.ErrorCode;
import de.bwaldvogel.mongo.exception.MongoServerError;
import de.bwaldvogel.mongo.exception.MongoServerException;
Expand All @@ -46,12 +47,15 @@ public abstract class AbstractMongoBackend implements MongoBackend {

static final String ADMIN_DB_NAME = "admin";

private static final int LOGICAL_SESSION_TIMEOUT_MINUTES = 30;

private final Map<String, MongoDatabase> databases = new ConcurrentHashMap<>();

private MongoVersion version = ServerVersion.MONGO_7_0;

private final Clock clock;
private final Instant started;
private final ObjectId processId = new ObjectId();

private final CursorRegistry cursorRegistry = new CursorRegistry();

Expand Down Expand Up @@ -295,6 +299,7 @@ private MongoCollection<?> resolveCollection(String namespace) {
public Document handleCommand(String databaseName, DatabaseCommand command, Document query) {
return switch (command.getCommand()) {
case IS_MASTER -> handleIsMaster();
case HELLO -> handleHello();
case BUILD_INFO -> handleBuildInfo();
case DROP_DATABASE -> handleDropDatabase(databaseName);
case GET_MORE -> handleGetMore(databaseName, command.getQueryValue(), query);
Expand Down Expand Up @@ -339,6 +344,24 @@ private Document handleIsMaster() {
return response;
}

private Document handleHello() {
Document response = new Document("isWritablePrimary", Boolean.TRUE);
response.put("topologyVersion", new Document()
.append("processId", processId)
.append("counter", 0L));
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
response.put("maxWriteBatchSize", Integer.valueOf(MongoWireProtocolHandler.MAX_WRITE_BATCH_SIZE));
response.put("maxMessageSizeBytes", Integer.valueOf(MongoWireProtocolHandler.MAX_MESSAGE_SIZE_BYTES));
response.put("maxWireVersion", Integer.valueOf(version.getWireVersion()));
response.put("minWireVersion", Integer.valueOf(0));
response.put("localTime", Instant.now(clock));
response.put("logicalSessionTimeoutMinutes", Integer.valueOf(LOGICAL_SESSION_TIMEOUT_MINUTES));
response.put("connectionId", Integer.valueOf(1));
response.put("readOnly", false);
Utils.markOkay(response);
return response;
}

private Document handleBuildInfo() {
Document response = new Document("version", version.toVersionString());
response.put("versionArray", version.getVersionArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Command {
UPDATE("update"),

// Replication
HELLO("hello"),
IS_MASTER("isMaster"),
REPL_SET_GET_STATUS("replSetGetStatus"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3907,6 +3907,30 @@ void testIsMaster() {
assertThat(isMaster.getInteger("maxMessageSizeBytes")).isGreaterThan(maxBsonObjectSize);
}

@Test
public void testHello() {
Document hello = runCommand(Command.HELLO);
assertThat(hello)
.containsOnlyKeys("topologyVersion", "isWritablePrimary", "maxBsonObjectSize", "maxMessageSizeBytes",
"maxWriteBatchSize", "localTime", "logicalSessionTimeoutMinutes", "connectionId", "minWireVersion",
"maxWireVersion", "readOnly", "ok")
.containsEntry("isWritablePrimary", true)
.containsEntry("readOnly", false)
.containsEntry("maxBsonObjectSize", 16777216)
.containsEntry("maxMessageSizeBytes", 48000000)
.containsEntry("minWireVersion", 0)
.containsEntry("ok", 1.0);
assertThat(hello.getDate("localTime")).isInstanceOf(Date.class);
assertThat(hello.getInteger("maxWireVersion")).isPositive();
assertThat(hello.getInteger("maxWriteBatchSize")).isPositive();
}

@Test
void testHelloOnNonAdminDatabase() {
Document hello = db.runCommand(new Document("hello", 1).append("comment", "test"));
assertThat(hello.getBoolean("isWritablePrimary")).isTrue();
}

// https://github.com/foursquare/fongo/pull/26
// http://stackoverflow.com/questions/12403240/storing-null-vs-not-storing-the-key-at-all-in-mongodb
@Test
Expand Down
Loading