Search in sources :

Example 21 with PartitionedTopicMetadata

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

the class NonPersistentTopics method createPartitionedTopic.

@PUT
@Path("/{property}/{cluster}/{namespace}/{topic}/partitions")
@ApiOperation(hidden = true, 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("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic, int numPartitions, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateTopicName(property, cluster, 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) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 22 with PartitionedTopicMetadata

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

the class PersistentTopicsImpl method getPartitionedTopicMetadataAsync.

@Override
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadataAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "partitions");
    final CompletableFuture<PartitionedTopicMetadata> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<PartitionedTopicMetadata>() {

        @Override
        public void completed(PartitionedTopicMetadata response) {
            future.complete(response);
        }

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

Example 23 with PartitionedTopicMetadata

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

the class BinaryProtoLookupService method getPartitionedTopicMetadata.

private CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(InetSocketAddress socketAddress, TopicName topicName) {
    CompletableFuture<PartitionedTopicMetadata> partitionFuture = new CompletableFuture<PartitionedTopicMetadata>();
    client.getCnxPool().getConnection(socketAddress).thenAccept(clientCnx -> {
        long requestId = client.newRequestId();
        ByteBuf request = Commands.newPartitionMetadataRequest(topicName.toString(), requestId);
        clientCnx.newLookup(request, requestId).thenAccept(lookupDataResult -> {
            try {
                partitionFuture.complete(new PartitionedTopicMetadata(lookupDataResult.partitions));
            } catch (Exception e) {
                partitionFuture.completeExceptionally(new PulsarClientException.LookupException(format("Failed to parse partition-response redirect=%s , partitions with %s", lookupDataResult.redirect, lookupDataResult.partitions, e.getMessage())));
            }
        }).exceptionally((e) -> {
            log.warn("[{}] failed to get Partitioned metadata : {}", topicName.toString(), e.getCause().getMessage(), e);
            partitionFuture.completeExceptionally(e);
            return null;
        });
    }).exceptionally(connectionException -> {
        partitionFuture.completeExceptionally(connectionException);
        return null;
    });
    return partitionFuture;
}
Also used : Commands(org.apache.pulsar.common.api.Commands) TopicName(org.apache.pulsar.common.naming.TopicName) Logger(org.slf4j.Logger) ClosedChannelException(java.nio.channels.ClosedChannelException) LoggerFactory(org.slf4j.LoggerFactory) LookupType(org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse.LookupType) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) InetSocketAddress(java.net.InetSocketAddress) String.format(java.lang.String.format) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) CommandLookupTopicResponse(org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse) ByteBuf(io.netty.buffer.ByteBuf) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuf(io.netty.buffer.ByteBuf) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) ClosedChannelException(java.nio.channels.ClosedChannelException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 24 with PartitionedTopicMetadata

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

the class PulsarClientImpl method getPartitionedTopicMetadata.

public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) {
    CompletableFuture<PartitionedTopicMetadata> metadataFuture;
    try {
        TopicName topicName = TopicName.get(topic);
        metadataFuture = lookup.getPartitionedTopicMetadata(topicName);
    } catch (IllegalArgumentException e) {
        return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage()));
    }
    return metadataFuture;
}
Also used : PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 25 with PartitionedTopicMetadata

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

the class BrokerServiceLookupTest method testPartitionedMetadataWithDeprecatedVersion.

@Test
public void testPartitionedMetadataWithDeprecatedVersion() throws Exception {
    final String cluster = "use2";
    final String property = "my-property2";
    final String namespace = "my-ns";
    final String topicName = "my-partitioned";
    final int totalPartitions = 10;
    final TopicName dest = TopicName.get("persistent", property, cluster, namespace, topicName);
    admin.clusters().createCluster(cluster, new ClusterData("http://127.0.0.1:" + BROKER_WEBSERVICE_PORT, null, null, null));
    admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet(cluster)));
    admin.namespaces().createNamespace(property + "/" + cluster + "/" + namespace);
    admin.persistentTopics().createPartitionedTopic(dest.toString(), totalPartitions);
    stopBroker();
    conf.setClientLibraryVersionCheckEnabled(true);
    startBroker();
    URI brokerServiceUrl = new URI(pulsar.getWebServiceAddress());
    URL url = brokerServiceUrl.toURL();
    String path = String.format("admin/%s/partitions", dest.getLookupName());
    AsyncHttpClient httpClient = getHttpClient("Pulsar-Java-1.20");
    PartitionedTopicMetadata metadata = getPartitionedMetadata(httpClient, url, path);
    assertEquals(metadata.partitions, totalPartitions);
    httpClient.close();
    httpClient = getHttpClient("Pulsar-CPP-v1.21");
    metadata = getPartitionedMetadata(httpClient, url, path);
    assertEquals(metadata.partitions, totalPartitions);
    httpClient.close();
    httpClient = getHttpClient("Pulsar-CPP-v1.21-SNAPSHOT");
    metadata = getPartitionedMetadata(httpClient, url, path);
    assertEquals(metadata.partitions, totalPartitions);
    httpClient.close();
    httpClient = getHttpClient("");
    try {
        metadata = getPartitionedMetadata(httpClient, url, path);
        fail("should have failed due to invalid version");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof PulsarClientException);
    }
    httpClient.close();
    httpClient = getHttpClient("Pulsar-CPP-v1.20-SNAPSHOT");
    try {
        metadata = getPartitionedMetadata(httpClient, url, path);
        fail("should have failed due to invalid version");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof PulsarClientException);
    }
    httpClient.close();
    httpClient = getHttpClient("Pulsar-CPP-v1.20");
    try {
        metadata = getPartitionedMetadata(httpClient, url, path);
        fail("should have failed due to invalid version");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof PulsarClientException);
    }
    httpClient.close();
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) URL(java.net.URL) TopicName(org.apache.pulsar.common.naming.TopicName) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) Test(org.testng.annotations.Test)

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