无论是第一次,还是之后的每次数据块汇报,名字名字节点都会对汇报上来的数据块进行检测,看看其是否为损坏的数据块。那么,损坏数据块是如何被检测的呢?本文,我们将研究下损坏数据块检测的checkReplicaCorrupt()方法。
关于数据块及其副本的状态,请阅读《HDFS源码分析之数据块及副本状态BlockUCState、ReplicaState》一文。
checkReplicaCorrupt()方法专门用于损坏数据块检测,代码如下:
/**
* The next two methods test the various cases under which we must conclude
* the replica is corrupt, or under construction. These are laid out
* as switch statements, on the theory that it is easier to understand
* the combinatorics of reportedState and ucState that way. It should be
* at least as efficient as boolean expressions.
*
* @return a BlockToMarkCorrupt object, or null if the replica is not corrupt
*/
private BlockToMarkCorrupt checkReplicaCorrupt(
Block reported, ReplicaState reportedState,
BlockInfo storedBlock, BlockUCState ucState,
DatanodeDescriptor dn) {
// 检测数据节点DataNode上的数据块副本状态ReplicaState实例reportedState
switch(reportedState) {
case FINALIZED:// 数据块副本如果为FINALIZED状态,即没有被修改的状态
// 需要再看名字节点数据块状态BlockUCState,即ucState
switch(ucState) {
case COMPLETE:// 如果是COMPLETE状态,不应被汇报为坏块:这种情况下,数据节点副本已经处于不会被修改的FINALIZED状态,
// 而名字节点中的数据块状态为COMPLETE,也是不会被修改的状态,
// 并且其他数据节点已经汇报过该数据块对应的一个副本,所以不会是损坏的数据块
case COMMITTED:// 如果是COMMITTED状态,虽然数据块不会被修改,但是还没有任何数据节点汇报过副本,还需要做以下时间戳和大小的判断:
if (storedBlock.getGenerationStamp() != reported.g