Search in sources :

Example 1 with LedgerEntries

use of org.apache.bookkeeper.client.api.LedgerEntries in project herddb by diennea.

the class BookKeeperDataStorageManager method readIndexPage.

@Override
public <X> X readIndexPage(String tableSpace, String indexName, Long pageId, DataReader<X> reader) throws DataStorageManagerException {
    long _start = System.currentTimeMillis();
    TableSpacePagesMapping tableSpacePagesMapping = getTableSpacePagesMapping(tableSpace);
    Long ledgerId = tableSpacePagesMapping.getIndexPagesMapping(indexName).getLedgerIdForPage(pageId);
    if (ledgerId == null) {
        throw new DataPageDoesNotExistException("No such page for index : " + tableSpace + "_" + indexName + "." + pageId);
    }
    byte[] data;
    try (ReadHandle read = FutureUtils.result(bk.getBookKeeper().newOpenLedgerOp().withLedgerId(ledgerId).withPassword(EMPTY_ARRAY).execute(), BKException.HANDLER)) {
        try (LedgerEntries entries = read.readUnconfirmed(0, 0)) {
            data = entries.getEntry(0).getEntryBytes();
        }
        X result = readIndexPage(data, reader);
        long _stop = System.currentTimeMillis();
        long delta = _stop - _start;
        LOGGER.log(Level.FINE, "readIndexPage {0}.{1} {2} ms", new Object[] { tableSpace, indexName, delta + "" });
        indexPageReads.registerSuccessfulEvent(delta, TimeUnit.MILLISECONDS);
        return result;
    } catch (BKException.BKNoSuchLedgerExistsException err) {
        throw new DataStorageManagerException(err);
    } catch (BKException.BKNoSuchLedgerExistsOnMetadataServerException err) {
        throw new DataStorageManagerException(err);
    } catch (org.apache.bookkeeper.client.api.BKException err) {
        throw new DataStorageManagerException(err);
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new DataStorageManagerException(err);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) IOException(java.io.IOException) DataPageDoesNotExistException(herddb.storage.DataPageDoesNotExistException) ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) BKNoSuchLedgerExistsOnMetadataServerException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsOnMetadataServerException) BKException(org.apache.bookkeeper.client.BKException)

Example 2 with LedgerEntries

use of org.apache.bookkeeper.client.api.LedgerEntries in project herddb by diennea.

the class BookkeeperCommitLogManager method scanRawLedger.

public static void scanRawLedger(long ledgerId, long fromId, long toId, herddb.client.ClientConfiguration clientConfiguration, ZookeeperMetadataStorageManager metadataStorageManager, Consumer<LogEntryWithSequenceNumber> consumer) throws Exception {
    ClientConfiguration config = new ClientConfiguration();
    config.setZkServers(metadataStorageManager.getZkAddress());
    config.setZkTimeout(metadataStorageManager.getZkSessionTimeout());
    config.setZkLedgersRootPath(clientConfiguration.getString(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH, ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH_DEFAULT));
    config.setEnableParallelRecoveryRead(true);
    config.setEnableDigestTypeAutodetection(true);
    try (org.apache.bookkeeper.client.api.BookKeeper bookKeeper = org.apache.bookkeeper.client.api.BookKeeper.newBuilder(config).build()) {
        try (ReadHandle lh = bookKeeper.newOpenLedgerOp().withRecovery(false).withLedgerId(ledgerId).withPassword(BookkeeperCommitLog.SHARED_SECRET.getBytes(StandardCharsets.UTF_8)).execute().get()) {
            long lastAddConfirmed = lh.readLastAddConfirmed();
            if (toId < 0) {
                toId = lastAddConfirmed;
            }
            LOG.log(Level.INFO, "Scanning Ledger {0} from {1} to {2} LAC {3}", new Object[] { ledgerId, fromId, toId, lastAddConfirmed });
            for (long id = fromId; id <= toId; id++) {
                try (LedgerEntries entries = lh.readUnconfirmed(id, id)) {
                    LedgerEntry entry = entries.getEntry(id);
                    LogEntry lEntry = LogEntry.deserialize(entry.getEntryBytes());
                    LogEntryWithSequenceNumber e = new LogEntryWithSequenceNumber(new LogSequenceNumber(ledgerId, id), lEntry);
                    consumer.accept(e);
                }
            }
        }
    }
}
Also used : ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) LedgerEntry(org.apache.bookkeeper.client.api.LedgerEntry) LogSequenceNumber(herddb.log.LogSequenceNumber) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) LogEntry(herddb.log.LogEntry)

Example 3 with LedgerEntries

use of org.apache.bookkeeper.client.api.LedgerEntries in project herddb by diennea.

the class BookkeeperCommitLog method followTheLeader.

@Override
public void followTheLeader(LogSequenceNumber lastPosition, EntryAcceptor consumer, FollowerContext context) throws LogNotAvailableException {
    if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.finer(tableSpaceDescription() + " followTheLeader lastPosition:" + lastPosition);
    }
    BKFollowerContext fContext = (BKFollowerContext) context;
    try {
        fContext.ensureOpenReader(lastPosition);
        if (fContext.currentLedger == null) {
            // no data to read
            if (LOGGER.isLoggable(Level.FINER)) {
                LOGGER.finer(tableSpaceDescription() + " no more data to read for now");
            }
            return;
        }
        long nextEntry = fContext.nextEntryToRead;
        long lastAddConfirmed = fContext.currentLedger.getLastAddConfirmed();
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(tableSpaceDescription() + " next entry to read " + nextEntry + " from ledger " + fContext.currentLedger.getId() + " lastAddConfiremd " + lastAddConfirmed);
        }
        if (lastAddConfirmed < nextEntry) {
            if (LOGGER.isLoggable(Level.FINER)) {
                LOGGER.finer(tableSpaceDescription() + " ledger not closed but there is nothing to read by now");
            }
            return;
        }
        ReadHandle lh = fContext.currentLedger;
        try (LastConfirmedAndEntry entryAndLac = lh.readLastAddConfirmedAndEntry(nextEntry, LONG_POLL_TIMEOUT, false)) {
            if (entryAndLac.hasEntry()) {
                org.apache.bookkeeper.client.api.LedgerEntry e = entryAndLac.getEntry();
                boolean canContinue = acceptEntryForFollower(e, consumer);
                if (!canContinue) {
                    LOGGER.log(Level.INFO, "exit follower {0}", tableSpaceDescription());
                    return;
                }
                long startEntry = nextEntry + 1;
                long endEntry = entryAndLac.getLastAddConfirmed();
                if (startEntry > endEntry) {
                    return;
                }
                // to be read then read them
                if (endEntry - startEntry > MAX_ENTRY_TO_TAIL) {
                    // put a bound on the max entries to read per round
                    endEntry = startEntry + MAX_ENTRY_TO_TAIL;
                }
                try (LedgerEntries entries = lh.read(startEntry, endEntry)) {
                    for (org.apache.bookkeeper.client.api.LedgerEntry ee : entries) {
                        acceptEntryForFollower(ee, consumer);
                    }
                }
            }
        }
    } catch (BKClientClosedException err) {
        LOGGER.log(Level.FINE, "stop following " + tableSpaceDescription(), err);
    } catch (org.apache.bookkeeper.client.api.BKException err) {
        LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal BK error", err);
        throw new LogNotAvailableException(err);
    } catch (InterruptedException err) {
        LOGGER.log(Level.SEVERE, tableSpaceDescription() + " interrupted", err);
        Thread.currentThread().interrupt();
        throw new LogNotAvailableException(err);
    } catch (LogNotAvailableException err) {
        LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal error", err);
        throw err;
    } catch (Exception err) {
        LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal error", err);
        throw new LogNotAvailableException(err);
    }
}
Also used : LastConfirmedAndEntry(org.apache.bookkeeper.client.api.LastConfirmedAndEntry) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) LogNotAvailableException(herddb.log.LogNotAvailableException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) BKException(org.apache.bookkeeper.client.BKException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 4 with LedgerEntries

use of org.apache.bookkeeper.client.api.LedgerEntries in project bookkeeper by apache.

the class BookieWriteLedgerTest method readEntries.

private void readEntries(ReadHandle reader, List<ByteBuf> entries) throws Exception {
    assertEquals("Not enough entries in ledger " + reader.getId(), reader.getLastAddConfirmed(), entries.size() - 1);
    try (LedgerEntries readEntries = reader.read(0, reader.getLastAddConfirmed())) {
        int i = 0;
        for (org.apache.bookkeeper.client.api.LedgerEntry e : readEntries) {
            int entryId = i++;
            ByteBuf origEntry = entries.get(entryId);
            ByteBuf readEntry = e.getEntryBuffer();
            assertEquals("Unexpected contents in " + reader.getId() + ":" + entryId, origEntry, readEntry);
        }
    }
}
Also used : LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with LedgerEntries

use of org.apache.bookkeeper.client.api.LedgerEntries in project herddb by diennea.

the class BookKeeperDataStorageManager method readPage.

@Override
public List<Record> readPage(String tableSpace, String tableName, Long pageId) throws DataStorageManagerException {
    long _start = System.currentTimeMillis();
    TableSpacePagesMapping tableSpacePagesMapping = getTableSpacePagesMapping(tableSpace);
    Long ledgerId = tableSpacePagesMapping.getTablePagesMapping(tableName).getLedgerIdForPage(pageId);
    if (ledgerId == null) {
        throw new DataPageDoesNotExistException("No such page: " + tableSpace + "_" + tableName + "." + pageId);
    }
    byte[] data;
    try (ReadHandle read = FutureUtils.result(bk.getBookKeeper().newOpenLedgerOp().withLedgerId(ledgerId).withPassword(EMPTY_ARRAY).execute(), BKException.HANDLER)) {
        try (LedgerEntries entries = read.readUnconfirmed(0, 0)) {
            data = entries.getEntry(0).getEntryBytes();
        }
        List<Record> result = rawReadDataPage(data);
        long _stop = System.currentTimeMillis();
        long delta = _stop - _start;
        LOGGER.log(Level.FINE, "readPage {0}.{1} {2} ms", new Object[] { tableSpace, tableName, delta + "" });
        dataPageReads.registerSuccessfulEvent(delta, TimeUnit.MILLISECONDS);
        return result;
    } catch (BKException.BKNoSuchLedgerExistsException err) {
        throw new DataStorageManagerException(err);
    } catch (BKException.BKNoSuchLedgerExistsOnMetadataServerException err) {
        throw new DataStorageManagerException(err);
    } catch (org.apache.bookkeeper.client.api.BKException err) {
        throw new DataStorageManagerException(err);
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new DataStorageManagerException(err);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) IOException(java.io.IOException) DataPageDoesNotExistException(herddb.storage.DataPageDoesNotExistException) ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) BKNoSuchLedgerExistsOnMetadataServerException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsOnMetadataServerException) Record(herddb.model.Record) BKException(org.apache.bookkeeper.client.BKException)

Aggregations

LedgerEntries (org.apache.bookkeeper.client.api.LedgerEntries)6 ReadHandle (org.apache.bookkeeper.client.api.ReadHandle)5 IOException (java.io.IOException)4 BKException (org.apache.bookkeeper.client.BKException)4 FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)2 LogEntry (herddb.log.LogEntry)2 LogNotAvailableException (herddb.log.LogNotAvailableException)2 LogSequenceNumber (herddb.log.LogSequenceNumber)2 DataPageDoesNotExistException (herddb.storage.DataPageDoesNotExistException)2 DataStorageManagerException (herddb.storage.DataStorageManagerException)2 CompletionException (java.util.concurrent.CompletionException)2 BKClientClosedException (org.apache.bookkeeper.client.BKException.BKClientClosedException)2 BKNoSuchLedgerExistsOnMetadataServerException (org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsOnMetadataServerException)2 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)2 Record (herddb.model.Record)1 ByteBuf (io.netty.buffer.ByteBuf)1 LastConfirmedAndEntry (org.apache.bookkeeper.client.api.LastConfirmedAndEntry)1 LedgerEntry (org.apache.bookkeeper.client.api.LedgerEntry)1 LedgerMetadata (org.apache.bookkeeper.client.api.LedgerMetadata)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1