Search in sources :

Example 1 with SeldonAPIException

use of io.seldon.apife.exception.SeldonAPIException in project seldon-core by SeldonIO.

the class RestClientController method prediction.

@RequestMapping(value = "/api/v0.1/predictions", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> prediction(RequestEntity<String> requestEntity, Principal principal) {
    String clientId = principal.getName();
    String json = requestEntity.getBody();
    logger.info(String.format("[%s] [%s] [%s] [%s]", "POST", requestEntity.getUrl().getPath(), clientId, json));
    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 SeldonAPIException(ApiExceptionType.APIFE_INVALID_JSON, requestEntity.getBody());
    }
    HttpStatus httpStatus = HttpStatus.OK;
    // At present passes JSON string. Could use gRPC?
    String ret = predictionService.predict(json, clientId);
    SeldonMessage response;
    try {
        SeldonMessage.Builder builder = SeldonMessage.newBuilder();
        ProtoBufUtils.updateMessageBuilderFromJson(builder, ret);
        response = builder.build();
    } catch (InvalidProtocolBufferException e) {
        logger.error("Bad response", e);
        throw new SeldonAPIException(ApiExceptionType.APIFE_INVALID_RESPONSE_JSON, requestEntity.getBody());
    }
    kafkaProducer.send(clientId, RequestResponse.newBuilder().setRequest(request).setResponse(response).build());
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);
    ResponseEntity<String> responseEntity = new ResponseEntity<String>(ret, responseHeaders, httpStatus);
    return responseEntity;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) HttpStatus(org.springframework.http.HttpStatus) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SeldonAPIException

use of io.seldon.apife.exception.SeldonAPIException in project seldon-core by SeldonIO.

the class SeldonGrpcServer method getChannel.

/**
 * Using the principal from authorization return a client gRPC channel to connect to the engine running the prediction graph.
 * @return ManagedChannel
 */
public ManagedChannel getChannel() {
    final String principal = getPrincipal();
    if (principal == null) {
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_GRPC_NO_PRINCIPAL_FOUND, "");
    }
    final DeploymentSpec deploymentSpec = deploymentStore.getDeployment(principal);
    if (deploymentSpec == null) {
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_NO_RUNNING_DEPLOYMENT, "Principal is " + principal);
    }
    ManagedChannel channel = channelStore.get(principal);
    if (channel == null) {
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_GRPC_NO_GRPC_CHANNEL_FOUND, "Principal is " + principal);
    }
    return channel;
}
Also used : SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) DeploymentSpec(io.seldon.protos.DeploymentProtos.DeploymentSpec) ManagedChannel(io.grpc.ManagedChannel)

Example 3 with SeldonAPIException

use of io.seldon.apife.exception.SeldonAPIException in project seldon-core by SeldonIO.

the class InternalPredictionService method sendFeedbackREST.

public void sendFeedbackREST(String feedback, String serviceName) {
    long timeNow = System.currentTimeMillis();
    URI uri;
    try {
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(serviceName).setPort(appProperties.getEngineContainerPort()).setPath("/api/v0.1/feedback");
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_INVALID_ENDPOINT_URL, "Host: " + serviceName + " port:" + appProperties.getEngineContainerPort());
    }
    StringEntity requestEntity = new StringEntity(feedback, ContentType.APPLICATION_JSON);
    HttpContext context = HttpClientContext.create();
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(requestEntity);
    try {
        if (logger.isDebugEnabled())
            logger.debug("Requesting " + httpPost.getURI().toString());
        CloseableHttpResponse resp = httpClient.execute(httpPost, context);
        try {
            resp.getEntity();
        } finally {
            if (resp != null)
                resp.close();
            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 SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_MICROSERVICE_ERROR, e.toString());
    } catch (Exception e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_MICROSERVICE_ERROR, e.toString());
    } finally {
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 4 with SeldonAPIException

use of io.seldon.apife.exception.SeldonAPIException 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")
@ResponseStatus(value = HttpStatus.OK)
public void feedback(RequestEntity<String> requestEntity, Principal principal) {
    String clientId = principal.getName();
    String json = requestEntity.getBody();
    Feedback feedback;
    try {
        Feedback.Builder builder = Feedback.newBuilder();
        ProtoBufUtils.updateMessageBuilderFromJson(builder, requestEntity.getBody());
        feedback = builder.build();
        Iterable<Tag> tags = asList(tagsProvider.principal(clientId), tagsProvider.projectName(clientId), tagsProvider.deploymentName(clientId), tagsProvider.deploymentVersion(clientId));
        Counter.builder("seldon_api_ingress_server_feedback_reward").tags(tags).register(Metrics.globalRegistry).increment(feedback.getReward());
        Counter.builder("seldon_api_ingress_server_feedback").tags(tags).register(Metrics.globalRegistry).increment();
    } catch (InvalidProtocolBufferException e) {
        logger.error("Bad request", e);
        throw new SeldonAPIException(ApiExceptionType.APIFE_INVALID_RESPONSE_JSON, requestEntity.getBody());
    }
    predictionService.sendFeedback(json, clientId);
}
Also used : SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) Feedback(io.seldon.protos.PredictionProtos.Feedback) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Tag(io.micrometer.core.instrument.Tag) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with SeldonAPIException

use of io.seldon.apife.exception.SeldonAPIException in project seldon-core by SeldonIO.

the class SeldonService method predict.

@Override
public void predict(io.seldon.protos.PredictionProtos.SeldonMessage request, io.grpc.stub.StreamObserver<io.seldon.protos.PredictionProtos.SeldonMessage> responseObserver) {
    try {
        ManagedChannel channel = server.getChannel();
        final SeldonGrpc.SeldonBlockingStub blockingStub = SeldonGrpc.newBlockingStub(channel);
        responseObserver.onNext(blockingStub.predict(request));
    } catch (SeldonAPIException e) {
        responseObserver.onError(e);
    }
    responseObserver.onCompleted();
}
Also used : SeldonGrpc(io.seldon.protos.SeldonGrpc) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) ManagedChannel(io.grpc.ManagedChannel)

Aggregations

SeldonAPIException (io.seldon.apife.exception.SeldonAPIException)6 ManagedChannel (io.grpc.ManagedChannel)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 SeldonGrpc (io.seldon.protos.SeldonGrpc)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Tag (io.micrometer.core.instrument.Tag)1 DeploymentSpec (io.seldon.protos.DeploymentProtos.DeploymentSpec)1 Feedback (io.seldon.protos.PredictionProtos.Feedback)1 SeldonMessage (io.seldon.protos.PredictionProtos.SeldonMessage)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 StringEntity (org.apache.http.entity.StringEntity)1 HttpContext (org.apache.http.protocol.HttpContext)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 ResponseEntity (org.springframework.http.ResponseEntity)1