3. HDFS源代码分析之DataNode BlockScanner实现

本文深入剖析Hadoop中DataNode的BlockScanner机制,详细介绍了BlockScanner如何通过VolumeScanner周期性验证存储的所有数据块,以及如何处理可疑块和报告损坏块至NameNode,确保数据的完整性和可靠性。
BlockScanner实现

每个DataNode都会有一个BlockScanner周期性的验证DataNode上存储的所有数据块的正确性,并把损坏的数据块报告给NameNode。
VolumeScanner是专门针对每个存储目录做块扫描的服务,由于DataNode可以使用多目录,所以BlockScanner会持有多个VolumeScanner。

public class BlockScanner {
   
   
  ...
  // 保存VolumeScanner
  private final TreeMap<String, VolumeScanner> scanners =
      new TreeMap<String, VolumeScanner>();
  ...
	// 添加VolumeScanner对象
   public synchronized void addVolumeScanner(FsVolumeReference ref) {
   
   
	   boolean success = false;
	   try {
   
   
	     FsVolumeSpi volume = ref.getVolume();
	     if (!isEnabled()) {
   
   
	       LOG.debug("Not adding volume scanner for {}, because the block " +
	           "scanner is disabled.", volume.getBasePath());
	       return;
	     }
	     // 在map查找VolumeScanner对象
	     VolumeScanner scanner = scanners.get(volume.getStorageID());
	     if (scanner != null) {
   
   
	       LOG.error("Already have a scanner for volume {}.",
	           volume.getBasePath());
	       return;
	     }
	     LOG.debug("Adding scanner for volume {} (StorageID {})",
	         volume.getBasePath(), volume.getStorageID());
	     // 创建VolumeScanner
	     scanner = new VolumeScanner(conf, datanode, ref);
	     // 启动VolumeScanner
	     scanner.start();
	     // 将VolumeScanner加入map中
	     scanners.put(volume.getStorageID(), scanner);
	     success = true;
	   } finally {
   
   
	     if (!success) {
   
   
	       // If we didn't create a new VolumeScanner object, we don't
	       // need this reference to the volume.
	       IOUtils.cleanup(null, ref);
	     }
	 }
 }

}
VolumeScanner实现
public class VolumeScanner extends Thread {
   
   
...
  // 可疑块列表,scanner会优先扫描
  private final LinkedHashSet<ExtendedBlock> suspectBlocks =
      new LinkedHashSet<ExtendedBlock>();
...

  public void run() {
   
   
    // Record the minute on which the scanner started.
    // 记录扫描时间
    this.startMinute =
        TimeUnit.MINUTES.convert(Time.monotonicNow(), TimeUnit.MILLISECONDS);
    this.curMinute = startMinute;
    try {
   
   
      LOG.trace("{}: thread starting.", this);
      resultHandler.setup(this);
      try {
   
   
        long timeout 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值