Search in sources :

Example 11 with BookKeeper

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();
}
Also used : LedgerManager(org.apache.bookkeeper.meta.LedgerManager) AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) ExecutionException(java.util.concurrent.ExecutionException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) BookkeeperInternalCallbacks(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks) AtomicLong(java.util.concurrent.atomic.AtomicLong) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException)

Example 12 with BookKeeper

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) AsyncCallback(org.apache.bookkeeper.client.AsyncCallback) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) BookKeeper(org.apache.bookkeeper.client.BookKeeper) LogSegmentRandomAccessEntryReader(org.apache.distributedlog.logsegment.LogSegmentRandomAccessEntryReader) IOException(java.io.IOException)

Example 13 with BookKeeper

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;
}
Also used : BookKeeper(org.apache.bookkeeper.client.BookKeeper) IOException(java.io.IOException)

Example 14 with BookKeeper

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;
}
Also used : BookKeeper(org.apache.bookkeeper.client.BookKeeper) IOException(java.io.IOException)

Example 15 with BookKeeper

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));
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Aggregations

BookKeeper (org.apache.bookkeeper.client.BookKeeper)76 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)48 Test (org.junit.Test)25 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)24 BKException (org.apache.bookkeeper.client.BKException)18 IOException (java.io.IOException)17 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)12 List (java.util.List)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)10 ArrayList (java.util.ArrayList)9 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)7 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)7 BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)6 TableSpaceManager (herddb.core.TableSpaceManager)6 DataScanner (herddb.model.DataScanner)6 StatementExecutionException (herddb.model.StatementExecutionException)6