use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.
the class PredictiveUnitBean method getBranchIndex.
// -------------------------------------------
//
// -------------------------------------------
private int getBranchIndex(SeldonMessage routerReturn, PredictiveUnitState state) {
int branchIndex = 0;
try {
INDArray dataArray = PredictorUtils.getINDArray(routerReturn.getData());
branchIndex = (int) dataArray.getInt(0);
} catch (IndexOutOfBoundsException e) {
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_ROUTING, "Router that caused the exception: id=" + state.name + " name=" + state.name);
}
return branchIndex;
}
use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.
the class RestClientController method feedback.
@RequestMapping(value = "/api/v0.1/feedback", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> feedback(RequestEntity<String> requestEntity) {
Feedback feedback;
try {
Feedback.Builder builder = Feedback.newBuilder();
ProtoBufUtils.updateMessageBuilderFromJson(builder, requestEntity.getBody());
feedback = builder.build();
} catch (InvalidProtocolBufferException e) {
logger.error("Bad request", e);
throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, requestEntity.getBody());
}
try {
predictionService.sendFeedback(feedback);
String json = "{}";
return new ResponseEntity<String>(json, HttpStatus.OK);
} catch (InterruptedException e) {
throw new APIException(ApiExceptionType.ENGINE_INTERRUPTED, e.getMessage());
} catch (ExecutionException e) {
if (e.getCause().getClass() == APIException.class) {
throw (APIException) e.getCause();
} else {
throw new APIException(ApiExceptionType.ENGINE_EXECUTION_FAILURE, e.getMessage());
}
} catch (InvalidProtocolBufferException e) {
throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, "");
}
}
use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.
the class InternalPredictionService method queryREST.
private SeldonMessage queryREST(String path, String dataString, PredictiveUnitState state, Endpoint endpoint, boolean isDefault) {
long timeNow = System.currentTimeMillis();
URI uri;
try {
URIBuilder builder = new URIBuilder().setScheme("http").setHost(endpoint.getServiceHost()).setPort(endpoint.getServicePort()).setPath("/" + path);
uri = builder.build();
} catch (URISyntaxException e) {
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_ENDPOINT_URL, "Host: " + endpoint.getServiceHost() + " port:" + endpoint.getServicePort());
}
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add(MODEL_NAME_HEADER, state.name);
headers.add(MODEL_IMAGE_HEADER, state.imageName);
headers.add(MODEL_VERSION_HEADER, state.imageVersion);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("json", dataString);
map.add("isDefault", Boolean.toString(isDefault));
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
logger.info("Requesting " + uri.toString());
ResponseEntity<String> httpResponse = restTemplate.postForEntity(uri, request, String.class);
try {
if (httpResponse.getStatusCode().is2xxSuccessful()) {
SeldonMessage.Builder builder = SeldonMessage.newBuilder();
String response = httpResponse.getBody();
logger.info(response);
JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
return builder.build();
} else {
logger.error("Couldn't retrieve prediction from external prediction server -- bad http return code: " + httpResponse.getStatusCode());
throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, String.format("Bad return code %d", httpResponse.getStatusCode()));
}
} finally {
if (logger.isDebugEnabled())
logger.debug("External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
}
} catch (IOException e) {
logger.error("Couldn't retrieve prediction from external prediction server - ", e);
throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
} catch (Exception e) {
logger.error("Couldn't retrieve prediction from external prediction server - ", e);
throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
} finally {
}
}
use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.
the class InternalPredictionService method aggregate.
public SeldonMessage aggregate(List<SeldonMessage> outputs, PredictiveUnitState state) throws InvalidProtocolBufferException {
final Endpoint endpoint = state.endpoint;
SeldonMessageList outputsList = SeldonMessageList.newBuilder().addAllSeldonMessages(outputs).build();
switch(endpoint.getType()) {
case REST:
String dataString = ProtoBufUtils.toJson(outputsList);
return queryREST("aggregate", dataString, state, endpoint, true);
case GRPC:
if (state.type == PredictiveUnitType.UNKNOWN_TYPE) {
GenericBlockingStub stub = GenericGrpc.newBlockingStub(getChannel(endpoint)).withDeadlineAfter(TIMEOUT, TimeUnit.SECONDS);
return stub.aggregate(outputsList);
} else {
CombinerBlockingStub stub = CombinerGrpc.newBlockingStub(getChannel(endpoint)).withDeadlineAfter(TIMEOUT, TimeUnit.SECONDS);
return stub.aggregate(outputsList);
}
}
throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, "no service available");
}
use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.
the class RestClientController method predictions.
@RequestMapping(value = "/api/v0.1/predictions", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> predictions(RequestEntity<String> requestEntity) {
SeldonMessage request;
try {
SeldonMessage.Builder builder = SeldonMessage.newBuilder();
ProtoBufUtils.updateMessageBuilderFromJson(builder, requestEntity.getBody());
request = builder.build();
} catch (InvalidProtocolBufferException e) {
logger.error("Bad request", e);
throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, requestEntity.getBody());
}
try {
SeldonMessage response = predictionService.predict(request);
String json = ProtoBufUtils.toJson(response);
return new ResponseEntity<String>(json, HttpStatus.OK);
} catch (InterruptedException e) {
throw new APIException(ApiExceptionType.ENGINE_INTERRUPTED, e.getMessage());
} catch (ExecutionException e) {
if (e.getCause().getClass() == APIException.class) {
throw (APIException) e.getCause();
} else {
throw new APIException(ApiExceptionType.ENGINE_EXECUTION_FAILURE, e.getMessage());
}
} catch (InvalidProtocolBufferException e) {
throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, "");
}
}
Aggregations