Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/improve exareme response status #295

Merged
merged 2 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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