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