/// <summary> /// Move a persisted replica from lazypersist directory to a subdirectory /// under finalized. /// </summary> /// <exception cref="System.IO.IOException"/> internal virtual FilePath ActivateSavedReplica(Block b, FilePath metaFile, FilePath blockFile) { FilePath blockDir = DatanodeUtil.IdToBlockDir(finalizedDir, b.GetBlockId()); FilePath targetBlockFile = new FilePath(blockDir, blockFile.GetName()); FilePath targetMetaFile = new FilePath(blockDir, metaFile.GetName()); FileUtils.MoveFile(blockFile, targetBlockFile); FsDatasetImpl.Log.Info("Moved " + blockFile + " to " + targetBlockFile); FileUtils.MoveFile(metaFile, targetMetaFile); FsDatasetImpl.Log.Info("Moved " + metaFile + " to " + targetMetaFile); return(targetBlockFile); }
/// <exception cref="System.IO.IOException"/> internal virtual FilePath AddBlock(Block b, FilePath f) { FilePath blockDir = DatanodeUtil.IdToBlockDir(finalizedDir, b.GetBlockId()); if (!blockDir.Exists()) { if (!blockDir.Mkdirs()) { throw new IOException("Failed to mkdirs " + blockDir); } } FilePath blockFile = FsDatasetImpl.MoveBlockFiles(b, f, blockDir); FilePath metaFile = FsDatasetUtil.GetMetaFile(blockFile, b.GetGenerationStamp()); dfsUsage.IncDfsUsed(b.GetNumBytes() + metaFile.Length()); return(blockFile); }
/// <exception cref="System.IO.IOException"/> internal virtual bool IsBPDirEmpty(string bpid) { FilePath volumeCurrentDir = this.GetCurrentDir(); FilePath bpDir = new FilePath(volumeCurrentDir, bpid); FilePath bpCurrentDir = new FilePath(bpDir, DataStorage.StorageDirCurrent); FilePath finalizedDir = new FilePath(bpCurrentDir, DataStorage.StorageDirFinalized ); FilePath rbwDir = new FilePath(bpCurrentDir, DataStorage.StorageDirRbw); if (finalizedDir.Exists() && !DatanodeUtil.DirNoFilesRecursive(finalizedDir)) { return(false); } if (rbwDir.Exists() && FileUtil.List(rbwDir).Length != 0) { return(false); } return(true); }
/// <exception cref="System.IO.IOException"/> private static void CreateUnlinkTmpFile(ReplicaInfo replicaInfo, bool changeBlockFile , bool isRename) { FilePath src; if (changeBlockFile) { src = replicaInfo.GetBlockFile(); } else { src = replicaInfo.GetMetaFile(); } FilePath dst = DatanodeUtil.GetUnlinkTmpFile(src); if (isRename) { src.RenameTo(dst); } else { FileInputStream @in = new FileInputStream(src); try { FileOutputStream @out = new FileOutputStream(dst); try { IOUtils.CopyBytes(@in, @out, 1); } finally { @out.Close(); } } finally { @in.Close(); } } }
protected internal bool VerifyBlockDeletedFromDir(FilePath dir, LocatedBlocks locatedBlocks ) { foreach (LocatedBlock lb in locatedBlocks.GetLocatedBlocks()) { FilePath targetDir = DatanodeUtil.IdToBlockDir(dir, lb.GetBlock().GetBlockId()); FilePath blockFile = new FilePath(targetDir, lb.GetBlock().GetBlockName()); if (blockFile.Exists()) { Log.Warn("blockFile: " + blockFile.GetAbsolutePath() + " exists after deletion."); return(false); } FilePath metaFile = new FilePath(targetDir, DatanodeUtil.GetMetaName(lb.GetBlock( ).GetBlockName(), lb.GetBlock().GetGenerationStamp())); if (metaFile.Exists()) { Log.Warn("metaFile: " + metaFile.GetAbsolutePath() + " exists after deletion."); return(false); } } return(true); }
/// <summary>Make sure at least one non-transient volume has a saved copy of the replica. /// </summary> /// <remarks> /// Make sure at least one non-transient volume has a saved copy of the replica. /// An infinite loop is used to ensure the async lazy persist tasks are completely /// done before verification. Caller of ensureLazyPersistBlocksAreSaved expects /// either a successful pass or timeout failure. /// </remarks> /// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> protected internal void EnsureLazyPersistBlocksAreSaved(LocatedBlocks locatedBlocks ) { string bpid = cluster.GetNamesystem().GetBlockPoolId(); IList <FsVolumeSpi> volumes = cluster.GetDataNodes()[0].GetFSDataset().GetVolumes( ); ICollection <long> persistedBlockIds = new HashSet <long>(); while (persistedBlockIds.Count < locatedBlocks.GetLocatedBlocks().Count) { // Take 1 second sleep before each verification iteration Sharpen.Thread.Sleep(1000); foreach (LocatedBlock lb in locatedBlocks.GetLocatedBlocks()) { foreach (FsVolumeSpi v in volumes) { if (v.IsTransientStorage()) { continue; } FsVolumeImpl volume = (FsVolumeImpl)v; FilePath lazyPersistDir = volume.GetBlockPoolSlice(bpid).GetLazypersistDir(); long blockId = lb.GetBlock().GetBlockId(); FilePath targetDir = DatanodeUtil.IdToBlockDir(lazyPersistDir, blockId); FilePath blockFile = new FilePath(targetDir, lb.GetBlock().GetBlockName()); if (blockFile.Exists()) { // Found a persisted copy for this block and added to the Set persistedBlockIds.AddItem(blockId); } } } } // We should have found a persisted copy for each located block. Assert.AssertThat(persistedBlockIds.Count, IS.Is(locatedBlocks.GetLocatedBlocks() .Count)); }
public virtual void Run() { bool success = false; FileInputStream blockIn = null; FileInputStream metaIn = null; MappableBlock mappableBlock = null; ExtendedBlock extBlk = new ExtendedBlock(this.key.GetBlockPoolId(), this.key.GetBlockId (), this.length, this.genstamp); long newUsedBytes = this._enclosing.usedBytesCount.Reserve(this.length); bool reservedBytes = false; try { if (newUsedBytes < 0) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": could not reserve " + this.length + " more bytes in the cache: " + DFSConfigKeys.DfsDatanodeMaxLockedMemoryKey + " of " + this._enclosing.maxBytes + " exceeded."); return; } reservedBytes = true; try { blockIn = (FileInputStream)this._enclosing.dataset.GetBlockInputStream(extBlk, 0); metaIn = DatanodeUtil.GetMetaDataInputStream(extBlk, this._enclosing.dataset); } catch (InvalidCastException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": Underlying blocks are not backed by files." , e); return; } catch (FileNotFoundException) { FsDatasetCache.Log.Info("Failed to cache " + this.key + ": failed to find backing " + "files."); return; } catch (IOException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": failed to open file", e); return; } try { mappableBlock = MappableBlock.Load(this.length, blockIn, metaIn, this.blockFileName ); } catch (ChecksumException) { // Exception message is bogus since this wasn't caused by a file read FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": checksum verification failed." ); return; } catch (IOException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key, e); return; } lock (this._enclosing) { FsDatasetCache.Value value = this._enclosing.mappableBlockMap[this.key]; Preconditions.CheckNotNull(value); Preconditions.CheckState(value.state == FsDatasetCache.State.Caching || value.state == FsDatasetCache.State.CachingCancelled); if (value.state == FsDatasetCache.State.CachingCancelled) { Sharpen.Collections.Remove(this._enclosing.mappableBlockMap, this.key); FsDatasetCache.Log.Warn("Caching of " + this.key + " was cancelled."); return; } this._enclosing.mappableBlockMap[this.key] = new FsDatasetCache.Value(mappableBlock , FsDatasetCache.State.Cached); } FsDatasetCache.Log.Debug("Successfully cached {}. We are now caching {} bytes in" + " total.", this.key, newUsedBytes); this._enclosing.dataset.datanode.GetShortCircuitRegistry().ProcessBlockMlockEvent (this.key); this._enclosing.numBlocksCached.AddAndGet(1); this._enclosing.dataset.datanode.GetMetrics().IncrBlocksCached(1); success = true; } finally { IOUtils.CloseQuietly(blockIn); IOUtils.CloseQuietly(metaIn); if (!success) { if (reservedBytes) { this._enclosing.usedBytesCount.Release(this.length); } FsDatasetCache.Log.Debug("Caching of {} was aborted. We are now caching only {} " + "bytes in total.", this.key, this._enclosing.usedBytesCount.Get()); if (mappableBlock != null) { mappableBlock.Close(); } this._enclosing.numBlocksFailedToCache.IncrementAndGet(); lock (this._enclosing) { Sharpen.Collections.Remove(this._enclosing.mappableBlockMap, this.key); } } } }
/// <exception cref="System.IO.IOException"/> internal virtual void DeleteBPDirectories(string bpid, bool force) { FilePath volumeCurrentDir = this.GetCurrentDir(); FilePath bpDir = new FilePath(volumeCurrentDir, bpid); if (!bpDir.IsDirectory()) { // nothing to be deleted return; } FilePath tmpDir = new FilePath(bpDir, DataStorage.StorageDirTmp); FilePath bpCurrentDir = new FilePath(bpDir, DataStorage.StorageDirCurrent); FilePath finalizedDir = new FilePath(bpCurrentDir, DataStorage.StorageDirFinalized ); FilePath lazypersistDir = new FilePath(bpCurrentDir, DataStorage.StorageDirLazyPersist ); FilePath rbwDir = new FilePath(bpCurrentDir, DataStorage.StorageDirRbw); if (force) { FileUtil.FullyDelete(bpDir); } else { if (!rbwDir.Delete()) { throw new IOException("Failed to delete " + rbwDir); } if (!DatanodeUtil.DirNoFilesRecursive(finalizedDir) || !FileUtil.FullyDelete(finalizedDir )) { throw new IOException("Failed to delete " + finalizedDir); } if (lazypersistDir.Exists() && ((!DatanodeUtil.DirNoFilesRecursive(lazypersistDir ) || !FileUtil.FullyDelete(lazypersistDir)))) { throw new IOException("Failed to delete " + lazypersistDir); } FileUtil.FullyDelete(tmpDir); foreach (FilePath f in FileUtil.ListFiles(bpCurrentDir)) { if (!f.Delete()) { throw new IOException("Failed to delete " + f); } } if (!bpCurrentDir.Delete()) { throw new IOException("Failed to delete " + bpCurrentDir); } foreach (FilePath f_1 in FileUtil.ListFiles(bpDir)) { if (!f_1.Delete()) { throw new IOException("Failed to delete " + f_1); } } if (!bpDir.Delete()) { throw new IOException("Failed to delete " + bpDir); } } }
/// <summary> /// Move replicas in the lazy persist directory to their corresponding locations /// in the finalized directory. /// </summary> /// <returns>number of replicas recovered.</returns> /// <exception cref="System.IO.IOException"/> private int MoveLazyPersistReplicasToFinalized(FilePath source) { FilePath[] files = FileUtil.ListFiles(source); int numRecovered = 0; foreach (FilePath file in files) { if (file.IsDirectory()) { numRecovered += MoveLazyPersistReplicasToFinalized(file); } if (Block.IsMetaFilename(file.GetName())) { FilePath metaFile = file; FilePath blockFile = Block.MetaToBlockFile(metaFile); long blockId = Block.Filename2id(blockFile.GetName()); FilePath targetDir = DatanodeUtil.IdToBlockDir(finalizedDir, blockId); if (blockFile.Exists()) { if (!targetDir.Exists() && !targetDir.Mkdirs()) { Log.Warn("Failed to mkdirs " + targetDir); continue; } FilePath targetMetaFile = new FilePath(targetDir, metaFile.GetName()); try { NativeIO.RenameTo(metaFile, targetMetaFile); } catch (IOException e) { Log.Warn("Failed to move meta file from " + metaFile + " to " + targetMetaFile, e ); continue; } FilePath targetBlockFile = new FilePath(targetDir, blockFile.GetName()); try { NativeIO.RenameTo(blockFile, targetBlockFile); } catch (IOException e) { Log.Warn("Failed to move block file from " + blockFile + " to " + targetBlockFile , e); continue; } if (targetBlockFile.Exists() && targetMetaFile.Exists()) { ++numRecovered; } else { // Failure should be rare. Log.Warn("Failed to move " + blockFile + " to " + targetDir); } } } } FileUtil.FullyDelete(source); return(numRecovered); }
/// <summary>RBW files.</summary> /// <remarks> /// RBW files. They get moved to the finalized block directory when /// the block is finalized. /// </remarks> /// <exception cref="System.IO.IOException"/> internal virtual FilePath CreateRbwFile(Block b) { FilePath f = new FilePath(rbwDir, b.GetBlockName()); return(DatanodeUtil.CreateTmpFile(b, f)); }
internal static FilePath GetMetaFile(FilePath f, long gs) { return(new FilePath(f.GetParent(), DatanodeUtil.GetMetaName(f.GetName(), gs))); }