Search in sources :

Example 6 with RequestResult

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);
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Response(javax.ws.rs.core.Response) RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) InputStream(java.io.InputStream) IOException(java.io.IOException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with RequestResult

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;
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceRequestInfo(org.apache.pulsar.functions.worker.request.ServiceRequestInfo) MessageId(org.apache.pulsar.client.api.MessageId) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with RequestResult

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();
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData)

Example 9 with RequestResult

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);
    }
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) ServiceRequestInfo(org.apache.pulsar.functions.worker.request.ServiceRequestInfo)

Example 10 with 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());
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Response(javax.ws.rs.core.Response) RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) InputStream(java.io.InputStream) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RequestResult (org.apache.pulsar.functions.worker.request.RequestResult)13 Response (javax.ws.rs.core.Response)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Test (org.testng.annotations.Test)9 ErrorData (org.apache.pulsar.common.policies.data.ErrorData)8 InputStream (java.io.InputStream)6 Namespace (org.apache.distributedlog.api.namespace.Namespace)6 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)6 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)2 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)2 ServiceRequestInfo (org.apache.pulsar.functions.worker.request.ServiceRequestInfo)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 MessageId (org.apache.pulsar.client.api.MessageId)1