@ JmxOperation ( description = "swapFiles changes this store to use the new data directory" ) public void swapFiles ( String newStoreDirectory ) { logger . info ( "Swapping files for store '" + getName ( ) + "' to " + newStoreDirectory ) ; File newVersionDir = new File ( newStoreDirectory ) ; if ( ! newVersionDir . exists ( ) ) throw new VoldemortException ( "File " + newVersionDir . getAbsolutePath ( ) + " does not exist." ) ; if ( ! ( newVersionDir . getParentFile ( ) . compareTo ( storeDir . getAbsoluteFile ( ) ) == 0 && ReadOnlyUtils . checkVersionDirName ( newVersionDir ) ) ) throw new VoldemortException ( "Invalid version folder name '" + newVersionDir + "'. Either parent directory is incorrect or format(version-n) is incorrect" ) ; // retrieve previous version for (a) check if last write is winning // (b) if failure, rollback use File previousVersionDir = ReadOnlyUtils . getCurrentVersion ( storeDir ) ; if ( previousVersionDir == null ) throw new VoldemortException ( "Could not find any latest directory to swap with in store '" + getName ( ) + "'" ) ; long newVersionId = ReadOnlyUtils . getVersionId ( newVersionDir ) ; long previousVersionId = ReadOnlyUtils . getVersionId ( previousVersionDir ) ; if ( newVersionId == - 1 || previousVersionId == - 1 ) throw new VoldemortException ( "Unable to parse folder names (" + newVersionDir . getName ( ) + "," + previousVersionDir . getName ( ) + ") since format(version-n) is incorrect" ) ; // check if we're greater than latest since we want last write to win if ( previousVersionId > newVersionId ) { logger . info ( "No swap required since current latest version " + previousVersionId + " is greater than swap version " + newVersionId ) ; deleteBackups ( ) ; return ; } logger . info ( "Acquiring write lock on '" + getName ( ) + "':" ) ; fileModificationLock . writeLock ( ) . lock ( ) ; boolean success = false ; try { close ( ) ; logger . info ( "Opening primary files for store '" + getName ( ) + "' at " + newStoreDirectory ) ; // open the latest store open ( newVersionDir ) ; success = true ; } finally { try { // we failed to do the swap, attempt a rollback to last version if ( ! success ) rollback ( previousVersionDir ) ; } finally { fileModificationLock . writeLock ( ) . unlock ( ) ; if ( success ) logger . info ( "Swap operation completed successfully on store " + getName ( ) + ", releasing lock." ) ; else logger . error ( "Swap operation failed." ) ; } } // okay we have released the lock and the store is now open again, it is // safe to do a potentially slow delete if we have one too many backups deleteBackups ( ) ; }
Swap the current version folder for a new one