Skip to content

Commit d9de9ad

Browse files
authored
Fixed NullPointerException when mongoDB connection goes down (#430)
* Fixed NullPointerException when mongoDB connection goes down
1 parent a6733a2 commit d9de9ad

File tree

4 files changed

+57
-39
lines changed

4 files changed

+57
-39
lines changed

src/main/java/com/ericsson/ei/handlers/ObjectHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.fasterxml.jackson.databind.JsonNode;
3838
import com.fasterxml.jackson.databind.ObjectMapper;
3939
import com.mongodb.BasicDBObject;
40+
import com.mongodb.MongoClientException;
4041
import com.mongodb.util.JSON;
4142

4243
import lombok.Getter;
@@ -152,7 +153,7 @@ public void updateObject(JsonNode aggregatedObject, RulesObject rulesObject, Str
152153
* @param query query to base search on
153154
* @return List of documents
154155
*/
155-
public List<String> findObjectsByCondition(MongoQuery query) {
156+
public List<String> findObjectsByCondition(MongoQuery query) throws MongoClientException {
156157
return mongoDbHandler.find(databaseName, collectionName, query);
157158
}
158159

@@ -162,7 +163,7 @@ public List<String> findObjectsByCondition(MongoQuery query) {
162163
* @param id An id to search for in the database
163164
* @return document
164165
*/
165-
public String findObjectById(String id) {
166+
public String findObjectById(String id) throws MongoClientException {
166167
final MongoCondition condition = MongoCondition.idCondition(id);
167168
String document = "";
168169
List<String> documents = findObjectsByCondition(condition);

src/main/java/com/ericsson/ei/handlers/UpStreamEventsHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public void runHistoryExtractionRulesOnAllUpstreamEvents(String aggregatedObject
8383
}
8484

8585
final JsonNode upstreamLinkObjects = searchResult.get("upstreamLinkObjects");
86+
87+
if (upstreamLinkObjects == null) {
88+
LOGGER.warn("Asked for upstream from {} but got null result back!", aggregatedObjectId);
89+
return;
90+
}
91+
8692
if (!upstreamLinkObjects.isArray()) {
8793
LOGGER.warn("Expected upstreamLinkObjects to be an array but is: {}", upstreamLinkObjects.getNodeType());
8894
}

src/main/java/com/ericsson/ei/mongo/MongoDBHandler.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void close() {
8686
* @return
8787
*/
8888
public void insertDocument(String dataBaseName, String collectionName, String input)
89-
throws MongoWriteException {
89+
throws MongoWriteException, MongoClientException {
9090
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
9191
if (collection != null) {
9292
final Document dbObjectInput = Document.parse(input);
@@ -123,7 +123,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
123123
}
124124
}
125125
} catch (Exception e) {
126-
LOGGER.error("Failed to retrieve documents.", e);
126+
LOGGER.error("Failed to retrieve documents from the collection {} as {}", collectionName, e.getMessage());
127127
}
128128
return result;
129129
}
@@ -137,7 +137,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
137137
* @return
138138
*/
139139
public ArrayList<String> find(String dataBaseName, String collectionName,
140-
MongoQuery query) {
140+
MongoQuery query) throws MongoClientException {
141141
ArrayList<String> result = new ArrayList<>(0);
142142

143143
try {
@@ -161,7 +161,7 @@ public ArrayList<String> find(String dataBaseName, String collectionName,
161161
*/
162162
public boolean updateDocument(String dataBaseName, String collectionName,
163163
MongoQuery queryFilter,
164-
String updateInput) {
164+
String updateInput) throws MongoClientException {
165165
try {
166166
return doUpdate(dataBaseName, collectionName, queryFilter, updateInput);
167167
} catch (Exception e) {
@@ -219,7 +219,7 @@ public boolean dropDocument(String dataBaseName, String collectionName, MongoQue
219219
* @param ttlValue seconds
220220
*/
221221
public void createTTLIndex(String dataBaseName, String collectionName, String fieldName,
222-
int ttlValue) {
222+
int ttlValue) throws MongoClientException {
223223
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
224224
IndexOptions indexOptions = new IndexOptions().expireAfter((long) ttlValue,
225225
TimeUnit.SECONDS);
@@ -274,7 +274,7 @@ private void createMongoClient() throws AbortExecutionException {
274274
}
275275

276276
private ArrayList<String> doFind(String dataBaseName, String collectionName,
277-
MongoQuery query) {
277+
MongoQuery query) throws MongoClientException {
278278
LOGGER.debug(
279279
"Find and retrieve data from database.\nDatabase: {}\nCollection: {}\nCondition/Query: {}",
280280
dataBaseName, collectionName, query.getQueryString());
@@ -310,7 +310,7 @@ private ArrayList<String> doFind(String dataBaseName, String collectionName,
310310

311311
private Document doFindAndModify(String dataBaseName, String collectionName,
312312
MongoQuery queryFilter,
313-
String updateInput) {
313+
String updateInput) throws MongoClientException {
314314
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
315315
if (collection == null) {
316316
return null;
@@ -327,7 +327,7 @@ private Document doFindAndModify(String dataBaseName, String collectionName,
327327
}
328328

329329
private boolean doUpdate(String dataBaseName, String collectionName, MongoQuery queryFilter,
330-
String updateInput) {
330+
String updateInput) throws MongoClientException {
331331
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
332332
if (collection == null) {
333333
return false;
@@ -344,7 +344,7 @@ private boolean doUpdate(String dataBaseName, String collectionName, MongoQuery
344344
return updateWasPerformed;
345345
}
346346

347-
private boolean doDrop(String dataBaseName, String collectionName, MongoQuery query) {
347+
private boolean doDrop(String dataBaseName, String collectionName, MongoQuery query) throws MongoClientException {
348348
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
349349
if (collection == null) {
350350
return false;
@@ -364,33 +364,27 @@ private boolean doDrop(String dataBaseName, String collectionName, MongoQuery qu
364364

365365
}
366366

367-
private MongoCollection<Document> getMongoCollection(String databaseName,
368-
String collectionName) {
367+
private MongoCollection<Document> getMongoCollection(String databaseName, String collectionName)
368+
throws MongoClientException {
369369
if (mongoClient == null) {
370-
return null;
370+
throw new MongoClientException("Failed to connect MongoDB");
371371
}
372372

373-
try {
374-
verifyExistanceOfCollection(databaseName, collectionName);
375-
376-
MongoDatabase db = mongoClient.getDatabase(databaseName);
377-
MongoCollection<Document> collection = db.getCollection(collectionName);
378-
return collection;
379-
} catch (MongoClientException e) {
380-
LOGGER.error("Failure when handling Mongo collection: {} ", e.getMessage(), e);
381-
return null;
382-
}
373+
verifyExistenceOfCollection(databaseName, collectionName);
383374

375+
MongoDatabase db = mongoClient.getDatabase(databaseName);
376+
MongoCollection<Document> collection = db.getCollection(collectionName);
377+
return collection;
384378
}
385379

386-
private void verifyExistanceOfCollection(String databaseName, String collectionName) {
380+
private void verifyExistenceOfCollection(String databaseName, String collectionName) throws MongoClientException {
387381
List<String> collectionList = getCollectionList(databaseName);
388382
if (!collectionList.contains(collectionName)) {
389383
createCollection(databaseName, collectionName);
390384
}
391385
}
392386

393-
private List<String> getCollectionList(String databaseName) {
387+
private List<String> getCollectionList(String databaseName) throws MongoClientException {
394388
try {
395389
MongoDatabase db = mongoClient.getDatabase(databaseName);
396390
List<String> collectionList = db.listCollectionNames().into(new ArrayList<String>());
@@ -404,7 +398,7 @@ private List<String> getCollectionList(String databaseName) {
404398
}
405399
}
406400

407-
private void createCollection(String databaseName, String collectionName) {
401+
private void createCollection(String databaseName, String collectionName) throws MongoClientException {
408402
try {
409403
LOGGER.debug(
410404
"The requested database({}) / collection({}) not found in mongodb, Creating.",
@@ -429,7 +423,7 @@ private void createCollection(String databaseName, String collectionName) {
429423
* @param e
430424
*/
431425
private void checkIfCollectionExistError(String databaseName, String collectionName,
432-
MongoCommandException e) {
426+
MongoCommandException e) throws MongoClientException {
433427
String collectionExistsError = String.format("collection '%s.%s' already exists",
434428
databaseName, collectionName);
435429

src/main/java/com/ericsson/ei/waitlist/WaitListWorker.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.ericsson.ei.waitlist;
1818

19+
import java.io.IOException;
1920
import java.util.Collection;
2021
import java.util.List;
2122

@@ -35,6 +36,7 @@
3536
import com.ericsson.ei.rules.RulesObject;
3637
import com.fasterxml.jackson.databind.JsonNode;
3738
import com.fasterxml.jackson.databind.ObjectMapper;
39+
import com.mongodb.MongoClientException;
3840

3941
@Component
4042
public class WaitListWorker {
@@ -59,31 +61,46 @@ public class WaitListWorker {
5961
private EventToObjectMapHandler eventToObjectMapHandler;
6062

6163
private boolean shutdownInProgress = false;
64+
private static final String ID = "_id";
65+
private static final String EVENT = "Event";
66+
private static final String TIME = "Time";
6267

6368
@Scheduled(initialDelayString = "${waitlist.resend.initial.delay}", fixedRateString = "${waitlist.resend.fixed.rate}")
6469
public void run() {
6570
if(shutdownInProgress) {
6671
return;
6772
}
73+
try {
74+
getAllDocumentsAndCheckTargetAggregations();
75+
} catch (MongoClientException e) {
76+
LOGGER.error("Failed to get documents from MongoDB", e.getMessage());
77+
}
78+
}
79+
80+
private void getAllDocumentsAndCheckTargetAggregations() {
6881
List<String> documents = waitListStorageHandler.getWaitList();
6982
for (String document : documents) {
7083
try {
71-
ObjectMapper objectMapper = new ObjectMapper();
72-
JsonNode eventJson = objectMapper.readTree(document);
73-
String id = eventJson.get("_id").asText();
74-
if (eventToObjectMapHandler.isEventInEventObjectMap(id)) {
75-
waitListStorageHandler.dropDocumentFromWaitList(document);
76-
} else {
77-
checkTargetAggregationsExistAndRepublishEvent(eventJson);
78-
}
84+
checkAggregationsExistAndRepublishEvent(document);
7985
} catch (Exception e) {
8086
LOGGER.error("Exception occured while trying to resend event: {}", document, e);
8187
}
8288
}
8389
}
8490

91+
private void checkAggregationsExistAndRepublishEvent(String document) throws IOException {
92+
ObjectMapper objectMapper = new ObjectMapper();
93+
JsonNode eventJson = objectMapper.readTree(document);
94+
String id = eventJson.get(ID).asText();
95+
if (eventToObjectMapHandler.isEventInEventObjectMap(id)) {
96+
waitListStorageHandler.dropDocumentFromWaitList(document);
97+
} else {
98+
checkTargetAggregationsExistAndRepublishEvent(eventJson);
99+
}
100+
}
101+
85102
public void checkTargetAggregationsExistAndRepublishEvent(JsonNode eventJson) {
86-
JsonNode event = eventJson.get("Event");
103+
JsonNode event = eventJson.get(EVENT);
87104
String eventStr = event.asText();
88105
RulesObject rulesObject = rulesHandler.getRulesForEvent(eventStr);
89106
String idRule = rulesObject.getIdentifyRules();
@@ -92,8 +109,8 @@ public void checkTargetAggregationsExistAndRepublishEvent(JsonNode eventJson) {
92109
if (idRule != null && !idRule.isEmpty()) {
93110
JsonNode ids = jmesPathInterface.runRuleOnEvent(idRule, eventStr);
94111
if (ids.isArray()) {
95-
JsonNode idNode = eventJson.get("_id");
96-
JsonNode timeNode = eventJson.get("Time");
112+
JsonNode idNode = eventJson.get(ID);
113+
JsonNode timeNode = eventJson.get(TIME);
97114
LOGGER.debug("[EIFFEL EVENT RESENT FROM WAITLIST: {}] id:{} time:{}", waitlistId, idNode.textValue(),
98115
timeNode);
99116
for (final JsonNode idJsonObj : ids) {

0 commit comments

Comments
 (0)