use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class DLAuditor method calculateLedgerSpaceUsage.
private long calculateLedgerSpaceUsage(BookKeeperClient bkc, final ExecutorService executorService) throws IOException {
final AtomicLong totalBytes = new AtomicLong(0);
final AtomicLong totalEntries = new AtomicLong(0);
final AtomicLong numLedgers = new AtomicLong(0);
LedgerManager lm = BookKeeperAccessor.getLedgerManager(bkc.get());
final CompletableFuture<Void> doneFuture = FutureUtils.createFuture();
final BookKeeper bk = bkc.get();
BookkeeperInternalCallbacks.Processor<Long> collector = new BookkeeperInternalCallbacks.Processor<Long>() {
@Override
public void process(final Long lid, final AsyncCallback.VoidCallback cb) {
numLedgers.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
bk.asyncOpenLedgerNoRecovery(lid, BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8), new org.apache.bookkeeper.client.AsyncCallback.OpenCallback() {
@Override
public void openComplete(int rc, LedgerHandle lh, Object ctx) {
final int cbRc;
if (BKException.Code.OK == rc) {
totalBytes.addAndGet(lh.getLength());
totalEntries.addAndGet(lh.getLastAddConfirmed() + 1);
cbRc = rc;
} else {
cbRc = BKException.Code.ZKException;
}
executorService.submit(new Runnable() {
@Override
public void run() {
cb.processResult(cbRc, null, null);
}
});
}
}, null);
}
});
}
};
AsyncCallback.VoidCallback finalCb = new AsyncCallback.VoidCallback() {
@Override
public void processResult(int rc, String path, Object ctx) {
if (BKException.Code.OK == rc) {
doneFuture.complete(null);
} else {
doneFuture.completeExceptionally(BKException.create(rc));
}
}
};
lm.asyncProcessLedgers(collector, finalCb, null, BKException.Code.OK, BKException.Code.ZKException);
try {
doneFuture.get();
logger.info("calculated {} ledgers\n\ttotal bytes = {}\n\ttotal entries = {}", new Object[] { numLedgers.get(), totalBytes.get(), totalEntries.get() });
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DLInterruptedException("Interrupted on calculating ledger space : ", e);
} catch (ExecutionException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) (e.getCause());
} else {
throw new IOException("Failed to calculate ledger space : ", e.getCause());
}
}
return totalBytes.get();
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class BKLogSegmentEntryStore method openRandomAccessReader.
@Override
public CompletableFuture<LogSegmentRandomAccessEntryReader> openRandomAccessReader(final LogSegmentMetadata segment, final boolean fence) {
final BookKeeper bk;
try {
bk = this.bkc.get();
} catch (IOException e) {
return FutureUtils.exception(e);
}
final CompletableFuture<LogSegmentRandomAccessEntryReader> openPromise = new CompletableFuture<LogSegmentRandomAccessEntryReader>();
AsyncCallback.OpenCallback openCallback = new AsyncCallback.OpenCallback() {
@Override
public void openComplete(int rc, LedgerHandle lh, Object ctx) {
if (BKException.Code.OK != rc) {
FutureUtils.completeExceptionally(openPromise, new BKTransmitException("Failed to open ledger handle for log segment " + segment, rc));
return;
}
LogSegmentRandomAccessEntryReader reader = new BKLogSegmentRandomAccessEntryReader(segment, lh, conf);
FutureUtils.complete(openPromise, reader);
}
};
if (segment.isInProgress() && !fence) {
bk.asyncOpenLedgerNoRecovery(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, openCallback, null);
} else {
bk.asyncOpenLedger(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, openCallback, null);
}
return openPromise;
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class BKLogSegmentEntryStore method deleteLogSegment.
@Override
public CompletableFuture<LogSegmentMetadata> deleteLogSegment(LogSegmentMetadata segment) {
DeleteLogSegmentRequest request = new DeleteLogSegmentRequest(segment);
BookKeeper bk;
try {
bk = this.bkc.get();
} catch (IOException e) {
return FutureUtils.exception(e);
}
bk.asyncDeleteLedger(segment.getLogSegmentId(), this, request);
return request.deletePromise;
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class BKLogSegmentEntryStore method openReader.
//
// Readers
//
@Override
public CompletableFuture<LogSegmentEntryReader> openReader(LogSegmentMetadata segment, long startEntryId) {
BookKeeper bk;
try {
bk = this.bkc.get();
} catch (IOException e) {
return FutureUtils.exception(e);
}
OpenReaderRequest request = new OpenReaderRequest(segment, startEntryId);
if (segment.isInProgress()) {
bk.asyncOpenLedgerNoRecovery(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, this, request);
} else {
bk.asyncOpenLedger(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, this, request);
}
return request.openPromise;
}
use of org.apache.bookkeeper.client.BookKeeper in project bookkeeper by apache.
the class TestHttpService method testReadLedgerEntryService.
@Test
public void testReadLedgerEntryService() throws Exception {
baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
int numLedgers = 1;
int numMsgs = 100;
LedgerHandle[] lh = new LedgerHandle[numLedgers];
// create ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i] = bkc.createLedger(digestType, "".getBytes());
}
String content = "Apache BookKeeper is cool!";
// add entries
for (int i = 0; i < numMsgs; i++) {
for (int j = 0; j < numLedgers; j++) {
lh[j].addEntry(content.getBytes());
}
}
// close ledgers
for (int i = 0; i < numLedgers; i++) {
lh[i].close();
}
HttpEndpointService readLedgerEntryService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.READ_LEDGER_ENTRY);
// 1, null parameters of GET, should return NOT_FOUND
HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
HttpServiceResponse response1 = readLedgerEntryService.handle(request1);
assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
// 2, parameters for GET first ledger, should return OK
// no start/end entry id, so return all the 100 entries.
HashMap<String, String> params = Maps.newHashMap();
Long ledgerId = Long.valueOf(lh[0].getId());
params.put("ledger_id", ledgerId.toString());
HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, params);
HttpServiceResponse response2 = readLedgerEntryService.handle(request2);
assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
@SuppressWarnings("unchecked") HashMap<String, String> respBody = JsonUtil.fromJson(response2.getBody(), HashMap.class);
// default return all the entries. so should have 100 entries return
assertEquals(100, respBody.size());
// 2, parameters for GET first ledger, should return OK
// start_entry_id=1, end_entry_id=77, so return 77 entries.
HashMap<String, String> params3 = Maps.newHashMap();
params3.put("ledger_id", ledgerId.toString());
params3.put("start_entry_id", "1");
params3.put("end_entry_id", "77");
HttpServiceRequest request3 = new HttpServiceRequest(null, HttpServer.Method.GET, params3);
HttpServiceResponse response3 = readLedgerEntryService.handle(request3);
assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
@SuppressWarnings("unchecked") HashMap<String, String> respBody3 = JsonUtil.fromJson(response3.getBody(), HashMap.class);
assertEquals(77, respBody3.size());
// Verify the entry content that we got.
assertTrue(respBody3.get("17").equals(content));
}
Aggregations