Skip to content

Commit

Permalink
upgrade to mongo 7.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangs18 committed Dec 13, 2023
1 parent 1408e96 commit 6382a1f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
<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/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
22 changes: 17 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 @@ -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(
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
100 changes: 100 additions & 0 deletions src/us/kbase/test/auth2/MongoController.java
Original file line number Diff line number Diff line change
@@ -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 [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 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<String> command = new LinkedList<String>();
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();
}

}

8 changes: 4 additions & 4 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,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));
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6382a1f

Please sign in to comment.