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