---
title: "Hdfs INode结构说明"
author: "sid1211"
date: "2011-03-16T10:31:18Z"
tags: ["Unix", "数据结构"]
categories: ["企业架构"]
slug: "hdfs-inode"
draft: false
lastmod: "2011-03-16T11:02:32Z"
id: 963856
url: "https://sid1211.iteye.com/blog/963856"
---

INode类在Namenode中代表了一个树状结构即Namespace,表示的是目录和文件的抽象，INode类是一个抽象类，INodeFile和INodeDirectory是具体实现。 1. INode的变量有 protected byte\[] name; protected INodeDirectory parent; protected long modificationTime; protected long accessTime; private long permission; name表示的是该文件或目录名称，parent代表其父节点，modificationTime为修改时间，accessTime是最后访问时间,permission是权限，这些属性都是仿照unix系统的文件信息显示的。 2. boolean isRoot() 方法 检查是否是根节点，方法是 return name.length == 0; 3. public final ContentSummary computeContentSummary() 计算这个INode对象树状目录下的所有文件数，block占据的空间等 4. static String\[] getPathNames(String path) 将整体目录转化为单一目录数组，举例来说，对于\\test\\a 返回一个String数组，长度是2，第一个元素是test,第二个元素是a 5. static byte\[]\[] getPathComponents(String\[] strings) 将4中的String数组转化为byte\[]\[]数组 6. boolean removeNode() 将INode对象从树状目录结构中删除，方法是首先找到其父节点，将其从父节点中删除，在从该INode对象中删除其父节点，一边垃圾回收。 parent.removeChild(this); parent = null; INodeDirectory表示了对目录结构的抽象 1. private List children; 子结点结构，所有该INode底下的子目录和子文件。 2. INode removeChild(INode node) 删除子结点，根据二分法删除。 3. void replaceChild(INode newChild) 将同名子结点替换为新的结点 4. private INode getChildINode(byte\[] name) 根据二分法获得子结点INode 5. INode getNode(String path) 这是一个对外接口，调用的是private INode getNode(byte\[]\[] components) 最后调用的功能函数是getExistingPathINodes(components, inode); 6. int getExistingPathINodes(byte\[]\[] components, INode\[] existing) 这个函数十分重要，是从目录结构中查询特定的文件名或目录名。 代码为： INode curNode = this; int count = 0; int index = existing.length - components.length; if (index &gt; 0) index = 0; while ((count &lt; components.length) && (curNode != null)) { if (index &gt;= 0) existing\[index] = curNode; if (!curNode.isDirectory() || (count == components.length - 1)) break; // no more child, stop here INodeDirectory parentDir = (INodeDirectory)curNode; curNode = parentDir.getChildINode(components\[count + 1]); count += 1; index += 1; } 一步一步遍历，通过getChildINode获得是否存在查询的INode对象，最后返回的如果存在则返回由String数据定义的文件名对应的INode对象，如果不存在则返回null值。 7. INode\[] getExistingPathINodes(String path) 方法与6相同，区别是返回的是INode\[]数组。 8. T addChild(final T node, boolean inheritPermission) 有序插入结点，向child位置的相应部分(根据二分法获得位置)插入INode 9. INodeDirectory addToParent() 根据参数将INode对象插入到相应位置。 INodeFile表示对文件的抽象 1. protected BlockInfo blocks\[] = null; 每个文件分为多个块保存，每个块大小一定，如64M。 2. protected short blockReplication; 每个块都有一定的副本个数，放置到不同的datanode中 3. protected long preferredBlockSize; 块的大小 4. Block getLastBlock() 获得最后一个块，用于append等操作 5. void addBlock(BlockInfo newblock) 因为每个块的大小都比较大，所以块的个数都比较多。这样块数据用的是数组这种数据结构，当增加新的块后开辟一个新的数组空间，大小+1，将原数组复制到新的数组中，新添加的block放到最后。具体方法为： int size = this.blocks.length; BlockInfo\[] newlist = new BlockInfo\[size + 1]; System.arraycopy(this.blocks, 0, newlist, 0, size); newlist\[size] = newblock; this.blocks = newlist; 6. int collectSubtreeBlocksAndClear(List v) 将目录下的所有block清除，并放置在List中。 7. long\[] computeContentSummary(long\[] summary) 计算信息，见INode说明。 8. Block getPenultimateBlock() 获得倒数第二个块。 INodeFileUnderConstruction表示文件正在被写入新的块，删除块等独占操作，需要同步，这里同步采用的是租约(lease) 1. String clientName; private final String clientMachine; private final DatanodeDescriptor clientNode; 表示的是正在进行操作的client，也就是拥有这个块lease(租约)的client。租约稍后再写。 2. private DatanodeDescriptor\[] targets = null; 最后一个块对应的Datanode位置信息等。 3. void addTarget(DatanodeDescriptor node) 向最后一个块加入一个datanode target 4. INodeFile convertToInodeFile() 将INodeFileUnderconstrutor转化为INodeFile对象 5. void removeBlock(Block oldblock) throws IOException 将最后一个块删除，只能删除最后一个块！ 6. synchronized void setLastBlock(BlockInfo newblock, DatanodeDescriptor\[] newtargets) 将newblock设置为新的最后的一个block，同时更新target为newtargets 7. void assignPrimaryDatanode() 设置存活的一个Datanode为主结点，并且更新其租约。