Skip to content

Commit

Permalink
Merge pull request #17 from manuelkasiske4idealo/configurable-write-c…
Browse files Browse the repository at this point in the history
…oncern

feat: made write-concern configurable
  • Loading branch information
kagahd authored Oct 13, 2024
2 parents 553892d + f53e665 commit 129f1fb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ java -cp ./latest-version/mongodb-performance-test.jar de.idealo.mongodb.perf.Ma
This will print a description how to use the program:
```
usage: de.idealo.mongodb.perf.Main [-H] [-v] [-m <MODE>] [-o <OPERATIONS_COUNT>] [-t <THREADS>] [-d <DURATION>] [-dropdb] [-s <RANDOM_TEXT_SIZE>] [-h <HOST>]
[-port <PORT>] [-db <DB>] [-c <COLLECTION>] [-url <URL>] [-u <USER>] [-p <PASSWORD>] [-adb <AUTH_DB>] [-ssl]
[-port <PORT>] [-db <DB>] [-c <COLLECTION>] [-url <URL>] [-u <USER>] [-p <PASSWORD>] [-adb <AUTH_DB>] [-ssl] [-wc <WRITE_CONCERN>]
*** mongoDB performance test (version 1.1.3)***
Please run first mode=INSERT in order to have a non-empty collection to test on.
You may add option 'dropdb' in order to drop the database before inserting documents.
Expand Down Expand Up @@ -83,6 +83,7 @@ Options:
-p,--password <PASSWORD> mongoDB password
-adb,--authdb <AUTH_DB> mongoDB database to be authenticated against (default: value of parameter -db)
-ssl,--ssl use SSL to connect to mongoDB
-wc, --writeconcern <WRITE_CONCERN> write concern for the operations (default: ACKNOWLEDGED) (options: ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY)
```

### Some examples
Expand Down Expand Up @@ -184,6 +185,8 @@ For example, 50 threads inserted a total of 1 million of documents, so the range

## Version history

* v1.2.0
+ improvement: added `--writeconcern` option to pass [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) (ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY) to the operations.
* v1.1.4
+ improvement: replace `logback-classic` v1.4.6 by v1.4.12 to close a potential security vulnerability
* v1.1.3
Expand Down
Binary file modified latest-version/mongodb-performance-test.jar
Binary file not shown.
44 changes: 35 additions & 9 deletions src/main/java/de/idealo/mongodb/perf/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.idealo.mongodb.perf;

import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import de.idealo.mongodb.perf.operations.*;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class Main {
boolean dropDb = false;
private final String version;
private int randomFieldLength = 0;
private WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;

public Main(){
version = getClass().getPackage().getImplementationVersion();
Expand Down Expand Up @@ -127,7 +129,6 @@ private void validateInput(String... args){
}
}


if (cmdLine.hasOption("t")) {
final String[] t_arg = cmdLine.getOptionValues("t");
for (int i = 0; i < t_arg.length; i++) {
Expand Down Expand Up @@ -166,7 +167,25 @@ private void validateInput(String... args){
}
}


if (cmdLine.hasOption("writeconcern")) {
String wcOption = cmdLine.getOptionValue("writeconcern").toUpperCase();
switch (wcOption) {
case "ACKNOWLEDGED":
writeConcern = WriteConcern.ACKNOWLEDGED;
break;
case "UNACKNOWLEDGED":
writeConcern = WriteConcern.UNACKNOWLEDGED;
break;
case "JOURNALED":
writeConcern = WriteConcern.JOURNALED;
break;
case "MAJORITY":
writeConcern = WriteConcern.MAJORITY;
break;
default:
throw new IllegalArgumentException("Invalid WriteConcern value: " + wcOption);
}
}

} catch (Exception e) {
LOG.error(e.getMessage());
Expand Down Expand Up @@ -263,15 +282,25 @@ private static Options cliOptions() {
.addOption(Option.builder("p").longOpt("password").hasArg().argName("PASSWORD").desc("mongoDB password").build())
.addOption(Option.builder("adb").longOpt("authdb").hasArg().argName("AUTH_DB").desc("mongoDB database to be authenticated against (default: value of parameter -db)").build())
.addOption(new Option("ssl", "ssl", false, "use SSL to connect to mongoDB"))
;

.addOption(Option.builder("wc").longOpt("writeconcern").hasArg().argName("WRITE_CONCERN")
.desc("WriteConcern for MongoDB operations (ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY)")
.build());
return options;
}

private void executeOperations() {

final ServerAddress serverAddress = new ServerAddress(host, port);
final MongoDbAccessor mongoDbAccessor = new MongoDbAccessor(user, password, authDb, ssl, url,serverAddress);
final MongoDbAccessor mongoDbAccessor = new MongoDbAccessor(
-1, // socketTimeOut
user,
password,
authDb,
ssl,
url,
writeConcern,
serverAddress
);

int run=0;
CountDownLatch runModeLatch = new CountDownLatch(modes.size());
Expand Down Expand Up @@ -336,14 +365,11 @@ private void executeOperations() {
executor.shutdown();
mongoDbAccessor.closeConnections();
}

}

public static void main(String... args){
Main m = new Main();
m.validateInput(args);
m.executeOperations();
}


}
}
45 changes: 19 additions & 26 deletions src/main/java/de/idealo/mongodb/perf/MongoDbAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

import java.net.UnknownHostException;

/**
* Created by kay.agahd on 23.11.16.
*/
public class MongoDbAccessor {

private static final Logger LOG = LoggerFactory.getLogger(MongoDbAccessor.class);
Expand All @@ -25,27 +22,29 @@ public class MongoDbAccessor {
private final String pw;
private final String authDb;
private final boolean ssl;
private final WriteConcern writeConcern;
private MongoClient mongo;

private MongoDbAccessor() {
this(-1, null, null, null, false, null);
this(-1, null, null, null, false, null, WriteConcern.ACKNOWLEDGED);
};

public MongoDbAccessor(String user, String pw, String authDb, boolean ssl, String url, ServerAddress... serverAddress) {
this(-1, user, pw, authDb, ssl, url, serverAddress);
}

public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, String url, ServerAddress... serverAddress) {
this.serverAddress = serverAddress;
this.user = user;
this.pw = pw;
this.authDb = authDb != null && !authDb.isEmpty() ? authDb : "admin";
this.ssl = ssl;
this.url = url;
this(-1, user, pw, authDb, ssl, url, WriteConcern.ACKNOWLEDGED, serverAddress);
}

public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, String url, WriteConcern writeConcern, ServerAddress... serverAddress) {
this.serverAddress = serverAddress;
this.user = user;
this.pw = pw;
this.authDb = authDb != null && !authDb.isEmpty() ? authDb : "admin";
this.ssl = ssl;
this.url = url;
this.socketTimeOut = socketTimeOut;
init();
}

this.writeConcern = writeConcern != null ? writeConcern : WriteConcern.ACKNOWLEDGED; // Use default if null
init();
}

public MongoDatabase getMongoDatabase(String dbName) {

if (mongo == null)
Expand All @@ -57,15 +56,9 @@ public MongoDatabase getMongoDatabase(String dbName) {
public void init() {
LOG.info(">>> init {}", serverAddress);
try {
MongoClientOptions options = MongoClientOptions.builder().connectTimeout(1000 * 10). // fail fast, so we
// know this node is
// unavailable
// maxConnectionIdleTime(1000 * 60).
// maxConnectionLifeTime(1000 * 60).
// socketTimeout(socketTimeOut == -1 ? 1000 * 120 : socketTimeOut). // use
// default (no timeout)
readPreference(ReadPreference.secondaryPreferred()).connectionsPerHost(5000)
.threadsAllowedToBlockForConnectionMultiplier(10).writeConcern(WriteConcern.ACKNOWLEDGED)
MongoClientOptions options = MongoClientOptions.builder().connectTimeout(1000 * 10) // fail fast, so we know this node is unavailable
.readPreference(ReadPreference.secondaryPreferred()).connectionsPerHost(5000)
.threadsAllowedToBlockForConnectionMultiplier(10).writeConcern(writeConcern) // Use configurable WriteConcern
.sslEnabled(ssl).sslInvalidHostNameAllowed(true).build();

if (url != null && !url.isEmpty()) {
Expand Down

0 comments on commit 129f1fb

Please sign in to comment.