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