Search in sources :

Example 1 with CommandProducer

use of com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer in project pulsar by yahoo.

the class Commands method newProducer.

public static ByteBuf newProducer(String topic, long producerId, long requestId, String producerName) {
    CommandProducer.Builder producerBuilder = CommandProducer.newBuilder();
    producerBuilder.setTopic(topic);
    producerBuilder.setProducerId(producerId);
    producerBuilder.setRequestId(requestId);
    if (producerName != null) {
        producerBuilder.setProducerName(producerName);
    }
    CommandProducer producer = producerBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.PRODUCER).setProducer(producer));
    producerBuilder.recycle();
    producer.recycle();
    return res;
}
Also used : CommandProducer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf) RecyclableDuplicateByteBuf(io.netty.buffer.RecyclableDuplicateByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with CommandProducer

use of com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer in project pulsar by yahoo.

the class ServerCnx method handleProducer.

@Override
protected void handleProducer(final CommandProducer cmdProducer) {
    checkArgument(state == State.Connected);
    CompletableFuture<Boolean> authorizationFuture;
    if (service.isAuthorizationEnabled()) {
        authorizationFuture = service.getAuthorizationManager().canProduceAsync(DestinationName.get(cmdProducer.getTopic().toString()), authRole);
    } else {
        authorizationFuture = CompletableFuture.completedFuture(true);
    }
    // Use producer name provided by client if present
    final String producerName = cmdProducer.hasProducerName() ? cmdProducer.getProducerName() : service.generateUniqueProducerName();
    final String topicName = cmdProducer.getTopic();
    final long producerId = cmdProducer.getProducerId();
    final long requestId = cmdProducer.getRequestId();
    authorizationFuture.thenApply(isAuthorized -> {
        if (isAuthorized) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Client is authorized to Produce with role {}", remoteAddress, authRole);
            }
            CompletableFuture<Producer> producerFuture = new CompletableFuture<>();
            CompletableFuture<Producer> existingProducerFuture = producers.putIfAbsent(producerId, producerFuture);
            if (existingProducerFuture != null) {
                if (existingProducerFuture.isDone() && !existingProducerFuture.isCompletedExceptionally()) {
                    Producer producer = existingProducerFuture.getNow(null);
                    log.info("[{}] Producer with the same id is already created: {}", remoteAddress, producer);
                    ctx.writeAndFlush(Commands.newProducerSuccess(requestId, producer.getProducerName()));
                    return null;
                } else {
                    ServerError error = !existingProducerFuture.isDone() ? ServerError.ServiceNotReady : getErrorCode(existingProducerFuture);
                    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;
                }
            }
            log.info("[{}][{}] Creating producer. producerId={}", remoteAddress, topicName, producerId);
            service.getTopic(topicName).thenAccept((Topic topic) -> {
                if (topic.isBacklogQuotaExceeded(producerName)) {
                    IllegalStateException illegalStateException = new IllegalStateException("Cannot create producer on topic with backlog quota exceeded");
                    BacklogQuota.RetentionPolicy retentionPolicy = topic.getBacklogQuota().getPolicy();
                    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()));
                    }
                    producerFuture.completeExceptionally(illegalStateException);
                    producers.remove(producerId, producerFuture);
                    return;
                }
                disableTcpNoDelayIfNeeded(topicName, producerName);
                Producer producer = new Producer(topic, ServerCnx.this, producerId, producerName, authRole);
                try {
                    topic.addProducer(producer);
                    if (isActive()) {
                        if (producerFuture.complete(producer)) {
                            log.info("[{}] Created new producer: {}", remoteAddress, producer);
                            ctx.writeAndFlush(Commands.newProducerSuccess(requestId, producerName));
                            return;
                        } else {
                            producer.closeNow();
                            log.info("[{}] Cleared producer created after timeout on client side {}", remoteAddress, producer);
                        }
                    } else {
                        producer.closeNow();
                        log.info("[{}] Cleared producer created after connection was closed: {}", remoteAddress, producer);
                        producerFuture.completeExceptionally(new IllegalStateException("Producer created after connection was closed"));
                    }
                } catch (BrokerServiceException ise) {
                    log.error("[{}] Failed to add producer to topic {}: {}", remoteAddress, topicName, ise.getMessage());
                    ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(ise), ise.getMessage()));
                    producerFuture.completeExceptionally(ise);
                }
                producers.remove(producerId, producerFuture);
            }).exceptionally(exception -> {
                Throwable cause = exception.getCause();
                if (!(cause instanceof ServiceUnitNotReadyException)) {
                    log.error("[{}] Failed to create topic {}", remoteAddress, topicName, exception);
                }
                if (producerFuture.completeExceptionally(exception)) {
                    ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(cause), cause.getMessage()));
                }
                producers.remove(producerId, producerFuture);
                return null;
            });
        } else {
            String msg = "Client is not authorized to Produce";
            log.warn("[{}] {} with role {}", remoteAddress, msg, authRole);
            ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, msg));
        }
        return null;
    });
}
Also used : ServerError(com.yahoo.pulsar.common.api.proto.PulsarApi.ServerError) ServiceUnitNotReadyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) CompletableFuture(java.util.concurrent.CompletableFuture) CommandProducer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer) CommandCloseProducer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseProducer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) CommandLookupTopic(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandLookupTopic)

Aggregations

CommandProducer (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer)2 ServiceUnitNotReadyException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)1 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)1 CommandCloseProducer (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseProducer)1 CommandLookupTopic (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandLookupTopic)1 ServerError (com.yahoo.pulsar.common.api.proto.PulsarApi.ServerError)1 ByteBuf (io.netty.buffer.ByteBuf)1 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)1 UnpooledHeapByteBuf (io.netty.buffer.UnpooledHeapByteBuf)1 CompletableFuture (java.util.concurrent.CompletableFuture)1