Search in sources :

Example 11 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.

the class BookieReadWriteTest method testMultiLedger.

@Test
public void testMultiLedger() throws IOException {
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        lh2 = bkc.createLedger(digestType, ledgerPassword);
        long ledgerId = lh.getId();
        long ledgerId2 = lh2.getId();
        final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite * 2);
        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
        // bkc.initMessageDigest("SHA1");
        LOG.info("Ledger ID 1: " + lh.getId() + ", Ledger ID 2: " + lh2.getId());
        for (int i = 0; i < numEntriesToWrite; i++) {
            lh.asyncAddEntry(new byte[0], new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rc2);
                    completeLatch.countDown();
                }
            }, null);
            lh2.asyncAddEntry(new byte[0], new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rc2);
                    completeLatch.countDown();
                }
            }, null);
        }
        completeLatch.await();
        if (rc.get() != BKException.Code.OK) {
            throw BKException.create(rc.get());
        }
        lh.close();
        lh2.close();
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        lh2 = bkc.openLedger(ledgerId2, digestType, ledgerPassword);
        LOG.debug("Number of entries written: " + lh.getLastAddConfirmed() + ", " + lh2.getLastAddConfirmed());
        assertTrue("Verifying number of entries written lh (" + lh.getLastAddConfirmed() + ")", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
        assertTrue("Verifying number of entries written lh2 (" + lh2.getLastAddConfirmed() + ")", lh2.getLastAddConfirmed() == (numEntriesToWrite - 1));
        Enumeration<LedgerEntry> ls = lh.readEntries(0, numEntriesToWrite - 1);
        int i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            LOG.debug("Length of result: " + result.capacity());
            assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
        }
        lh.close();
        ls = lh2.readEntries(0, numEntriesToWrite - 1);
        i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            LOG.debug("Length of result: " + result.capacity());
            assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
        }
        lh2.close();
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.

the class LedgerInputStream method refill.

/**
 * refill the buffer, we need to read more bytes.
 *
 * @return if we can refill or not
 */
private synchronized boolean refill() throws IOException {
    bytebuff.clear();
    if (!ledgerSeq.hasMoreElements() && lastEntry >= lh.getLastAddConfirmed()) {
        return false;
    }
    if (!ledgerSeq.hasMoreElements()) {
        // do refill
        long last = Math.min(lastEntry + increment, lh.getLastAddConfirmed());
        try {
            ledgerSeq = lh.readEntries(lastEntry + 1, last);
        } catch (BKException bk) {
            IOException ie = new IOException(bk.getMessage());
            ie.initCause(bk);
            throw ie;
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        lastEntry = last;
    }
    LedgerEntry le = ledgerSeq.nextElement();
    bbytes = le.getEntry();
    bytebuff = ByteBuffer.wrap(bbytes);
    return true;
}
Also used : LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException)

Example 13 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.

the class ReadLedgerEntryService method handle.

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    Map<String, String> params = request.getParams();
    if (HttpServer.Method.GET == request.getMethod() && (params != null) && params.containsKey("ledger_id")) {
        Long ledgerId = Long.parseLong(params.get("ledger_id"));
        Long startEntryId = 0L;
        Long endEntryId = -1L;
        if (params.containsKey("start_entry_id")) {
            startEntryId = Long.parseLong(params.get("start_entry_id"));
        }
        if (params.containsKey("end_entry_id")) {
            endEntryId = Long.parseLong(params.get("end_entry_id"));
        }
        // output <entryid: entry_content>
        Map<String, String> output = Maps.newHashMap();
        // Page index should start from 1;
        Integer pageIndex = params.containsKey("page") ? Integer.parseInt(params.get("page")) : -1;
        if (pageIndex > 0) {
            // start and end ledger index for wanted page.
            Long startIndexInPage = (pageIndex - 1) * ENTRIES_PER_PAE;
            Long endIndexInPage = startIndexInPage + ENTRIES_PER_PAE - 1;
            if ((startEntryId == 0L) || (startEntryId < startIndexInPage)) {
                startEntryId = startIndexInPage;
            }
            if ((endEntryId == -1L) || (endEntryId > endIndexInPage)) {
                endEntryId = endIndexInPage;
            }
            output.put("Entries for page: ", pageIndex.toString());
        }
        if (endEntryId != -1L && startEntryId > endEntryId) {
            response.setCode(HttpServer.StatusCode.INTERNAL_ERROR);
            response.setBody("parameter for start_entry_id: " + startEntryId + " and end_entry_id: " + endEntryId + " conflict with page=" + pageIndex);
            return response;
        }
        Iterator<LedgerEntry> entries = bka.readEntries(ledgerId, startEntryId, endEntryId).iterator();
        while (entries.hasNext()) {
            LedgerEntry entry = entries.next();
            output.put(Long.valueOf(entry.getEntryId()).toString(), new String(entry.getEntry(), US_ASCII));
        }
        String jsonResponse = JsonUtil.toJson(output);
        LOG.debug("output body:" + jsonResponse);
        response.setBody(jsonResponse);
        response.setCode(HttpServer.StatusCode.OK);
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be GET method, with ledger_id provided");
        return response;
    }
}
Also used : LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 14 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.

the class BenchReadThroughputLatency method readLedger.

private static void readLedger(ClientConfiguration conf, long ledgerId, byte[] passwd) {
    LOG.info("Reading ledger {}", ledgerId);
    BookKeeper bk = null;
    long time = 0;
    long entriesRead = 0;
    long lastRead = 0;
    int nochange = 0;
    long absoluteLimit = 5000000;
    LedgerHandle lh = null;
    try {
        bk = new BookKeeper(conf);
        while (true) {
            lh = bk.openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, passwd);
            long lastConfirmed = Math.min(lh.getLastAddConfirmed(), absoluteLimit);
            if (lastConfirmed == lastRead) {
                nochange++;
                if (nochange == 10) {
                    break;
                } else {
                    Thread.sleep(1000);
                    continue;
                }
            } else {
                nochange = 0;
            }
            long starttime = System.nanoTime();
            while (lastRead < lastConfirmed) {
                long nextLimit = lastRead + 100000;
                long readTo = Math.min(nextLimit, lastConfirmed);
                Enumeration<LedgerEntry> entries = lh.readEntries(lastRead + 1, readTo);
                lastRead = readTo;
                while (entries.hasMoreElements()) {
                    LedgerEntry e = entries.nextElement();
                    entriesRead++;
                    if ((entriesRead % 10000) == 0) {
                        LOG.info("{} entries read", entriesRead);
                    }
                }
            }
            long endtime = System.nanoTime();
            time += endtime - starttime;
            lh.close();
            lh = null;
            Thread.sleep(1000);
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        LOG.error("Exception in reader", e);
    } finally {
        LOG.info("Read {} in {}ms", entriesRead, time / 1000 / 1000);
        try {
            if (lh != null) {
                lh.close();
            }
            if (bk != null) {
                bk.close();
            }
        } catch (Exception e) {
            LOG.error("Exception closing stuff", e);
        }
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BookKeeper(org.apache.bookkeeper.client.BookKeeper)

Example 15 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.

the class BookieJournalNoSyncTest method testWriteToJournal.

@Test
public void testWriteToJournal() throws Exception {
    LedgerHandle lh = bkc.createLedger(1, 1, DigestType.CRC32, new byte[0]);
    int n = 10;
    long ledgerId = lh.getId();
    for (int i = 0; i < n; i++) {
        lh.addEntry(("entry-" + i).getBytes());
    }
    restartBookies();
    LedgerHandle readLh = bkc.openLedger(ledgerId, DigestType.CRC32, new byte[0]);
    Enumeration<LedgerEntry> entries = readLh.readEntries(0, n - 1);
    for (int i = 0; i < n; i++) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("entry-" + i, new String(entry.getEntry()));
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.junit.Test)

Aggregations

LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)54 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)38 BKException (org.apache.bookkeeper.client.BKException)21 Test (org.junit.Test)20 BookKeeper (org.apache.bookkeeper.client.BookKeeper)10 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)7 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Bookie (org.apache.bookkeeper.bookie.Bookie)4 LedgerDirsManager (org.apache.bookkeeper.bookie.LedgerDirsManager)4 MLDataFormats (org.apache.bookkeeper.mledger.proto.MLDataFormats)4 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)3 Versioned (org.apache.bookkeeper.versioning.Versioned)3