Skip to content

Commit

Permalink
Merge pull request #295 from madgik/dev/improve_exareme_response_status
Browse files Browse the repository at this point in the history
Dev/improve exareme response status
  • Loading branch information
ThanKarab authored Nov 19, 2020
2 parents 6ac4212 + 70e8161 commit 72951e7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package madgik.exareme.master.gateway.async.handler.HBP.Exceptions;

public class RequestException extends Exception {
public RequestException(String algorithmName, String message) {
super(message + " Algorithm: " + algorithmName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package madgik.exareme.master.gateway.async.handler.HBP;

import com.google.gson.JsonSyntaxException;
import madgik.exareme.common.consts.HBPConstants;
import madgik.exareme.master.client.AdpDBClient;
import madgik.exareme.master.client.AdpDBClientFactory;
Expand All @@ -14,12 +13,12 @@
import madgik.exareme.master.engine.iterations.handler.NIterativeAlgorithmResultEntity;
import madgik.exareme.master.engine.iterations.state.IterativeAlgorithmState;
import madgik.exareme.master.gateway.ExaremeGatewayUtils;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.RequestException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.UserException;
import madgik.exareme.master.gateway.async.handler.entity.NQueryResultEntity;
import madgik.exareme.master.queryProcessor.HBP.AlgorithmProperties;
import madgik.exareme.master.queryProcessor.HBP.Algorithms;
import madgik.exareme.master.queryProcessor.HBP.Composer;
import madgik.exareme.master.queryProcessor.HBP.Exceptions.AlgorithmException;
import madgik.exareme.worker.art.container.ContainerProxy;
import org.apache.http.*;
import org.apache.http.entity.BasicHttpEntity;
Expand Down Expand Up @@ -100,9 +99,14 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
preExecutionChecks(request);

String algorithmName = getAlgorithmName(request);
AlgorithmProperties algorithmProperties = Algorithms.getInstance().getAlgorithmProperties(algorithmName);
if (algorithmProperties == null)
throw new RequestException(algorithmName, "The algorithm '" + algorithmName + "' does not exist.");

String algorithmKey = algorithmName + "_" + System.currentTimeMillis();
log.info("Executing algorithm: " + algorithmName + " with key: " + algorithmKey);

// Logging the algorithm execution parameters
log.info("Executing algorithm: " + algorithmName + " with key: " + algorithmKey);
HashMap<String, String> algorithmParameters = HBPQueryHelper.getAlgorithmParameters(request);
log.info("Request for algorithm: " + algorithmName);
if (algorithmParameters != null) {
Expand All @@ -114,10 +118,6 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo

AdpDBClientQueryStatus queryStatus;

AlgorithmProperties algorithmProperties = Algorithms.getInstance().getAlgorithmProperties(algorithmName);
if (algorithmProperties == null)
throw new AlgorithmException(algorithmName, "The algorithm '" + algorithmName + "' does not exist.");

algorithmProperties.mergeWithAlgorithmParameters(algorithmParameters);

DataSerialization ds = DataSerialization.summary;
Expand Down Expand Up @@ -167,27 +167,27 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(entity);
}
} catch (IterationsFatalException e) {
log.error(e);
if (e.getErroneousAlgorithmKey() != null)
iterationsHandler.removeIterativeAlgorithmStateInstanceFromISM(
e.getErroneousAlgorithmKey());
log.error(e);
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
} catch (UserException e) {
log.error(e.getMessage());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.user_error;
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (UserException e) {
} catch (RequestException e) {
log.error(e.getMessage());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.user_error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (JsonSyntaxException e) {
log.error("Could not parse the algorithms properly.");
} catch (IterationsFatalException e) {
log.error(e);
if (e.getErroneousAlgorithmKey() != null)
iterationsHandler.removeIterativeAlgorithmStateInstanceFromISM(
e.getErroneousAlgorithmKey());
log.error(e);
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(serverErrorOccurred, errorType));
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (Exception e) {
log.error(e.getMessage());
Expand All @@ -197,7 +197,7 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
}
log.error(e.getStackTrace());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
response.setEntity(createErrorResponseEntity(serverErrorOccurred, errorType));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import madgik.exareme.master.engine.AdpDBManager;
import madgik.exareme.master.engine.AdpDBManagerLocator;
import madgik.exareme.master.gateway.ExaremeGatewayUtils;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.RequestException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.UserException;
import madgik.exareme.master.gateway.async.handler.entity.NQueryResultEntity;
import madgik.exareme.master.queryProcessor.HBP.AlgorithmProperties;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void handle(
HttpResponse response = httpexchange.getResponse();
try {
handleInternal(request, response, context);
} catch (AlgorithmException | CDEsMetadataException | ComposerException | UserException e) {
} catch (AlgorithmException | CDEsMetadataException | ComposerException | UserException | RequestException e) {
e.printStackTrace();
}
httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
Expand All @@ -64,7 +65,7 @@ private void handleInternal(
final HttpRequest request,
final HttpResponse response,
final HttpContext context
) throws HttpException, IOException, AlgorithmException, CDEsMetadataException, ComposerException, UserException {
) throws HttpException, IOException, AlgorithmException, CDEsMetadataException, ComposerException, UserException, RequestException {

String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
Expand All @@ -87,7 +88,7 @@ private void handleInternal(
AlgorithmProperties algorithmProperties = Algorithms.getInstance().getAlgorithmProperties(algorithmName);

if (algorithmProperties == null)
throw new AlgorithmException(algorithmName, "The algorithm does not exist.");
throw new RequestException(algorithmName, "The algorithm does not exist.");

algorithmProperties.mergeWithAlgorithmParameters(inputContent);

Expand Down

0 comments on commit 72951e7

Please sign in to comment.