Search in sources :

Example 1 with OrderedExecutor

use of org.apache.bookkeeper.common.util.OrderedExecutor in project bookkeeper by apache.

the class BookieRequestProcessor method processReadRequestV3.

private void processReadRequestV3(final BookkeeperProtocol.Request r, final Channel c) {
    ExecutorService fenceThread = null == highPriorityThreadPool ? null : highPriorityThreadPool.chooseThread(c);
    final ReadEntryProcessorV3 read;
    final OrderedExecutor threadPool;
    if (RequestUtils.isLongPollReadRequest(r.getReadRequest())) {
        ExecutorService lpThread = null == longPollThreadPool ? null : longPollThreadPool.chooseThread(c);
        read = new LongPollReadEntryProcessorV3(r, c, this, fenceThread, lpThread, requestTimer);
        threadPool = longPollThreadPool;
    } else {
        read = new ReadEntryProcessorV3(r, c, this, fenceThread);
        // If it's a high priority read (fencing or as part of recovery process), we want to make sure it
        // gets executed as fast as possible, so bypass the normal readThreadPool
        // and execute in highPriorityThreadPool
        boolean isHighPriority = RequestUtils.isHighPriority(r) || hasFlag(r.getReadRequest(), BookkeeperProtocol.ReadRequest.Flag.FENCE_LEDGER);
        if (isHighPriority) {
            threadPool = highPriorityThreadPool;
        } else {
            threadPool = readThreadPool;
        }
    }
    if (null == threadPool) {
        read.run();
    } else {
        try {
            threadPool.executeOrdered(r.getReadRequest().getLedgerId(), read);
        } catch (RejectedExecutionException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to process request to read entry at {}:{}. Too many pending requests", r.getReadRequest().getLedgerId(), r.getReadRequest().getEntryId());
            }
            BookkeeperProtocol.ReadResponse.Builder readResponse = BookkeeperProtocol.ReadResponse.newBuilder().setLedgerId(r.getAddRequest().getLedgerId()).setEntryId(r.getAddRequest().getEntryId()).setStatus(BookkeeperProtocol.StatusCode.ETOOMANYREQUESTS);
            BookkeeperProtocol.Response.Builder response = BookkeeperProtocol.Response.newBuilder().setHeader(read.getHeader()).setStatus(readResponse.getStatus()).setReadResponse(readResponse);
            BookkeeperProtocol.Response resp = response.build();
            read.sendResponse(readResponse.getStatus(), resp, readRequestStats);
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ExecutorService(java.util.concurrent.ExecutorService) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with OrderedExecutor

use of org.apache.bookkeeper.common.util.OrderedExecutor in project bookkeeper by apache.

the class TestPerChannelBookieClient method testConnectCloseRace.

/**
 * Test that a race does not exist between connection completion
 * and client closure. If a race does exist, this test will simply
 * hang at releaseExternalResources() as it is uninterruptible.
 * This specific race was found in
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-485}.
 */
@Test
public void testConnectCloseRace() throws Exception {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    for (int i = 0; i < 1000; i++) {
        PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
        client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {

            @Override
            public void operationComplete(int rc, PerChannelBookieClient client) {
            // do nothing, we don't care about doing anything with the connection,
            // we just want to trigger it connecting.
            }
        });
        client.close();
    }
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 3 with OrderedExecutor

use of org.apache.bookkeeper.common.util.OrderedExecutor in project bookkeeper by apache.

the class TestPerChannelBookieClient method testDisconnectRace.

/**
 * Test that all resources are freed if connections and disconnections
 * are interleaved randomly.
 *
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-620}
 */
@Test
public void testDisconnectRace() throws Exception {
    final GenericCallback<PerChannelBookieClient> nullop = new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(int rc, PerChannelBookieClient client) {
        // do nothing, we don't care about doing anything with the connection,
        // we just want to trigger it connecting.
        }
    };
    final int iterations = 100000;
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
    final AtomicBoolean shouldFail = new AtomicBoolean(false);
    final AtomicBoolean running = new AtomicBoolean(true);
    final CountDownLatch disconnectRunning = new CountDownLatch(1);
    Thread connectThread = new Thread() {

        public void run() {
            try {
                if (!disconnectRunning.await(10, TimeUnit.SECONDS)) {
                    LOG.error("Disconnect thread never started");
                    shouldFail.set(true);
                }
            } catch (InterruptedException ie) {
                LOG.error("Connect thread interrupted", ie);
                Thread.currentThread().interrupt();
                running.set(false);
            }
            for (int i = 0; i < iterations && running.get(); i++) {
                client.connectIfNeededAndDoOp(nullop);
            }
            running.set(false);
        }
    };
    Thread disconnectThread = new Thread() {

        public void run() {
            disconnectRunning.countDown();
            while (running.get()) {
                client.disconnect();
            }
        }
    };
    Thread checkThread = new Thread() {

        public void run() {
            ConnectionState state;
            Channel channel;
            while (running.get()) {
                synchronized (client) {
                    state = client.state;
                    channel = client.channel;
                    if ((state == ConnectionState.CONNECTED && (channel == null || !channel.isActive())) || (state != ConnectionState.CONNECTED && channel != null && channel.isActive())) {
                        LOG.error("State({}) and channel({}) inconsistent " + channel, state, channel == null ? null : channel.isActive());
                        shouldFail.set(true);
                        running.set(false);
                    }
                }
            }
        }
    };
    connectThread.start();
    disconnectThread.start();
    checkThread.start();
    connectThread.join();
    disconnectThread.join();
    checkThread.join();
    assertFalse("Failure in threads, check logs", shouldFail.get());
    client.close();
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : Channel(io.netty.channel.Channel) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ConnectionState(org.apache.bookkeeper.proto.PerChannelBookieClient.ConnectionState) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 4 with OrderedExecutor

use of org.apache.bookkeeper.common.util.OrderedExecutor in project bookkeeper by apache.

the class TestPerChannelBookieClient method testConnectRace.

/**
 * Test race scenario found in {@link https://issues.apache.org/jira/browse/BOOKKEEPER-5}
 * where multiple clients try to connect a channel simultaneously. If not synchronised
 * correctly, this causes the netty channel to get orphaned.
 */
@Test
public void testConnectRace() throws Exception {
    GenericCallback<PerChannelBookieClient> nullop = new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(int rc, PerChannelBookieClient pcbc) {
        // do nothing, we don't care about doing anything with the connection,
        // we just want to trigger it connecting.
        }
    };
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    for (int i = 0; i < 100; i++) {
        PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
        for (int j = i; j < 10; j++) {
            client.connectIfNeededAndDoOp(nullop);
        }
        client.close();
    }
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 5 with OrderedExecutor

use of org.apache.bookkeeper.common.util.OrderedExecutor in project bookkeeper by apache.

the class TestPerChannelBookieClient method testRequestCompletesAfterDisconnectRace.

/**
 * Test that requests are completed even if the channel is disconnected
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-668}.
 */
@Test
public void testRequestCompletesAfterDisconnectRace() throws Exception {
    ServerConfiguration conf = killBookie(0);
    Bookie delayBookie = new Bookie(conf) {

        @Override
        public ByteBuf readEntry(long ledgerId, long entryId) throws IOException, NoLedgerException {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new IOException("Interrupted waiting", ie);
            }
            return super.readEntry(ledgerId, entryId);
        }
    };
    bsConfs.add(conf);
    bs.add(startBookie(conf, delayBookie));
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    final OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
    final CountDownLatch completion = new CountDownLatch(1);
    final ReadEntryCallback cb = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) {
            completion.countDown();
        }
    };
    client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(final int rc, PerChannelBookieClient pcbc) {
            if (rc != BKException.Code.OK) {
                executor.executeOrdered(1, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        cb.readEntryComplete(rc, 1, 1, null, null);
                    }
                });
                return;
            }
            client.readEntry(1, 1, cb, null, BookieProtocol.FLAG_DO_FENCING, "00000111112222233333".getBytes());
        }
    });
    Thread.sleep(1000);
    client.disconnect();
    client.close();
    assertTrue("Request should have completed", completion.await(5, TimeUnit.SECONDS));
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) IOException(java.io.IOException) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bookie(org.apache.bookkeeper.bookie.Bookie) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Aggregations

OrderedExecutor (org.apache.bookkeeper.common.util.OrderedExecutor)10 EventLoopGroup (io.netty.channel.EventLoopGroup)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 Test (org.junit.Test)4 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 ByteBuf (io.netty.buffer.ByteBuf)2 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 GenericCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback)2 Channel (io.netty.channel.Channel)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)1