Skip to content

Commit

Permalink
Merge pull request #1804 from akto-api-security/hotfix/fix_handling_w…
Browse files Browse the repository at this point in the history
…rite_concern

Changing write concern for mongo
  • Loading branch information
avneesh-akto authored Dec 7, 2024
2 parents 1861331 + 3f36c8d commit e5a1d94
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import com.mongodb.BasicDBObject;
import com.mongodb.ConnectionString;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.*;
Expand Down Expand Up @@ -2245,10 +2246,12 @@ public void contextInitialized(javax.servlet.ServletContextEvent sce) {
public void run() {

ReadPreference readPreference = ReadPreference.primary();
WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
if (runJobFunctions || DashboardMode.isSaasDeployment()) {
readPreference = ReadPreference.secondary();
readPreference = ReadPreference.primary();
writeConcern = WriteConcern.W1;
}
DaoInit.init(new ConnectionString(mongoURI), readPreference);
DaoInit.init(new ConnectionString(mongoURI), readPreference, writeConcern);

connectedToMongo = false;
do {
Expand Down
6 changes: 4 additions & 2 deletions libs/dao/src/main/java/com/akto/DaoInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClients;

import org.bson.codecs.configuration.CodecRegistries;
Expand Down Expand Up @@ -351,7 +352,7 @@ public static CodecRegistry createCodecRegistry(){
customEnumCodecs);
}

public static void init(ConnectionString connectionString, ReadPreference readPreference) {
public static void init(ConnectionString connectionString, ReadPreference readPreference, WriteConcern writeConcern) {
DbMode.refreshDbType(connectionString.getConnectionString());
logger.info("DB type: {}", DbMode.dbType);
DbMode.refreshSetupType(connectionString);
Expand All @@ -361,14 +362,15 @@ public static void init(ConnectionString connectionString, ReadPreference readPr

MongoClientSettings clientSettings = MongoClientSettings.builder()
.readPreference(readPreference)
.writeConcern(writeConcern)
.applyConnectionString(connectionString)
.codecRegistry(codecRegistry)
.build();

clients[0] = MongoClients.create(clientSettings);
}
public static void init(ConnectionString connectionString) {
init(connectionString, ReadPreference.secondary());
init(connectionString, ReadPreference.secondary(), WriteConcern.ACKNOWLEDGED);
}

public static void createIndices() {
Expand Down

0 comments on commit e5a1d94

Please sign in to comment.