Search in sources :

Example 11 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class NonPersistentTopics method createPartitionedTopic.

@PUT
@Path("/{property}/{namespace}/{topic}/partitions")
@ApiOperation(value = "Create a partitioned topic.", notes = "It needs to be called before creating a producer on a partitioned topic.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 409, message = "Partitioned topic already exist") })
public void createPartitionedTopic(@PathParam("property") String property, @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic, int numPartitions, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateTopicName(property, namespace, encodedTopic);
    validateAdminAccessOnProperty(topicName.getProperty());
    if (numPartitions <= 1) {
        throw new RestException(Status.NOT_ACCEPTABLE, "Number of partitions should be more than 1");
    }
    try {
        String path = path(PARTITIONED_TOPIC_PATH_ZNODE, namespaceName.toString(), domain(), topicName.getEncodedLocalName());
        byte[] data = jsonMapper().writeValueAsBytes(new PartitionedTopicMetadata(numPartitions));
        zkCreateOptimistic(path, data);
        // we wait for the data to be synced in all quorums and the observers
        Thread.sleep(PARTITIONED_TOPIC_WAIT_SYNC_TIME_MS);
        log.info("[{}] Successfully created partitioned topic {}", clientAppId(), topicName);
    } catch (KeeperException.NodeExistsException e) {
        log.warn("[{}] Failed to create already existing partitioned topic {}", clientAppId(), topicName);
        throw new RestException(Status.CONFLICT, "Partitioned topic already exist");
    } catch (Exception e) {
        log.error("[{}] Failed to create partitioned topic {}", clientAppId(), topicName, e);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 12 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method getPartitionedMetadata.

private PartitionedTopicMetadata getPartitionedMetadata(AsyncHttpClient httpClient, URL url, String path) throws Exception {
    final CompletableFuture<PartitionedTopicMetadata> future = new CompletableFuture<>();
    try {
        String requestUrl = new URL(url, path).toString();
        BoundRequestBuilder builder = httpClient.prepareGet(requestUrl);
        final ListenableFuture<Response> responseFuture = builder.setHeader("Accept", "application/json").execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
                future.completeExceptionally(new PulsarClientException(t));
            }
        });
        responseFuture.addListener(() -> {
            try {
                Response response = responseFuture.get();
                if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
                    log.warn("[{}] HTTP get request failed: {}", requestUrl, response.getStatusText());
                    future.completeExceptionally(new PulsarClientException("HTTP get request failed: " + response.getStatusText()));
                    return;
                }
                PartitionedTopicMetadata data = ObjectMapperFactory.getThreadLocal().readValue(response.getResponseBodyAsBytes(), PartitionedTopicMetadata.class);
                future.complete(data);
            } catch (Exception e) {
                log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
                future.completeExceptionally(new PulsarClientException(e));
            }
        }, MoreExecutors.directExecutor());
    } catch (Exception e) {
        log.warn("[{}] Failed to get authentication data for lookup: {}", path, e.getMessage());
        if (e instanceof PulsarClientException) {
            future.completeExceptionally(e);
        } else {
            future.completeExceptionally(new PulsarClientException(e));
        }
    }
    return future.get();
}
Also used : URL(java.net.URL) AuthenticationException(javax.naming.AuthenticationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpResponse(io.netty.handler.codec.http.HttpResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata)

Example 13 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project flink by apache.

the class PulsarRuntimeOperator method deleteTopic.

/**
 * Delete a Pulsar topic.
 *
 * @param topic The topic name.
 */
public void deleteTopic(String topic) {
    String topicName = topicName(topic);
    PartitionedTopicMetadata metadata;
    try {
        metadata = admin().topics().getPartitionedTopicMetadata(topicName);
    } catch (NotFoundException e) {
        // This topic doesn't exist. Just skip deletion.
        return;
    } catch (PulsarAdminException e) {
        sneakyThrow(e);
        return;
    }
    removeConsumers(topic);
    removeProducers(topic);
    if (metadata.partitions <= 0) {
        sneakyAdmin(() -> admin().topics().delete(topicName));
    } else {
        sneakyAdmin(() -> admin().topics().deletePartitionedTopic(topicName));
    }
}
Also used : NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata)

Example 14 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class DiscoveryServiceTest method testGetPartitionsMetadata.

@Test
public void testGetPartitionsMetadata() throws Exception {
    TopicName topic1 = TopicName.get("persistent://test/local/ns/my-topic-1");
    PartitionedTopicMetadata m = service.getDiscoveryProvider().getPartitionedTopicMetadata(service, topic1, "role", null).get();
    assertEquals(m.partitions, 0);
    // Simulate ZK error
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    TopicName topic2 = TopicName.get("persistent://test/local/ns/my-topic-2");
    CompletableFuture<PartitionedTopicMetadata> future = service.getDiscoveryProvider().getPartitionedTopicMetadata(service, topic2, "role", null);
    try {
        future.get();
        fail("Partition metadata lookup should have failed");
    } catch (ExecutionException e) {
        assertEquals(e.getCause().getClass(), SessionExpiredException.class);
    }
}
Also used : SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) ExecutionException(java.util.concurrent.ExecutionException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 15 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalCreatePartitionedTopic.

protected void internalCreatePartitionedTopic(int numPartitions, boolean authoritative) {
    validateAdminAccessOnProperty(topicName.getProperty());
    if (numPartitions <= 1) {
        throw new RestException(Status.NOT_ACCEPTABLE, "Number of partitions should be more than 1");
    }
    try {
        String path = ZkAdminPaths.partitionedTopicPath(topicName);
        byte[] data = jsonMapper().writeValueAsBytes(new PartitionedTopicMetadata(numPartitions));
        zkCreateOptimistic(path, data);
        // we wait for the data to be synced in all quorums and the observers
        Thread.sleep(PARTITIONED_TOPIC_WAIT_SYNC_TIME_MS);
        log.info("[{}] Successfully created partitioned topic {}", clientAppId(), topicName);
    } catch (KeeperException.NodeExistsException e) {
        log.warn("[{}] Failed to create already existing partitioned topic {}", clientAppId(), topicName);
        throw new RestException(Status.CONFLICT, "Partitioned topic already exist");
    } catch (Exception e) {
        log.error("[{}] Failed to create partitioned topic {}", clientAppId(), topicName, e);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) KeeperException(org.apache.zookeeper.KeeperException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)28 ExecutionException (java.util.concurrent.ExecutionException)18 RestException (org.apache.pulsar.broker.web.RestException)17 KeeperException (org.apache.zookeeper.KeeperException)17 IOException (java.io.IOException)15 WebApplicationException (javax.ws.rs.WebApplicationException)15 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)15 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)15 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)15 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)15 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)14 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)14 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)14 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)14 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)14 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)11 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 TopicName (org.apache.pulsar.common.naming.TopicName)6 WebTarget (javax.ws.rs.client.WebTarget)3