use of herddb.core.AbstractTableManager.TableCheckpoint in project herddb by diennea.
the class TableSpaceManager method checkpoint.
TableSpaceCheckpoint checkpoint(boolean full, boolean pin) throws DataStorageManagerException, LogNotAvailableException {
if (virtual) {
return null;
}
long _start = System.currentTimeMillis();
LogSequenceNumber logSequenceNumber;
LogSequenceNumber _logSequenceNumber;
List<PostCheckpointAction> actions = new ArrayList<>();
Map<String, LogSequenceNumber> checkpoints = new HashMap<>();
generalLock.writeLock().lock();
try {
logSequenceNumber = log.getLastSequenceNumber();
if (logSequenceNumber.isStartOfTime()) {
LOGGER.log(Level.INFO, nodeId + " checkpoint " + tableSpaceName + " at " + logSequenceNumber + ". skipped (no write ever issued to log)");
return new TableSpaceCheckpoint(logSequenceNumber, checkpoints);
}
LOGGER.log(Level.INFO, nodeId + " checkpoint start " + tableSpaceName + " at " + logSequenceNumber);
if (actualLogSequenceNumber == null) {
throw new DataStorageManagerException("actualLogSequenceNumber cannot be null");
}
// TODO: transactions checkpoint is not atomic
actions.addAll(dataStorageManager.writeTransactionsAtCheckpoint(tableSpaceUUID, logSequenceNumber, new ArrayList<>(transactions.values())));
actions.addAll(writeTablesOnDataStorageManager(new CommitLogResult(logSequenceNumber, false)));
// we checkpoint all data to disk and save the actual log sequence number
for (AbstractTableManager tableManager : tables.values()) {
if (!tableManager.isSystemTable()) {
TableCheckpoint checkpoint = full ? tableManager.fullCheckpoint(pin) : tableManager.checkpoint(pin);
if (checkpoint != null) {
actions.addAll(checkpoint.actions);
checkpoints.put(checkpoint.tableName, checkpoint.sequenceNumber);
if (afterTableCheckPointAction != null) {
afterTableCheckPointAction.run();
}
}
}
}
// we are sure that all data as been flushed. upon recovery we will replay the log starting from this position
actions.addAll(dataStorageManager.writeCheckpointSequenceNumber(tableSpaceUUID, logSequenceNumber));
/* Indexes checkpoint is handled by TableManagers */
log.dropOldLedgers(logSequenceNumber);
_logSequenceNumber = log.getLastSequenceNumber();
} finally {
generalLock.writeLock().unlock();
}
for (PostCheckpointAction action : actions) {
try {
action.run();
} catch (Exception error) {
LOGGER.log(Level.SEVERE, "postcheckpoint error:" + error, error);
}
}
long _stop = System.currentTimeMillis();
LOGGER.log(Level.INFO, "{0} checkpoint finish {1} started ad {2}, finished at {3}, total time {4} ms", new Object[] { nodeId, tableSpaceName, logSequenceNumber, _logSequenceNumber, Long.toString(_stop - _start) });
return new TableSpaceCheckpoint(logSequenceNumber, checkpoints);
}
Aggregations