void doUnsubscribe(final long requestId) { final ChannelHandlerContext ctx = cnx.ctx(); subscription.doUnsubscribe(this).thenAccept(v -> { log.info("Unsubscribed successfully from {}", subscription); cnx.removedConsumer(this); ctx.writeAndFlush(Commands.newSuccess(requestId)); }).exceptionally(exception -> { log.warn("Unsubscribe failed for {}", subscription, exception); ctx.writeAndFlush( Commands.newError(requestId, BrokerServiceException.getClientErrorCode(exception.getCause()), exception.getCause().getMessage())); return null; }); }
private TopicName validateTopicName(String topic, long requestId, GeneratedMessageLite requestCommand) { try { return TopicName.get(topic); } catch (Throwable t) { if (log.isDebugEnabled()) { log.debug("[{}] Failed to parse topic name '{}'", remoteAddress, topic, t); } if (requestCommand instanceof CommandLookupTopic) { ctx.writeAndFlush(Commands.newLookupErrorResponse(ServerError.InvalidTopicName, "Invalid topic name: " + t.getMessage(), requestId)); } else if (requestCommand instanceof CommandPartitionedTopicMetadata) { ctx.writeAndFlush(Commands.newPartitionMetadataResponse(ServerError.InvalidTopicName, "Invalid topic name: " + t.getMessage(), requestId)); } else { ctx.writeAndFlush(Commands.newError(requestId, ServerError.InvalidTopicName, "Invalid topic name: " + t.getMessage())); } return null; } }
CommandGetTopicsOfNamespace.Mode mode) { if (numberOfRetries == 0) { proxyConnection.ctx().writeAndFlush(Commands.newError(clientRequestId, ServerError.ServiceNotReady, "Reached max number of redirections")); return; } catch (URISyntaxException e) { proxyConnection.ctx().writeAndFlush( Commands.newError(clientRequestId, ServerError.MetadataError, e.getMessage())); return; log.warn("[{}] Failed to get TopicsOfNamespace {}: {}", clientAddress, namespaceName, ex.getMessage()); proxyConnection.ctx().writeAndFlush( Commands.newError(clientRequestId, ServerError.ServiceNotReady, ex.getMessage())); return null; }); Commands.newError(clientRequestId, ServerError.ServiceNotReady, ex.getMessage())); return null; });
public void handleGetTopicsOfNamespace(CommandGetTopicsOfNamespace commandGetTopicsOfNamespace) { getTopicsOfNamespaceRequestss.inc(); if (log.isDebugEnabled()) { log.debug("[{}] Received GetTopicsOfNamespace", clientAddress); } final long requestId = commandGetTopicsOfNamespace.getRequestId(); if (this.service.getLookupRequestSemaphore().tryAcquire()) { handleGetTopicsOfNamespace(commandGetTopicsOfNamespace, requestId); this.service.getLookupRequestSemaphore().release(); } else { rejectedGetTopicsOfNamespaceRequests.inc(); if (log.isDebugEnabled()) { log.debug("GetTopicsOfNamespace Request ID {} from {} rejected - {}.", requestId, clientAddress, throttlingErrorMessage); } proxyConnection.ctx().writeAndFlush(Commands.newError( requestId, ServerError.ServiceNotReady, throttlingErrorMessage )); } }
@Override protected void handleUnsubscribe(CommandUnsubscribe unsubscribe) { checkArgument(state == State.Connected); CompletableFuture<Consumer> consumerFuture = consumers.get(unsubscribe.getConsumerId()); if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) { consumerFuture.getNow(null).doUnsubscribe(unsubscribe.getRequestId()); } else { ctx.writeAndFlush( Commands.newError(unsubscribe.getRequestId(), ServerError.MetadataError, "Consumer not found")); } }
if (consumerFuture == null) { log.warn("[{}] Consumer was not registered on the connection: {}", consumerId, remoteAddress); ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, "Consumer not found")); return; log.warn("[{]] Error closing consumer: ", remoteAddress, consumer, e); ctx.writeAndFlush( Commands.newError(requestId, BrokerServiceException.getClientErrorCode(e), e.getMessage()));
@Override protected void handleSeek(CommandSeek seek) { checkArgument(state == State.Connected); final long requestId = seek.getRequestId(); CompletableFuture<Consumer> consumerFuture = consumers.get(seek.getConsumerId()); // Currently only seeking on a message id is supported if (!seek.hasMessageId()) { ctx.writeAndFlush( Commands.newError(requestId, ServerError.MetadataError, "Message id was not present")); return; } if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) { Consumer consumer = consumerFuture.getNow(null); Subscription subscription = consumer.getSubscription(); MessageIdData msgIdData = seek.getMessageId(); Position position = new PositionImpl(msgIdData.getLedgerId(), msgIdData.getEntryId()); subscription.resetCursor(position).thenRun(() -> { log.info("[{}] [{}][{}] Reset subscription to message id {}", remoteAddress, subscription.getTopic().getName(), subscription.getName(), position); ctx.writeAndFlush(Commands.newSuccess(requestId)); }).exceptionally(ex -> { log.warn("[{}][{}] Failed to reset subscription: {}", remoteAddress, subscription, ex.getMessage(), ex); ctx.writeAndFlush(Commands.newError(requestId, ServerError.UnknownError, "Error when resetting subscription: " + ex.getCause().getMessage())); return null; }); } else { ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, "Consumer not found")); } }
if (producerFuture == null) { log.warn("[{}] Producer {} was not registered on the connection", remoteAddress, producerId); ctx.writeAndFlush(Commands.newError(requestId, ServerError.UnknownError, "Producer was not registered on the connection")); return;
@Override protected void handleGetTopicsOfNamespace(CommandGetTopicsOfNamespace commandGetTopicsOfNamespace) { final long requestId = commandGetTopicsOfNamespace.getRequestId(); final String namespace = commandGetTopicsOfNamespace.getNamespace(); final CommandGetTopicsOfNamespace.Mode mode = commandGetTopicsOfNamespace.getMode(); try { final NamespaceName namespaceName = NamespaceName.get(namespace); final List<String> topics = getBrokerService().pulsar().getNamespaceService() .getListOfTopics(namespaceName, mode); if (log.isDebugEnabled()) { log.debug("[{}] Received CommandGetTopicsOfNamespace for namespace [//{}] by {}, size:{}", remoteAddress, namespace, requestId, topics.size()); } ctx.writeAndFlush(Commands.newGetTopicsOfNamespaceResponse(topics, requestId)); } catch (Exception e) { log.warn("[{]] Error GetTopicsOfNamespace for namespace [//{}] by {}", remoteAddress, namespace, requestId); ctx.writeAndFlush( Commands.newError(requestId, BrokerServiceException.getClientErrorCode(new ServerMetadataException(e)), e.getMessage())); } }
log.warn("[{}] {} with role {} and proxyClientAuthRole {} on topic {}", remoteAddress, msg, authRole, originalPrincipal, topicName); ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, msg)); return; log.warn("[{}][{}] Producer is already present on the connection", remoteAddress, topicName); ctx.writeAndFlush(Commands.newError(requestId, error, "Producer is already present on the connection")); return null; if (retentionPolicy == BacklogQuota.RetentionPolicy.producer_request_hold) { ctx.writeAndFlush( Commands.newError(requestId, ServerError.ProducerBlockedQuotaExceededError, illegalStateException.getMessage())); } else if (retentionPolicy == BacklogQuota.RetentionPolicy.producer_exception) { ctx.writeAndFlush(Commands.newError(requestId, ServerError.ProducerBlockedQuotaExceededException, illegalStateException.getMessage())); String msg = String.format("Encryption is required in %s", topicName); log.warn("[{}] {}", remoteAddress, msg); ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, msg)); return; ctx.writeAndFlush(Commands.newError(requestId, ServerError.UnknownError, exception.getMessage())); producers.remove(producerId, producerFuture); return null;
String msg = "Unable to authenticate"; LOG.warn("[{}] {}: {}", remoteAddress, msg, e.getMessage()); ctx.writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg)); close(); return;
private void handleGetTopicsOfNamespace(CommandGetTopicsOfNamespace commandGetTopicsOfNamespace, long clientRequestId) { String serviceUrl; if (isBlank(brokerServiceURL)) { ServiceLookupData availableBroker; try { availableBroker = service.getDiscoveryProvider().nextBroker(); } catch (Exception e) { log.warn("[{}] Failed to get next active broker {}", clientAddress, e.getMessage(), e); proxyConnection.ctx().writeAndFlush(Commands.newError( clientRequestId, ServerError.ServiceNotReady, e.getMessage() )); return; } serviceUrl = this.connectWithTLS ? availableBroker.getPulsarServiceUrlTls() : availableBroker.getPulsarServiceUrl(); } else { serviceUrl = this.connectWithTLS ? service.getConfiguration().getBrokerServiceURLTLS() : service.getConfiguration().getBrokerServiceURL(); } performGetTopicsOfNamespace(clientRequestId, commandGetTopicsOfNamespace.getNamespace(), serviceUrl, 10, commandGetTopicsOfNamespace.getMode()); }
ctx.writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, "Failed to authenticate")); close(); return;
log.warn("[{}] {} with role {} and proxyClientAuthRole {} on topic {}", remoteAddress, msg, authRole, originalPrincipal, topicName); ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, msg)); return; } catch (IllegalArgumentException iae) { final String msg = iae.getMessage(); ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, msg)); return null; ServerError error = !existingConsumerFuture.isDone() ? ServerError.ServiceNotReady : getErrorCode(existingConsumerFuture); ctx.writeAndFlush(Commands.newError(requestId, error, "Consumer is already present on the connection")); return null; ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(exception.getCause()), exception.getCause().getMessage())); String msg = "Client is not authorized to subscribe"; log.warn("[{}] {} with role {}", remoteAddress, msg, authRole); ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, msg)); String msg = String.format("[%s] %s with role %s", remoteAddress, e.getMessage(), authRole); log.warn(msg); ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, e.getMessage())); return null; });
String msg = "Unable to authenticate"; log.warn("[{}] {}: {}", remoteAddress, msg, e.getMessage()); ctx.writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg)); close(); return;
@Override protected void handleGetLastMessageId(CommandGetLastMessageId getLastMessageId) { checkArgument(state == State.Connected); CompletableFuture<Consumer> consumerFuture = consumers.get(getLastMessageId.getConsumerId()); if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) { Consumer consumer = consumerFuture.getNow(null); long requestId = getLastMessageId.getRequestId(); Topic topic = consumer.getSubscription().getTopic(); Position position = topic.getLastMessageId(); int partitionIndex = TopicName.getPartitionIndex(topic.getName()); if (log.isDebugEnabled()) { log.debug("[{}] [{}][{}] Get LastMessageId {} partitionIndex {}", remoteAddress, topic.getName(), consumer.getSubscription().getName(), position, partitionIndex); } MessageIdData messageId = MessageIdData.newBuilder() .setLedgerId(((PositionImpl)position).getLedgerId()) .setEntryId(((PositionImpl)position).getEntryId()) .setPartition(partitionIndex) .build(); ctx.writeAndFlush(Commands.newGetLastMessageIdResponse(requestId, messageId)); } else { ctx.writeAndFlush(Commands.newError(getLastMessageId.getRequestId(), ServerError.MetadataError, "Consumer not found")); } }