Search in sources :

Example 6 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class Utils method getPulsarAdminClient.

public static PulsarAdmin getPulsarAdminClient(String pulsarWebServiceUrl) {
    URL url = null;
    try {
        url = new URL(pulsarWebServiceUrl);
    } catch (MalformedURLException e) {
        log.error("Error when parsing pulsar web service url", e);
        throw new RuntimeException(e);
    }
    PulsarAdmin admin = null;
    try {
        admin = new PulsarAdmin(url, new org.apache.pulsar.client.api.ClientConfiguration());
    } catch (PulsarClientException e) {
        log.error("Error creating pulsar admin client", e);
        throw new RuntimeException(e);
    }
    return admin;
}
Also used : MalformedURLException(java.net.MalformedURLException) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) URL(java.net.URL)

Example 7 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ServerCnx method handlePartitionMetadataRequest.

@Override
protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata partitionMetadata) {
    final long requestId = partitionMetadata.getRequestId();
    if (log.isDebugEnabled()) {
        log.debug("[{}] Received PartitionMetadataLookup from {} for {}", partitionMetadata.getTopic(), remoteAddress, requestId);
    }
    TopicName topicName = validateTopicName(partitionMetadata.getTopic(), requestId, partitionMetadata);
    if (topicName == null) {
        return;
    }
    String originalPrincipal = null;
    if (authenticateOriginalAuthData && partitionMetadata.hasOriginalAuthData()) {
        originalPrincipal = validateOriginalPrincipal(partitionMetadata.hasOriginalAuthData() ? partitionMetadata.getOriginalAuthData() : null, partitionMetadata.hasOriginalAuthMethod() ? partitionMetadata.getOriginalAuthMethod() : null, partitionMetadata.hasOriginalPrincipal() ? partitionMetadata.getOriginalPrincipal() : this.originalPrincipal, requestId, partitionMetadata);
        if (originalPrincipal == null) {
            return;
        }
    } else {
        originalPrincipal = partitionMetadata.hasOriginalPrincipal() ? partitionMetadata.getOriginalPrincipal() : this.originalPrincipal;
    }
    final Semaphore lookupSemaphore = service.getLookupRequestSemaphore();
    if (lookupSemaphore.tryAcquire()) {
        if (invalidOriginalPrincipal(originalPrincipal)) {
            final String msg = "Valid Proxy Client role should be provided for getPartitionMetadataRequest ";
            log.warn("[{}] {} with role {} and proxyClientAuthRole {} on topic {}", remoteAddress, msg, authRole, originalPrincipal, topicName);
            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.AuthorizationError, msg, requestId));
            lookupSemaphore.release();
            return;
        }
        CompletableFuture<Boolean> isProxyAuthorizedFuture;
        if (service.isAuthorizationEnabled() && originalPrincipal != null) {
            isProxyAuthorizedFuture = service.getAuthorizationService().canLookupAsync(topicName, authRole, authenticationData);
        } else {
            isProxyAuthorizedFuture = CompletableFuture.completedFuture(true);
        }
        String finalOriginalPrincipal = originalPrincipal;
        isProxyAuthorizedFuture.thenApply(isProxyAuthorized -> {
            if (isProxyAuthorized) {
                getPartitionedTopicMetadata(getBrokerService().pulsar(), finalOriginalPrincipal != null ? finalOriginalPrincipal : authRole, authenticationData, topicName).handle((metadata, ex) -> {
                    if (ex == null) {
                        int partitions = metadata.partitions;
                        ctx.writeAndFlush(Commands.newPartitionMetadataResponse(partitions, requestId));
                    } else {
                        if (ex instanceof PulsarClientException) {
                            log.warn("Failed to authorize {} at [{}] on topic {} : {}", getRole(), remoteAddress, topicName, ex.getMessage());
                            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.AuthorizationError, ex.getMessage(), requestId));
                        } else {
                            log.warn("Failed to get Partitioned Metadata [{}] {}: {}", remoteAddress, topicName, ex.getMessage(), ex);
                            ServerError error = (ex instanceof RestException) && ((RestException) ex).getResponse().getStatus() < 500 ? ServerError.MetadataError : ServerError.ServiceNotReady;
                            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(error, ex.getMessage(), requestId));
                        }
                    }
                    lookupSemaphore.release();
                    return null;
                });
            } else {
                final String msg = "Proxy Client is not authorized to Get Partition Metadata";
                log.warn("[{}] {} with role {} on topic {}", remoteAddress, msg, authRole, topicName);
                ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.AuthorizationError, msg, requestId));
                lookupSemaphore.release();
            }
            return null;
        }).exceptionally(ex -> {
            final String msg = "Exception occured while trying to authorize get Partition Metadata";
            log.warn("[{}] {} with role {} on topic {}", remoteAddress, msg, authRole, topicName);
            ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.AuthorizationError, msg, requestId));
            lookupSemaphore.release();
            return null;
        });
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Failed Partition-Metadata lookup due to too many lookup-requests {}", remoteAddress, topicName);
        }
        ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.TooManyRequests, "Failed due to too many pending lookup requests", requestId));
    }
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) CommandUtils(org.apache.pulsar.common.api.CommandUtils) SocketAddress(java.net.SocketAddress) PersistentTopicsBase.getPartitionedTopicMetadata(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase.getPartitionedTopicMetadata) SchemaVersion(org.apache.pulsar.common.schema.SchemaVersion) CommandAck(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck) LoggerFactory(org.slf4j.LoggerFactory) AuthenticationException(javax.naming.AuthenticationException) StringUtils(org.apache.commons.lang3.StringUtils) CommandCloseConsumer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer) CommandGetLastMessageId(org.apache.pulsar.common.api.proto.PulsarApi.CommandGetLastMessageId) CommandSend(org.apache.pulsar.common.api.proto.PulsarApi.CommandSend) ServerError(org.apache.pulsar.common.api.proto.PulsarApi.ServerError) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PulsarHandler(org.apache.pulsar.common.api.PulsarHandler) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) CommandGetTopicsOfNamespace(org.apache.pulsar.common.api.proto.PulsarApi.CommandGetTopicsOfNamespace) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ProtocolVersion.v5(org.apache.pulsar.common.api.proto.PulsarApi.ProtocolVersion.v5) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CommandConnect(org.apache.pulsar.common.api.proto.PulsarApi.CommandConnect) Commands(org.apache.pulsar.common.api.Commands) Commands.newLookupErrorResponse(org.apache.pulsar.common.api.Commands.newLookupErrorResponse) CommandSubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) Set(java.util.Set) CommandRedeliverUnacknowledgedMessages(org.apache.pulsar.common.api.proto.PulsarApi.CommandRedeliverUnacknowledgedMessages) Position(org.apache.bookkeeper.mledger.Position) CommandCloseProducer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer) CommandFlow(org.apache.pulsar.common.api.proto.PulsarApi.CommandFlow) Collectors(java.util.stream.Collectors) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) SafeRun(org.apache.bookkeeper.mledger.util.SafeRun) SslHandler(io.netty.handler.ssl.SslHandler) SchemaData(org.apache.pulsar.common.schema.SchemaData) CommandConsumerStatsResponse(org.apache.pulsar.common.api.proto.PulsarApi.CommandConsumerStatsResponse) CommandPartitionedTopicMetadata(org.apache.pulsar.common.api.proto.PulsarApi.CommandPartitionedTopicMetadata) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) TopicName(org.apache.pulsar.common.naming.TopicName) ChannelOption(io.netty.channel.ChannelOption) CommandConsumerStats(org.apache.pulsar.common.api.proto.PulsarApi.CommandConsumerStats) GeneratedMessageLite(com.google.protobuf.GeneratedMessageLite) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) CompletableFuture(java.util.concurrent.CompletableFuture) ProtocolVersion(org.apache.pulsar.common.api.proto.PulsarApi.ProtocolVersion) SchemaType(org.apache.pulsar.common.schema.SchemaType) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) CommandProducer(org.apache.pulsar.common.api.proto.PulsarApi.CommandProducer) CommandSeek(org.apache.pulsar.common.api.proto.PulsarApi.CommandSeek) AuthenticationDataCommand(org.apache.pulsar.broker.authentication.AuthenticationDataCommand) Metadata(org.apache.pulsar.common.naming.Metadata) Logger(org.slf4j.Logger) SubType(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType) Semaphore(java.util.concurrent.Semaphore) CommandLookupTopic(org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopic) TimeUnit(java.util.concurrent.TimeUnit) CommandUnsubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandUnsubscribe) ConcurrentLongHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongHashMap) TopicLookup.lookupTopicAsync(org.apache.pulsar.broker.lookup.TopicLookup.lookupTopicAsync) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) ChannelHandler(io.netty.channel.ChannelHandler) ConsumerStats(org.apache.pulsar.common.policies.data.ConsumerStats) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ServerError(org.apache.pulsar.common.api.proto.PulsarApi.ServerError) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) RestException(org.apache.pulsar.broker.web.RestException) Semaphore(java.util.concurrent.Semaphore) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 8 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConnectionHandler method handleConnectionError.

private Void handleConnectionError(Throwable exception) {
    log.warn("[{}] [{}] Error connecting to broker: {}", state.topic, state.getHandlerName(), exception.getMessage());
    connection.connectionFailed(new PulsarClientException(exception));
    State state = this.state.getState();
    if (state == State.Uninitialized || state == State.Connecting || state == State.Ready) {
        reconnectLater(exception);
    }
    return null;
}
Also used : State(org.apache.pulsar.client.impl.HandlerState.State) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 9 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConnectionPool method createConnection.

private CompletableFuture<ClientCnx> createConnection(InetSocketAddress logicalAddress, InetSocketAddress physicalAddress, int connectionKey) {
    if (log.isDebugEnabled()) {
        log.debug("Connection for {} not found in cache", logicalAddress);
    }
    final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();
    // Trigger async connect to broker
    createConnection(physicalAddress).thenAccept(channel -> {
        log.info("[{}] Connected to server", channel);
        channel.closeFuture().addListener(v -> {
            // Remove connection from pool when it gets closed
            if (log.isDebugEnabled()) {
                log.debug("Removing closed connection from pool: {}", v);
            }
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
        });
        // We are connected to broker, but need to wait until the connect/connected handshake is
        // complete
        final ClientCnx cnx = (ClientCnx) channel.pipeline().get("handler");
        if (!channel.isActive() || cnx == null) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection was already closed by the time we got notified", channel);
            }
            cnxFuture.completeExceptionally(new ChannelException("Connection already closed"));
            return;
        }
        if (!logicalAddress.equals(physicalAddress)) {
            // We are connecting through a proxy. We need to set the target broker in the ClientCnx object so that
            // it can be specified when sending the CommandConnect.
            // That phase will happen in the ClientCnx.connectionActive() which will be invoked immediately after
            // this method.
            cnx.setTargetBroker(logicalAddress);
        }
        cnx.setRemoteHostName(physicalAddress.getHostName());
        cnx.connectionFuture().thenRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection handshake completed", cnx.channel());
            }
            cnxFuture.complete(cnx);
        }).exceptionally(exception -> {
            log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
            cnxFuture.completeExceptionally(exception);
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
            cnx.ctx().close();
            return null;
        });
    }).exceptionally(exception -> {
        eventLoopGroup.execute(() -> {
            log.warn("Failed to open connection to {} : {}", physicalAddress, exception.getMessage());
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
            cnxFuture.completeExceptionally(new PulsarClientException(exception));
        });
        return null;
    });
    return cnxFuture;
}
Also used : X509Certificate(java.security.cert.X509Certificate) DnsNameResolverBuilder(io.netty.resolver.dns.DnsNameResolverBuilder) ChannelOption(io.netty.channel.ChannelOption) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair) SecurityUtility(org.apache.pulsar.common.util.SecurityUtility) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ConcurrentMap(java.util.concurrent.ConcurrentMap) InetAddress(java.net.InetAddress) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) SocketChannel(io.netty.channel.socket.SocketChannel) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) EventLoopUtil(org.apache.pulsar.common.util.netty.EventLoopUtil) Logger(org.slf4j.Logger) EventLoopGroup(io.netty.channel.EventLoopGroup) DnsNameResolver(io.netty.resolver.dns.DnsNameResolver) Iterator(java.util.Iterator) ChannelInitializer(io.netty.channel.ChannelInitializer) SslContext(io.netty.handler.ssl.SslContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) ChannelException(io.netty.channel.ChannelException) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) Closeable(java.io.Closeable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AuthenticationDataProvider(org.apache.pulsar.client.api.AuthenticationDataProvider) Future(io.netty.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ChannelException(io.netty.channel.ChannelException)

Example 10 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class ConsumerImpl method sendAcknowledge.

private CompletableFuture<Void> sendAcknowledge(MessageId messageId, AckType ackType, Map<String, Long> properties) {
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    final ByteBuf cmd = Commands.newAck(consumerId, msgId.getLedgerId(), msgId.getEntryId(), ackType, null, properties);
    // There's no actual response from ack messages
    final CompletableFuture<Void> ackFuture = new CompletableFuture<Void>();
    if (isConnected()) {
        cnx().ctx().writeAndFlush(cmd).addListener(new GenericFutureListener<Future<Void>>() {

            @Override
            public void operationComplete(Future<Void> future) throws Exception {
                if (future.isSuccess()) {
                    if (ackType == AckType.Individual) {
                        unAckedMessageTracker.remove(msgId);
                        // increment counter by 1 for non-batch msg
                        if (!(messageId instanceof BatchMessageIdImpl)) {
                            stats.incrementNumAcksSent(1);
                        }
                    } else if (ackType == AckType.Cumulative) {
                        stats.incrementNumAcksSent(unAckedMessageTracker.removeMessagesTill(msgId));
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] [{}] [{}] Successfully acknowledged message - {}, acktype {}", subscription, topic, consumerName, messageId, ackType);
                    }
                    ackFuture.complete(null);
                } else {
                    stats.incrementNumAcksFailed();
                    ackFuture.completeExceptionally(new PulsarClientException(future.cause()));
                }
            }
        });
    } else {
        stats.incrementNumAcksFailed();
        ackFuture.completeExceptionally(new PulsarClientException("Not connected to broker. State: " + getState()));
    }
    return ackFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) ByteBuf(io.netty.buffer.ByteBuf) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)65 Test (org.testng.annotations.Test)24 CompletableFuture (java.util.concurrent.CompletableFuture)17 Message (org.apache.pulsar.client.api.Message)15 IOException (java.io.IOException)14 PulsarClient (org.apache.pulsar.client.api.PulsarClient)13 ExecutionException (java.util.concurrent.ExecutionException)12 Consumer (org.apache.pulsar.client.api.Consumer)12 MessageId (org.apache.pulsar.client.api.MessageId)12 Producer (org.apache.pulsar.client.api.Producer)12 ByteBuf (io.netty.buffer.ByteBuf)11 List (java.util.List)8 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)8 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 TimeUnit (java.util.concurrent.TimeUnit)5