Skip to content

Commit d4e56b7

Browse files
committed
MongoDB secondary connection support
1 parent e132f4d commit d4e56b7

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDBStack.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class MongoDBStack extends CoreStack {
5252
" \"w\":\"majority\"," + //
5353
" \"retryWrites\":\"true\"," + //
5454
" \"retryReads\":\"true\"," + //
55-
" \"maxPoolSize\":10," + //
55+
" \"maxPoolSize\":20," + //
5656
" \"compressors\":\"zstd\"" + //
5757
"}";
5858

@@ -134,9 +134,6 @@ public static String getFullConnectionURL_primary(GenericConvertMap<String, Obje
134134
+ "is highly recommended for replica clusters to ensure read after write consistency.");
135135
}
136136

137-
// In the future we may want to support opt_map
138-
// GenericConvertMap<String,Object> optMap = config.getGenericConvertStringMap("opt_map", "{}");
139-
140137
// Lets build the auth str
141138
String authStr = "";
142139
if (user != null && pass != null) {
@@ -180,10 +177,10 @@ public static String getFullConnectionURL_secondary(GenericConvertMap<String, Ob
180177
String host = config.getString("host", "localhost");
181178
int port = config.getInt("port", 27017);
182179

183-
// Hanlding of option string
180+
// Hanlding of option string, default sec_opt uses `secondaryPreferred`
184181
GenericConvertMap<String, Object> optMap = new GenericConvertHashMap<>();
185182
optMap.putAll(config.getGenericConvertStringMap("opt", defaultOptJson));
186-
optMap.putAll(config.getGenericConvertStringMap("sec_opt", "{}"));
183+
optMap.putAll(config.getGenericConvertStringMap("sec_opt", "{ \"readPreference\":\"secondaryPreferred\" }"));
187184

188185
// The opt string overwrite
189186
String optStr = config.getString("sec_opt_str", mapToOptStr(optMap));
@@ -204,13 +201,10 @@ public static String getFullConnectionURL_secondary(GenericConvertMap<String, Ob
204201
LOGGER
205202
.warning("MongoDB is configured without readConcernLevel for the secondary connection, "
206203
+ "this is alright for a single node, but `readConcernLevel=linearizable`"
207-
+ "or `readPreference=master&readConcernLevel=majority`"
204+
+ "or `readPreference=secondaryPreferred&readConcernLevel=majority`"
208205
+ "is highly recommended for replica clusters to ensure read after write consistency.");
209206
}
210207

211-
// In the future we may want to support opt_map
212-
// GenericConvertMap<String,Object> optMap = config.getGenericConvertStringMap("opt_map", "{}");
213-
214208
// Lets build the auth str
215209
String authStr = "";
216210
if (user != null && pass != null) {

src/main/java/picoded/dstack/mongodb/MongoDB_DataObjectMap.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public class MongoDB_DataObjectMap extends Core_DataObjectMap {
6060
/** MongoDB instance representing the backend connection */
6161
MongoCollection<Document> collection = null;
6262

63+
/** Secondary connection, and its applicable mode */
64+
MongoCollection<Document> sec_collection = null;
65+
String sec_mode = null;
66+
6367
/**
6468
* Constructor, with name constructor
6569
*
@@ -69,6 +73,12 @@ public class MongoDB_DataObjectMap extends Core_DataObjectMap {
6973
public MongoDB_DataObjectMap(MongoDBStack inStack, String name) {
7074
super();
7175
collection = inStack.db_conn.getCollection(name);
76+
77+
// Get the secondary collection if applicable
78+
if (inStack.sec_db_conn != null) {
79+
sec_collection = inStack.sec_db_conn.getCollection(name);
80+
sec_mode = inStack.sec_mode;
81+
}
7282
}
7383

7484
//--------------------------------------------------------------------------
@@ -418,7 +428,28 @@ public String[] query_id(Query queryClause, String orderByStr, int offset, int l
418428
}
419429

420430
// Lets fetch the data, for the various _oid
421-
FindIterable<Document> search = collection.find(bsonFilter);
431+
FindIterable<Document> search = null;
432+
433+
// Chose the respective serach mode
434+
if (sec_mode == null) {
435+
// NULL safety
436+
search = collection.find(bsonFilter);
437+
} else if (sec_mode.equals("LIKE")) {
438+
// Use secondary connection for LIKE query
439+
if (queryClause.toSqlString().toUpperCase().indexOf("LIKE") > 0) {
440+
search = sec_collection.find(bsonFilter);
441+
}
442+
} else if (sec_mode.equals("QUERY")) {
443+
// Use secondary for all queries
444+
search = sec_collection.find(bsonFilter);
445+
}
446+
447+
// Fallback to main collection query
448+
if (search == null) {
449+
search = collection.find(bsonFilter);
450+
}
451+
452+
// Apply the projection, to only fetch _oid
422453
search = search.projection(Projections.include("_oid"));
423454

424455
// Build the orderBy clause

0 commit comments

Comments
 (0)