use of herddb.log.CommitLogListener in project herddb by diennea.
the class TableSpaceManager method dumpTableSpace.
void dumpTableSpace(String dumpId, Channel _channel, int fetchSize, boolean includeLog) throws DataStorageManagerException, LogNotAvailableException {
LOGGER.log(Level.SEVERE, "dumpTableSpace dumpId:" + dumpId + " channel " + _channel + " fetchSize:" + fetchSize + ", includeLog:" + includeLog);
TableSpaceCheckpoint checkpoint;
List<DumpedLogEntry> txlogentries = new CopyOnWriteArrayList<>();
CommitLogListener logDumpReceiver = new CommitLogListener() {
@Override
public void logEntry(LogSequenceNumber logPos, LogEntry data) {
// we are going to capture all the changes to the tablespace during the dump, in order to replay
// eventually 'missed' changes during the dump
txlogentries.add(new DumpedLogEntry(logPos, data.serialize()));
LOGGER.log(Level.SEVERE, "dumping entry " + logPos + ", " + data + " nentries: " + txlogentries.size());
}
};
generalLock.writeLock().lock();
try {
if (includeLog) {
log.attachCommitLogListener(logDumpReceiver);
}
checkpoint = checkpoint(true, true);
/* Downgrade lock */
generalLock.readLock().lock();
} finally {
generalLock.writeLock().unlock();
}
try {
final int timeout = 60000;
Map<String, Object> startData = new HashMap<>();
startData.put("command", "start");
LogSequenceNumber logSequenceNumber = log.getLastSequenceNumber();
startData.put("ledgerid", logSequenceNumber.ledgerId);
startData.put("offset", logSequenceNumber.offset);
Message response_to_start = _channel.sendMessageWithReply(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, startData), timeout);
if (response_to_start.type != Message.TYPE_ACK) {
LOGGER.log(Level.SEVERE, "error response at start command: " + response_to_start.parameters);
return;
}
if (includeLog) {
List<Transaction> transactionsSnapshot = new ArrayList<>();
dataStorageManager.loadTransactions(logSequenceNumber, tableSpaceUUID, transactionsSnapshot::add);
List<Transaction> batch = new ArrayList<>();
for (Transaction t : transactionsSnapshot) {
batch.add(t);
if (batch.size() == 10) {
sendTransactionsDump(batch, _channel, dumpId, timeout, response_to_start);
}
}
sendTransactionsDump(batch, _channel, dumpId, timeout, response_to_start);
}
for (Entry<String, LogSequenceNumber> entry : checkpoint.tablesCheckpoints.entrySet()) {
final AbstractTableManager tableManager = tables.get(entry.getKey());
final LogSequenceNumber sequenceNumber = entry.getValue();
if (tableManager.isSystemTable()) {
continue;
}
try {
FullTableScanConsumer sink = new SingleTableDumper(tableSpaceName, tableManager, _channel, dumpId, timeout, fetchSize);
tableManager.dump(sequenceNumber, sink);
} catch (DataStorageManagerException err) {
Map<String, Object> errorOnData = new HashMap<>();
errorOnData.put("command", "error");
_channel.sendMessageWithReply(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, errorOnData), timeout);
LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId, err);
return;
}
}
if (!txlogentries.isEmpty()) {
txlogentries.sort(Comparator.naturalOrder());
sendDumpedCommitLog(txlogentries, _channel, dumpId, timeout);
}
Map<String, Object> finishData = new HashMap<>();
LogSequenceNumber finishLogSequenceNumber = log.getLastSequenceNumber();
finishData.put("ledgerid", finishLogSequenceNumber.ledgerId);
finishData.put("offset", finishLogSequenceNumber.offset);
finishData.put("command", "finish");
_channel.sendOneWayMessage(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, finishData), new SendResultCallback() {
@Override
public void messageSent(Message originalMessage, Throwable error) {
}
});
} catch (InterruptedException | TimeoutException error) {
LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId);
} finally {
generalLock.readLock().unlock();
if (includeLog) {
log.removeCommitLogListener(logDumpReceiver);
}
for (Entry<String, LogSequenceNumber> entry : checkpoint.tablesCheckpoints.entrySet()) {
dataStorageManager.unPinTableCheckpoint(tableSpaceUUID, entry.getKey(), entry.getValue());
}
}
}
Aggregations