Search in sources :

Example 1 with CommandCloseProducer

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer in project incubator-pulsar by apache.

the class Commands method newCloseProducer.

public static ByteBuf newCloseProducer(long producerId, long requestId) {
    CommandCloseProducer.Builder closeProducerBuilder = CommandCloseProducer.newBuilder();
    closeProducerBuilder.setProducerId(producerId);
    closeProducerBuilder.setRequestId(requestId);
    CommandCloseProducer closeProducer = closeProducerBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.CLOSE_PRODUCER).setCloseProducer(closeProducerBuilder));
    closeProducerBuilder.recycle();
    closeProducer.recycle();
    return res;
}
Also used : CommandCloseProducer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with CommandCloseProducer

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer in project incubator-pulsar by apache.

the class ServerCnx method handleCloseProducer.

@Override
protected void handleCloseProducer(CommandCloseProducer closeProducer) {
    checkArgument(state == State.Connected);
    final long producerId = closeProducer.getProducerId();
    final long requestId = closeProducer.getRequestId();
    CompletableFuture<Producer> producerFuture = producers.get(producerId);
    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;
    }
    if (!producerFuture.isDone() && producerFuture.completeExceptionally(new IllegalStateException("Closed producer before creation was complete"))) {
        // We have received a request to close the producer before it was actually completed, we have marked the
        // producer future as failed and we can tell the client the close operation was successful. When the actual
        // create operation will complete, the new producer will be discarded.
        log.info("[{}] Closed producer {} before its creation was completed", remoteAddress, producerId);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        return;
    } else if (producerFuture.isCompletedExceptionally()) {
        log.info("[{}] Closed producer {} that already failed to be created", remoteAddress, producerId);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        return;
    }
    // Proceed with normal close, the producer
    Producer producer = producerFuture.getNow(null);
    log.info("[{}][{}] Closing producer on cnx {}", producer.getTopic(), producer.getProducerName(), remoteAddress);
    producer.close().thenAccept(v -> {
        log.info("[{}][{}] Closed producer on cnx {}", producer.getTopic(), producer.getProducerName(), remoteAddress);
        ctx.writeAndFlush(Commands.newSuccess(requestId));
        producers.remove(producerId, producerFuture);
    });
}
Also used : CommandCloseProducer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer) CommandProducer(org.apache.pulsar.common.api.proto.PulsarApi.CommandProducer)

Aggregations

CommandCloseProducer (org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseProducer)2 ByteBuf (io.netty.buffer.ByteBuf)1 CommandProducer (org.apache.pulsar.common.api.proto.PulsarApi.CommandProducer)1