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;
}
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;
}
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 {
}
}
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);
}
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();
}
Aggregations