`

namenode任务线程之HeartbeatMonitor

 
阅读更多

上篇分析了PendingReplicationMonitor   这次分析HeartbeatMonitor

 

看到这个类名,给我的第一印象就是server端定时心跳client端,其实在以前的话都是这么来做的,但是当client端太多的时候就不适合这种心跳检测模式了,改而换成client给server端发送心跳信息,server端只是负责接收心跳而已,他只是需要在一个端口listen心跳信息即可,如果在一定时间内没收到心跳信息就认为这个client挂了,这种实现方式在client多的情况下很有效,而且对server端的压力大大降低。

 

好了有了上面的铺垫,那么我们来看看HeartbeatMonitor具体做了什么

首先我们定位到具体执行心跳检测的方法上heartbeatCheck,然后看下他的java doc

 /**
   * Check if there are any expired heartbeats, and if so,
   * whether any blocks have to be re-replicated.
   * While removing dead datanodes, make sure that only one datanode is marked
   * dead at a time within the synchronized section. Otherwise, a cascading
   * effect causes more datanodes to be declared dead.
   */
  void heartbeatCheck() 

   注释说的很明白我就不翻译了,我们来看看程序的具体实现逻辑,所有的心跳信息都存放在一个列表中,

 

/**
   * Stores a set of DatanodeDescriptor objects.
   * This is a subset of {@link #datanodeMap}, containing nodes that are 
   * considered alive.
   * The {@link HeartbeatMonitor} periodically checks for outdated entries,
   * and removes them from the list.
   */
  ArrayList<DatanodeDescriptor> heartbeats 

    那么这些心跳数据从何而来呢,顺腾摸瓜最后到了DataNode,调用过程如下

 

DataNode.create()------->NameNode.register()------->FSNamesystem.registerDatanode

 

   首先看下心跳的间隔时间和过期时间

 

 long heartbeatInterval = conf.getLong("dfs.heartbeat.interval", 3) * 1000;

 this.heartbeatRecheckInterval = conf.getInt(
        "heartbeat.recheck.interval", 5 * 60 * 1000); // 5 minutes  心跳检测一次dataNode的心跳信息
 
this.heartbeatExpireInterval = 2 * heartbeatRecheckInterval +
      10 * heartbeatInterval;                                    //datanode的心跳过期时间 超过这个时间的就认为死了

 

  检测分2步

 

1:// locate the first dead node.
比较 datanode发来的心跳信息更新时间 与 过期时间 确定是否已经是死datanode

2:如果是死节点则进一步执行一些与这个节点相关的操作
例如 : 1)删除心跳检测信息
           2)更新统计信息
           3)删除块映射信息
           4)从无效集合中删除该节点信息(这个无效节点是干啥的?)
           5)删除节点集群中该节点的信息(这个信息是干啥的?)
 

上面只是在datanode创建的时候发送的心跳信息,其实在正常运行过程中也是需要发送心跳信息的:

发送过程如下:

DataNode.create------->DataNode.start------->DataNode.offerService---->NameNode.sendHeartbeat---->FSNamesystem.handleHeartbeat

 在handleHeartbeat中主要通过更新DatanodeDescriptor这个对象实现的,因为同一个DatanodeDescriptor既放在

heartbeats列表中又放在datanodeMap中,引用是一个。

 

然后这里有一点不好的是其实列表里的对象是没有重复的,那就应该使用set了,结果搞了个list,equal只比较了name和storageID

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics