Skip to content

Commit 4c23f02

Browse files
authored
EI Backend Instances fault handling and logging improvements. (#199)
1 parent b17a830 commit 4c23f02

File tree

10 files changed

+152
-32
lines changed

10 files changed

+152
-32
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ install:
3131
- chmod +x pom.xml
3232
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
3333
- export EIFFEL_WAR=$(ls target/*.war)
34-
- java -Dspring.config.location=file:../src/integrationtest/resources/integration-test.properties -Dserver.port=${EI_BACKEND_PORT} -jar ${EIFFEL_WAR} &
34+
- java -Dspring.config.additional-location=file:../src/integrationtest/resources/integration-test.properties -Dserver.port=${EI_BACKEND_PORT} -jar ${EIFFEL_WAR} &
3535
- cd ..
3636

3737

src/integrationtest/resources/integration-test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ testaggregated.enabled: true
9393
threads.corePoolSize: 100
9494
threads.queueCapacity: 5000
9595
threads.maxPoolSize: 150
96+
9697
# name of the collection where missed notifications are stored
9798
missedNotificationCollectionName: Notification
9899
# name of the database where missed notifications are stored

src/main/java/com/ericsson/ei/frontend/BackEndInformationController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public class BackEndInformationController {
4242
@CrossOrigin
4343
@RequestMapping(value = "/backend", method = RequestMethod.GET)
4444
public ResponseEntity<String> getInstances(Model model, HttpServletRequest request) {
45-
LOG.debug("Recieved request for instances.");
45+
LOG.debug("Received request for retrieving EI Backend instances list.");
4646
ResponseEntity<String> response = backEndInfoContUtils.handleRequestForInstances(request);
4747
return response;
4848
}
4949

5050
@CrossOrigin
5151
@RequestMapping(value = "/backend", method = RequestMethod.PUT)
5252
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
53-
LOG.debug("Recieved request to switch back end.");
53+
LOG.debug("Received request to switch EI back end instance.");
5454
ResponseEntity<String> response = backEndInfoContUtils.handleRequestToSwitchBackEnd(request);
5555
return response;
5656

@@ -59,15 +59,15 @@ public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequ
5959
@CrossOrigin
6060
@RequestMapping(value = "/backend", method = RequestMethod.DELETE)
6161
public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) {
62-
LOG.debug("Recieved request to delete back end.");
62+
LOG.debug("Received request to delete EI back end instance.");
6363
ResponseEntity<String> response = backEndInfoContUtils.handleRequestToDeleteBackEnd(request);
6464
return response;
6565
}
6666

6767
@CrossOrigin
6868
@RequestMapping(value = "/backend", method = RequestMethod.POST)
6969
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
70-
LOG.debug("Recieved request to add back end.");
70+
LOG.debug("Received request to add EI back end instance.");
7171
ResponseEntity<String> response = backEndInfoContUtils.handleRequestToAddBackEnd(request);
7272
return response;
7373
}

src/main/java/com/ericsson/ei/frontend/EIRequestsController.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.web.bind.annotation.RequestMethod;
4949
import org.springframework.web.bind.annotation.RestController;
5050

51+
import com.ericsson.ei.frontend.exceptions.EiBackendInstancesException;
5152
import com.ericsson.ei.frontend.utils.EIRequestsControllerUtils;
5253

5354
@RestController
@@ -73,7 +74,14 @@ public class EIRequestsController {
7374
"/queryAggregatedObject", "/queryMissedNotifications", "/query",
7475
"/rules/rule-check/testRulePageEnabled" }, method = RequestMethod.GET)
7576
public ResponseEntity<String> getRequests(Model model, HttpServletRequest incomingRequest) {
76-
String eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
77+
String eiRequestUrl;
78+
try {
79+
eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
80+
} catch (EiBackendInstancesException e) {
81+
LOG.info("Some failure when forwarding request to EI Backend. Error: " + e.getMessage());
82+
String response = "{\"message\": \"Internal Error: " + e.getMessage() + "\"}";
83+
return new ResponseEntity<>(response, null, HttpStatus.INTERNAL_SERVER_ERROR);
84+
}
7785
HttpGet outgoingRequest = new HttpGet(eiRequestUrl);
7886

7987
outgoingRequest = (HttpGet) addHeadersToRequest(outgoingRequest, incomingRequest);
@@ -92,7 +100,14 @@ public ResponseEntity<String> getRequests(Model model, HttpServletRequest incomi
92100
@RequestMapping(value = { "/subscriptions", "/rules/rule-check/aggregation",
93101
"/query" }, method = RequestMethod.POST)
94102
public ResponseEntity<String> postRequests(Model model, HttpServletRequest incomingRequest) {
95-
String eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
103+
String eiRequestUrl;
104+
try {
105+
eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
106+
} catch (EiBackendInstancesException e) {
107+
LOG.info("Some failure when forwarding request to EI Backend. Error: " + e.getMessage());
108+
String response = "{\"message\": \"Internal Error: " + e.getMessage() + "\"}";
109+
return new ResponseEntity<>(response, null, HttpStatus.INTERNAL_SERVER_ERROR);
110+
}
96111
String requestBody = "";
97112

98113
try {
@@ -129,7 +144,14 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest incom
129144
@CrossOrigin
130145
@RequestMapping(value = "/subscriptions", method = RequestMethod.PUT)
131146
public ResponseEntity<String> putRequests(Model model, HttpServletRequest incomingRequest) {
132-
String eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
147+
String eiRequestUrl;
148+
try {
149+
eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
150+
} catch (EiBackendInstancesException e) {
151+
LOG.info("Some failure when forwarding request to EI Backend. Error: " + e.getMessage());
152+
String response = "{\"message\": \"Internal Error: " + e.getMessage() + "\"}";
153+
return new ResponseEntity<>(response, null, HttpStatus.INTERNAL_SERVER_ERROR);
154+
}
133155
String requestBody = "";
134156

135157
try {
@@ -164,8 +186,14 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest incomi
164186
@CrossOrigin
165187
@RequestMapping(value = "/subscriptions/*", method = RequestMethod.DELETE)
166188
public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest incomingRequest) {
167-
String eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
168-
189+
String eiRequestUrl;
190+
try {
191+
eiRequestUrl = eiRequestsControllerUtils.getEIRequestURL(incomingRequest);
192+
} catch (EiBackendInstancesException e) {
193+
LOG.info("Some failure when forwarding request to EI Backend. Error: " + e.getMessage());
194+
String response = "{\"message\": \"Internal Error: " + e.getMessage() + "\"}";
195+
return new ResponseEntity<>(response, null, HttpStatus.INTERNAL_SERVER_ERROR);
196+
}
169197
HttpDelete outgoingRequest = new HttpDelete(eiRequestUrl);
170198

171199
outgoingRequest = (HttpDelete) addHeadersToRequest(outgoingRequest, incomingRequest);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ericsson.ei.frontend.config;
2+
3+
import java.io.IOException;
4+
5+
import javax.annotation.PostConstruct;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.core.env.Environment;
11+
import org.springframework.stereotype.Component;
12+
13+
14+
@Component
15+
public class CheckEIConfigurations {
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(CheckEIConfigurations.class);
18+
19+
@Autowired
20+
Environment env;
21+
22+
@PostConstruct
23+
public void logAndCheckConfiguration() {
24+
25+
LOGGER.debug("EI Frontend started with following configurations:\n"
26+
+ "server.port: " + env.getProperty("server.port") + "\n"
27+
+ "ei.frontend.service.port: " + env.getProperty("ei.frontend.service.port") + "\n"
28+
+ "ei.frontend.service.host: " + env.getProperty("ei.frontend.service.host") + "\n"
29+
+ "ei.frontend.context.path: " + env.getProperty("ei.frontend.context.path") + "\n"
30+
+ "ei.use.secure.http.frontend: " + env.getProperty("ei.use.secure.http.frontend") + "\n"
31+
+ "ei.backend.instances.filepath: " + env.getProperty("ei.backend.instances.filepath") + "\n"
32+
+ "ei.backend.instances.list.json.content: " + env.getProperty("ei.backend.instances.list.json.content") + "\n"
33+
+ "ei.eiffel.documentation.urls: " + env.getProperty("ei.eiffel.documentation.urls") + "\n"
34+
+ "logging.level.root: " + env.getProperty("logging.level.root") + "\n"
35+
+ "logging.level.org.springframework.web: " + env.getProperty("logging.level.org.springframework.web") + "\n"
36+
+ "logging.level.com.ericsson.ei: " + env.getProperty("logging.level.com.ericsson.ei") + "\n"
37+
+ "\nThese properties is only some of the configurations, more configurations may have been provided.\n");
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2019 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.frontend.exceptions;
18+
19+
public class EiBackendInstancesException extends Exception {
20+
21+
private static final long serialVersionUID = 2L;
22+
23+
public EiBackendInstancesException() {
24+
super();
25+
}
26+
27+
public EiBackendInstancesException(String message) {
28+
super(message);
29+
}
30+
31+
public EiBackendInstancesException(String message, Throwable e) {
32+
super(message, e);
33+
}
34+
35+
}

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstanceFileUtils.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.PrintWriter;
56
import java.nio.file.Files;
67
import java.nio.file.Path;
78
import java.nio.file.Paths;
@@ -75,6 +76,7 @@ public void init() throws IOException {
7576
LOG.debug("EI Instances List file path is not provided! " +
7677
"Will create a default EI Instances List file at file path: " + eiInstancesPath);
7778
}
79+
LOG.info("Loading EI-Backend instances from EI Instances List file: " + eiInstancesPath);
7880
parseAndSetEiInstancesList();
7981
}
8082

@@ -155,14 +157,19 @@ private void setDefaultEiBackendInstance(JsonArray jArray) {
155157
* @return JsonArray
156158
*/
157159
public JsonArray getInstancesFromFile() {
160+
LOG.debug("Reading EI Instances File: " + eiInstancesPath);
158161
try {
159162
ensureValidFile();
160163

161164
JsonArray inputBackEndInstances = new JsonParser()
162165
.parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
166+
if (inputBackEndInstances.isJsonNull() || inputBackEndInstances.size() < 1) {
167+
LOG.debug("EI Instance file does not exist or is empty. Returning an empty EI Instance json list.");
168+
return new JsonArray();
169+
}
163170
return inputBackEndInstances;
164-
} catch (IOException e) {
165-
LOG.error("Failure when try to parse json file" + e.getMessage());
171+
} catch (Exception e) {
172+
LOG.error("Failure when try to parse EI instances json file. Error: " + e.getMessage());
166173
return new JsonArray();
167174
}
168175
}
@@ -176,7 +183,7 @@ public void dumpJsonArray(JsonArray jsonArrayToDump) {
176183
try {
177184
Files.write(Paths.get(eiInstancesPath), jsonArrayToDump.toString().getBytes());
178185
} catch (IOException e) {
179-
LOG.error("Couldn't add instance to file " + e.getMessage());
186+
LOG.error("Couldn't add EI instance to EI Instances file. Error: " + e.getMessage());
180187
}
181188

182189
}
@@ -189,7 +196,7 @@ private void ensureValidFile() {
189196
}
190197

191198
if (!fileContainsJsonArray()) {
192-
LOG.error("File does not contain valid json! JSON:' "
199+
LOG.error("EI Instances file does not contain valid json! JSON:' "
193200
+ new String(Files.readAllBytes(Paths.get(eiInstancesPath))) + "'.");
194201
System.exit(-1);
195202
}
@@ -211,17 +218,19 @@ private void createFileWithDirs() throws IOException {
211218
eiInstancesParentFolder.mkdirs();
212219
}
213220

214-
LOG.debug("File does not exist! Trying to create file.");
221+
LOG.debug("EI Instances file does not exist! Trying to create file.");
215222
Files.createFile(Paths.get(eiInstancesPath));
216-
Files.write(Paths.get(eiInstancesPath), "[]".getBytes());
223+
PrintWriter eiInstanceFilePrintWriter = new PrintWriter(eiInstancesPath);
224+
eiInstanceFilePrintWriter.println("[]");
225+
eiInstanceFilePrintWriter.close();
217226
}
218227

219228
private boolean fileContainsJsonArray() {
220229
try {
221230
new JsonParser().parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
222231
return true;
223232
} catch (Exception e) {
224-
LOG.error("Failure when try to parse json file" + e.getMessage());
233+
LOG.error("Failure when try to parse EI Instances json file. Error: " + e.getMessage());
225234
return false;
226235
}
227236
}

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstancesUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public BackEndInformation getBackEndInformationByName(String backEndName) {
194194
}
195195

196196
if (backEndInformationList.size() == 0) {
197-
LOG.error("No backend information found!");
198197
return null;
199198
}
200199

@@ -240,6 +239,7 @@ public void deleteBackEnd(JsonObject objectToDelete) {
240239
* @return JsonArray with back end information data.
241240
*/
242241
public JsonArray getBackEndsAsJsonArray() {
242+
LOG.debug("Read and return EI Backend instances as JsonArray.");
243243
parseBackEndInstances();
244244
return parseBackEndsAsJsonArray();
245245
}
@@ -261,6 +261,7 @@ public void setDefaultBackEndInstanceToNull() {
261261
* @param def
262262
*/
263263
public void setDefaultBackEndInstance(String name, String host, int port, String contextPath, boolean def) {
264+
LOG.debug("Setting default EI Instance: " + name + ", " + host + ":" + port);
264265
getDefaultBackendInformation().setName(name);
265266
getDefaultBackendInformation().setHost(host);
266267
getDefaultBackendInformation().setPort(String.valueOf(port));
@@ -348,7 +349,7 @@ private boolean parsingIsApplicable() {
348349

349350
private void ensureDefaultBackEnd() {
350351
if (defaultBackendInformation.getHost() == null || defaultBackendInformation.getPort() == null) {
351-
LOG.debug("No default Host or Port set!");
352+
LOG.debug("No default EI Backend instance Host and/or Port has been set!");
352353
return;
353354
}
354355
defaultBackendInformation.setDefaultBackend(true);

0 commit comments

Comments
 (0)