Search in sources :

Example 1 with APIException

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;
}
Also used : APIException(io.seldon.engine.exception.APIException) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Example 2 with APIException

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, "");
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) APIException(io.seldon.engine.exception.APIException) Feedback(io.seldon.protos.PredictionProtos.Feedback) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ExecutionException(java.util.concurrent.ExecutionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with APIException

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 {
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) APIException(io.seldon.engine.exception.APIException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) APIException(io.seldon.engine.exception.APIException) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 4 with APIException

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");
}
Also used : APIException(io.seldon.engine.exception.APIException) Endpoint(io.seldon.protos.DeploymentProtos.Endpoint) GenericBlockingStub(io.seldon.protos.GenericGrpc.GenericBlockingStub) CombinerBlockingStub(io.seldon.protos.CombinerGrpc.CombinerBlockingStub) SeldonMessageList(io.seldon.protos.PredictionProtos.SeldonMessageList)

Example 5 with APIException

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, "");
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) APIException(io.seldon.engine.exception.APIException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ExecutionException(java.util.concurrent.ExecutionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

APIException (io.seldon.engine.exception.APIException)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 SeldonMessage (io.seldon.protos.PredictionProtos.SeldonMessage)3 ExecutionException (java.util.concurrent.ExecutionException)2 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 CombinerBlockingStub (io.seldon.protos.CombinerGrpc.CombinerBlockingStub)1 Endpoint (io.seldon.protos.DeploymentProtos.Endpoint)1 GenericBlockingStub (io.seldon.protos.GenericGrpc.GenericBlockingStub)1 DefaultData (io.seldon.protos.PredictionProtos.DefaultData)1 Feedback (io.seldon.protos.PredictionProtos.Feedback)1 SeldonMessageList (io.seldon.protos.PredictionProtos.SeldonMessageList)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1