Search in sources :

Example 6 with BookieClient

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

the class BookieClientTest method testNoLedger.

@Test
public void testNoLedger() throws Exception {
    ResultStruct arc = new ResultStruct();
    BookieSocketAddress addr = bs.getLocalAddress();
    BookieClient bc = new BookieClient(new ClientConfiguration(), eventLoopGroup, executor, scheduler, NullStatsLogger.INSTANCE);
    synchronized (arc) {
        bc.readEntry(addr, 2, 13, recb, arc, BookieProtocol.FLAG_NONE);
        arc.wait(1000);
        assertEquals(BKException.Code.NoSuchLedgerExistsException, arc.rc);
    }
}
Also used : BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 7 with BookieClient

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

the class BookieInfoReader method getBookieInfo.

Map<BookieSocketAddress, BookieInfo> getBookieInfo() throws BKException, InterruptedException {
    BookieClient bkc = bk.getBookieClient();
    final AtomicInteger totalSent = new AtomicInteger();
    final AtomicInteger totalCompleted = new AtomicInteger();
    final ConcurrentMap<BookieSocketAddress, BookieInfo> map = new ConcurrentHashMap<BookieSocketAddress, BookieInfo>();
    final CountDownLatch latch = new CountDownLatch(1);
    long requested = BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE;
    Collection<BookieSocketAddress> bookies;
    bookies = bk.bookieWatcher.getBookies();
    bookies.addAll(bk.bookieWatcher.getReadOnlyBookies());
    totalSent.set(bookies.size());
    for (BookieSocketAddress b : bookies) {
        bkc.getBookieInfo(b, requested, new GetBookieInfoCallback() {

            @Override
            public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
                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));
                    }
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Free disk space on bookie {} is {}.", b, bInfo.getFreeDiskSpace());
                    }
                    map.put(b, bInfo);
                }
                if (totalCompleted.incrementAndGet() == totalSent.get()) {
                    latch.countDown();
                }
            }
        }, b);
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Received InterruptedException ", e);
        throw e;
    }
    return map;
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) WeightedObject(org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with BookieClient

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

the class TestGetBookieInfoTimeout method testGetBookieInfoTimeout.

@Test
public void testGetBookieInfoTimeout() throws Exception {
    // connect to the bookies and create a ledger
    LedgerHandle writelh = bkc.createLedger(3, 3, digestType, "testPasswd".getBytes());
    String tmp = "Foobar";
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        writelh.addEntry(tmp.getBytes());
    }
    // set timeout for getBookieInfo to be 2 secs and cause one of the bookies to go to sleep for 3X that time
    ClientConfiguration cConf = new ClientConfiguration();
    cConf.setGetBookieInfoTimeout(2);
    final BookieSocketAddress bookieToSleep = writelh.getLedgerMetadata().getEnsemble(0).get(0);
    int sleeptime = cConf.getBookieInfoTimeout() * 3;
    CountDownLatch latch = sleepBookie(bookieToSleep, sleeptime);
    latch.await();
    // try to get bookie info from the sleeping bookie. It should fail with timeout error
    BookieSocketAddress addr = new BookieSocketAddress(bookieToSleep.getSocketAddress().getHostString(), bookieToSleep.getPort());
    BookieClient bc = new BookieClient(cConf, eventLoopGroup, 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;

        @SuppressWarnings("unused")
        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();
    LOG.debug("Return code: " + obj.rc);
    assertTrue("GetBookieInfo failed with unexpected error code: " + obj.rc, obj.rc == Code.TimeoutException);
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) CountDownLatch(java.util.concurrent.CountDownLatch) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

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