Search in sources :

Example 1 with BookieClient

use of org.apache.bookkeeper.proto.BookieClient in project bookkeeper by apache.

the class BookieClientTest method testWriteGaps.

@Test
public void testWriteGaps() throws Exception {
    final Object notifyObject = new Object();
    byte[] passwd = new byte[20];
    Arrays.fill(passwd, (byte) 'a');
    BookieSocketAddress addr = bs.getLocalAddress();
    ResultStruct arc = new ResultStruct();
    BookieClient bc = new BookieClient(new ClientConfiguration(), eventLoopGroup, executor, scheduler, NullStatsLogger.INSTANCE);
    ByteBufList bb = createByteBuffer(1, 1, 1);
    bc.addEntry(addr, 1, passwd, 1, bb, wrcb, arc, BookieProtocol.FLAG_NONE);
    synchronized (arc) {
        arc.wait(1000);
        assertEquals(0, arc.rc);
        bc.readEntry(addr, 1, 1, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(1, arc.entry.getInt());
    }
    bb = createByteBuffer(2, 1, 2);
    bc.addEntry(addr, 1, passwd, 2, bb, wrcb, null, BookieProtocol.FLAG_NONE);
    bb = createByteBuffer(3, 1, 3);
    bc.addEntry(addr, 1, passwd, 3, bb, wrcb, null, BookieProtocol.FLAG_NONE);
    bb = createByteBuffer(5, 1, 5);
    bc.addEntry(addr, 1, passwd, 5, bb, wrcb, null, BookieProtocol.FLAG_NONE);
    bb = createByteBuffer(7, 1, 7);
    bc.addEntry(addr, 1, passwd, 7, bb, wrcb, null, BookieProtocol.FLAG_NONE);
    synchronized (notifyObject) {
        bb = createByteBuffer(11, 1, 11);
        bc.addEntry(addr, 1, passwd, 11, bb, wrcb, notifyObject, BookieProtocol.FLAG_NONE);
        notifyObject.wait();
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 6, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchEntryException, arc.rc);
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 7, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(7, arc.entry.getInt(), BookieProtocol.FLAG_NONE);
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 1, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(1, arc.entry.getInt());
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 2, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(2, arc.entry.getInt());
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 3, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(3, arc.entry.getInt());
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 4, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchEntryException, arc.rc);
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 11, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(11, arc.entry.getInt());
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 5, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(0, arc.rc);
        assertEquals(5, arc.entry.getInt());
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 10, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchEntryException, arc.rc);
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 12, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchEntryException, arc.rc);
    }
    synchronized (arc) {
        bc.readEntry(addr, 1, 13, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchEntryException, arc.rc);
    }
}
Also used : ByteBufList(org.apache.bookkeeper.util.ByteBufList) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 2 with BookieClient

use of org.apache.bookkeeper.proto.BookieClient in project bookkeeper by apache.

the class BookieClientTest method testGetBookieInfo.

@Test
public void testGetBookieInfo() throws IOException, InterruptedException {
    BookieSocketAddress addr = bs.getLocalAddress();
    BookieClient bc = new BookieClient(new ClientConfiguration(), new NioEventLoopGroup(), executor, scheduler, NullStatsLogger.INSTANCE);
    long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE;
    class CallbackObj {

        int rc;

        long requested;

        long freeDiskSpace, totalDiskCapacity;

        CountDownLatch latch = new CountDownLatch(1);

        CallbackObj(long requested) {
            this.requested = requested;
            this.rc = 0;
            this.freeDiskSpace = 0L;
            this.totalDiskCapacity = 0L;
        }
    }
    CallbackObj obj = new CallbackObj(flags);
    bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() {

        @Override
        public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
            CallbackObj obj = (CallbackObj) ctx;
            obj.rc = rc;
            if (rc == Code.OK) {
                if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
                    obj.freeDiskSpace = bInfo.getFreeDiskSpace();
                }
                if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
                    obj.totalDiskCapacity = bInfo.getTotalDiskSpace();
                }
            }
            obj.latch.countDown();
        }
    }, obj);
    obj.latch.await();
    System.out.println("Return code: " + obj.rc + "FreeDiskSpace: " + obj.freeDiskSpace + " TotalCapacity: " + obj.totalDiskCapacity);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.rc == Code.OK);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.freeDiskSpace <= obj.totalDiskCapacity);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.totalDiskCapacity > 0);
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 3 with BookieClient

use of org.apache.bookkeeper.proto.BookieClient in project bookkeeper by apache.

the class MockBookKeeperTestCase method setup.

@Before
public void setup() throws Exception {
    mockLedgerMetadataRegistry = new ConcurrentHashMap<>();
    mockLedgerData = new ConcurrentHashMap<>();
    mockNextLedgerId = new AtomicLong(1);
    fencedLedgers = new ConcurrentSkipListSet<>();
    scheduler = OrderedScheduler.newSchedulerBuilder().numThreads(4).name("bk-test").build();
    executor = OrderedExecutor.newBuilder().build();
    bookieWatcher = mock(BookieWatcher.class);
    bookieClient = mock(BookieClient.class);
    ledgerManager = mock(LedgerManager.class);
    ledgerIdGenerator = mock(LedgerIdGenerator.class);
    bk = mock(BookKeeper.class);
    NullStatsLogger nullStatsLogger = setupLoggers();
    failedBookies = new ArrayList<>();
    availableBookies = new HashSet<>();
    when(bk.getCloseLock()).thenReturn(new ReentrantReadWriteLock());
    when(bk.isClosed()).thenReturn(false);
    when(bk.getBookieWatcher()).thenReturn(bookieWatcher);
    when(bk.getDisableEnsembleChangeFeature()).thenReturn(mock(Feature.class));
    when(bk.getExplicitLacInterval()).thenReturn(0);
    when(bk.getMainWorkerPool()).thenReturn(executor);
    when(bk.getBookieClient()).thenReturn(bookieClient);
    when(bk.getScheduler()).thenReturn(scheduler);
    when(bk.getReadSpeculativeRequestPolicy()).thenReturn(Optional.absent());
    mockBookKeeperGetConf(new ClientConfiguration());
    when(bk.getStatsLogger()).thenReturn(nullStatsLogger);
    when(bk.getLedgerManager()).thenReturn(ledgerManager);
    when(bk.getLedgerIdGenerator()).thenReturn(ledgerIdGenerator);
    when(bk.getReturnRc(anyInt())).thenAnswer(invocationOnMock -> invocationOnMock.getArgument(0));
    setupLedgerIdGenerator();
    setupCreateLedgerMetadata();
    setupReadLedgerMetadata();
    setupWriteLedgerMetadata();
    setupRemoveLedgerMetadata();
    setupRegisterLedgerMetadataListener();
    setupBookieWatcherForNewEnsemble();
    setupBookieWatcherForEnsembleChange();
    setupBookieClientReadEntry();
    setupBookieClientAddEntry();
}
Also used : BookieClient(org.apache.bookkeeper.proto.BookieClient) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerIdGenerator(org.apache.bookkeeper.meta.LedgerIdGenerator) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Feature(org.apache.bookkeeper.feature.Feature) AtomicLong(java.util.concurrent.atomic.AtomicLong) NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Before(org.junit.Before)

Example 4 with BookieClient

use of org.apache.bookkeeper.proto.BookieClient in project bookkeeper by apache.

the class BenchBookie method main.

/**
 * @param args
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException, ParseException, IOException, BKException, KeeperException {
    Options options = new Options();
    options.addOption("host", true, "Hostname or IP of bookie to benchmark");
    options.addOption("port", true, "Port of bookie to benchmark (default 3181)");
    options.addOption("zookeeper", true, "Zookeeper ensemble, (default \"localhost:2181\")");
    options.addOption("size", true, "Size of message to send, in bytes (default 1024)");
    options.addOption("warmupCount", true, "Number of messages in warmup phase (default 999)");
    options.addOption("latencyCount", true, "Number of messages in latency phase (default 5000)");
    options.addOption("throughputCount", true, "Number of messages in throughput phase (default 50000)");
    options.addOption("help", false, "This message");
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("help") || !cmd.hasOption("host")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("BenchBookie <options>", options);
        System.exit(-1);
    }
    String addr = cmd.getOptionValue("host");
    int port = Integer.parseInt(cmd.getOptionValue("port", "3181"));
    int size = Integer.parseInt(cmd.getOptionValue("size", "1024"));
    String servers = cmd.getOptionValue("zookeeper", "localhost:2181");
    int warmUpCount = Integer.parseInt(cmd.getOptionValue("warmupCount", "999"));
    int latencyCount = Integer.parseInt(cmd.getOptionValue("latencyCount", "5000"));
    int throughputCount = Integer.parseInt(cmd.getOptionValue("throughputCount", "50000"));
    EventLoopGroup eventLoop;
    if (SystemUtils.IS_OS_LINUX) {
        try {
            eventLoop = new EpollEventLoopGroup();
        } catch (Throwable t) {
            LOG.warn("Could not use Netty Epoll event loop for benchmark {}", t.getMessage());
            eventLoop = new NioEventLoopGroup();
        }
    } else {
        eventLoop = new NioEventLoopGroup();
    }
    OrderedExecutor executor = OrderedExecutor.newBuilder().name("BenchBookieClientScheduler").numThreads(1).build();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("BookKeeperClientScheduler"));
    ClientConfiguration conf = new ClientConfiguration();
    BookieClient bc = new BookieClient(conf, eventLoop, executor, scheduler, NullStatsLogger.INSTANCE);
    LatencyCallback lc = new LatencyCallback();
    ThroughputCallback tc = new ThroughputCallback();
    long ledger = getValidLedgerId(servers);
    for (long entry = 0; entry < warmUpCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), tc, null, BookieProtocol.FLAG_NONE);
    }
    LOG.info("Waiting for warmup");
    tc.waitFor(warmUpCount);
    ledger = getValidLedgerId(servers);
    LOG.info("Benchmarking latency");
    long startTime = System.nanoTime();
    for (long entry = 0; entry < latencyCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        lc.resetComplete();
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), lc, null, BookieProtocol.FLAG_NONE);
        lc.waitForComplete();
    }
    long endTime = System.nanoTime();
    LOG.info("Latency: " + (((double) (endTime - startTime)) / ((double) latencyCount)) / 1000000.0);
    ledger = getValidLedgerId(servers);
    LOG.info("Benchmarking throughput");
    startTime = System.currentTimeMillis();
    tc = new ThroughputCallback();
    for (long entry = 0; entry < throughputCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), tc, null, BookieProtocol.FLAG_NONE);
    }
    tc.waitFor(throughputCount);
    endTime = System.currentTimeMillis();
    LOG.info("Throughput: " + ((long) throughputCount) * 1000 / (endTime - startTime));
    bc.close();
    scheduler.shutdown();
    eventLoop.shutdownGracefully();
    executor.shutdown();
}
Also used : Options(org.apache.commons.cli.Options) BookieClient(org.apache.bookkeeper.proto.BookieClient) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PosixParser(org.apache.commons.cli.PosixParser) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) ByteBuf(io.netty.buffer.ByteBuf) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) CommandLine(org.apache.commons.cli.CommandLine) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) CommandLineParser(org.apache.commons.cli.CommandLineParser) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Example 5 with BookieClient

use of org.apache.bookkeeper.proto.BookieClient in project bookkeeper by apache.

the class BookieInfoReader method getReadWriteBookieInfo.

/**
 * Performs scan described by instanceState using the cached bookie information
 * in bookieInfoMap.
 */
synchronized void getReadWriteBookieInfo() {
    State queuedType = instanceState.getAndClearQueuedType();
    Collection<BookieSocketAddress> toScan;
    if (queuedType == State.FULL) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Doing full scan");
        }
        toScan = bookieInfoMap.getFullScanTargets();
    } else if (queuedType == State.PARTIAL) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Doing partial scan");
        }
        toScan = bookieInfoMap.getPartialScanTargets();
    } else {
        if (LOG.isErrorEnabled()) {
            LOG.error("Invalid state, queuedType cannot be UNQUEUED in getReadWriteBookieInfo");
        }
        assert (queuedType != State.UNQUEUED);
        return;
    }
    BookieClient bkc = bk.getBookieClient();
    final long requested = BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE;
    totalSent = 0;
    completedCnt = 0;
    errorCnt = 0;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Getting bookie info for: {}", toScan);
    }
    for (BookieSocketAddress b : toScan) {
        bkc.getBookieInfo(b, requested, new GetBookieInfoCallback() {

            void processReadInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
                synchronized (BookieInfoReader.this) {
                    BookieSocketAddress b = (BookieSocketAddress) ctx;
                    if (rc != BKException.Code.OK) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("Reading bookie info from bookie {} failed due to {}", b, BKException.codeLogger(rc));
                        }
                        // We reread bookies missing from the map each time, so remove to ensure
                        // we get to it on the next scan
                        bookieInfoMap.clearInfo(b);
                        errorCnt++;
                    } else {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Bookie Info for bookie {} is {}", b, bInfo);
                        }
                        bookieInfoMap.gotInfo(b, bInfo);
                    }
                    completedCnt++;
                    if (totalSent == completedCnt) {
                        onExit();
                    }
                }
            }

            @Override
            public void getBookieInfoComplete(final int rc, final BookieInfo bInfo, final Object ctx) {
                scheduler.submit(new Runnable() {

                    @Override
                    public void run() {
                        processReadInfoComplete(rc, bInfo, ctx);
                    }
                });
            }
        }, b);
        totalSent++;
    }
    if (totalSent == 0) {
        onExit();
    }
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) WeightedObject(org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject)

Aggregations

BookieClient (org.apache.bookkeeper.proto.BookieClient)8 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)7 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)6 GetBookieInfoCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 BookieInfo (org.apache.bookkeeper.client.BookieInfoReader.BookieInfo)2 WeightedObject (org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject)2 ByteBuf (io.netty.buffer.ByteBuf)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 OrderedExecutor (org.apache.bookkeeper.common.util.OrderedExecutor)1 Feature (org.apache.bookkeeper.feature.Feature)1