`

namenode这个类的主要功能

阅读更多

今天来总看下namenode这个类的主要功能

首先看下这个类的注释说明:

 

Java代码   收藏代码
  1. /**********************************************************  
  2.  * NameNode serves as both directory namespace manager and  
  3.  * "inode table" for the Hadoop DFS.  There is a single NameNode  
  4.  * running in any DFS deployment.  (Well, except when there  
  5.  * is a second backup/failover NameNode.)  
  6.  *  
  7.  * The NameNode controls two critical tables:  
  8.  *   1)  filename->blocksequence (namespace)  
  9.  *   2)  block->machinelist ("inodes")  
  10.  *  
  11.  * The first table is stored on disk and is very precious.  
  12.  * The second table is rebuilt every time the NameNode comes  
  13.  * up.  
  14.  *  
  15.  * 'NameNode' refers to both this class as well as the 'NameNode server'.  
  16.  * The 'FSNamesystem' class actually performs most of the filesystem  
  17.  * management.  The majority of the 'NameNode' class itself is concerned  
  18.  * with exposing the IPC interface and the http server to the outside world,  
  19.  * plus some configuration management.  
  20.  *  
  21.  * NameNode implements the ClientProtocol interface, which allows  
  22.  * clients to ask for DFS services.  ClientProtocol is not  
  23.  * designed for direct use by authors of DFS client code.  End-users  
  24.  * should instead use the org.apache.nutch.hadoop.fs.FileSystem class.  
  25.  *  
  26.  * NameNode also implements the DatanodeProtocol interface, used by  
  27.  * DataNode programs that actually store DFS data blocks.  These  
  28.  * methods are invoked repeatedly and automatically by all the  
  29.  * DataNodes in a DFS deployment.  
  30.  *  
  31.  * NameNode also implements the NamenodeProtocol interface, used by  
  32.  * secondary namenodes or rebalancing processes to get partial namenode's  
  33.  * state, for example partial blocksMap etc.  
  34.  **********************************************************/   

 

注释里说namenode这个节点维护的信息是:文件与文件块的关联关系(也叫namespache),这个主要由之前blog里的INodeFile这个类体现出来。文件块与机器节点之间的关系,可以用DatanodeDescriptor和BlockInfo体现出来,以后会写篇blog分析如何依据这3个模型来查询与新增修改数据关系等。

file与block的对应关系这个会持久化,如果namenode重启时需要加载这些数据,对于block与datanode的关系,因为只有持有数据的人才有资格说话,所以这个数据为了保证是最新的,需要由datanode来report给namenode,这个report是每次在心跳时一起发送的(具体分析datanode时再细说),在namenode重启时进入到safemode,datanode就会连接上namenode然后report自己磁盘上所有的block信息,当达到一定的额度就认为所有的datanode与block的信息report完毕然后退出safemode。

主要的NameNode Server的工作都是交给FSNamesystem来完成的,这个类有4700多行,他自己只是参与IPC与jetty web的功能,所以Namenode的

主要功能都在   FSNamesystem这个类里实现的。

 

因为Namenode自身的重要性,所以他作为一个枢纽联系着client  datanode  secondNameNode,从设计哲学上说就变成了namenode这个类需要实现满足枢纽功能的三个接口,分别对应与client datanode secondName做交互,当你觉得namenode需要另外一种职责时可以定义新的接口,然后让namenode实现即可。

 

首先是ClientProtocol,先看下他的注释说明

 

Java代码   收藏代码
  1. /**********************************************************************  
  2.  * ClientProtocol is used by user code via   
  3.  * {@link org.apache.hadoop.hdfs.DistributedFileSystem} class to communicate   
  4.  * with the NameNode.  User code can manipulate the directory namespace,   
  5.  * as well as open/close file streams, etc.  
  6.  *  
  7.  **********************************************************************/   

 

 例如client可以调用FileSystem来完成文件的上传,删除等等,我们可以看到这个接口里比较常用的2个函数

  新建文件的

 

Java代码   收藏代码
  1.   /**  
  2.    * Create a new file entry in the namespace.  
  3.    * <p>  
  4.    * This will create an empty file specified by the source path.  
  5.    * The path should reflect a full path originated at the root.  
  6.    * The name-node does not have a notion of "current" directory for a client.  
  7.    * <p>  
  8.    * Once created, the file is visible and available for read to other clients.  
  9.    * Although, other clients cannot {@link #delete(String)}, re-create or   
  10.    * {@link #rename(String, String)} it until the file is completed  
  11.    * or explicitly as a result of lease expiration.  
  12.    * <p>  
  13.    * Blocks have a maximum size.  Clients that intend to  
  14.    * create multi-block files must also use {@link #addBlock(String, String)}.  
  15.    *  
  16.    * @param src path of the file being created.  
  17.    * @param masked masked permission.  
  18.    * @param clientName name of the current client.  
  19.    * @param overwrite indicates whether the file should be   
  20.    * overwritten if it already exists.  
  21.    * @param replication block replication factor.  
  22.    * @param blockSize maximum block size.  
  23.    *   
  24.    * @throws AccessControlException if permission to create file is   
  25.    * denied by the system. As usually on the client side the exception will   
  26.    * be wrapped into {@link org.apache.hadoop.ipc.RemoteException}.  
  27.    * @throws QuotaExceededException if the file creation violates   
  28.    *                                any quota restriction  
  29.    * @throws IOException if other errors occur.  
  30.    */   
  31.   
  32. public   void  create(String src,   
  33.                      FsPermission masked,  
  34.                              String clientName,   
  35.                              boolean  overwrite,   
  36.                              short  replication,  
  37.                              long  blockSize  
  38.                              )  

 

 删除文件的,对应的fs命令为rm -rm/-rmr

 

Java代码   收藏代码
  1. /**  
  2.    * Delete the given file or directory from the file system.  
  3.    * <p>  
  4.    * same as delete but provides a way to avoid accidentally   
  5.    * deleting non empty directories programmatically.   
  6.    * @param src existing name  
  7.    * @param recursive if true deletes a non empty directory recursively,  
  8.    * else throws an exception.  
  9.    * @return true only if the existing file or directory was actually removed   
  10.    * from the file system.   
  11.    */   
  12.   public   boolean  delete(String src,  boolean  recursive)  

 

 

再来看下DatanodeProtocol,以下是注释说明

 

Java代码   收藏代码
  1. /* NameNode also implements the DatanodeProtocol interface, used by  
  2.  * DataNode programs that actually store DFS data blocks.  These  
  3.  * methods are invoked repeatedly and automatically by all the  
  4.  * DataNodes in a DFS deployment.  
  5.  
  6. **********************************************************************/   
  7.   
  8. /**********************************************************************  
  9.  * Protocol that a DFS datanode uses to communicate with the NameNode.  
  10.  * It's used to upload current load information and block reports.  
  11.  *  
  12.  * The only way a NameNode can communicate with a DataNode is by  
  13.  * returning values from these functions.  
  14.  *  
  15.  **********************************************************************/   

 

 

   具体看几个常用函数就明白这个意思了,同时注意还有个返回值,是namenode返回给datanode的,这个等分析datanode再说。

  

Java代码   收藏代码
  1. /** Register Datanode.  
  2.   *  
  3.   * @see org.apache.hadoop.hdfs.server.datanode.DataNode#dnRegistration  
  4.   * @see org.apache.hadoop.hdfs.server.namenode.FSNamesystem#registerDatanode(DatanodeRegistration)  
  5.   *   
  6.   * @return updated {@link org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration}, which contains   
  7.   * new storageID if the datanode did not have one and  
  8.   * registration ID for further communication.  
  9.   */   
  10.  public  DatanodeRegistration register(DatanodeRegistration registration)  
  11. 当datanode启动时就调用这个。  
  12.     
  13.   /** sendHeartbeat() tells the NameNode that the DataNode is still  
  14.   * alive and well.  Includes some status info, too.   
  15.   * It also gives the NameNode a chance to return   
  16.   * an array of "DatanodeCommand" objects.  
  17.   * A DatanodeCommand tells the DataNode to invalidate local block(s),   
  18.   * or to copy them to other DataNodes, etc.  
  19.   */   
  20.  public  DatanodeCommand[] sendHeartbeat(DatanodeRegistration registration,  
  21.                                       long  capacity,  
  22.                                       long  dfsUsed,  long  remaining,  
  23.                                       int  xmitsInProgress,  
  24.                                       int  xceiverCount)  
  25.   
  26.   
  27.   
  28. /**  
  29.   * blockReport() tells the NameNode about all the locally-stored blocks.  
  30.   * The NameNode returns an array of Blocks that have become obsolete  
  31.   * and should be deleted.  This function is meant to upload *all*  
  32.   * the locally-stored blocks.  It's invoked upon startup and then  
  33.   * infrequently afterwards.  
  34.   * @param registration  
  35.   * @param blocks - the block list as an array of longs.  
  36.   *     Each block is represented as 2 longs.  
  37.   *     This is done instead of Block[] to reduce memory used by block reports.  
  38.   *       
  39.   * @return - the next command for DN to process.  
  40.   * @throws IOException  
  41.   */   
  42.  public  DatanodeCommand blockReport(DatanodeRegistration registration,  
  43.                                     long [] blocks)  

 

再来看下NamenodeProtocol这个接口,首先看下注释

 

Java代码   收藏代码
  1. /* NameNode also implements the NamenodeProtocol interface, used by  
  2.  * secondary namenodes or rebalancing processes to get partial namenode's  
  3.  * state, for example partial blocksMap etc.  
  4.  */   
  5. /*****************************************************************************  
  6.  * Protocol that a secondary NameNode uses to communicate with the NameNode.  
  7.  * It's used to get part of the name node state  
  8.  *****************************************************************************/   

 

  这个接口提供了2个功能,一个是和secondNamenode之间的交互使用,另外一个是给balance使用,那么分别看看这2个功能接口。

balance主要是查看单个datanode机器上的block数,如果发现某个机器上的数很大,超出普通的那就需要将这个机器上的block移动到一个block数少的机器上了,那么做操作之前需要有一些数据支持,所以由namenode来提供了,接口是

 

Java代码   收藏代码
  1. /** Get a list of blocks belonged to <code>datanode</code>  
  2.    * whose total size is equal to <code>size</code>  
  3.   * @param datanode  a data node  
  4.   * @param size      requested size  
  5.   * @return          a list of blocks & their locations  
  6.   * @throws RemoteException if size is less than or equal to 0 or  
  7.                                   datanode does not exist  
  8.   */   
  9.  public  BlocksWithLocations getBlocks(DatanodeInfo datanode,  long  size)  
  10.  throws  IOException;  

 这个具体的调用实现等分析到balance时再细细分析下。

   和secondeNamenode相关的接口如下:

 

Java代码   收藏代码
  1. /**  
  2.   * Closes the current edit log and opens a new one. The   
  3.   * call fails if the file system is in SafeMode.  
  4.   * @throws IOException  
  5.   * @return a unique token to identify this transaction.  
  6.   */   
  7.  public  CheckpointSignature rollEditLog()  throws  IOException;  
  8.   
  9.  /**  
  10.   * Rolls the fsImage log. It removes the old fsImage, copies the  
  11.   * new image to fsImage, removes the old edits and renames edits.new   
  12.   * to edits. The call fails if any of the four files are missing.  
  13.   * @throws IOException  
  14.   */   
  15.  public   void  rollFsImage()  throws  IOException;  

 

 针对备份或者说二级namenode的具体实施过程需要有个流程图的东西来解释下。

更多信息请查看 java进阶网 http://www.javady.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics