Search in sources :

Example 1 with FunctionState

use of org.apache.pulsar.common.functions.FunctionState in project pulsar by apache.

the class FunctionsImpl method getFunctionStateAsync.

@Override
public CompletableFuture<FunctionState> getFunctionStateAsync(String tenant, String namespace, String function, String key) {
    WebTarget path = functions.path(tenant).path(namespace).path(function).path("state").path(key);
    final CompletableFuture<FunctionState> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<Response>() {

        @Override
        public void completed(Response response) {
            if (response.getStatus() != Response.Status.OK.getStatusCode()) {
                future.completeExceptionally(getApiException(response));
            } else {
                future.complete(response.readEntity(FunctionState.class));
            }
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) FunctionState(org.apache.pulsar.common.functions.FunctionState) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with FunctionState

use of org.apache.pulsar.common.functions.FunctionState in project pulsar by apache.

the class PulsarStateTest method testSourceState.

@Test(groups = { "java_state", "state", "function", "java_function" })
public void testSourceState() throws Exception {
    String outputTopicName = "test-state-source-output-" + randomName(8);
    String sourceName = "test-state-source-" + randomName(8);
    submitSourceConnector(sourceName, outputTopicName, "org.apache.pulsar.tests.integration.io.TestStateSource", JAVAJAR);
    // get source info
    getSourceInfoSuccess(sourceName);
    // get source status
    getSourceStatus(sourceName);
    try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(container.getHttpServiceUrl()).build()) {
        Awaitility.await().ignoreExceptions().untilAsserted(() -> {
            SourceStatus status = admin.sources().getSourceStatus("public", "default", sourceName);
            assertEquals(status.getInstances().size(), 1);
            assertTrue(status.getInstances().get(0).getStatus().numWritten > 0);
        });
        {
            FunctionState functionState = admin.functions().getFunctionState("public", "default", sourceName, "initial");
            assertEquals(functionState.getStringValue(), "val1");
        }
        Awaitility.await().ignoreExceptions().untilAsserted(() -> {
            FunctionState functionState = admin.functions().getFunctionState("public", "default", sourceName, "now");
            assertTrue(functionState.getStringValue().matches("val1-.*"));
        });
    }
    // delete source
    deleteSource(sourceName);
    getSourceInfoNotFound(sourceName);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) FunctionState(org.apache.pulsar.common.functions.FunctionState) Test(org.testng.annotations.Test)

Example 3 with FunctionState

use of org.apache.pulsar.common.functions.FunctionState in project pulsar by apache.

the class FunctionsImplV2 method getFunctionState.

@Override
public Response getFunctionState(String tenant, String namespace, String functionName, String key, String clientRole) {
    FunctionState functionState = delegate.getFunctionState(tenant, namespace, functionName, key, clientRole, null);
    String value;
    if (functionState.getNumberValue() != null) {
        value = "value : " + functionState.getNumberValue() + ", version : " + functionState.getVersion();
    } else {
        value = "value : " + functionState.getStringValue() + ", version : " + functionState.getVersion();
    }
    return Response.status(Response.Status.OK).entity(value).build();
}
Also used : FunctionState(org.apache.pulsar.common.functions.FunctionState)

Example 4 with FunctionState

use of org.apache.pulsar.common.functions.FunctionState in project pulsar by yahoo.

the class ComponentImpl method getFunctionState.

@Override
public FunctionState getFunctionState(final String tenant, final String namespace, final String functionName, final String key, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    try {
        if (!isAuthorizedRole(tenant, namespace, clientRole, clientAuthenticationDataHttps)) {
            log.warn("{}/{}/{} Client [{}] is not authorized to get state for {}", tenant, namespace, functionName, clientRole, ComponentTypeUtils.toString(componentType));
            throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
        }
    } catch (PulsarAdminException e) {
        log.error("{}/{}/{} Failed to authorize [{}]", tenant, namespace, functionName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    if (null == worker().getStateStoreAdminClient()) {
        throwStateStoreUnvailableResponse();
    }
    // validate parameters
    try {
        validateFunctionStateParams(tenant, namespace, functionName, key);
    } catch (IllegalArgumentException e) {
        log.error("Invalid getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }
    String tableNs = getStateNamespace(tenant, namespace);
    String tableName = functionName;
    String stateStorageServiceUrl = worker().getWorkerConfig().getStateStorageServiceUrl();
    if (storageClient.get() == null) {
        storageClient.compareAndSet(null, StorageClientBuilder.newBuilder().withSettings(StorageClientSettings.newBuilder().serviceUri(stateStorageServiceUrl).clientName("functions-admin").build()).withNamespace(tableNs).build());
    }
    FunctionState value;
    try (Table<ByteBuf, ByteBuf> table = result(storageClient.get().openTable(tableName))) {
        try (KeyValue<ByteBuf, ByteBuf> kv = result(table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))))) {
            if (null == kv) {
                throw new RestException(Status.NOT_FOUND, "key '" + key + "' doesn't exist.");
            } else {
                if (kv.isNumber()) {
                    value = new FunctionState(key, null, null, kv.numberValue(), kv.version());
                } else {
                    try {
                        value = new FunctionState(key, new String(ByteBufUtil.getBytes(kv.value(), kv.value().readerIndex(), kv.value().readableBytes()), UTF_8), null, null, kv.version());
                    } catch (Exception e) {
                        value = new FunctionState(key, null, ByteBufUtil.getBytes(kv.value()), null, kv.version());
                    }
                }
            }
        }
    } catch (RestException e) {
        throw e;
    } catch (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException | StreamNotFoundException e) {
        log.debug("State not found while processing getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.NOT_FOUND, e.getMessage());
    } catch (Exception e) {
        log.error("Error while getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return value;
}
Also used : FunctionState(org.apache.pulsar.common.functions.FunctionState) RestException(org.apache.pulsar.common.util.RestException) ByteBuf(io.netty.buffer.ByteBuf) RestUtils.throwUnavailableException(org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) WebApplicationException(javax.ws.rs.WebApplicationException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) RestException(org.apache.pulsar.common.util.RestException) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Example 5 with FunctionState

use of org.apache.pulsar.common.functions.FunctionState in project pulsar by yahoo.

the class FunctionsImplV2 method getFunctionState.

@Override
public Response getFunctionState(String tenant, String namespace, String functionName, String key, String clientRole) {
    FunctionState functionState = delegate.getFunctionState(tenant, namespace, functionName, key, clientRole, null);
    String value;
    if (functionState.getNumberValue() != null) {
        value = "value : " + functionState.getNumberValue() + ", version : " + functionState.getVersion();
    } else {
        value = "value : " + functionState.getStringValue() + ", version : " + functionState.getVersion();
    }
    return Response.status(Response.Status.OK).entity(value).build();
}
Also used : FunctionState(org.apache.pulsar.common.functions.FunctionState)

Aggregations

FunctionState (org.apache.pulsar.common.functions.FunctionState)18 IOException (java.io.IOException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 WebTarget (javax.ws.rs.client.WebTarget)6 Response (javax.ws.rs.core.Response)6 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)6 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)6 Test (org.testng.annotations.Test)6 Gson (com.google.gson.Gson)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 FileChannel (java.nio.channels.FileChannel)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Entity (javax.ws.rs.client.Entity)3 InvocationCallback (javax.ws.rs.client.InvocationCallback)3 GenericType (javax.ws.rs.core.GenericType)3 MediaType (javax.ws.rs.core.MediaType)3