ReplicationMonitor是HDFS中关于数据块复制的监控线程,它的主要作用就是计算DataNode工作,并将复制请求超时的块重新加入到待调度队列。其定义及作为线程核心的run()方法如下:
/**
* Periodically calls computeReplicationWork().
* 周期性调用computeReplicationWork()方法
*/
private class ReplicationMonitor implements Runnable {
@Override
public void run() {
// 如果namesystem持续运行,while循环一直进行
while (namesystem.isRunning()) {
try {
// Process replication work only when active NN is out of safe mode.
if (namesystem.isPopulatingReplQueues()) {
// 计算数据节点工作
computeDatanodeWork();
// 将复制请求超时的块重新加入到待调度队列
processPendingReplications();
}
// 线程休眠replicationRecheckInterval时间
Thread.sleep(replicationRecheckInterval);
} catch (Throwable t) {
if (!namesystem.isRunning()) {
LOG.info("Stopping ReplicationMonitor.");
if (!(t instanceof InterruptedException)) {
LOG.info("ReplicationMonitor received an exception"
+ " while shutting down.", t);
}
break;
} else if (!checkNSRunning && t instanceof InterruptedException) {
LOG.info("Stopping ReplicationMonitor for testing.");
break;
}
LOG.fatal("ReplicationMonitor thread received Runtime exception. ", t);
terminate(1, t);
}
}
}
}