Search in sources :

Example 41 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future 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)

Example 42 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project incubator-pulsar by apache.

the class ConsumerImpl method internalGetLastMessageIdAsync.

private void internalGetLastMessageIdAsync(final Backoff backoff, final AtomicLong remainingTime, CompletableFuture<MessageId> future) {
    ClientCnx cnx = cnx();
    if (isConnected() && cnx != null) {
        if (!Commands.peerSupportsGetLastMessageId(cnx.getRemoteEndpointProtocolVersion())) {
            future.completeExceptionally(new PulsarClientException.NotSupportedException("GetLastMessageId Not supported for ProtocolVersion: " + cnx.getRemoteEndpointProtocolVersion()));
        }
        long requestId = client.newRequestId();
        ByteBuf getLastIdCmd = Commands.newGetLastMessageId(consumerId, requestId);
        log.info("[{}][{}] Get topic last message Id", topic, subscription);
        cnx.sendGetLastMessageId(getLastIdCmd, requestId).thenAccept((result) -> {
            log.info("[{}][{}] Successfully getLastMessageId {}:{}", topic, subscription, result.getLedgerId(), result.getEntryId());
            future.complete(new MessageIdImpl(result.getLedgerId(), result.getEntryId(), result.getPartition()));
        }).exceptionally(e -> {
            log.error("[{}][{}] Failed getLastMessageId command", topic, subscription);
            future.completeExceptionally(e.getCause());
            return null;
        });
    } else {
        long nextDelay = Math.min(backoff.next(), remainingTime.get());
        if (nextDelay <= 0) {
            future.completeExceptionally(new PulsarClientException.TimeoutException("Could not getLastMessageId within configured timeout."));
            return;
        }
        ((ScheduledExecutorService) listenerExecutor).schedule(() -> {
            log.warn("[{}] [{}] Could not get connection while getLastMessageId -- Will try again in {} ms", topic, getHandlerName(), nextDelay);
            remainingTime.addAndGet(-nextDelay);
            internalGetLastMessageIdAsync(backoff, remainingTime, future);
        }, nextDelay, TimeUnit.MILLISECONDS);
    }
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) Commands.hasChecksum(org.apache.pulsar.common.api.Commands.hasChecksum) LoggerFactory(org.slf4j.LoggerFactory) AckType(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck.AckType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Crc32cIntChecksum.computeChecksum(com.scurrilous.circe.checksum.Crc32cIntChecksum.computeChecksum) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) PulsarDecoder(org.apache.pulsar.common.api.PulsarDecoder) Commands(org.apache.pulsar.common.api.Commands) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) ConsumerCryptoFailureAction(org.apache.pulsar.client.api.ConsumerCryptoFailureAction) Iterables(com.google.common.collect.Iterables) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ProtocolVersion(org.apache.pulsar.common.api.proto.PulsarApi.ProtocolVersion) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Message(org.apache.pulsar.client.api.Message) SubscriptionInitialPosition(org.apache.pulsar.client.api.SubscriptionInitialPosition) ArrayList(java.util.ArrayList) ConsumerStats(org.apache.pulsar.client.api.ConsumerStats) ByteBuf(io.netty.buffer.ByteBuf) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) ExecutorService(java.util.concurrent.ExecutorService) Timeout(io.netty.util.Timeout) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) IOException(java.io.IOException) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Schema(org.apache.pulsar.client.api.Schema) Commands.readChecksum(org.apache.pulsar.common.api.Commands.readChecksum) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ValidationError(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck.ValidationError) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageId(org.apache.pulsar.client.api.MessageId) CompressionType(org.apache.pulsar.common.api.proto.PulsarApi.CompressionType) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf)

Example 43 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project UnderNet by itsMatoosh.

the class Client method connect.

/**
 * Connects the client to a node.
 */
public void connect(Node node) {
    if (status == InterfaceStatus.STOPPING) {
        logger.error("Can't connect to nodes, while the client is stopping!");
        return;
    }
    if (status != InterfaceStatus.STARTED) {
        EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTED));
    }
    logger.info("Connecting to node: " + node.address);
    // Making sure the list of client futures exists.
    if (closeFutures == null) {
        closeFutures = new ArrayList<>();
    }
    // Starting the client.
    Bootstrap clientBootstrap = new Bootstrap();
    // Assigning the channel to the client event loop group.
    clientBootstrap.group(workerEventLoopGroup);
    // Using the non blocking io.
    clientBootstrap.channel(NioSocketChannel.class);
    // Making sure the connection is sending the keep alive signal.
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    clientBootstrap.handler(new ClientChannelInitializer(this));
    // Connecting
    // Connecting to the node.
    ChannelFuture future = clientBootstrap.connect(node.address);
    ChannelFuture closeFuture = future.channel().closeFuture();
    closeFuture.addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            // Removing the future from future list.
            closeFutures.remove(future);
            if (closeFutures.size() == 0) {
                // Stopping the worker group.
                EventManager.callEvent(new ClientStatusEvent(Client.this, InterfaceStatus.STOPPED));
            }
        }
    });
    closeFutures.add(closeFuture);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent)

Example 44 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project drill by axbaretto.

the class WebSessionResourcesTest method testCloseWithListener.

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and {@link TestClosedListener}
 * getting invoked which is added to the close future.
 * @throws Exception
 */
@Test
public void testCloseWithListener() throws Exception {
    try {
        // Assign latch, executor and closeListener for this test case
        GenericFutureListener<Future<Void>> closeListener = new TestClosedListener();
        latch = new CountDownLatch(1);
        executor = TransportCheck.createEventLoopGroup(1, "Test-Thread").next();
        ChannelPromise closeFuture = new DefaultChannelPromise(null, executor);
        // create WebSessionResources with above ChannelPromise to notify listener
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        // Add the Test Listener to close future
        assertTrue(!listenerComplete);
        closeFuture.addListener(closeListener);
        // Close the WebSessionResources
        webSessionResources.close();
        // Verify the states
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        assertTrue(webSessionResources.getCloseFuture() == null);
        // Since listener will be invoked so test should not wait forever
        latch.await();
        assertTrue(listenerComplete);
    } catch (Exception e) {
        fail();
    } finally {
        listenerComplete = false;
        executor.shutdownGracefully();
    }
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Future(io.netty.util.concurrent.Future) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 45 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project reactor-netty by reactor.

the class DefaultLoopResources method disposeLater.

@Override
public Mono<Void> disposeLater() {
    return Mono.defer(() -> {
        EventLoopGroup cacheNativeClientGroup = cacheNativeClientLoops.get();
        EventLoopGroup cacheNativeSelectGroup = cacheNativeSelectLoops.get();
        EventLoopGroup cacheNativeServerGroup = cacheNativeServerLoops.get();
        if (running.compareAndSet(true, false)) {
            clientLoops.shutdownGracefully();
            serverSelectLoops.shutdownGracefully();
            serverLoops.shutdownGracefully();
            if (cacheNativeClientGroup != null) {
                cacheNativeClientGroup.shutdownGracefully();
            }
            if (cacheNativeSelectGroup != null) {
                cacheNativeSelectGroup.shutdownGracefully();
            }
            if (cacheNativeServerGroup != null) {
                cacheNativeServerGroup.shutdownGracefully();
            }
        }
        Mono<?> clMono = FutureMono.from((Future) clientLoops.terminationFuture());
        Mono<?> sslMono = FutureMono.from((Future) serverSelectLoops.terminationFuture());
        Mono<?> slMono = FutureMono.from((Future) serverLoops.terminationFuture());
        Mono<?> cnclMono = Mono.empty();
        if (cacheNativeClientGroup != null) {
            cnclMono = FutureMono.from((Future) cacheNativeClientGroup.terminationFuture());
        }
        Mono<?> cnslMono = Mono.empty();
        if (cacheNativeSelectGroup != null) {
            cnslMono = FutureMono.from((Future) cacheNativeSelectGroup.terminationFuture());
        }
        Mono<?> cnsrvlMono = Mono.empty();
        if (cacheNativeServerGroup != null) {
            cnsrvlMono = FutureMono.from((Future) cacheNativeServerGroup.terminationFuture());
        }
        return Mono.when(clMono, sslMono, slMono, cnclMono, cnslMono, cnsrvlMono);
    });
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Future(io.netty.util.concurrent.Future)

Aggregations

Future (io.netty.util.concurrent.Future)177 Channel (io.netty.channel.Channel)61 ChannelFuture (io.netty.channel.ChannelFuture)58 InetSocketAddress (java.net.InetSocketAddress)45 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)44 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)42 CompletableFuture (java.util.concurrent.CompletableFuture)40 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)35 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)34 List (java.util.List)34 ChannelFutureListener (io.netty.channel.ChannelFutureListener)31 EventLoopGroup (io.netty.channel.EventLoopGroup)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)30 FutureListener (io.netty.util.concurrent.FutureListener)28 Logger (org.slf4j.Logger)28 LoggerFactory (org.slf4j.LoggerFactory)28 TimeUnit (java.util.concurrent.TimeUnit)27 Bootstrap (io.netty.bootstrap.Bootstrap)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25