From 6382a1f9bdfb8d5564edbeef00afbad9519e3f38 Mon Sep 17 00:00:00 2001 From: Sijie Date: Wed, 13 Dec 2023 14:25:47 -0800 Subject: [PATCH] upgrade to mongo 7.0.4 --- .github/workflows/test.yml | 4 +- build.xml | 6 +- src/us/kbase/auth2/service/AuthBuilder.java | 22 +++- .../auth2/service/AuthenticationService.java | 2 +- src/us/kbase/test/auth2/MongoController.java | 100 ++++++++++++++++++ .../test/auth2/MongoStorageTestManager.java | 8 +- .../lib/storage/mongo/MongoStorageTester.java | 5 +- 7 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 src/us/kbase/test/auth2/MongoController.java diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1aee87ec..398cff35 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,10 +25,10 @@ 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' + mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4' wired_tiger: 'true' steps: diff --git a/build.xml b/build.xml index 659d03d2..836e6de3 100644 --- a/build.xml +++ b/build.xml @@ -30,7 +30,11 @@ - + + + + + diff --git a/src/us/kbase/auth2/service/AuthBuilder.java b/src/us/kbase/auth2/service/AuthBuilder.java index 5fda63be..e38ee980 100644 --- a/src/us/kbase/auth2/service/AuthBuilder.java +++ b/src/us/kbase/auth2/service/AuthBuilder.java @@ -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; @@ -67,10 +69,20 @@ private MongoClient buildMongo(final AuthStartupConfig c) throws StorageInitExce 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( + MongoClientSettings.builder() + .credential(creds) + .applyToClusterSettings(builder -> + builder.hosts(Arrays.asList( + new ServerAddress(c.getMongoHost())))) + .build()); } else { - return new MongoClient(new ServerAddress(c.getMongoHost())); + return MongoClients.create( + MongoClientSettings.builder() + .applyToClusterSettings(builder -> + builder.hosts(Arrays.asList( + new ServerAddress(c.getMongoHost())))) + .build()); } } catch (MongoException e) { LoggerFactory.getLogger(getClass()).error( diff --git a/src/us/kbase/auth2/service/AuthenticationService.java b/src/us/kbase/auth2/service/AuthenticationService.java index b51db4a2..a69f8706 100644 --- a/src/us/kbase/auth2/service/AuthenticationService.java +++ b/src/us/kbase/auth2/service/AuthenticationService.java @@ -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; diff --git a/src/us/kbase/test/auth2/MongoController.java b/src/us/kbase/test/auth2/MongoController.java new file mode 100644 index 00000000..25de7959 --- /dev/null +++ b/src/us/kbase/test/auth2/MongoController.java @@ -0,0 +1,100 @@ +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.IOException; +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 org.apache.commons.io.FileUtils; + + +/** Q&D Utility to run a Mongo server for the purposes of testing from + * Java. + * @author gaprice@lbl.gov + * + */ +public class MongoController { + + private final static String DATA_DIR = "data"; + + private final static List tempDirectories = + new LinkedList(); + static { + tempDirectories.add(DATA_DIR); + } + + 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(); + + List command = new LinkedList(); + command.addAll(Arrays.asList(mongoExe, "--port", "" + port, + "--dbpath", tempDir.resolve(DATA_DIR).toString())); + if (useWiredTiger) { + command.addAll(Arrays.asList("--storageEngine", "wiredTiger")); + } + ProcessBuilder servpb = new ProcessBuilder(command) + .redirectErrorStream(true) + .redirectOutput(tempDir.resolve("mongo.log").toFile()); + + mongo = servpb.start(); + Thread.sleep(1000); //wait for server to start up + } + + 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()); + } + } + + 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(); + } + +} + diff --git a/src/us/kbase/test/auth2/MongoStorageTestManager.java b/src/us/kbase/test/auth2/MongoStorageTestManager.java index 45d79ab9..486232fe 100644 --- a/src/us/kbase/test/auth2/MongoStorageTestManager.java +++ b/src/us/kbase/test/auth2/MongoStorageTestManager.java @@ -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 { @@ -42,7 +42,7 @@ 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)); @@ -51,7 +51,7 @@ public MongoStorageTestManager(final String dbName) throws Exception { indexVer = mongoDBVer.greaterThanOrEqualTo(Version.forIntegers(3, 4)) ? 2 : 1; reset(); } - + public void destroy() throws Exception { if (mc != null) { mc.close(); diff --git a/src/us/kbase/test/auth2/lib/storage/mongo/MongoStorageTester.java b/src/us/kbase/test/auth2/lib/storage/mongo/MongoStorageTester.java index 0e53ca84..c628fa71 100644 --- a/src/us/kbase/test/auth2/lib/storage/mongo/MongoStorageTester.java +++ b/src/us/kbase/test/auth2/lib/storage/mongo/MongoStorageTester.java @@ -7,12 +7,13 @@ import org.junit.BeforeClass; import com.github.zafarkhaja.semver.Version; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; 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; +import us.kbase.test.auth2.MongoController; +//import us.kbase.common.test.controllers.mongo.MongoController; import us.kbase.test.auth2.MongoStorageTestManager; public class MongoStorageTester {