无论是第一次,还是之后的每次数据块汇报,名字名字节点都会对汇报上来的数据块进行检测,看看其是否为损坏的数据块。那么,损坏数据块是如何被检测的呢?本文,我们将研究下损坏数据块检测的checkReplicaCorrupt()方法。

关于数据块及其副本的状态,请阅读《HDFS源码分析之数据块及副本状态BlockUCState、ReplicaState》一文。

checkReplicaCorrupt()方法专门用于损坏数据块检测,代码如下:

  1. /**
  2. * The next two methods test the various cases under which we must conclude
  3. * the replica is corrupt, or under construction.  These are laid out
  4. * as switch statements, on the theory that it is easier to understand
  5. * the combinatorics of reportedState and ucState that way.  It should be
  6. * at least as efficient as boolean expressions.
  7. *
  8. * @return a BlockToMarkCorrupt object, or null if the replica is not corrupt
  9. */
  10. private BlockToMarkCorrupt checkReplicaCorrupt(
  11. Block reported, ReplicaState reportedState,
  12. BlockInfo storedBlock, BlockUCState ucState,
  13. DatanodeDescriptor dn) {
  14. // 检测数据节点DataNode上的数据块副本状态ReplicaState实例reportedState
  15. switch(reportedState) {
  16. case FINALIZED:// 数据块副本如果为FINALIZED状态,即没有被修改的状态
  17. // 需要再看名字节点数据块状态BlockUCState,即ucState
  18. switch(ucState) {
  19. case COMPLETE:// 如果是COMPLETE状态,不应被汇报为坏块:这种情况下,数据节点副本已经处于不会被修改的FINALIZED状态,
  20. // 而名字节点中的数据块状态为COMPLETE,也是不会被修改的状态,
  21. // 并且其他数据节点已经汇报过该数据块对应的一个副本,所以不会是损坏的数据块
  22. case COMMITTED:// 如果是COMMITTED状态,虽然数据块不会被修改,但是还没有任何数据节点汇报过副本,还需要做以下时间戳和大小的判断:
  23. if (storedBlock.getGenerationStamp() != reported.getGenerationStamp()) {
  24. // 如果存储的数据块时间戳不等于汇报的数据块时间戳,需要汇报为坏块,
  25. // 且损坏原因为Reason.GENSTAMP_MISMATCH,即时间戳不匹配
  26. final long reportedGS = reported.getGenerationStamp();
  27. return new BlockToMarkCorrupt(storedBlock, reportedGS,
  28. "block is " + ucState + " and reported genstamp " + reportedGS
  29. + " does not match genstamp in block map "
  30. + storedBlock.getGenerationStamp(), Reason.GENSTAMP_MISMATCH);
  31. } else if (storedBlock.getNumBytes() != reported.getNumBytes()) {
  32. // 如果存储的数据块大小不等于汇报的数据块大小,需要汇报为坏块
  33. // 且损坏原因为Reason.SIZE_MISMATCH,即大小不匹配
  34. return new BlockToMarkCorrupt(storedBlock,
  35. "block is " + ucState + " and reported length " +
  36. reported.getNumBytes() + " does not match " +
  37. "length in block map " + storedBlock.getNumBytes(),
  38. Reason.SIZE_MISMATCH);
  39. } else {
  40. // 其它情况下不是一个坏块
  41. return null; // not corrupt
  42. }
  43. case UNDER_CONSTRUCTION:// 如果是UNDER_CONSTRUCTION状态,数据还在被写入,所以需要做以下时间戳的判断:
  44. if (storedBlock.getGenerationStamp() > reported.getGenerationStamp()) {
  45. // 如果存储的数据块时间戳不等于汇报的数据块时间戳,需要汇报为坏块,
  46. // 且损坏原因为Reason.GENSTAMP_MISMATCH,即时间戳不匹配
  47. final long reportedGS = reported.getGenerationStamp();
  48. return new BlockToMarkCorrupt(storedBlock, reportedGS, "block is "
  49. + ucState + " and reported state " + reportedState
  50. + ", But reported genstamp " + reportedGS
  51. + " does not match genstamp in block map "
  52. + storedBlock.getGenerationStamp(), Reason.GENSTAMP_MISMATCH);
  53. }
  54. return null;
  55. default:
  56. // 其他情况下均不是损坏的数据块
  57. return null;
  58. }
  59. case RBW:// 数据块副本为正在被写入状态,不应被汇报为坏块
  60. case RWR:// 数据块副本为正等待被恢复状态
  61. if (!storedBlock.isComplete()) {
  62. // 如果存储的数据块不是Complete状态,则不是一个坏块
  63. return null; // not corrupt
  64. } else if (storedBlock.getGenerationStamp() != reported.getGenerationStamp()) {
  65. // 否则需要判断时间戳是否一致
  66. final long reportedGS = reported.getGenerationStamp();
  67. return new BlockToMarkCorrupt(storedBlock, reportedGS,
  68. "reported " + reportedState + " replica with genstamp " + reportedGS
  69. + " does not match COMPLETE block's genstamp in block map "
  70. + storedBlock.getGenerationStamp(), Reason.GENSTAMP_MISMATCH);
  71. } else { // COMPLETE block, same genstamp
  72. if (reportedState == ReplicaState.RBW) {
  73. // 如果数据块副本为正在写入状态RBW,不会是一个损坏的数据块
  74. // If it's a RBW report for a COMPLETE block, it may just be that
  75. // the block report got a little bit delayed after the pipeline
  76. // closed. So, ignore this report, assuming we will get a
  77. // FINALIZED replica later. See HDFS-2791
  78. LOG.info("Received an RBW replica for " + storedBlock +
  79. " on " + dn + ": ignoring it, since it is " +
  80. "complete with the same genstamp");
  81. return null;
  82. } else {
  83. // 否则为一个损坏的数据块,且损坏原因为Reason.INVALID_STATE
  84. return new BlockToMarkCorrupt(storedBlock,
  85. "reported replica has invalid state " + reportedState,
  86. Reason.INVALID_STATE);
  87. }
  88. }
  89. case RUR:       // should not be reported 副本处于恢复状态下,不应被汇报为坏块
  90. case TEMPORARY: // should not be reported 副本为仅为复制而创建的临时副本,不应被汇报为坏块
  91. default:
  92. String msg = "Unexpected replica state " + reportedState
  93. + " for block: " + storedBlock +
  94. " on " + dn + " size " + storedBlock.getNumBytes();
  95. // log here at WARN level since this is really a broken HDFS invariant
  96. LOG.warn(msg);
  97. return new BlockToMarkCorrupt(storedBlock, msg, Reason.INVALID_STATE);
  98. }
  99. }

checkReplicaCorrupt()方法处理逻辑略显复杂,但是还算清晰,它需要被汇报的数据块Block实例reported、副本状态实例ReplicaState、数据块状态BlockUCState等参数,主要处理逻辑如下:

基于数据节点DataNode上的数据块副本状态ReplicaState实例reportedState进行检测,如果:

1、数据块副本状态ReplicaState为正在写入RBW、正在恢复RUR、为复制而创建的临时副本TEMPORARY三种状态,则肯定不是损坏的数据块,因为这些数据块副本还处于不确定的状态,还需要被写入或者被舍弃等;

2、数据块副本状态ReplicaState为FINALIZED的话,说明数据已被完全写入,数据块副本大小及时间戳均不会再发生变化,此时,就等着其所在数据节点DataNode进行数据块汇报,将该数据块副本汇报给名字节点NameNode,那么我们需要看数据块Block在名字节点内的状态:

2.1、如果是COMPLETE状态,不应被汇报为坏块:这种情况下,数据节点副本已经处于不会被修改的FINALIZED状态,而名字节点中的数据块状态为COMPLETE,也是不会被修改的状态,并且其他数据节点已经汇报过该数据块对应的一个副本,所以不会是损坏的数据块;

2.2、如果是COMMITTED状态,虽然数据块不会被修改,但是还没有任何数据节点汇报过副本,还需要做以下时间戳和大小的判断:

2.2.1、如果名字节点存储的数据块时间戳不等于汇报的数据块时间戳,需要汇报为坏块,且损坏原因为Reason.GENSTAMP_MISMATCH,即时间戳不匹配;

2.2.2、如果名字节点存储的数据块大小不等于汇报的数据块大小,需要汇报为坏块,且损坏原因为Reason.SIZE_MISMATCH,即大小不匹配;

2.2.3、其它情况下不是一个坏块;

2.3、如果是UNDER_CONSTRUCTION状态,数据还在被写入,所以可以忽略大小,只做以下时间戳的判断:

2.3.1、如果名字节点存储的数据块时间戳不等于汇报的数据块时间戳,需要汇报为坏块,且损坏原因为Reason.GENSTAMP_MISMATCH,即时间戳不匹配;

2.3.2、其它情况下不是一个坏块;

2.4、其他情况下均不是损坏的数据块;

3、数据块副本为正等待被恢复状态RWR的话,需要看数据块在名字节点NameNode的状态:

3.1、如果名字节点存储的数据块Block不是COMPLETE状态,则不是一个坏块,此时数据块尚未上报过名字节点NameNode;

3.2、如果名字节点存储的数据块是COMPLETE状态,说明之前已经上报过,需要判断时间戳是否一致,如果时间戳不一致的话,则说明其是一个坏块;

3.3、如果名字节点存储的数据块是COMPLETE状态,说明之前已经上报过,且时间戳一致的话,如果数据块副本为正在写入状态RBW,不会是一个损坏的数据块,否则为一个损坏的数据块,且损坏原因为Reason.INVALID_STATE;

4、其他情况下均为一个坏块,且损坏原因为Reason.INVALID_STATE。

BlockToMarkCorrupt是一个数据块标记为坏块的抽象数据结构,它包含四个成员变量,如下:

  1. /** The corrupted block in a datanode. */
  2. 数据节点上的坏块数据块信息
  3. final BlockInfo corrupted;
  4. /** The corresponding block stored in the BlockManager. */
  5. // 名字节点BlockManager中存储的相应的数据块信息
  6. final BlockInfo stored;
  7. // 损坏原因:字符串类型
  8. /** The reason to mark corrupt. */
  9. final String reason;
  10. // 损坏原因code:枚举类型
  11. /** The reason code to be stored */
  12. final Reason reasonCode;

其他的均是构造方法,及覆写的toString()方法,不再赘述!

最新文章

  1. APP切图标记PS的外挂神器-Assistor PS(转)
  2. Intellij IDEA 创建Web项目并在Tomcat中部署运行(不使用maven)【转载】
  3. mysql 存储过程简介
  4. The data is said to include information from networks
  5. 基于bootstrap的图片轮播效果展示
  6. String 和 document 的相互转换总结
  7. 三层架构与MVC的区别
  8. 破解 keyme2程序(固定明码比较)
  9. 通过手机Web实现手机摇一摇的功能
  10. Axis学习的第一天
  11. 补充:tableView优化总结
  12. iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/
  13. NOIP2005 谁拿了最多奖学金
  14. Eclipse+Maven构建web项目及部署时Maven lib依赖问题的解决
  15. According to TLD or attribute directive in tag file, attribute value does not accept any expressions
  16. ecshop 后台批量上传商品 完整上传
  17. 重要的几个热键[Tab], [ctrl]-c, [ctrl]-d
  18. Linux权限解释
  19. 小议IE10下的DrawToBitmap方法
  20. (21)纹理缓存(Texture Cache)

热门文章

  1. BZOJ【1606】购买干草
  2. Javascript&Html-location对象
  3. StringBuilder 字符串拼接扩容
  4. hdu 3440(差分约束好题)
  5. ef core 使用include进行外键连接查询
  6. 转:浅析Collections.sort
  7. Mac 奇淫巧技 哈哈
  8. PROFILE - 库存:物料状态支持 控制【物料状态定义】禁止的事务处理
  9. 【报错】项目启动,仅仅报错 One or more listeners failed to start. Full details will be found in the appropriate container log file
  10. Ubuntu免安装配置MySQL