Search in sources :

Example 1 with GetBookieInfoCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback 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 2 with GetBookieInfoCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback 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)

Example 3 with GetBookieInfoCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback 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 4 with GetBookieInfoCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback 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

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)4 BookieClient (org.apache.bookkeeper.proto.BookieClient)4 GetBookieInfoCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 BookieInfo (org.apache.bookkeeper.client.BookieInfoReader.BookieInfo)2 WeightedObject (org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 Test (org.junit.Test)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1