Skip to content

Commit d382239

Browse files
zburswaSwathi Burada
andauthored
Merging EI maintenance branch to master branch (#490)
* [DURACI-25515] Merging EI maintenance branch to master branch Added the changes from EI maintenance branch to master branch * Did the code formatting. * Removed some code formatting * Removed some unnecessary formatting * Added MongodBConnectionException in Javadoc Co-authored-by: Swathi Burada <ebrdswt@seliiuts03269.seli.gic.ericsson.se>
1 parent 961094b commit d382239

22 files changed

+409
-104
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ jobs:
4343
name: report_coverage
4444
# Generating test coverage report and publishing to Codacy
4545
script:
46-
- mvn jacoco:prepare-agent test jacoco:report -B
47-
- curl -Ls -o codacy-coverage-reporter-assembly.jar https://github.com/codacy/codacy-coverage-reporter/releases/download/6.0.2/codacy-coverage-reporter-6.0.2-assembly.jar
48-
- java -jar codacy-coverage-reporter-assembly.jar report -l Java -r target/site/jacoco/jacoco.xml
46+
- mvn cobertura:cobertura -B
47+
- curl -LSs $(curl -LSs https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r '.assets | map({content_type, browser_download_url} | select(.content_type | contains("application/octet-stream"))) | .[0].browser_download_url') -o codacy-coverage-reporter-assembly.jar
48+
- java -jar codacy-coverage-reporter-assembly.jar report -l Java -r target/site/cobertura/coverage.xml

src/functionaltests/java/com/ericsson/ei/notifications/ttl/TestTTLSteps.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.util.SocketUtils;
2727

2828
import com.ericsson.ei.exception.AuthenticationException;
29+
import com.ericsson.ei.exception.MongoDBConnectionException;
2930
import com.ericsson.ei.mongo.MongoCondition;
3031
import com.ericsson.ei.mongo.MongoDBHandler;
3132
import com.ericsson.ei.notifications.InformSubscriber;
@@ -118,7 +119,7 @@ public void create_subscription_object() throws IOException, JSONException {
118119
}
119120

120121
@When("^I want to inform subscriber$")
121-
public void inform_subscriber() throws IOException, AuthenticationException {
122+
public void inform_subscriber() throws IOException, AuthenticationException, MongoDBConnectionException {
122123
JsonNode aggregatedObject = eventManager.getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH);
123124
informSubscriber.informSubscriber(aggregatedObject.toString(), subscriptionObject);
124125
}

src/functionaltests/java/com/ericsson/ei/rabbitmq/connection/RabbitMQTestConnectionSteps.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ public class RabbitMQTestConnectionSteps extends FunctionalTestBase {
5656
private String rabbitMQPort;
5757

5858
private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQTestConnectionSteps.class);
59-
private static final String EIFFEL_EVENTS = "src/functionaltests/resources/eiffel_events_for_thread_testing.json";
59+
private static final String EIFFEL_EVENTS = "src/functionaltests/resources/eiffel_events_for_test.json";
6060

6161
private static final String BINDING_KEY_1 = "binding-key-1";
6262
private static final String BINDING_KEY_2 = "binding-key-2";
63+
private static final String DEFAULT_ROUTING_KEY = "#";
6364

6465
private AMQPBrokerManager amqpBroker;
6566

@@ -95,12 +96,6 @@ public void connect_to_message_bus() throws Exception {
9596

9697
RabbitAdmin rabbitAdmin = createExchange(rmqHandler);
9798
RabbitTemplate rabbitTemplate = rabbitAdmin.getRabbitTemplate();
98-
rabbitTemplate.setConfirmCallback(new ConfirmCallback() {
99-
@Override
100-
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
101-
LOGGER.info("Received confirm with result : {}", ack);
102-
}
103-
});
10499

105100
rmqHandler.setRabbitTemplate(rabbitTemplate);
106101
rmqHandler.getContainer().setRabbitAdmin(rabbitAdmin);
@@ -134,7 +129,7 @@ public void can_send_events_which_are_put_in_the_waitlist() throws Exception {
134129
TimeUnit.SECONDS.sleep(2);
135130
waitListSize = dbManager.waitListSize();
136131
}
137-
assertEquals(4, waitListSize);
132+
assertEquals(1, waitListSize);
138133
}
139134

140135
@When("^add the binding documents to mongoDB$")
@@ -156,10 +151,7 @@ public void compare_the_binding_keys_and_remove_the_old_binding_keys_from_rabbit
156151
*/
157152
protected List<String> getEventNamesToSend() {
158153
List<String> eventNames = new ArrayList<>();
159-
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
160154
eventNames.add("event_EiffelArtifactPublishedEvent_3");
161-
eventNames.add("event_EiffelTestCaseTriggeredEvent_3");
162-
eventNames.add("event_EiffelTestCaseStartedEvent_3");
163155
return eventNames;
164156
}
165157

@@ -179,7 +171,7 @@ private RabbitAdmin createExchange(final RMQHandler rmqHandler) {
179171
admin.getQueueProperties(queueName);
180172
RabbitTemplate rabbitTemplate = admin.getRabbitTemplate();
181173
rabbitTemplate.setExchange(exchangeName);
182-
rabbitTemplate.setRoutingKey(rmqProperties.getWaitlistQueueName());
174+
rabbitTemplate.setRoutingKey(DEFAULT_ROUTING_KEY);
183175
rabbitTemplate.setQueue(queueName);
184176
return admin;
185177
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2021 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
package com.ericsson.ei.exception;
15+
16+
public class MongoDBConnectionException extends Exception {
17+
18+
private static final long serialVersionUID = 2L;
19+
20+
public MongoDBConnectionException() {
21+
super();
22+
}
23+
24+
public MongoDBConnectionException(String message) {
25+
super(message);
26+
}
27+
28+
public MongoDBConnectionException(String message, Throwable e) {
29+
super(message, e);
30+
}
31+
32+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.stereotype.Component;
2626

27+
import com.ericsson.ei.exception.MongoDBConnectionException;
2728
import com.ericsson.ei.jmespath.JmesPathInterface;
2829
import com.ericsson.ei.rules.RulesObject;
2930
import com.ericsson.ei.waitlist.WaitListStorageHandler;
@@ -50,7 +51,8 @@ public void setJmesPathInterface(JmesPathInterface jmesPathInterface) {
5051
this.jmesPathInterface = jmesPathInterface;
5152
}
5253

53-
public void runIdRules(RulesObject rulesObject, String event) {
54+
public void runIdRules(RulesObject rulesObject, String event)
55+
throws MongoDBConnectionException {
5456
if (rulesObject != null && event != null) {
5557
JsonNode idsJsonObj = getIds(rulesObject, event);
5658
List<String> objects;

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

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

19+
import com.ericsson.ei.exception.MongoDBConnectionException;
1920
import com.ericsson.ei.rules.IdRulesHandler;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
@@ -30,6 +31,8 @@
3031
import com.fasterxml.jackson.databind.JsonNode;
3132
import com.fasterxml.jackson.databind.ObjectMapper;
3233
import com.rabbitmq.client.Channel;
34+
import com.ericsson.ei.utils.MongoDBMonitorThread;
35+
import com.ericsson.ei.utils.SpringContext;
3336

3437
@Component
3538
public class EventHandler {
@@ -39,6 +42,9 @@ public class EventHandler {
3942
@Autowired
4043
RulesHandler rulesHandler;
4144

45+
@Autowired
46+
MongoDBMonitorThread mongoDBMonitorThread;
47+
4248
@Autowired
4349
IdRulesHandler idRulesHandler;
4450

@@ -52,7 +58,7 @@ public RulesHandler getRulesHandler() {
5258
return rulesHandler;
5359
}
5460

55-
public void eventReceived(String event) {
61+
public void eventReceived(String event) throws MongoDBConnectionException {
5662
RulesObject eventRules = rulesHandler.getRulesForEvent(event);
5763
idRulesHandler.runIdRules(eventRules, event);
5864
}
@@ -63,13 +69,49 @@ public void onMessage(Message message, Channel channel) throws Exception {
6369
ObjectMapper objectMapper = new ObjectMapper();
6470
JsonNode node = objectMapper.readTree(messageBody);
6571
String id = node.get("meta").get("id").toString();
66-
LOGGER.debug("Thread id {} spawned for EventHandler", Thread.currentThread().getId());
67-
LOGGER.debug("Event {} received", id);
6872

69-
eventReceived(messageBody);
7073
long deliveryTag = message.getMessageProperties().getDeliveryTag();
71-
channel.basicAck(deliveryTag, false);
72-
73-
LOGGER.debug("Event {} processed", id);
74+
LOGGER.debug("Thread id {} spawned for EventHandler", Thread.currentThread().getId());
75+
try {
76+
eventReceived(messageBody);
77+
channel.basicAck(deliveryTag, false);
78+
LOGGER.info("Event {} processed", id);
79+
} catch (MongoDBConnectionException mdce) {
80+
if (mdce.getMessage().equalsIgnoreCase("MongoDB Connection down")) {
81+
if (mongoDBMonitorThread.getState() == Thread.State.NEW
82+
|| mongoDBMonitorThread.getState() == Thread.State.TERMINATED) {
83+
// if the previous Thread state is TERMINATED then get a new
84+
// mongoDBMonitorThread instance
85+
synchronized (this) {
86+
if (mongoDBMonitorThread.getState() == Thread.State.TERMINATED) {
87+
mongoDBMonitorThread = SpringContext.getBean(
88+
MongoDBMonitorThread.class);
89+
}
90+
// New thread will start to monitor the mongoDB connection status
91+
if (mongoDBMonitorThread.getState() == Thread.State.NEW) {
92+
mongoDBMonitorThread.setMongoDBConnected(false);
93+
mongoDBMonitorThread.start();
94+
}
95+
}
96+
}
97+
// Continue the loop till the mongoDB connection is Re-established
98+
while (!mongoDBMonitorThread.isMongoDBConnected()) {
99+
try {
100+
Thread.sleep(30000);
101+
LOGGER.debug("Waiting for MongoDB connection...");
102+
} catch (InterruptedException ie) {
103+
LOGGER.error("MongoDBMonitorThread got Interrupted");
104+
}
105+
}
106+
}
107+
// once the mongoDB Connection is up event will be sent back to queue with
108+
// un-acknowledgement
109+
channel.basicNack(deliveryTag, false, true);
110+
LOGGER.debug(
111+
"Sent back the event to queue with un-acknowledgement: " + message.getBody());
112+
} catch (Exception e) {
113+
LOGGER.error("Event is not Re-queued due to exception " + e);
114+
}
74115
}
116+
75117
}

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class EventToObjectMapHandler {
5050

5151
@Value("${event.object.map.collection.name}") private String collectionName;
5252
@Value("${spring.data.mongodb.database}") private String databaseName;
53+
5354

5455
private final String listPropertyName = "objects";
5556

@@ -84,28 +85,43 @@ public ArrayList<String> getObjectsForEventId(String eventId) {
8485
return getEventToObjectList(eventId);
8586
}
8687

87-
public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String event, String objectId) {
88+
/**
89+
* To check and save the eventIds to the objectId in the mapped database.
90+
*
91+
* @param rulesObject
92+
* @param event
93+
* @param objectId aggregated event object Id
94+
*/
95+
public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String event,
96+
String objectId) {
8897
String eventId = getEventId(rulesObject, event);
98+
8999
final MongoCondition condition = MongoCondition.idCondition(eventId);
90-
ArrayList<String> list = getEventToObjectList(eventId);
91-
boolean firstTime = list.isEmpty();
92-
list = updateList(list, eventId, objectId);
93-
ObjectMapper mapper = new ObjectMapper();
94-
JsonNode entry = null;
100+
LOGGER.debug(
101+
"Checking document exists in the collection with condition : {}\n EventId : {}",
102+
condition, eventId);
103+
boolean docExists = mongodbhandler.checkDocumentExists(databaseName, collectionName,
104+
condition);
95105

96106
try {
97-
entry = new ObjectMapper().readValue(condition.toString(), JsonNode.class);
98-
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
99-
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
100-
String mapStr = entry.toString();
101-
LOGGER.debug("MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}", mapStr, databaseName, collectionName);
102-
if (firstTime) {
107+
if (!docExists) {
108+
ArrayList<String> list = new ArrayList<String>();
109+
list.add(eventId);
110+
final ObjectMapper mapper = new ObjectMapper();
111+
JsonNode entry = new ObjectMapper().readValue(condition.toString(), JsonNode.class);
112+
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
113+
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
114+
final String mapStr = entry.toString();
115+
LOGGER.debug(
116+
"MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}",
117+
mapStr, databaseName, collectionName);
103118
mongodbhandler.insertDocument(databaseName, collectionName, mapStr);
104119
} else {
105-
mongodbhandler.updateDocument(databaseName, collectionName, condition, mapStr);
120+
mongodbhandler.updateDocumentAddToSet(databaseName, collectionName, condition,
121+
eventId);
106122
}
107123
} catch (Exception e) {
108-
LOGGER.info("Failed to update event object list.", e);
124+
LOGGER.error("Failed to update event object list.", e);
109125
}
110126
}
111127

@@ -130,9 +146,9 @@ public ArrayList<String> getEventToObjectList(String eventId) {
130146
try {
131147
JsonNode document = mapper.readValue(mapStr, JsonNode.class);
132148
JsonNode value = document.get(listPropertyName);
133-
list = new ObjectMapper().readValue(value.traverse(), new TypeReference<ArrayList<String>>(){});
149+
list = new ObjectMapper().readValue(value.traverse(), new TypeReference<ArrayList<String>>() {});
134150
} catch (Exception e) {
135-
LOGGER.info("Failed to deserialize event object list.", e);
151+
LOGGER.error("Failed to deserialize event object list.", e);
136152
}
137153
}
138154
return list;
@@ -157,5 +173,4 @@ public boolean isEventInEventObjectMap(String eventId) {
157173
return !documents.isEmpty();
158174
}
159175

160-
161176
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.stereotype.Component;
2424

25+
import com.ericsson.ei.exception.MongoDBConnectionException;
2526
import com.ericsson.ei.exception.PropertyNotFoundException;
2627
import com.ericsson.ei.jmespath.JmesPathInterface;
2728
import com.ericsson.ei.jsonmerge.MergeHandler;
@@ -61,17 +62,20 @@ public void setObjectHandler(ObjectHandler objectHandler) {
6162
this.objectHandler = objectHandler;
6263
}
6364

64-
public void runExtraction(RulesObject rulesObject, String id, String event, String aggregatedDbObject) {
65+
public void runExtraction(RulesObject rulesObject, String id, String event, String aggregatedDbObject) throws MongoDBConnectionException {
6566
try {
6667
ObjectMapper mapper = new ObjectMapper();
6768
JsonNode aggregatedJsonObject = mapper.readTree(aggregatedDbObject);
6869
runExtraction(rulesObject, id, event, aggregatedJsonObject);
6970
} catch (Exception e) {
70-
LOGGER.info("Failed with extraction.", e);
71+
LOGGER.error("Failed with extraction.", e);
72+
if (e.getMessage().equalsIgnoreCase("MongoDB Connection down")) {
73+
throw new MongoDBConnectionException("MongoDB Connection down");
74+
}
7175
}
7276
}
7377

74-
public void runExtraction(RulesObject rulesObject, String mergeId, String event, JsonNode aggregatedDbObject) {
78+
public void runExtraction(RulesObject rulesObject, String mergeId, String event, JsonNode aggregatedDbObject) throws MongoDBConnectionException {
7579
try {
7680
JsonNode extractedContent = extractContent(rulesObject, event);
7781

@@ -93,6 +97,9 @@ public void runExtraction(RulesObject rulesObject, String mergeId, String event,
9397
LOGGER.debug("Did not run history extraction on upstream events.", e);
9498
} catch (Exception e) {
9599
LOGGER.error("Failed to run extraction for event {}", event, e);
100+
if (e.getMessage().equalsIgnoreCase("MongoDB Connection down")) {
101+
throw new MongoDBConnectionException("MongoDB Connection down");
102+
}
96103
}
97104
}
98105

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.annotation.Value;
3131
import org.springframework.stereotype.Component;
3232

33+
import com.ericsson.ei.exception.MongoDBConnectionException;
3334
import com.ericsson.ei.jmespath.JmesPathInterface;
3435
import com.ericsson.ei.rules.RulesObject;
3536
import com.ericsson.ei.subscription.SubscriptionHandler;
@@ -86,9 +87,10 @@ public class ObjectHandler {
8687
* @param rulesObject RulesObject
8788
* @param event String representation of event, used to fetch id if not specified
8889
* @param givenId String id is stored together with aggregated object in database
90+
* @throws MongoDBConnectionException
8991
*/
9092
public void insertObject(String aggregatedObject, RulesObject rulesObject, String event,
91-
String givenId) {
93+
String givenId) throws MongoDBConnectionException {
9294
String id = givenId;
9395
if (id == null) {
9496
String idRules = rulesObject.getIdRule();
@@ -109,7 +111,7 @@ public void insertObject(String aggregatedObject, RulesObject rulesObject, Strin
109111
}
110112

111113
public void insertObject(JsonNode aggregatedObject, RulesObject rulesObject, String event,
112-
String id) {
114+
String id) throws MongoDBConnectionException {
113115
insertObject(aggregatedObject.toString(), rulesObject, event, id);
114116
}
115117

0 commit comments

Comments
 (0)