public void logSync ( boolean doWait ) { long syncStart = 0 ; boolean thisThreadSuccess = false ; boolean thisThreadSyncing = false ; EditLogOutputStream logStream = null ; try { synchronized ( this ) { long mytxid = myTransactionId . get ( ) . txid ; myTransactionId . get ( ) . txid = - 1L ; if ( mytxid == - 1 ) { mytxid = txid ; } printStatistics ( false ) ; // if somebody is already syncing, then wait while ( mytxid > synctxid && isSyncRunning ) { if ( ! doWait ) { long delayedId = Server . delayResponse ( ) ; List < Long > responses = delayedSyncs . get ( mytxid ) ; if ( responses == null ) { responses = new LinkedList < Long > ( ) ; delayedSyncs . put ( mytxid , responses ) ; } responses . add ( delayedId ) ; return ; } try { wait ( 1000 ) ; } catch ( InterruptedException ie ) { } } // // If this transaction was already flushed, then nothing to do // if ( mytxid <= synctxid ) { numTransactionsBatchedInSync ++ ; if ( metrics != null ) // Metrics is non-null only when used inside name // node metrics . transactionsBatchedInSync . inc ( ) ; return ; } // now, this thread will do the sync syncStart = txid ; isSyncRunning = true ; thisThreadSyncing = true ; // swap buffers try { if ( journalSet . isEmpty ( ) ) { throw new IOException ( "No journals available to flush, journalset is empty" ) ; } if ( editLogStream == null ) { throw new IOException ( "No journals available to flush, editlogstream is null" ) ; } editLogStream . setReadyToFlush ( ) ; } catch ( IOException e ) { LOG . fatal ( "Could not sync enough journals to persistent storage. " + "Unsynced transactions: " + ( txid - synctxid ) , new Exception ( e ) ) ; runtime . exit ( 1 ) ; } // editLogStream may become null, // so store a local variable for flush. logStream = editLogStream ; } // do the sync sync ( logStream , syncStart ) ; thisThreadSuccess = true ; } finally { synchronized ( this ) { if ( thisThreadSyncing ) { if ( thisThreadSuccess ) { // only set this if the sync succeeded synctxid = syncStart ; } // if this thread was syncing, clear isSyncRunning isSyncRunning = false ; } this . notifyAll ( ) ; } } endDelay ( syncStart ) ; }
Sync all modifications done by this thread .