Пример #1
0
        /// <summary>Rename src to dst.</summary>
        /// <remarks>
        /// Rename src to dst.
        /// See
        /// <see cref="Org.Apache.Hadoop.Hdfs.DistributedFileSystem.Rename(Org.Apache.Hadoop.FS.Path, Org.Apache.Hadoop.FS.Path, Org.Apache.Hadoop.FS.Options.Rename[])
        ///     "/>
        /// for details related to rename semantics and exceptions.
        /// </remarks>
        /// <param name="fsd">FSDirectory</param>
        /// <param name="src">source path</param>
        /// <param name="dst">destination path</param>
        /// <param name="timestamp">modification time</param>
        /// <param name="collectedBlocks">blocks to be removed</param>
        /// <param name="options">Rename options</param>
        /// <returns>whether a file/directory gets overwritten in the dst path</returns>
        /// <exception cref="System.IO.IOException"/>
        internal static bool UnprotectedRenameTo(FSDirectory fsd, string src, string dst,
                                                 INodesInPath srcIIP, INodesInPath dstIIP, long timestamp, INode.BlocksMapUpdateInfo
                                                 collectedBlocks, params Options.Rename[] options)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            bool overwrite = options != null && Arrays.AsList(options).Contains(Options.Rename
                                                                                .Overwrite);
            string error;
            INode  srcInode = srcIIP.GetLastINode();

            ValidateRenameSource(srcIIP);
            // validate the destination
            if (dst.Equals(src))
            {
                throw new FileAlreadyExistsException("The source " + src + " and destination " +
                                                     dst + " are the same");
            }
            ValidateDestination(src, dst, srcInode);
            if (dstIIP.Length() == 1)
            {
                error = "rename destination cannot be the root";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new IOException(error);
            }
            BlockStoragePolicySuite bsps = fsd.GetBlockStoragePolicySuite();

            fsd.ezManager.CheckMoveValidity(srcIIP, dstIIP, src);
            INode dstInode = dstIIP.GetLastINode();
            IList <INodeDirectory> snapshottableDirs = new AList <INodeDirectory>();

            if (dstInode != null)
            {
                // Destination exists
                ValidateOverwrite(src, dst, overwrite, srcInode, dstInode);
                FSDirSnapshotOp.CheckSnapshot(dstInode, snapshottableDirs);
            }
            INode dstParent = dstIIP.GetINode(-2);

            if (dstParent == null)
            {
                error = "rename destination parent " + dst + " not found.";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new FileNotFoundException(error);
            }
            if (!dstParent.IsDirectory())
            {
                error = "rename destination parent " + dst + " is a file.";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new ParentNotDirectoryException(error);
            }
            // Ensure dst has quota to accommodate rename
            VerifyFsLimitsForRename(fsd, srcIIP, dstIIP);
            VerifyQuotaForRename(fsd, srcIIP, dstIIP);
            FSDirRenameOp.RenameOperation tx = new FSDirRenameOp.RenameOperation(fsd, src, dst
                                                                                 , srcIIP, dstIIP);
            bool undoRemoveSrc = true;

            tx.RemoveSrc();
            bool undoRemoveDst = false;
            long removedNum    = 0;

            try
            {
                if (dstInode != null)
                {
                    // dst exists, remove it
                    removedNum = tx.RemoveDst();
                    if (removedNum != -1)
                    {
                        undoRemoveDst = true;
                    }
                }
                // add src as dst to complete rename
                if (tx.AddSourceToDestination())
                {
                    undoRemoveSrc = false;
                    if (NameNode.stateChangeLog.IsDebugEnabled())
                    {
                        NameNode.stateChangeLog.Debug("DIR* FSDirectory.unprotectedRenameTo: " + src + " is renamed to "
                                                      + dst);
                    }
                    tx.UpdateMtimeAndLease(timestamp);
                    // Collect the blocks and remove the lease for previous dst
                    bool filesDeleted = false;
                    if (undoRemoveDst)
                    {
                        undoRemoveDst = false;
                        if (removedNum > 0)
                        {
                            filesDeleted = tx.CleanDst(bsps, collectedBlocks);
                        }
                    }
                    if (snapshottableDirs.Count > 0)
                    {
                        // There are snapshottable directories (without snapshots) to be
                        // deleted. Need to update the SnapshotManager.
                        fsd.GetFSNamesystem().RemoveSnapshottableDirs(snapshottableDirs);
                    }
                    tx.UpdateQuotasInSourceTree(bsps);
                    return(filesDeleted);
                }
            }
            finally
            {
                if (undoRemoveSrc)
                {
                    tx.RestoreSource();
                }
                if (undoRemoveDst)
                {
                    // Rename failed - restore dst
                    tx.RestoreDst(bsps);
                }
            }
            NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                         + src + " to " + dst);
            throw new IOException("rename from " + src + " to " + dst + " failed.");
        }
Пример #2
0
        internal static bool UnprotectedRenameTo(FSDirectory fsd, string src, string dst,
                                                 INodesInPath srcIIP, INodesInPath dstIIP, long timestamp)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INode srcInode = srcIIP.GetLastINode();

            try
            {
                ValidateRenameSource(srcIIP);
            }
            catch (SnapshotException e)
            {
                throw;
            }
            catch (IOException)
            {
                return(false);
            }
            // validate the destination
            if (dst.Equals(src))
            {
                return(true);
            }
            try
            {
                ValidateDestination(src, dst, srcInode);
            }
            catch (IOException)
            {
                return(false);
            }
            if (dstIIP.GetLastINode() != null)
            {
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                             + src + " to " + dst + " because destination " + "exists");
                return(false);
            }
            INode dstParent = dstIIP.GetINode(-2);

            if (dstParent == null)
            {
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                             + src + " to " + dst + " because destination's " + "parent does not exist");
                return(false);
            }
            fsd.ezManager.CheckMoveValidity(srcIIP, dstIIP, src);
            // Ensure dst has quota to accommodate rename
            VerifyFsLimitsForRename(fsd, srcIIP, dstIIP);
            VerifyQuotaForRename(fsd, srcIIP, dstIIP);
            FSDirRenameOp.RenameOperation tx = new FSDirRenameOp.RenameOperation(fsd, src, dst
                                                                                 , srcIIP, dstIIP);
            bool added = false;

            try
            {
                // remove src
                if (!tx.RemoveSrc4OldRename())
                {
                    return(false);
                }
                added = tx.AddSourceToDestination();
                if (added)
                {
                    if (NameNode.stateChangeLog.IsDebugEnabled())
                    {
                        NameNode.stateChangeLog.Debug("DIR* FSDirectory" + ".unprotectedRenameTo: " + src
                                                      + " is renamed to " + dst);
                    }
                    tx.UpdateMtimeAndLease(timestamp);
                    tx.UpdateQuotasInSourceTree(fsd.GetBlockStoragePolicySuite());
                    return(true);
                }
            }
            finally
            {
                if (!added)
                {
                    tx.RestoreSource();
                }
            }
            NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                         + src + " to " + dst);
            return(false);
        }