Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo7 upgrade #394

Merged
merged 4 commits into from
Aug 26, 2024
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
9 changes: 2 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ jobs:
fail-fast: false
matrix:
include:
# the current production setup
- java: '8'
mongo: 'mongodb-linux-x86_64-3.6.13'
mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4'
wired_tiger: 'false'
gradle_test: 'test'
# test all code w/ java 11
- java: '11'
mongo: 'mongodb-linux-x86_64-3.6.23'
wired_tiger: 'true'
gradle_test: 'test'
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -64,8 +60,7 @@ jobs:

- name: Run tests
shell: bash
run: |
gradle ${{matrix.gradle_test}}
run: ./gradlew test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ dependencies {
exclude group: 'javax.servlet', module: 'servlet-api' // 2.5 vs 3.0.1 above
}
implementation 'org.ini4j:ini4j:0.5.2'
implementation 'org.mongodb:mongo-java-driver:3.8.2'
implementation 'org.mongodb:mongodb-driver-core:4.11.1'
implementation 'org.mongodb:mongodb-driver-sync:4.11.1'
implementation 'org.mongodb:bson-record-codec:4.11.1'
implementation 'org.mongodb:bson:4.11.1'
implementation 'org.apache.kafka:kafka-clients:2.1.0'
implementation 'com.google.guava:guava:18.0'
implementation 'org.slf4j:slf4j-api:1.7.25'
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/us/kbase/groups/build/GroupsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
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 @@ -106,15 +107,16 @@

private MongoClient buildMongo(final GroupsConfig 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()))));
Comment on lines +110 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're going to need the retryWrites parameter as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the retryWrites parameter will be included in the next PR.

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());

Check warning on line 117 in src/main/java/us/kbase/groups/build/GroupsBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/us/kbase/groups/build/GroupsBuilder.java#L117

Added line #L117 was not covered by tests
} 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/main/java/us/kbase/groups/service/GroupsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoClient;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
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.groups.core.resource.ResourceType;
Expand All @@ -39,7 +40,7 @@ public MongoStorageTestManager(final String dbName) throws Exception {
wiredTiger = TestCommon.useWiredTigerEngine();
System.out.println(String.format("Testing against mongo executable %s on port %s",
TestCommon.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public void unparseable() throws Exception {
fail("expected exception");
} catch (InvocationTargetException ex) {
TestCommon.assertExceptionCorrect((Exception) ex.getTargetException(),
new GroupsStorageException("Unable to parse duplicate key error: some "));
new GroupsStorageException("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
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ public void startUpWith2ConfigDocs() throws Exception {

final Pattern errorPattern = Pattern.compile(
"Failed to create index: Write failed with error code 11000 and error message " +
"'(exception: )?E11000 duplicate key error (index|collection): " +
"'(exception: )?(.*)E11000 duplicate key error (index|collection): " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is captured by (.*)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"startUpWith2ConfigDocs.config( index: |\\.\\$)schema_1\\s+dup key: " +
"\\{ : \"schema\" \\}'");
"(\\{ schema: \"schema\" \\}'|\\{ : \"schema\" \\}')");

try {
new MongoGroupsStorage(db, set());
fail("started mongo with bad config");
Expand Down Expand Up @@ -188,27 +189,21 @@ public void checkCollectionNames() throws Exception {

@Test
public void indexesConfig() {
final Set<Document> indexes = new HashSet<>();
manager.db.getCollection("config").listIndexes()
.forEach((Consumer<Document>) indexes::add);
final Set<Document> indexes = getAndNormalizeIndexes("config");
assertThat("incorrect indexes", indexes, is(set(
new Document("v", manager.indexVer)
.append("unique", true)
.append("key", new Document("schema", 1))
.append("name", "schema_1")
.append("ns", "test_mongogroupsstorage.config"),
.append("name", "schema_1"),
new Document("v", manager.indexVer)
.append("key", new Document("_id", 1))
.append("name", "_id_")
.append("ns", "test_mongogroupsstorage.config")
)));
}

@Test
public void indexesGroups() {
final Set<Document> indexes = new HashSet<>();
manager.db.getCollection("groups").listIndexes()
.forEach((Consumer<Document>) indexes::add);
final Set<Document> indexes = getAndNormalizeIndexes("groups");
assertThat("incorrect indexes", indexes, is(getDefaultGroupsIndexes()));
}

Expand All @@ -218,28 +213,22 @@ private Set<Document> getDefaultGroupsIndexes() {
new Document("v", manager.indexVer)
.append("unique", true)
.append("key", new Document("id", 1))
.append("name", "id_1")
.append("ns", col),
.append("name", "id_1"),
new Document("v", manager.indexVer)
.append("key", new Document("own", 1).append("id", 1))
.append("name", "own_1_id_1")
.append("ns", col),
.append("name", "own_1_id_1"),
new Document("v", manager.indexVer)
.append("key", new Document("admin", 1).append("id", 1))
.append("name", "admin_1_id_1")
.append("ns", col),
.append("name", "admin_1_id_1"),
new Document("v", manager.indexVer)
.append("key", new Document("_id", 1))
.append("name", "_id_")
.append("ns", col),
.append("name", "_id_"),
new Document("v", manager.indexVer)
.append("key", new Document("priv", 1).append("id", 1))
.append("name", "priv_1_id_1")
.append("ns", col),
.append("name", "priv_1_id_1"),
new Document("v", manager.indexVer)
.append("key", new Document("memb.user", 1).append("id", 1))
.append("name", "memb.user_1_id_1")
.append("ns", col)
);
}

Expand All @@ -250,10 +239,8 @@ public void indexesGroupsWithResourceTypes() throws Exception {
try {
new MongoGroupsStorage(
manager.db, Arrays.asList(new ResourceType("t1"), new ResourceType("t2")));
final Set<Document> indexes = new HashSet<>();
manager.db.getCollection("groups").listIndexes()
.forEach((Consumer<Document>) indexes::add);

final Set<Document> indexes = getAndNormalizeIndexes("groups");

final String col = "test_mongogroupsstorage.groups";
final Set<Document> expected = getDefaultGroupsIndexes();
for (final String t: set("t1", "t2")) {
Expand All @@ -263,19 +250,16 @@ public void indexesGroupsWithResourceTypes() throws Exception {
.append("key", new Document(resKey, 1)
.append("own", 1)
.append("id", 1))
.append("name", resKey + "_1_own_1_id_1")
.append("ns", col),
.append("name", resKey + "_1_own_1_id_1"),
new Document("v", manager.indexVer)
.append("key", new Document(resKey, 1)
.append("id", 1))
.append("name", resKey + "_1_id_1")
.append("ns", col),
.append("name", resKey + "_1_id_1"),
new Document("v", manager.indexVer)
.append("key", new Document(resKey, 1)
.append("priv", 1)
.append("id", 1))
.append("name", resKey + "_1_priv_1_id_1")
.append("ns", col)
));
}
assertThat("incorrect indexes", indexes, is(expected));
Expand All @@ -287,116 +271,113 @@ public void indexesGroupsWithResourceTypes() throws Exception {

@Test
public void indexesRequests() {
final Set<Document> indexes = new HashSet<>();
manager.db.getCollection("requests").listIndexes()
.forEach((Consumer<Document>) indexes::add);
final Set<Document> indexes = getAndNormalizeIndexes("requests");
final String col = "test_mongogroupsstorage.requests";
assertThat("incorrect indexes", indexes, is(set(
new Document("v", manager.indexVer)
.append("unique", true)
.append("key", new Document("id", 1))
.append("name", "id_1")
.append("ns", col),
.append("name", "id_1"),
new Document("v", manager.indexVer)
.append("key", new Document("gid", 1).append("type", 1).append("mod", 1))
.append("name", "gid_1_type_1_mod_1")
.append("ns", col),
.append("name", "gid_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("gid", 1)
.append("status", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "gid_1_status_1_type_1_mod_1")
.append("ns", col),
.append("name", "gid_1_status_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("requester", 1).append("mod", 1))
.append("name", "requester_1_mod_1")
.append("ns", col),
.append("name", "requester_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("requester", 1)
.append("status", 1)
.append("mod", 1))
.append("name", "requester_1_status_1_mod_1")
.append("ns", col),
.append("name", "requester_1_status_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resaid", 1)
.append("restype", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resaid_1_restype_1_type_1_mod_1")
.append("ns", col),
.append("name", "resaid_1_restype_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resaid", 1)
.append("restype", 1)
.append("status", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resaid_1_restype_1_status_1_type_1_mod_1")
.append("ns", col),
.append("name", "resaid_1_restype_1_status_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("restype", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_restype_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_restype_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("restype", 1)
.append("status", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_restype_1_status_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_restype_1_status_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("requester", 1)
.append("restype", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_requester_1_restype_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_requester_1_restype_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("requester", 1)
.append("restype", 1)
.append("status", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_requester_1_restype_1_status_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_requester_1_restype_1_status_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("gid", 1)
.append("restype", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_gid_1_restype_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_gid_1_restype_1_type_1_mod_1"),
new Document("v", manager.indexVer)
.append("key", new Document("resrid", 1)
.append("gid", 1)
.append("restype", 1)
.append("status", 1)
.append("type", 1)
.append("mod", 1))
.append("name", "resrid_1_gid_1_restype_1_status_1_type_1_mod_1")
.append("ns", col),
.append("name", "resrid_1_gid_1_restype_1_status_1_type_1_mod_1"),

new Document("v", manager.indexVer)
.append("key", new Document("expire", 1))
.append("name", "expire_1")
.append("ns", col),
.append("name", "expire_1"),
new Document("v", manager.indexVer)
.append("unique", true)
.append("sparse", true)
.append("key", new Document("charstr", 1))
.append("name", "charstr_1")
.append("ns", col),
.append("name", "charstr_1"),
new Document("v", manager.indexVer)
.append("key", new Document("_id", 1))
.append("name", "_id_")
.append("ns", col)
)));
}

private Set<Document> getAndNormalizeIndexes(final String collectionName) {
final Set<Document> indexes = new HashSet<>();
for (Document index: manager.db.getCollection(collectionName).listIndexes()) {
// In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getIndexes()
// no longer returns the namespace ns field in the index specification documents.
index.remove("ns");
// some versions of Mongo return ints, some longs. Convert all to longs.
if (index.containsKey("expireAfterSeconds")) {
index.put("expireAfterSeconds", ((Number) index.get("expireAfterSeconds")).longValue());
}
indexes.add(index);
}
return indexes;
}
}
Loading