private long recoverLastPersistedJournalEntry ( ) throws IOException { UfsJournalSnapshot snapshot = UfsJournalSnapshot . getSnapshot ( mJournal ) ; long lastPersistSeq = - 1 ; UfsJournalFile currentLog = snapshot . getCurrentLog ( mJournal ) ; if ( currentLog != null ) { LOG . info ( "Recovering from previous UFS journal write failure." + " Scanning for the last persisted journal entry." ) ; try ( JournalEntryStreamReader reader = new JournalEntryStreamReader ( mUfs . open ( currentLog . getLocation ( ) . toString ( ) , OpenOptions . defaults ( ) . setRecoverFailedOpen ( true ) ) ) ) { JournalEntry entry ; while ( ( entry = reader . readEntry ( ) ) != null ) { if ( entry . getSequenceNumber ( ) > lastPersistSeq ) { lastPersistSeq = entry . getSequenceNumber ( ) ; } } } catch ( IOException e ) { throw e ; } completeLog ( currentLog , lastPersistSeq + 1 ) ; } // Search for and scan the latest COMPLETE journal and find out the sequence number of the // last persisted journal entry, in case no entry has been found in the INCOMPLETE journal. if ( lastPersistSeq < 0 ) { // Re-evaluate snapshot because the incomplete journal will be destroyed if // it does not contain any valid entry. snapshot = UfsJournalSnapshot . getSnapshot ( mJournal ) ; // journalFiles[journalFiles.size()-1] is the latest complete journal file. List < UfsJournalFile > journalFiles = snapshot . getLogs ( ) ; if ( ! journalFiles . isEmpty ( ) ) { UfsJournalFile journal = journalFiles . get ( journalFiles . size ( ) - 1 ) ; lastPersistSeq = journal . getEnd ( ) - 1 ; LOG . info ( "Found last persisted journal entry with seq {} in {}." , lastPersistSeq , journal . getLocation ( ) . toString ( ) ) ; } } return lastPersistSeq ; }
Examine the UFS to determine the most recent journal entry and return its sequence number .