/// <summary>Delete old OIV fsimages.</summary> /// <remarks> /// Delete old OIV fsimages. Since the target dir is not a full blown /// storage directory, we simply list and keep the latest ones. For the /// same reason, no storage inspector is used. /// </remarks> internal virtual void PurgeOldLegacyOIVImages(string dir, long txid) { FilePath oivImageDir = new FilePath(dir); string oivImagePrefix = NNStorage.NameNodeFile.ImageLegacyOiv.GetName(); string[] filesInStorage; // Get the listing filesInStorage = oivImageDir.List(new _FilenameFilter_250(oivImagePrefix)); // Check whether there is any work to do. if (filesInStorage.Length <= numCheckpointsToRetain) { return; } // Create a sorted list of txids from the file names. TreeSet <long> sortedTxIds = new TreeSet <long>(); foreach (string fName in filesInStorage) { // Extract the transaction id from the file name. long fTxId; try { fTxId = long.Parse(Sharpen.Runtime.Substring(fName, oivImagePrefix.Length + 1)); } catch (FormatException) { // This should not happen since we have already filtered it. // Log and continue. Log.Warn("Invalid file name. Skipping " + fName); continue; } sortedTxIds.AddItem(Sharpen.Extensions.ValueOf(fTxId)); } int numFilesToDelete = sortedTxIds.Count - numCheckpointsToRetain; IEnumerator <long> iter = sortedTxIds.GetEnumerator(); while (numFilesToDelete > 0 && iter.HasNext()) { long txIdVal = iter.Next(); string fileName = NNStorage.GetLegacyOIVImageFileName(txIdVal); Log.Info("Deleting " + fileName); FilePath fileToDelete = new FilePath(oivImageDir, fileName); if (!fileToDelete.Delete()) { // deletion failed. Log.Warn("Failed to delete image file: " + fileToDelete); } numFilesToDelete--; } }