Search in sources :

Example 1 with FullRecoveryNeededException

use of herddb.log.FullRecoveryNeededException in project herddb by diennea.

the class TableSpaceManager method recover.

void recover(TableSpace tableSpaceInfo) throws DataStorageManagerException, LogNotAvailableException, MetadataStorageManagerException {
    LogSequenceNumber logSequenceNumber = dataStorageManager.getLastcheckpointSequenceNumber(tableSpaceUUID);
    actualLogSequenceNumber = logSequenceNumber;
    LOGGER.log(Level.INFO, "{0} recover {1}, logSequenceNumber from DataStorage: {2}", new Object[] { nodeId, tableSpaceName, logSequenceNumber });
    List<Table> tablesAtBoot = dataStorageManager.loadTables(logSequenceNumber, tableSpaceUUID);
    List<Index> indexesAtBoot = dataStorageManager.loadIndexes(logSequenceNumber, tableSpaceUUID);
    String tableNames = tablesAtBoot.stream().map(t -> {
        return t.name;
    }).collect(Collectors.joining(","));
    String indexNames = indexesAtBoot.stream().map(t -> {
        return t.name + " on table " + t.table;
    }).collect(Collectors.joining(","));
    LOGGER.log(Level.INFO, "{0} {1} tablesAtBoot {2}, indexesAtBoot {3}", new Object[] { nodeId, tableSpaceName, tableNames, indexNames });
    for (Table table : tablesAtBoot) {
        TableManager tableManager = bootTable(table, 0, null);
        for (Index index : indexesAtBoot) {
            if (index.table.equals(table.name)) {
                bootIndex(index, tableManager, 0, false);
            }
        }
    }
    dataStorageManager.loadTransactions(logSequenceNumber, tableSpaceUUID, t -> {
        transactions.put(t.transactionId, t);
        LOGGER.log(Level.FINER, "{0} {1} tx {2} at boot", new Object[] { nodeId, tableSpaceName, t.transactionId });
        try {
            if (t.newTables != null) {
                for (Table table : t.newTables.values()) {
                    if (!tables.containsKey(table.name)) {
                        bootTable(table, t.transactionId, null);
                    }
                }
            }
            if (t.newIndexes != null) {
                for (Index index : t.newIndexes.values()) {
                    if (!indexes.containsKey(index.name)) {
                        AbstractTableManager tableManager = tables.get(index.table);
                        bootIndex(index, tableManager, t.transactionId, false);
                    }
                }
            }
        } catch (Exception err) {
            LOGGER.log(Level.SEVERE, "error while booting tmp tables " + err, err);
            throw new RuntimeException(err);
        }
    });
    try {
        log.recovery(logSequenceNumber, new ApplyEntryOnRecovery(), false);
    } catch (FullRecoveryNeededException fullRecoveryNeeded) {
        LOGGER.log(Level.SEVERE, nodeId + " full recovery of data is needed for tableSpace " + tableSpaceName, fullRecoveryNeeded);
        downloadTableSpaceData();
        log.recovery(actualLogSequenceNumber, new ApplyEntryOnRecovery(), false);
    }
    checkpoint(false, false);
}
Also used : HDBException(herddb.client.HDBException) SystablesTableManager(herddb.core.system.SystablesTableManager) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) Table(herddb.model.Table) ClientConfiguration(herddb.client.ClientConfiguration) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) Map(java.util.Map) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) LogNotAvailableException(herddb.log.LogNotAvailableException) CommitLogResult(herddb.log.CommitLogResult) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) Set(java.util.Set) LogSequenceNumber(herddb.log.LogSequenceNumber) ScanStatement(herddb.model.commands.ScanStatement) HDBClient(herddb.client.HDBClient) DropTableStatement(herddb.model.commands.DropTableStatement) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Bytes(herddb.utils.Bytes) LogEntry(herddb.log.LogEntry) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) SysnodesTableManager(herddb.core.system.SysnodesTableManager) ArrayList(java.util.ArrayList) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) BRINIndexManager(herddb.index.brin.BRINIndexManager) BiConsumer(java.util.function.BiConsumer) SystablestatsTableManager(herddb.core.system.SystablestatsTableManager) CommitLogListener(herddb.log.CommitLogListener) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) TableSpaceManagerStats(herddb.core.stats.TableSpaceManagerStats) LogEntryType(herddb.log.LogEntryType) LogEntryFactory(herddb.log.LogEntryFactory) DataStorageManager(herddb.storage.DataStorageManager) DropIndexStatement(herddb.model.commands.DropIndexStatement) AtomicLong(java.util.concurrent.atomic.AtomicLong) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) ServerHostData(herddb.network.ServerHostData) TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) TimeoutException(java.util.concurrent.TimeoutException) CreateTableStatement(herddb.model.commands.CreateTableStatement) JMXUtils(herddb.jmx.JMXUtils) TransactionResult(herddb.model.TransactionResult) MetadataStorageManager(herddb.metadata.MetadataStorageManager) KeyValue(herddb.network.KeyValue) Channel(herddb.network.Channel) SendResultCallback(herddb.network.SendResultCallback) ServerConfiguration(herddb.server.ServerConfiguration) DataStorageManagerException(herddb.storage.DataStorageManagerException) Index(herddb.model.Index) TableDoesNotExistException(herddb.model.TableDoesNotExistException) AlterTableStatement(herddb.model.commands.AlterTableStatement) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Logger(java.util.logging.Logger) TableAwareStatement(herddb.model.TableAwareStatement) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) HDBConnection(herddb.client.HDBConnection) List(java.util.List) Message(herddb.network.Message) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) SystransactionsTableManager(herddb.core.system.SystransactionsTableManager) Entry(java.util.Map.Entry) NodeMetadata(herddb.model.NodeMetadata) Optional(java.util.Optional) TableSpace(herddb.model.TableSpace) SysconfigTableManager(herddb.core.system.SysconfigTableManager) SysindexesTableManager(herddb.core.system.SysindexesTableManager) Statement(herddb.model.Statement) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SystablespacesTableManager(herddb.core.system.SystablespacesTableManager) Level(java.util.logging.Level) HashSet(java.util.HashSet) SysclientsTableManager(herddb.core.system.SysclientsTableManager) DumpedLogEntry(herddb.backup.DumpedLogEntry) StatementExecutionResult(herddb.model.StatementExecutionResult) CommitLog(herddb.log.CommitLog) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ClientSideMetadataProvider(herddb.client.ClientSideMetadataProvider) SystablespacereplicastateTableManager(herddb.core.system.SystablespacereplicastateTableManager) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Comparator(java.util.Comparator) SyscolumnsTableManager(herddb.core.system.SyscolumnsTableManager) Collections(java.util.Collections) TableManagerStats(herddb.core.stats.TableManagerStats) MemoryHashIndexManager(herddb.index.MemoryHashIndexManager) Table(herddb.model.Table) LogSequenceNumber(herddb.log.LogSequenceNumber) Index(herddb.model.Index) HDBException(herddb.client.HDBException) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) LogNotAvailableException(herddb.log.LogNotAvailableException) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) TimeoutException(java.util.concurrent.TimeoutException) DataStorageManagerException(herddb.storage.DataStorageManagerException) TableDoesNotExistException(herddb.model.TableDoesNotExistException) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) EOFException(java.io.EOFException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) SystablesTableManager(herddb.core.system.SystablesTableManager) SysnodesTableManager(herddb.core.system.SysnodesTableManager) SystablestatsTableManager(herddb.core.system.SystablestatsTableManager) SystransactionsTableManager(herddb.core.system.SystransactionsTableManager) SysconfigTableManager(herddb.core.system.SysconfigTableManager) SysindexesTableManager(herddb.core.system.SysindexesTableManager) SystablespacesTableManager(herddb.core.system.SystablespacesTableManager) SysclientsTableManager(herddb.core.system.SysclientsTableManager) SystablespacereplicastateTableManager(herddb.core.system.SystablespacereplicastateTableManager) SyscolumnsTableManager(herddb.core.system.SyscolumnsTableManager) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException)

Example 2 with FullRecoveryNeededException

use of herddb.log.FullRecoveryNeededException in project herddb by diennea.

the class BookkeeperCommitLog method recovery.

@Override
public void recovery(LogSequenceNumber snapshotSequenceNumber, BiConsumer<LogSequenceNumber, LogEntry> consumer, boolean fencing) throws LogNotAvailableException {
    this.actualLedgersList = metadataManager.getActualLedgersList(tableSpaceUUID);
    LOGGER.log(Level.SEVERE, "Actual ledgers list:" + actualLedgersList + " tableSpace " + tableSpaceUUID);
    this.lastLedgerId = snapshotSequenceNumber.ledgerId;
    this.currentLedgerId = snapshotSequenceNumber.ledgerId;
    this.lastSequenceNumber.set(snapshotSequenceNumber.offset);
    LOGGER.log(Level.SEVERE, "recovery from latest snapshotSequenceNumber:" + snapshotSequenceNumber);
    if (currentLedgerId > 0 && !this.actualLedgersList.getActiveLedgers().contains(currentLedgerId) && !this.actualLedgersList.getActiveLedgers().isEmpty()) {
        // TODO: download snapshot from another remote broker
        throw new FullRecoveryNeededException(new Exception("Actual ledgers list does not include latest snapshot ledgerid:" + currentLedgerId));
    }
    if (snapshotSequenceNumber.isStartOfTime() && !this.actualLedgersList.getActiveLedgers().isEmpty() && !this.actualLedgersList.getActiveLedgers().contains(this.actualLedgersList.getFirstLedger())) {
        throw new FullRecoveryNeededException(new Exception("Local data is absent, and actual ledger list " + this.actualLedgersList.getActiveLedgers() + " does not contain first ledger of ever: " + this.actualLedgersList.getFirstLedger()));
    }
    try {
        for (long ledgerId : actualLedgersList.getActiveLedgers()) {
            if (ledgerId < snapshotSequenceNumber.ledgerId) {
                LOGGER.log(Level.SEVERE, "Skipping ledger " + ledgerId);
                continue;
            }
            LedgerHandle handle;
            if (fencing) {
                handle = bookKeeper.openLedger(ledgerId, BookKeeper.DigestType.CRC32, sharedSecret.getBytes(StandardCharsets.UTF_8));
            } else {
                handle = bookKeeper.openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, sharedSecret.getBytes(StandardCharsets.UTF_8));
            }
            try {
                long first;
                if (ledgerId == snapshotSequenceNumber.ledgerId) {
                    first = snapshotSequenceNumber.offset;
                    LOGGER.log(Level.SEVERE, "Recovering from latest snapshot ledger " + ledgerId + ", starting from entry " + first);
                } else {
                    first = 0;
                    LOGGER.log(Level.SEVERE, "Recovering from ledger " + ledgerId + ", starting from entry " + first);
                }
                long lastAddConfirmed = handle.getLastAddConfirmed();
                LOGGER.log(Level.SEVERE, "Recovering from ledger " + ledgerId + ", first=" + first + " lastAddConfirmed=" + lastAddConfirmed);
                final int BATCH_SIZE = 10000;
                if (lastAddConfirmed >= 0) {
                    for (long b = first; b <= lastAddConfirmed; ) {
                        long start = b;
                        long end = b + BATCH_SIZE;
                        if (end > lastAddConfirmed) {
                            end = lastAddConfirmed;
                        }
                        b = end + 1;
                        double percent = ((start - first) * 100.0 / (lastAddConfirmed + 1));
                        int entriesToRead = (int) (1 + end - start);
                        LOGGER.log(Level.SEVERE, "From entry {0}, to entry {1} ({2} %)", new Object[] { start, end, percent });
                        long _start = System.currentTimeMillis();
                        Enumeration<LedgerEntry> entries = handle.readEntries(start, end);
                        int localEntryCount = 0;
                        while (entries.hasMoreElements()) {
                            LedgerEntry entry = entries.nextElement();
                            long entryId = entry.getEntryId();
                            LogSequenceNumber number = new LogSequenceNumber(ledgerId, entryId);
                            LogEntry statusEdit = LogEntry.deserialize(entry.getEntry());
                            lastLedgerId = ledgerId;
                            currentLedgerId = ledgerId;
                            lastSequenceNumber.set(entryId);
                            if (number.after(snapshotSequenceNumber)) {
                                LOGGER.log(Level.FINEST, "RECOVER ENTRY #" + localEntryCount + " {0}, {1}", new Object[] { number, statusEdit });
                                consumer.accept(number, statusEdit);
                            } else {
                                LOGGER.log(Level.FINEST, "SKIP ENTRY #" + localEntryCount + " {0}<{1}, {2}", new Object[] { number, snapshotSequenceNumber, statusEdit });
                            }
                            localEntryCount++;
                        }
                        LOGGER.log(Level.SEVERE, "read " + localEntryCount + " entries from ledger " + ledgerId + ", expected " + entriesToRead);
                        LOGGER.log(Level.SEVERE, "finished waiting for " + entriesToRead + " entries to be read from ledger " + ledgerId);
                        if (localEntryCount != entriesToRead) {
                            throw new LogNotAvailableException("Read " + localEntryCount + " entries, expected " + entriesToRead);
                        }
                        lastLedgerId = ledgerId;
                        lastSequenceNumber.set(end);
                        long _stop = System.currentTimeMillis();
                        LOGGER.log(Level.SEVERE, "From entry {0}, to entry {1} ({2} %) read time {3}", new Object[] { start, end, percent, (_stop - _start) + " ms" });
                    }
                }
            } finally {
                handle.close();
            }
        }
        LOGGER.severe("After recovery of " + tableSpaceUUID + " lastSequenceNumber " + getLastSequenceNumber());
    } catch (InterruptedException | EOFException | RuntimeException | BKException err) {
        LOGGER.log(Level.SEVERE, "Fatal error during recovery", err);
        signalLogFailed();
        throw new LogNotAvailableException(err);
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LogSequenceNumber(herddb.log.LogSequenceNumber) LogNotAvailableException(herddb.log.LogNotAvailableException) EOFException(java.io.EOFException) BKException(org.apache.bookkeeper.client.BKException) ExecutionException(java.util.concurrent.ExecutionException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) EOFException(java.io.EOFException) BKException(org.apache.bookkeeper.client.BKException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) LogEntry(herddb.log.LogEntry) LogNotAvailableException(herddb.log.LogNotAvailableException)

Aggregations

FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)2 LogEntry (herddb.log.LogEntry)2 LogNotAvailableException (herddb.log.LogNotAvailableException)2 LogSequenceNumber (herddb.log.LogSequenceNumber)2 EOFException (java.io.EOFException)2 DumpedLogEntry (herddb.backup.DumpedLogEntry)1 ClientConfiguration (herddb.client.ClientConfiguration)1 ClientSideMetadataProvider (herddb.client.ClientSideMetadataProvider)1 ClientSideMetadataProviderException (herddb.client.ClientSideMetadataProviderException)1 HDBClient (herddb.client.HDBClient)1 HDBConnection (herddb.client.HDBConnection)1 HDBException (herddb.client.HDBException)1 TableCheckpoint (herddb.core.AbstractTableManager.TableCheckpoint)1 TableManagerStats (herddb.core.stats.TableManagerStats)1 TableSpaceManagerStats (herddb.core.stats.TableSpaceManagerStats)1 SysclientsTableManager (herddb.core.system.SysclientsTableManager)1 SyscolumnsTableManager (herddb.core.system.SyscolumnsTableManager)1 SysconfigTableManager (herddb.core.system.SysconfigTableManager)1 SysindexesTableManager (herddb.core.system.SysindexesTableManager)1 SysnodesTableManager (herddb.core.system.SysnodesTableManager)1