Search in sources :

Example 1 with SourceStatus

use of org.apache.pulsar.common.policies.data.SourceStatus in project pulsar by apache.

the class SourcesImpl method getSourceStatusAsync.

@Override
public CompletableFuture<SourceStatus> getSourceStatusAsync(String tenant, String namespace, String sourceName) {
    WebTarget path = source.path(tenant).path(namespace).path(sourceName).path("status");
    final CompletableFuture<SourceStatus> 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(SourceStatus.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) SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with SourceStatus

use of org.apache.pulsar.common.policies.data.SourceStatus 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 SourceStatus

use of org.apache.pulsar.common.policies.data.SourceStatus in project pulsar by apache.

the class BatchSourceTest method testGenericRecordSource.

@Test(groups = { "source" })
public void testGenericRecordSource() throws Exception {
    String outputTopicName = "test-state-source-output-" + randomName(8);
    String sourceName = "test-state-source-" + randomName(8);
    int numMessages = 10;
    try {
        submitSourceConnector(sourceName, outputTopicName, "builtin://batch-data-generator");
        // get source info
        getSourceInfoSuccess(container, sourceName);
        // get source status
        getSourceStatus(container, sourceName);
        try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(container.getHttpServiceUrl()).build()) {
            retryStrategically((test) -> {
                try {
                    SourceStatus status = admin.sources().getSourceStatus("public", "default", sourceName);
                    return status.getInstances().size() > 0 && status.getInstances().get(0).getStatus().numWritten >= 10;
                } catch (PulsarAdminException e) {
                    return false;
                }
            }, 10, 200);
            SourceStatus status = admin.sources().getSourceStatus("public", "default", sourceName);
            assertEquals(status.getInstances().size(), 1);
            assertTrue(status.getInstances().get(0).getStatus().numWritten >= 10);
        }
        consumeMessages(container, outputTopicName, numMessages);
        // delete source
        deleteSource(container, sourceName);
        getSourceInfoNotFound(container, sourceName);
    } finally {
        dumpFunctionLogs(sourceName);
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test)

Example 4 with SourceStatus

use of org.apache.pulsar.common.policies.data.SourceStatus in project pulsar by apache.

the class GenericRecordSourceTest method testGenericRecordSource.

@Test(groups = { "source" })
public void testGenericRecordSource() throws Exception {
    String outputTopicName = "test-state-source-output-" + randomName(8);
    String sourceName = "test-state-source-" + randomName(8);
    int numMessages = 10;
    try {
        submitSourceConnector(sourceName, outputTopicName, "org.apache.pulsar.tests.integration.io.GenericRecordSource", JAVAJAR);
        // get source info
        getSourceInfoSuccess(container, sourceName);
        // get source status
        getSourceStatus(container, sourceName);
        try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(container.getHttpServiceUrl()).build()) {
            retryStrategically((test) -> {
                try {
                    SourceStatus status = admin.sources().getSourceStatus("public", "default", sourceName);
                    return status.getInstances().size() > 0 && status.getInstances().get(0).getStatus().numWritten >= 10;
                } catch (PulsarAdminException e) {
                    return false;
                }
            }, 10, 200);
            SourceStatus status = admin.sources().getSourceStatus("public", "default", sourceName);
            assertEquals(status.getInstances().size(), 1);
            assertTrue(status.getInstances().get(0).getStatus().numWritten >= 10);
        }
        consumeMessages(container, outputTopicName, numMessages);
        // delete source
        deleteSource(container, sourceName);
        getSourceInfoNotFound(container, sourceName);
    } finally {
        dumpFunctionLogs(sourceName);
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) ToString(lombok.ToString) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test)

Example 5 with SourceStatus

use of org.apache.pulsar.common.policies.data.SourceStatus in project pulsar by apache.

the class PulsarIOSourceRunner method waitForProcessingSourceMessages.

protected void waitForProcessingSourceMessages(String tenant, String namespace, String sourceName, int numMessages) throws Exception {
    final String[] commands = { PulsarCluster.ADMIN_SCRIPT, "source", "status", "--tenant", tenant, "--namespace", namespace, "--name", sourceName };
    final ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
    log.info("Get source status : {}", result.getStdout());
    assertEquals(result.getExitCode(), 0);
    SourceStatus sourceStatus = SourceStatusUtil.decode(result.getStdout());
    assertEquals(sourceStatus.getNumInstances(), 1);
    assertEquals(sourceStatus.getNumRunning(), 1);
    assertEquals(sourceStatus.getInstances().size(), 1);
    assertEquals(sourceStatus.getInstances().get(0).getInstanceId(), 0);
    assertEquals(sourceStatus.getInstances().get(0).getStatus().isRunning(), true);
    assertTrue(sourceStatus.getInstances().get(0).getStatus().getLastReceivedTime() > 0);
    assertEquals(sourceStatus.getInstances().get(0).getStatus().getNumReceivedFromSource(), numMessages);
    assertEquals(sourceStatus.getInstances().get(0).getStatus().getNumWritten(), numMessages);
    assertEquals(sourceStatus.getInstances().get(0).getStatus().getNumRestarts(), 0);
    assertEquals(sourceStatus.getInstances().get(0).getStatus().getLatestSystemExceptions().size(), 0);
}
Also used : SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) ContainerExecResult(org.apache.pulsar.tests.integration.docker.ContainerExecResult)

Aggregations

SourceStatus (org.apache.pulsar.common.policies.data.SourceStatus)30 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)12 ContainerExecResult (org.apache.pulsar.tests.integration.docker.ContainerExecResult)12 Test (org.testng.annotations.Test)12 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)9 IOException (java.io.IOException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 WebTarget (javax.ws.rs.client.WebTarget)3 Response (javax.ws.rs.core.Response)3 Cleanup (lombok.Cleanup)3 ToString (lombok.ToString)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 FunctionState (org.apache.pulsar.common.functions.FunctionState)3 RestException (org.apache.pulsar.common.util.RestException)3 RestUtils.throwUnavailableException (org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException)3