Search in sources :

Example 1 with SendResultCallback

use of herddb.network.SendResultCallback 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());
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) DumpedLogEntry(herddb.backup.DumpedLogEntry) Message(herddb.network.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) SendResultCallback(herddb.network.SendResultCallback) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) TimeoutException(java.util.concurrent.TimeoutException) CommitLogListener(herddb.log.CommitLogListener) LogSequenceNumber(herddb.log.LogSequenceNumber) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) Transaction(herddb.model.Transaction) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with SendResultCallback

use of herddb.network.SendResultCallback in project herddb by diennea.

the class NettyChannel method sendReplyMessage.

@Override
public void sendReplyMessage(Message inAnswerTo, Message message) {
    if (message.getMessageId() == null) {
        message.assignMessageId();
    }
    if (this.socket == null) {
        LOGGER.log(Level.SEVERE, this + " channel not active, discarding reply message " + message);
        return;
    }
    message.setReplyMessageId(inAnswerTo.messageId);
    sendOneWayMessage(message, new SendResultCallback() {

        @Override
        public void messageSent(Message originalMessage, Throwable error) {
            if (error != null) {
                LOGGER.log(Level.SEVERE, this + " error:" + error, error);
            }
        }
    });
}
Also used : Message(herddb.network.Message) SendResultCallback(herddb.network.SendResultCallback)

Example 3 with SendResultCallback

use of herddb.network.SendResultCallback in project herddb by diennea.

the class NettyChannel method sendMessageWithAsyncReply.

@Override
public void sendMessageWithAsyncReply(Message message, long timeout, ReplyCallback callback) {
    if (message.getMessageId() == null) {
        message.assignMessageId();
    }
    if (!isValid()) {
        submitCallback(() -> {
            callback.replyReceived(message, null, new Exception(this + " connection is not active"));
        });
        return;
    }
    pendingReplyMessages.put(message.getMessageId(), callback);
    pendingReplyMessagesSource.put(message.getMessageId(), message);
    pendingReplyMessagesDeadline.put(message.getMessageId(), System.currentTimeMillis() + timeout);
    sendOneWayMessage(message, new SendResultCallback() {

        @Override
        public void messageSent(Message originalMessage, Throwable error) {
            if (error != null) {
                LOGGER.log(Level.SEVERE, this + ": error while sending reply message to " + originalMessage, error);
                submitCallback(() -> {
                    callback.replyReceived(message, null, new Exception(this + ": error while sending reply message to " + originalMessage, error));
                });
            }
        }
    });
}
Also used : Message(herddb.network.Message) SendResultCallback(herddb.network.SendResultCallback) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

Message (herddb.network.Message)3 SendResultCallback (herddb.network.SendResultCallback)3 DumpedLogEntry (herddb.backup.DumpedLogEntry)1 TableCheckpoint (herddb.core.AbstractTableManager.TableCheckpoint)1 CommitLogListener (herddb.log.CommitLogListener)1 LogEntry (herddb.log.LogEntry)1 LogSequenceNumber (herddb.log.LogSequenceNumber)1 Transaction (herddb.model.Transaction)1 DataStorageManagerException (herddb.storage.DataStorageManagerException)1 FullTableScanConsumer (herddb.storage.FullTableScanConsumer)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1