private void cleanUpHistory ( ) { long oldestAllowedTimestamp = System . currentTimeMillis ( ) - historyLength ; int trashedNotifications = 0 ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "History cleanup: Checking old notifications to remove from history list ..." ) ; } HistoryTreeEntry key = new HistoryTreeEntry ( oldestAllowedTimestamp , 0 , ( byte ) 0 ) ; int notificationsCount = 0 ; historyLock . writeLock ( ) . lock ( ) ; try { notificationsCount = orderedHistoryList . size ( ) ; LOG . warn ( "History cleanup: size of the history before cleanup: " + notificationsCount ) ; if ( ! historyLimitDisabled && notificationsCount > historyLimit ) { LOG . warn ( "History cleanup: Reached physical limit. Number of stored notifications: " + notificationsCount + ". Clearing ..." ) ; } int index = Collections . binarySearch ( orderedHistoryList , key , comparatorByTS ) ; int toDeleteByTS = index >= 0 ? index : - ( index + 1 ) ; int toDeleteByLimit = historyLimitDisabled ? 0 : notificationsCount - ( int ) historyLimit ; toDeleteByLimit = toDeleteByLimit > 0 ? toDeleteByLimit : 0 ; int toDelete = Math . max ( toDeleteByTS , toDeleteByLimit ) ; // Delete items which are too old if ( toDelete > 0 ) { LOG . warn ( "History cleanup: number of the history to cleanup: " + toDelete ) ; for ( int i = 0 ; i < toDelete ; i ++ ) { orderedHistoryList . get ( i ) . removeFromTree ( ) ; } orderedHistoryList . subList ( 0 , toDelete ) . clear ( ) ; if ( toDeleteByLimit > toDeleteByTS ) { // If we delete a notification because we don't have space left trashedNotifications ++ ; } notificationsCount = orderedHistoryList . size ( ) ; LOG . warn ( "History cleanup: size of the history after cleanup: " + notificationsCount ) ; // clean up history tree, remove the node that has no children and // no notifications associated with them. cleanUpHistoryTree ( historyTree ) ; } } finally { historyLock . writeLock ( ) . unlock ( ) ; } core . getMetrics ( ) . trashedHistoryNotifications . inc ( trashedNotifications ) ; core . getMetrics ( ) . historySize . set ( notificationsCount ) ; core . getMetrics ( ) . historyQueues . set ( historyQueuesCount ) ; }
Checks if there are notifications in our tree which are older than historyLength . It removes does which are older .