-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMongo.java
333 lines (296 loc) · 12.7 KB
/
Mongo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.mongo;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ReadPreference;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import sirius.db.mixing.Mixing;
import sirius.kernel.Sirius;
import sirius.kernel.Startable;
import sirius.kernel.Stoppable;
import sirius.kernel.async.CallContext;
import sirius.kernel.commons.Explain;
import sirius.kernel.commons.Tuple;
import sirius.kernel.commons.Watch;
import sirius.kernel.di.PartCollection;
import sirius.kernel.di.std.ConfigValue;
import sirius.kernel.di.std.Parts;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Average;
import sirius.kernel.health.Counter;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.Log;
import sirius.kernel.settings.Extension;
import sirius.kernel.settings.PortMapper;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* Provides a thin layer above Mongo DB with fluent APIs for CRUD operations.
*/
@Register(classes = {Mongo.class, Stoppable.class})
public class Mongo implements Startable, Stoppable {
private static final String SERVICE_NAME = "mongo";
@SuppressWarnings("squid:S1192")
@Explain("Constants have different semantics.")
public static final Log LOG = Log.get("mongo");
private static final int MONGO_PORT = 27017;
private final Map<String, Tuple<MongoClient, String>> mongoClients = new ConcurrentHashMap<>();
private final Map<String, Boolean> mongoClientConfigured = new ConcurrentHashMap<>();
@ConfigValue("mongo.logQueryThreshold")
private Duration logQueryThreshold;
private long logQueryThresholdMillis = -1;
@Parts(IndexDescription.class)
private PartCollection<IndexDescription> indexDescriptions;
protected Average callDuration = new Average();
protected Average secondaryCallDuration = new Average();
protected Counter numSlowQueries = new Counter();
/**
* Determines if access to Mongo DB is configured by checking if a host is given.
*
* @param database the database / configuration to check
* @return <tt>true</tt> if access to Mongo DB is configured, <tt>false</tt> otherwise
*/
public boolean isConfigured(String database) {
return mongoClientConfigured.computeIfAbsent(database,
db -> Sirius.getSettings()
.getExtension("mongo.databases", database)
.get("hosts")
.isFilled());
}
/**
* Determines if access to default Mongo DB is configured by checking if a host is given.
*
* @return <tt>true</tt> if access to Mongo DB is configured, <tt>false</tt> otherwise
*/
public boolean isConfigured() {
return isConfigured(Mixing.DEFAULT_REALM);
}
/**
* Provides direct access to the Mongo DB for non-trivial operations.
*
* @param database the name of the database configuration to use.
* @return an initialized client instance to access Mongo DB.
*/
@Nullable
public MongoDatabase db(String database) {
Tuple<MongoClient, String> clientAndDB = mongoClients.computeIfAbsent(database, this::setupClient);
return clientAndDB.getFirst().getDatabase(clientAndDB.getSecond());
}
/**
* Provides direct access to the default Mongo DB for non-trivial operations.
*
* @return an initialized client instance to access Mongo DB.
*/
public MongoDatabase db() {
return db(Mixing.DEFAULT_REALM);
}
@SuppressWarnings("squid:S2095")
@Explain("We cannot close the client here as it is part of the return value.")
protected synchronized Tuple<MongoClient, String> setupClient(String database) {
Extension config = Sirius.getSettings().getExtension("mongo.databases", database);
int port = config.get("port").asInt(MONGO_PORT);
String connectionString = Arrays.stream(config.get("hosts").asString().split(","))
.map(String::trim)
.map(hostname -> PortMapper.mapPort(SERVICE_NAME, hostname, port))
.map(hostAndPort -> hostAndPort.getFirst() + ":" + hostAndPort.getSecond())
.collect(Collectors.joining(","));
MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(
"mongodb://" + connectionString))
.applicationName(CallContext.getNodeName());
MongoCredential credentials = determineCredentials(config);
if (credentials != null) {
settingsBuilder.credential(credentials);
}
MongoClient mongoClient = MongoClients.create(settingsBuilder.build());
createIndices(database, mongoClient.getDatabase(config.get("db").asString()));
return Tuple.create(mongoClient, config.get("db").asString());
}
private MongoCredential determineCredentials(Extension config) {
if (config.get("user").isEmptyString() || config.get("password").isEmptyString()) {
return null;
}
return MongoCredential.createCredential(config.get("user").asString(),
config.get("userDatabase").asString(config.get("db").asString()),
config.get("password").asString().toCharArray());
}
private void createIndices(String database, MongoDatabase db) {
for (IndexDescription idx : indexDescriptions) {
Watch w = Watch.start();
try {
LOG.INFO("Creating indices in Mongo DB: %s", idx.getClass().getName());
idx.createIndices(database, db);
LOG.INFO("Completed indices for: %s (%s)", idx.getClass().getName(), w.duration());
} catch (Exception t) {
Exceptions.handle()
.to(LOG)
.error(t)
.withSystemErrorMessage("Error while creating indices for '%s': %s (%s)",
idx.getClass().getName())
.handle();
}
}
}
@Override
public int getPriority() {
return 75;
}
@Override
public void started() {
if (isConfigured()) {
// Force the creation of indices and the initialization of the database connection...
db();
}
}
@Override
public void stopped() {
mongoClients.values().stream().map(Tuple::getFirst).forEach(client -> {
try {
client.close();
} catch (Exception e) {
LOG.WARN(e);
}
});
mongoClients.clear();
}
/**
* Returns a fluent query builder to insert a document into the database.
*
* @param database the name of the database configuration to use
* @return a query builder to create an insert statement
*/
public Inserter insert(String database) {
return new Inserter(this, database);
}
/**
* Returns a fluent query builder to insert a document into the default database.
*
* @return a query builder to create an insert statement
*/
public Inserter insert() {
return insert(Mixing.DEFAULT_REALM);
}
/**
* Returns a fluent query builder to find one or more documents in the database
*
* @param database the name of the database configuration to use
* @param readPreference the read preference to enforce. Note that one should most probably rather use
* {@link #find(String)} or {@link #findInSecondary(String)} to enforce a proper read
* reference. However, as this only permits to use <tt>PRIMARY</tt> or <tt>NEAREST</tt>, we
* still permit to specify a custom preference here. Note that <tt>null</tt> and
* <tt>PRIMARY</tt> are treated indentically.
* @return a query builder to create a find statement
*/
public Finder find(String database, @Nullable ReadPreference readPreference) {
return new Finder(this, database, readPreference);
}
/**
* Returns a fluent query builder to find one or more documents in the database
*
* @param database the name of the database configuration to use
* @return a query builder to create a find statement
*/
public Finder find(String database) {
return find(database, null);
}
/**
* Returns a fluent query builder to find one or more documents in the secondary database.
* <p>
* This provides an essential boost in performance, as all nodes of a MongoDB cluster are utilized. However, this
* may return stale data if a secondary lags behind. Therefore, this data must not be stored back in the primary
* database. This should rather only be used to serve web requests or other queries where occasional stale date
* does no harm.
* <p>
* Also, this should NOT be used to fill any cache as this might poison the cache with already stale data.
*
* @param database the name of the database configuration to use
* @return a query builder to create a find statement
*/
public Finder findInSecondary(String database) {
return find(database, ReadPreference.nearest());
}
/**
* Returns a fluent query builder to find one or more documents in the default database
*
* @return a query builder to create a find statement
*/
public Finder find() {
return find(Mixing.DEFAULT_REALM);
}
/**
* Returns a fluent query builder to find one or more documents in the default database
* <p>
* This provides an essential boost in performance, as all nodes of a MongoDB cluster are utilized. However, this
* may return stale data if a secondary lags behind. Therefore this data must not be stored back in the primary
* database. This should rather only be used to serve web requests or other queries where occasional stale date
* does no harm.
* <p>
* Also, this should NOT be used to fill any cache as this might poison the cache with already stale data.
*
* @return a query builder to create a find statement
*/
public Finder findInSecondary() {
return findInSecondary(Mixing.DEFAULT_REALM);
}
/**
* Returns a fluent query builder to update one or more documents in the database.
*
* @param database the name of the database configuration to use.
* @return a query builder to create an update statement
*/
public Updater update(String database) {
return new Updater(this, database);
}
/**
* Returns a fluent query builder to update one or more documents in the default database.
*
* @return a query builder to create an update statement
*/
public Updater update() {
return update(Mixing.DEFAULT_REALM);
}
/**
* Returns a fluent query builder to delete one or more documents in the database.
*
* @param database the name of the database configuration to use.
* @return a query builder to create a delete statement
*/
public Deleter delete(String database) {
return new Deleter(this, database);
}
/**
* Returns a fluent query builder to delete one or more documents in the default database.
*
* @return a query builder to create a delete statement
*/
public Deleter delete() {
return delete(Mixing.DEFAULT_REALM);
}
/**
* Returns the query log threshold in millis.
* <p>
* If the execution duration of a query is longer than this threshold, it is logged into
* {@link sirius.db.DB#SLOW_DB_LOG} for further analysis.
*
* @return the log thresold for queries in milliseconds
*/
protected long getLogQueryThresholdMillis() {
if (logQueryThresholdMillis < 0) {
logQueryThresholdMillis = logQueryThreshold.toMillis();
}
return logQueryThresholdMillis;
}
}