private void deleteStaleBackups ( ) throws Exception { UfsStatus [ ] statuses = mUfs . listStatus ( mBackupDir ) ; if ( statuses . length <= mRetainedFiles ) { return ; } // Sort the backup files according to create time from oldest to newest TreeMap < Instant , String > timeToFile = new TreeMap <> ( ( a , b ) -> ( a . isBefore ( b ) ? - 1 : a . isAfter ( b ) ? 1 : 0 ) ) ; for ( UfsStatus status : statuses ) { if ( status . isFile ( ) ) { Matcher matcher = BackupManager . BACKUP_FILE_PATTERN . matcher ( status . getName ( ) ) ; if ( matcher . matches ( ) ) { timeToFile . put ( Instant . ofEpochMilli ( Long . parseLong ( matcher . group ( 1 ) ) ) , status . getName ( ) ) ; } } } int toDeleteFileNum = timeToFile . size ( ) - mRetainedFiles ; if ( toDeleteFileNum <= 0 ) { return ; } for ( int i = 0 ; i < toDeleteFileNum ; i ++ ) { String toDeleteFile = PathUtils . concatPath ( mBackupDir , timeToFile . pollFirstEntry ( ) . getValue ( ) ) ; mUfs . deleteExistingFile ( toDeleteFile ) ; } LOG . info ( "Deleted {} stale metadata backup files at {}" , toDeleteFileNum , mBackupDir ) ; }
Deletes stale backup files to avoid consuming too many spaces .