use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.
the class FunctionApiV2ResourceTest method testRegisterFunctionInterrupted.
@Test
public void testRegisterFunctionInterrupted() throws Exception {
mockStatic(Utils.class);
doNothing().when(Utils.class);
Utils.uploadToBookeeper(any(Namespace.class), any(InputStream.class), anyString());
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(false);
CompletableFuture<RequestResult> requestResult = FutureUtil.failedFuture(new IOException("Function registeration interrupted"));
when(mockedManager.updateFunction(any(FunctionMetaData.class))).thenReturn(requestResult);
Response response = registerDefaultFunction();
assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
assertEquals(new ErrorData("Function registeration interrupted").reason, ((ErrorData) response.getEntity()).reason);
}
use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.
the class FunctionMetaDataManager method submit.
@VisibleForTesting
CompletableFuture<RequestResult> submit(Request.ServiceRequest serviceRequest) {
ServiceRequestInfo serviceRequestInfo = ServiceRequestInfo.of(serviceRequest);
CompletableFuture<MessageId> messageIdCompletableFuture = this.serviceRequestManager.submitRequest(serviceRequest);
serviceRequestInfo.setCompletableFutureRequestMessageId(messageIdCompletableFuture);
CompletableFuture<RequestResult> requestResultCompletableFuture = new CompletableFuture<>();
serviceRequestInfo.setRequestResultCompletableFuture(requestResultCompletableFuture);
this.pendingServiceRequests.put(serviceRequestInfo.getServiceRequest().getRequestId(), serviceRequestInfo);
return requestResultCompletableFuture;
}
use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.
the class FunctionsImpl method updateRequest.
private Response updateRequest(FunctionMetaData functionMetaData, InputStream uploadedInputStream) {
// Upload to bookkeeper
try {
log.info("Uploading function package to {}", functionMetaData.getPackageLocation());
Utils.uploadToBookeeper(worker().getDlogNamespace(), uploadedInputStream, functionMetaData.getPackageLocation().getPackagePath());
} catch (IOException e) {
log.error("Error uploading file {}", functionMetaData.getPackageLocation(), e);
return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
}
// Submit to FMT
FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
CompletableFuture<RequestResult> completableFuture = functionMetaDataManager.updateFunction(functionMetaData);
RequestResult requestResult = null;
try {
requestResult = completableFuture.get();
if (!requestResult.isSuccess()) {
return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(requestResult.getMessage())).build();
}
} catch (ExecutionException e) {
return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
} catch (InterruptedException e) {
return Response.status(Status.REQUEST_TIMEOUT).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
}
return Response.status(Status.OK).build();
}
use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.
the class FunctionMetaDataManager method completeRequest.
/**
* Complete requests that this worker has pending
* @param serviceRequest
* @param isSuccess
* @param message
*/
private void completeRequest(Request.ServiceRequest serviceRequest, boolean isSuccess, String message) {
ServiceRequestInfo pendingServiceRequestInfo = this.pendingServiceRequests.getOrDefault(serviceRequest.getRequestId(), null);
if (pendingServiceRequestInfo != null) {
RequestResult requestResult = new RequestResult();
requestResult.setSuccess(isSuccess);
requestResult.setMessage(message);
pendingServiceRequestInfo.getRequestResultCompletableFuture().complete(requestResult);
}
}
use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.
the class FunctionApiV2ResourceTest method testUpdateFunctionSuccess.
@Test
public void testUpdateFunctionSuccess() throws Exception {
mockStatic(Utils.class);
doNothing().when(Utils.class);
Utils.uploadToBookeeper(any(Namespace.class), any(InputStream.class), anyString());
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(true);
RequestResult rr = new RequestResult().setSuccess(true).setMessage("function registered");
CompletableFuture<RequestResult> requestResult = CompletableFuture.completedFuture(rr);
when(mockedManager.updateFunction(any(FunctionMetaData.class))).thenReturn(requestResult);
Response response = updateDefaultFunction();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
Aggregations