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);
}
}
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;
}
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);
}
Aggregations