Example #1
0
        /// <summary>Add a child inode to the directory.</summary>
        /// <param name="node">INode to insert</param>
        /// <param name="setModTime">
        /// set modification time for the parent node
        /// not needed when replaying the addition and
        /// the parent already has the proper mod time
        /// </param>
        /// <returns>
        /// false if the child with this name already exists;
        /// otherwise, return true;
        /// </returns>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        public virtual bool AddChild(INode node, bool setModTime, int latestSnapshotId)
        {
            int low = SearchChildren(node.GetLocalNameBytes());

            if (low >= 0)
            {
                return(false);
            }
            if (IsInLatestSnapshot(latestSnapshotId))
            {
                // create snapshot feature if necessary
                DirectoryWithSnapshotFeature sf = this.GetDirectoryWithSnapshotFeature();
                if (sf == null)
                {
                    sf = this.AddSnapshotFeature(null);
                }
                return(sf.AddChild(this, node, setModTime, latestSnapshotId));
            }
            AddChild(node, low);
            if (setModTime)
            {
                // update modification time of the parent directory
                UpdateModificationTime(node.GetModificationTime(), latestSnapshotId);
            }
            return(true);
        }
Example #2
0
 internal SnapshotCopy(INode inode)
 {
     this.name       = inode.GetLocalNameBytes();
     this.permission = inode.GetPermissionLong();
     if (inode.GetAclFeature() != null)
     {
         aclFeature = AclStorage.AddAclFeature(inode.GetAclFeature());
     }
     else
     {
         aclFeature = null;
     }
     this.modificationTime = inode.GetModificationTime();
     this.accessTime       = inode.GetAccessTime();
     this.xAttrFeature     = inode.GetXAttrFeature();
 }
        /// <summary>for snapshot file while modifying file after snapshot.</summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestSnapshotPathINodesAfterModification()
        {
            // First check the INode for /TestSnapshot/sub1/file1
            string[]     names       = INode.GetPathNames(file1.ToString());
            byte[][]     components  = INode.GetPathComponents(names);
            INodesInPath nodesInPath = INodesInPath.Resolve(fsdir.rootDir, components, false);

            // The number of inodes should be equal to components.length
            NUnit.Framework.Assert.AreEqual(nodesInPath.Length(), components.Length);
            // The last INode should be associated with file1
            NUnit.Framework.Assert.AreEqual(nodesInPath.GetINode(components.Length - 1).GetFullPathName
                                                (), file1.ToString());
            // record the modification time of the inode
            long modTime = nodesInPath.GetINode(nodesInPath.Length() - 1).GetModificationTime
                               ();

            // Create a snapshot for the dir, and check the inodes for the path
            // pointing to a snapshot file
            hdfs.AllowSnapshot(sub1);
            hdfs.CreateSnapshot(sub1, "s3");
            // Modify file1
            DFSTestUtil.AppendFile(hdfs, file1, "the content for appending");
            // Check the INodes for snapshot of file1
            string snapshotPath = sub1.ToString() + "/.snapshot/s3/file1";

            names      = INode.GetPathNames(snapshotPath);
            components = INode.GetPathComponents(names);
            INodesInPath ssNodesInPath = INodesInPath.Resolve(fsdir.rootDir, components, false
                                                              );

            // Length of ssInodes should be (components.length - 1), since we will
            // ignore ".snapshot"
            NUnit.Framework.Assert.AreEqual(ssNodesInPath.Length(), components.Length - 1);
            Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot s3 = GetSnapshot(ssNodesInPath
                                                                                      , "s3", 3);
            AssertSnapshot(ssNodesInPath, true, s3, 3);
            // Check the INode for snapshot of file1
            INode snapshotFileNode = ssNodesInPath.GetLastINode();

            NUnit.Framework.Assert.AreEqual(snapshotFileNode.GetLocalName(), file1.GetName());
            NUnit.Framework.Assert.IsTrue(snapshotFileNode.AsFile().IsWithSnapshot());
            // The modification time of the snapshot INode should be the same with the
            // original INode before modification
            NUnit.Framework.Assert.AreEqual(modTime, snapshotFileNode.GetModificationTime(ssNodesInPath
                                                                                          .GetPathSnapshotId()));
            // Check the INode for /TestSnapshot/sub1/file1 again
            names      = INode.GetPathNames(file1.ToString());
            components = INode.GetPathComponents(names);
            INodesInPath newNodesInPath = INodesInPath.Resolve(fsdir.rootDir, components, false
                                                               );

            AssertSnapshot(newNodesInPath, false, s3, -1);
            // The number of inodes should be equal to components.length
            NUnit.Framework.Assert.AreEqual(newNodesInPath.Length(), components.Length);
            // The last INode should be associated with file1
            int last = components.Length - 1;

            NUnit.Framework.Assert.AreEqual(newNodesInPath.GetINode(last).GetFullPathName(),
                                            file1.ToString());
            // The modification time of the INode for file3 should have been changed
            NUnit.Framework.Assert.IsFalse(modTime == newNodesInPath.GetINode(last).GetModificationTime
                                               ());
            hdfs.DeleteSnapshot(sub1, "s3");
            hdfs.DisallowSnapshot(sub1);
        }
        /// <summary>Create FileStatus with location info by file INode</summary>
        /// <exception cref="System.IO.IOException"/>
        private static HdfsLocatedFileStatus CreateLocatedFileStatus(FSDirectory fsd, string
                                                                     fullPath, byte[] path, INode node, byte storagePolicy, int snapshot, bool isRawPath
                                                                     , INodesInPath iip)
        {
            System.Diagnostics.Debug.Assert(fsd.HasReadLock());
            long size = 0;
            // length is zero for directories
            short              replication = 0;
            long               blocksize   = 0;
            LocatedBlocks      loc         = null;
            bool               isEncrypted;
            FileEncryptionInfo feInfo = isRawPath ? null : fsd.GetFileEncryptionInfo(node, snapshot
                                                                                     , iip);

            if (node.IsFile())
            {
                INodeFile fileNode = node.AsFile();
                size        = fileNode.ComputeFileSize(snapshot);
                replication = fileNode.GetFileReplication(snapshot);
                blocksize   = fileNode.GetPreferredBlockSize();
                bool inSnapshot = snapshot != Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot
                                  .CurrentStateId;
                bool isUc     = !inSnapshot && fileNode.IsUnderConstruction();
                long fileSize = !inSnapshot && isUc?fileNode.ComputeFileSizeNotIncludingLastUcBlock
                                    () : size;

                loc = fsd.GetFSNamesystem().GetBlockManager().CreateLocatedBlocks(fileNode.GetBlocks
                                                                                      (snapshot), fileSize, isUc, 0L, size, false, inSnapshot, feInfo);
                if (loc == null)
                {
                    loc = new LocatedBlocks();
                }
                isEncrypted = (feInfo != null) || (isRawPath && fsd.IsInAnEZ(INodesInPath.FromINode
                                                                                 (node)));
            }
            else
            {
                isEncrypted = fsd.IsInAnEZ(INodesInPath.FromINode(node));
            }
            int childrenNum = node.IsDirectory() ? node.AsDirectory().GetChildrenNum(snapshot
                                                                                     ) : 0;
            INodeAttributes       nodeAttrs = fsd.GetAttributes(fullPath, path, node, snapshot);
            HdfsLocatedFileStatus status    = new HdfsLocatedFileStatus(size, node.IsDirectory()
                                                                        , replication, blocksize, node.GetModificationTime(snapshot), node.GetAccessTime
                                                                            (snapshot), GetPermissionForFileStatus(nodeAttrs, isEncrypted), nodeAttrs.GetUserName
                                                                            (), nodeAttrs.GetGroupName(), node.IsSymlink() ? node.AsSymlink().GetSymlink() :
                                                                        null, path, node.GetId(), loc, childrenNum, feInfo, storagePolicy);

            // Set caching information for the located blocks.
            if (loc != null)
            {
                CacheManager cacheManager = fsd.GetFSNamesystem().GetCacheManager();
                foreach (LocatedBlock lb in loc.GetLocatedBlocks())
                {
                    cacheManager.SetCachedLocations(lb);
                }
            }
            return(status);
        }
        /// <summary>Create FileStatus by file INode</summary>
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus CreateFileStatus(FSDirectory fsd, string fullPath,
                                                        byte[] path, INode node, byte storagePolicy, int snapshot, bool isRawPath, INodesInPath
                                                        iip)
        {
            long size = 0;
            // length is zero for directories
            short replication = 0;
            long  blocksize   = 0;
            bool  isEncrypted;
            FileEncryptionInfo feInfo = isRawPath ? null : fsd.GetFileEncryptionInfo(node, snapshot
                                                                                     , iip);

            if (node.IsFile())
            {
                INodeFile fileNode = node.AsFile();
                size        = fileNode.ComputeFileSize(snapshot);
                replication = fileNode.GetFileReplication(snapshot);
                blocksize   = fileNode.GetPreferredBlockSize();
                isEncrypted = (feInfo != null) || (isRawPath && fsd.IsInAnEZ(INodesInPath.FromINode
                                                                                 (node)));
            }
            else
            {
                isEncrypted = fsd.IsInAnEZ(INodesInPath.FromINode(node));
            }
            int childrenNum = node.IsDirectory() ? node.AsDirectory().GetChildrenNum(snapshot
                                                                                     ) : 0;
            INodeAttributes nodeAttrs = fsd.GetAttributes(fullPath, path, node, snapshot);

            return(new HdfsFileStatus(size, node.IsDirectory(), replication, blocksize, node.
                                      GetModificationTime(snapshot), node.GetAccessTime(snapshot), GetPermissionForFileStatus
                                          (nodeAttrs, isEncrypted), nodeAttrs.GetUserName(), nodeAttrs.GetGroupName(), node
                                      .IsSymlink() ? node.AsSymlink().GetSymlink() : null, path, node.GetId(), childrenNum
                                      , feInfo, storagePolicy));
        }