Search in sources :

Example 11 with RequestResult

use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.

the class FunctionApiV2ResourceTest method testUpdateFunctionInterrupted.

@Test
public void testUpdateFunctionInterrupted() 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);
    CompletableFuture<RequestResult> requestResult = FutureUtil.failedFuture(new IOException("Function registeration interrupted"));
    when(mockedManager.updateFunction(any(FunctionMetaData.class))).thenReturn(requestResult);
    Response response = updateDefaultFunction();
    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 12 with RequestResult

use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.

the class FunctionApiV2ResourceTest method testDeregisterFunctionInterrupted.

@Test
public void testDeregisterFunctionInterrupted() throws Exception {
    when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(true);
    CompletableFuture<RequestResult> requestResult = FutureUtil.failedFuture(new IOException("Function deregisteration interrupted"));
    when(mockedManager.deregisterFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(requestResult);
    Response response = deregisterDefaultFunction();
    assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
    assertEquals(new ErrorData("Function deregisteration interrupted").reason, ((ErrorData) response.getEntity()).reason);
}
Also used : Response(javax.ws.rs.core.Response) RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) IOException(java.io.IOException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with RequestResult

use of org.apache.pulsar.functions.worker.request.RequestResult in project incubator-pulsar by apache.

the class FunctionsImpl method deregisterFunction.

@DELETE
@Path("/{tenant}/{namespace}/{functionName}")
public Response deregisterFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName) {
    // validate parameters
    try {
        validateDeregisterRequestParams(tenant, namespace, functionName);
    } catch (IllegalArgumentException e) {
        log.error("Invalid deregister function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function to deregister does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    CompletableFuture<RequestResult> completableFuture = functionMetaDataManager.deregisterFunction(tenant, namespace, functionName);
    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) {
        log.error("Execution Exception while deregistering function @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
    } catch (InterruptedException e) {
        log.error("Interrupted Exception while deregistering function @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.REQUEST_TIMEOUT).type(MediaType.APPLICATION_JSON).build();
    }
    return Response.status(Status.OK).entity(requestResult.toJson()).build();
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) ExecutionException(java.util.concurrent.ExecutionException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

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