/* * [Retire.java] * * Summary: Retire old zip files with too much deadwood. * * Copyright: (c) 2002-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2002-09-28 */ package com.mindprod.replicatorsender; import com.mindprod.replicatorcommon.Config; import com.mindprod.replicatorcommon.ZipnameContext; import static java.lang.System.*; /** * Retire old zip files with too much deadwood. *

* Freed files are assigned to new zip * * @author Roedy Green, Canadian Mind Products * @version 1.0 2002-09-28 * @since 2002-09-28 */ final class Retire { /** * Decomission this Zip file by unassigning all its files and deletion markers for reassignment to new files. * * @param zd Zip file that is to be decomissioned. */ private static void decommissionZip( MaxiZD zd ) { final int retiringZipNumber = zd.getZipNumber(); for ( MaxiFD fd : ReplicatorSender.filesToDistribute ) { if ( fd.getZipNumber() == retiringZipNumber ) { fd.assignZip( null ); long clumping = fd.getClumping(); if ( clumping > ReplicatorSender.newestRepackagedTimestamp ) { ReplicatorSender.newestRepackagedTimestamp = clumping; } } } for ( MaxiFD fd : ReplicatorSender.filesToDelete ) { if ( fd.getZipNumber() == retiringZipNumber ) { fd.assignZip( null ); // repacked deletions count just like repacked files. long clumping = fd.getClumping(); if ( clumping > ReplicatorSender.newestRepackagedTimestamp ) { ReplicatorSender.newestRepackagedTimestamp = clumping; } } } zd.status = ZDStatus.C_DECOMMISSIONED; // will later stage through D_RETIRED, E_EMACIATED, F_DELETED zd.deadwood = 0; zd.livewood = 0; zd.setHighestClumpingInZip( Config.NULL_TIMESTAMP ); } /** * true if this zip is totally empty, no files, no zero length files, no deletes. * * @param zd Zip descriptor * * @return true if zip is empty (not counting deadwood ) */ @SuppressWarnings( { "BooleanMethodIsAlwaysInverted" } ) private static boolean isEmpty( MaxiZD zd ) { if ( zd.livewood != 0 ) { return false; } // no livewood, but might be some 0-length files or deletions final int zipNumber = zd.getZipNumber(); for ( MaxiFD fd : ReplicatorSender.filesToDelete ) { if ( fd.getZipNumber() == zipNumber ) { return false; } } for ( MaxiFD fd : ReplicatorSender.filesToDistribute ) { if ( fd.getZipNumber() == zipNumber ) { return false; } } return true; } /** * Retire zips that have too high a percentage of deadwood. Free up their files to be reassigned to new zips. */ public static void process() { // retiring does not zero or delete the zip, just mark it retired // It will no longer be mentioned in the zip manifest. // retire deadwood in zips boolean repacking = false; ReplicatorSender.newestRepackagedTimestamp = Config.NULL_TIMESTAMP; for ( MaxiZD zd : ReplicatorSender.allZips ) { if ( zd.status == ZDStatus.B_CREATED ) { if ( repacking ) { decommissionZip( zd ); } else { // old files are decommissioned for having a lot of deadwood. // recent files are decommissioned only for having a lot of // deadwood. // Zips might have 0 livewood, which would be just // residual deletions. They get repacked. // round up to next highest percentage final long dividend = zd.deadwood * 100; final long divisor = Math.max( 1, ( zd.deadwood + zd.livewood ) ); final int percentDead = ( int ) ( ( dividend + divisor - 1 ) / divisor ); if ( percentDead > ( zd.creationTimestamp > ConfigForSender.LAG_CUTOFF_TIMESTAMP ? ConfigForSender.RECENT_DEADWOOD_TOLERANCE_PERCENTAGE : ConfigForSender.OLD_DEADWOOD_TOLERANCE_PERCENTAGE ) ) { if ( ConfigForSender.DEBUGGING ) { out.println( ( zd.creationTimestamp > ConfigForSender.LAG_CUTOFF_TIMESTAMP ? "Pruning recent deadwood " : "Pruning old deadwood " ) + zd.getZipFilename( ZipnameContext.ON_SOURCE ) ); } // repack all subsequent zips too. // if isEmpty we don't need to repack subsequent // zips since we freed nothing to repack. repacking = !isEmpty( zd ); // we free up all its files and deletions to be assigned a new // zip. decommissionZip( zd ); } // end if } // end else } // end if } // end for // No longer retire small zips. Could get into loop if // a very large file followed. // we don't change the order of anything. } }