Search in sources :

Example 81 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class PersistentTopicsImpl method updatePartitionedTopicAsync.

@Override
public CompletableFuture<Void> updatePartitionedTopicAsync(String topic, int numPartitions) {
    checkArgument(numPartitions > 1, "Number of partitions must be more than 1");
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "partitions");
    return asyncPostRequest(path, Entity.entity(numPartitions, MediaType.APPLICATION_JSON));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 82 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class PersistentTopicsImpl method deleteAsync.

@Override
public CompletableFuture<Void> deleteAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn);
    return asyncDeleteRequest(path);
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 83 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class BinaryProtoLookupService method findBroker.

private CompletableFuture<Pair<InetSocketAddress, InetSocketAddress>> findBroker(InetSocketAddress socketAddress, boolean authoritative, TopicName topicName) {
    CompletableFuture<Pair<InetSocketAddress, InetSocketAddress>> addressFuture = new CompletableFuture<>();
    client.getCnxPool().getConnection(socketAddress).thenAccept(clientCnx -> {
        long requestId = client.newRequestId();
        ByteBuf request = Commands.newLookup(topicName.toString(), authoritative, requestId);
        clientCnx.newLookup(request, requestId).thenAccept(lookupDataResult -> {
            URI uri = null;
            try {
                // (1) build response broker-address
                if (useTls) {
                    uri = new URI(lookupDataResult.brokerUrlTls);
                } else {
                    String serviceUrl = lookupDataResult.brokerUrl;
                    uri = new URI(serviceUrl);
                }
                InetSocketAddress responseBrokerAddress = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
                // (2) redirect to given address if response is: redirect
                if (lookupDataResult.redirect) {
                    findBroker(responseBrokerAddress, lookupDataResult.authoritative, topicName).thenAccept(addressPair -> {
                        addressFuture.complete(addressPair);
                    }).exceptionally((lookupException) -> {
                        // lookup failed
                        log.warn("[{}] lookup failed : {}", topicName.toString(), lookupException.getMessage(), lookupException);
                        addressFuture.completeExceptionally(lookupException);
                        return null;
                    });
                } else {
                    // (3) received correct broker to connect
                    if (lookupDataResult.proxyThroughServiceUrl) {
                        // Connect through proxy
                        addressFuture.complete(Pair.of(responseBrokerAddress, serviceAddress));
                    } else {
                        // Normal result with direct connection to broker
                        addressFuture.complete(Pair.of(responseBrokerAddress, responseBrokerAddress));
                    }
                }
            } catch (Exception parseUrlException) {
                // Failed to parse url
                log.warn("[{}] invalid url {} : {}", topicName.toString(), uri, parseUrlException.getMessage(), parseUrlException);
                addressFuture.completeExceptionally(parseUrlException);
            }
        }).exceptionally((sendException) -> {
            // lookup failed
            log.warn("[{}] failed to send lookup request : {}", topicName.toString(), sendException.getMessage(), sendException instanceof ClosedChannelException ? null : sendException);
            addressFuture.completeExceptionally(sendException);
            return null;
        });
    }).exceptionally(connectionException -> {
        addressFuture.completeExceptionally(connectionException);
        return null;
    });
    return addressFuture;
}
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) ClosedChannelException(java.nio.channels.ClosedChannelException) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) URI(java.net.URI) ClosedChannelException(java.nio.channels.ClosedChannelException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Pair(org.apache.commons.lang3.tuple.Pair)

Example 84 with TopicName

use of org.apache.pulsar.common.naming.TopicName 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 85 with TopicName

use of org.apache.pulsar.common.naming.TopicName 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)

Aggregations

TopicName (org.apache.pulsar.common.naming.TopicName)127 Test (org.testng.annotations.Test)54 CompletableFuture (java.util.concurrent.CompletableFuture)43 WebTarget (javax.ws.rs.client.WebTarget)32 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)23 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)23 Logger (org.slf4j.Logger)23 LoggerFactory (org.slf4j.LoggerFactory)23 List (java.util.List)22 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)22 Map (java.util.Map)20 ExecutionException (java.util.concurrent.ExecutionException)20 TimeUnit (java.util.concurrent.TimeUnit)20 NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)18 Field (java.lang.reflect.Field)17 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)17 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)17 ByteBuf (io.netty.buffer.ByteBuf)15 Set (java.util.Set)15 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)14