Package org.exist.storage.journal

Examples of org.exist.storage.journal.Loggable


      final JournalReader reader = new JournalReader(broker, last, lastNum);
            try {
              // try to read the last log record to see if it is a checkpoint
              boolean checkpointFound = false;
          try {
                    final Loggable lastLog = reader.lastEntry();
                    if (lastLog != null && lastLog.getLogType() == LogEntryTypes.CHECKPOINT) {
                      final Checkpoint checkpoint = (Checkpoint) lastLog;
                      // Found a checkpoint. To be sure it is indeed a valid checkpoint
                      // record, we compare the LSN stored in it with the current LSN.
                      if (checkpoint.getStoredLsn() == checkpoint.getLsn()) {
                        checkpointFound = true;
                        LOG.debug("Database is in clean state. Last checkpoint: " +
                            checkpoint.getDateString());
                      }
                    }
                } catch (final LogException e) {
                    LOG.info("Reading last journal log entry failed: " + e.getMessage() + ". Will scan the log...");
                    // if an exception occurs at this point, the journal file is probably incomplete,
                    // which indicates a db crash
                    checkpointFound = false;
                }
          if (!checkpointFound) {
                    LOG.info("Unclean shutdown detected. Scanning journal...");
                    broker.getBrokerPool().reportStatus("Unclean shutdown detected. Scanning log...");
            reader.position(1);
            final Long2ObjectHashMap<Loggable> txnsStarted = new Long2ObjectHashMap<Loggable>();
            Checkpoint lastCheckpoint = null;
            long lastLsn = Lsn.LSN_INVALID;
                  Loggable next;
                  try {
            final ProgressBar progress = new ProgressBar("Scanning journal ", last.length());
                while ((next = reader.nextEntry()) != null) {
//                          LOG.debug(next.dump());
              progress.set(Lsn.getOffset(next.getLsn()));
              if (next.getLogType() == LogEntryTypes.TXN_START) {
                        // new transaction starts: add it to the transactions table
                        txnsStarted.put(next.getTransactionId(), next);
                    } else if (next.getLogType() == LogEntryTypes.TXN_ABORT) {
                      // transaction aborted: remove it from the transactions table
                      txnsStarted.remove(next.getTransactionId());
                    } else if (next.getLogType() == LogEntryTypes.CHECKPOINT) {
                      txnsStarted.clear();
                    lastCheckpoint = (Checkpoint) next;
                    }
                  lastLsn = next.getLsn();
                }
                  } catch (final LogException e) {
                      if (LOG.isDebugEnabled()) {
                            LOG.debug("Caught exception while reading log", e);
                        }
View Full Code Here


            // ------- REDO ---------
            if (LOG.isInfoEnabled())
                {LOG.info("First pass: redoing " + txnCount + " transactions...");}
            final ProgressBar progress = new ProgressBar("Redo ", last.length());
            Loggable next = null;
            int redoCnt = 0;
            try {
                while ((next = reader.nextEntry()) != null) {
                    SanityCheck.ASSERT(next.getLogType() != LogEntryTypes.CHECKPOINT,
                            "Found a checkpoint during recovery run! This should not ever happen.");
                    if (next.getLogType() == LogEntryTypes.TXN_START) {
                        // new transaction starts: add it to the transactions table
                        runningTxns.put(next.getTransactionId(), next);
                    } else if (next.getLogType() == LogEntryTypes.TXN_COMMIT) {
                        // transaction committed: remove it from the transactions table
                        runningTxns.remove(next.getTransactionId());
                        redoCnt++;
                    } else if (next.getLogType() == LogEntryTypes.TXN_ABORT) {
                        // transaction aborted: remove it from the transactions table
                        runningTxns.remove(next.getTransactionId());
                    }
        //            LOG.debug("Redo: " + next.dump());
                    // redo the log entry
                    next.redo();
                    progress.set(Lsn.getOffset(next.getLsn()));
                    if (next.getLsn() == lastLsn)
                        {break;} // last readable entry reached. Stop here.
                }
            } catch (final Exception e) {
                LOG.error("Exception caught while redoing transactions. Aborting recovery to avoid possible damage. " +
                    "Before starting again, make sure to run a check via the emergency export tool.", e);
                if (next != null)
                    {LOG.info("Log entry that caused the exception: " + next.dump());}
                throw new LogException("Recovery aborted. ");
            } finally {
                LOG.info("Redo processed " + redoCnt + " out of " + txnCount + " transactions.");
            }

            // ------- UNDO ---------
            if (LOG.isInfoEnabled())
                {LOG.info("Second pass: undoing dirty transactions. Uncommitted transactions: " +
                        runningTxns.size());}
            // see if there are uncommitted transactions pending
            if (runningTxns.size() > 0) {
                // do a reverse scan of the log, undoing all uncommitted transactions
                try {
                    while((next = reader.previousEntry()) != null) {
                        if (next.getLogType() == LogEntryTypes.TXN_START) {
                            if (runningTxns.get(next.getTransactionId()) != null) {
                                runningTxns.remove(next.getTransactionId());
                                if (runningTxns.size() == 0)
                                    // all dirty transactions undone
                                    {break;}
                            }
                        } else if (next.getLogType() == LogEntryTypes.TXN_COMMIT) {
                            // ignore already committed transaction
                        } else if (next.getLogType() == LogEntryTypes.CHECKPOINT) {
                            // found last checkpoint: undo is completed
                            break;
                        }

                        // undo the log entry if it belongs to an uncommitted transaction
                        if (runningTxns.get(next.getTransactionId()) != null) {
    //          LOG.debug("Undo: " + next.dump());
                            next.undo();
                        }
                    }
                } catch (final Exception e) {
                    LOG.warn("Exception caught while undoing dirty transactions. Remaining transactions " +
                            "to be undone: " + runningTxns.size() + ". Aborting recovery to avoid possible damage. " +
                            "Before starting again, make sure to run a check via the emergency export tool.", e);
                    if (next != null)
                        {LOG.warn("Log entry that caused the exception: " + next.dump());}
                    throw new LogException("Recovery aborted");
                }
            }
        } finally {
            broker.sync(Sync.MAJOR_SYNC);
View Full Code Here

TOP

Related Classes of org.exist.storage.journal.Loggable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.