Search in sources :

Example 1 with CommandCloseProducer

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

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 : CommandProducer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer) CommandCloseProducer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseProducer)

Example 2 with CommandCloseProducer

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

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(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseProducer) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf) RecyclableDuplicateByteBuf(io.netty.buffer.RecyclableDuplicateByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CommandCloseProducer (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseProducer)2 CommandProducer (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandProducer)1 ByteBuf (io.netty.buffer.ByteBuf)1 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)1 UnpooledHeapByteBuf (io.netty.buffer.UnpooledHeapByteBuf)1