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