Skip to content

Commit

Permalink
Upgrade to MongoDB 7.0.4 (#415)
Browse files Browse the repository at this point in the history
upgrade to Mongo 7.0.4
  • Loading branch information
Xiangs18 authored Jan 18, 2024
1 parent 1408e96 commit 5af2e93
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
# doesn't touch mongo against multiple mongo versions
include:
- java: '8'
mongo: 'mongodb-linux-x86_64-3.6.23'
mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4'
wired_tiger: 'false'
- java: '11'
mongo: 'mongodb-linux-x86_64-3.6.23'
Expand Down
5 changes: 4 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
<include name="google/guava-18.0.jar"/>
<include name="ini4j/ini4j-0.5.2.jar"/>
<include name="jcommander/jcommander-1.48.jar"/>
<include name="mongo/mongo-java-driver-3.8.2.jar"/>
<include name="mongo/mongodb-driver-core-4.11.1.jar"/>
<include name="mongo/mongodb-driver-sync-4.11.1.jar"/>
<include name="bson/bson-record-codec-4.11.1.jar"/>
<include name="bson/bson-4.11.1.jar"/>
<include name="mustache/compiler-0.9.3.jar"/>
<include name="nulab-inc/zxcvbn-1.2.2.jar"/>
<include name="jsemver/java-semver-0.9.0.jar"/>
Expand Down
14 changes: 9 additions & 5 deletions src/us/kbase/auth2/service/AuthBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
Expand Down Expand Up @@ -62,15 +64,17 @@ public AuthBuilder(

private MongoClient buildMongo(final AuthStartupConfig c) throws StorageInitException {
//TODO ZLATER MONGO handle shards & replica sets
final MongoClientSettings.Builder mongoBuilder = MongoClientSettings.builder().applyToClusterSettings(
builder -> builder.hosts(Arrays.asList(new ServerAddress(c.getMongoHost()))));

try {
if (c.getMongoUser().isPresent()) {
final MongoCredential creds = MongoCredential.createCredential(
c.getMongoUser().get(), c.getMongoDatabase(), c.getMongoPwd().get());
// unclear if and when it's safe to clear the password
return new MongoClient(new ServerAddress(c.getMongoHost()), creds,
MongoClientOptions.builder().build());
return MongoClients.create(mongoBuilder.credential(creds).build());
} else {
return new MongoClient(new ServerAddress(c.getMongoHost()));
return MongoClients.create(mongoBuilder.build());
}
} catch (MongoException e) {
LoggerFactory.getLogger(getClass()).error(
Expand Down
2 changes: 1 addition & 1 deletion src/us/kbase/auth2/service/AuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoClient;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
Expand Down
150 changes: 150 additions & 0 deletions src/us/kbase/test/auth2/MongoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package us.kbase.test.auth2;

import static us.kbase.common.test.controllers.ControllerCommon.checkExe;
import static us.kbase.common.test.controllers.ControllerCommon.findFreePort;
import static us.kbase.common.test.controllers.ControllerCommon.makeTempDirs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

import com.github.zafarkhaja.semver.Version;
import org.apache.commons.io.FileUtils;


/** Q&D Utility to run a Mongo server for the purposes of testing from
* Java.
* @author [email protected], [email protected]
*
*/
public class MongoController {

private final static String DATA_DIR = "data";

private final static List<String> tempDirectories =
new LinkedList<String>();
static {
tempDirectories.add(DATA_DIR);
}

private final static Version MONGO_DB_6_1 =
Version.forIntegers(6,1);

private final Path tempDir;

private final Process mongo;

private final int port;

public MongoController(
final String mongoExe,
final Path rootTempDir)
throws Exception {
this(mongoExe, rootTempDir, false);
}

public MongoController(
final String mongoExe,
final Path rootTempDir,
final boolean useWiredTiger)
throws Exception {
checkExe(mongoExe, "mongod server");
tempDir = makeTempDirs(rootTempDir, "MongoController-", tempDirectories);
port = findFreePort();
Version dbVer = getMongoDBVer(mongoExe);
List<String> command = getMongoServerStartCommand(mongoExe, useWiredTiger, dbVer);
mongo = startProcess(command);
}

public int getServerPort() {
return port;
}

public Path getTempDir() {
return tempDir;
}

public void destroy(boolean deleteTempFiles) throws IOException {
if (mongo != null) {
mongo.destroy();
}
if (tempDir != null && deleteTempFiles) {
FileUtils.deleteDirectory(tempDir.toFile());
}
}

private static Version getMongoDBVer(final String mongoExe) throws IOException {

// build MongoDB version check command
List<String> command = new LinkedList<String>();
command.addAll(Arrays.asList(mongoExe, "--version"));

// start MongoDB version check process
ProcessBuilder checkVerPb = new ProcessBuilder(command);
Process checkVerProcess = checkVerPb.start();

// parse mongod --version output string
String dbVer = new BufferedReader(
new InputStreamReader(checkVerProcess.getInputStream()))
.lines()
.collect(Collectors.joining(" "))
.split(" ")[2].substring(1);

System.out.println("MongoDB version: " + dbVer);
checkVerProcess.destroy();
return Version.valueOf(dbVer);
}

private List<String> getMongoServerStartCommand(final String mongoExe,
final boolean useWiredTiger,
final Version dbVer) {
List<String> command = new LinkedList<String>();
command.addAll(Arrays.asList(mongoExe, "--port", "" + port,
"--dbpath", tempDir.resolve(DATA_DIR).toString()));

// Starting in MongoDB 6.1, journaling is always enabled.
// As a result, MongoDB removes the storage.journal.enabled option
// and the corresponding --journal and --nojournal command-line options.
// https://www.mongodb.com/docs/manual/release-notes/6.1/#changes-to-journaling
if (dbVer.lessThan(MONGO_DB_6_1)) {
command.addAll(Arrays.asList("--nojournal"));
}
if (useWiredTiger) {
command.addAll(Arrays.asList("--storageEngine", "wiredTiger"));
}
return command;
}

private Process startProcess(List<String> command) throws Exception {
ProcessBuilder servpb = new ProcessBuilder(command)
.redirectErrorStream(true)
.redirectOutput(getTempDir().resolve("mongo.log").toFile());

Process mongoProcess = servpb.start();
Thread.sleep(1000); //wait for server to start up
return mongoProcess;
}

public static void main(String[] args) throws Exception {
us.kbase.common.test.controllers.mongo.MongoController ac = new us.kbase.common.test.controllers.mongo.MongoController(
"/kb/runtime/bin/mongod",
Paths.get("workspacetesttemp"));
System.out.println(ac.getServerPort());
System.out.println(ac.getTempDir());
Scanner reader = new Scanner(System.in);
System.out.println("any char to shut down");
//get user input for a
reader.next();
ac.destroy(false);
reader.close();
}

}

10 changes: 5 additions & 5 deletions src/us/kbase/test/auth2/MongoStorageTestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import org.bson.Document;

import com.github.zafarkhaja.semver.Version;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

import us.kbase.auth2.cryptutils.RandomDataGenerator;
import us.kbase.auth2.lib.storage.mongo.MongoStorage;
import us.kbase.common.test.controllers.mongo.MongoController;

public class MongoStorageTestManager {

Expand All @@ -42,16 +42,16 @@ public MongoStorageTestManager(final String dbName) throws Exception {
wiredTiger = useWiredTigerEngine();
System.out.println(String.format("Testing against mongo executable %s on port %s",
getMongoExe(), mongo.getServerPort()));
mc = new MongoClient("localhost:" + mongo.getServerPort());
mc = MongoClients.create("mongodb://localhost:" + mongo.getServerPort());
db = mc.getDatabase(dbName);

final Document bi = db.runCommand(new Document("buildinfo", 1));
final String version = bi.getString("version");
mongoDBVer = Version.valueOf(version);
indexVer = mongoDBVer.greaterThanOrEqualTo(Version.forIntegers(3, 4)) ? 2 : 1;
reset();
}

public void destroy() throws Exception {
if (mc != null) {
mc.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ public void unparseable() throws Exception {
fail("expected exception");
} catch (InvocationTargetException ex) {
TestCommon.assertExceptionCorrect((Exception) ex.getTargetException(),
new AuthStorageException("Unable to parse duplicate key error: some "));

new AuthStorageException("Unable to parse duplicate key error: " +
"Write operation error on server 127.0.0.1:27017. " +
"Write error: WriteError{code=11000, message='some "));
}
}

Expand Down
Loading

0 comments on commit 5af2e93

Please sign in to comment.