High availability of HDFS-NameNode mentions that NameNode has active and standby states, and it also has another state, which is SafeMode, which is safemode. In safe mode, HDFS does not accept requests for element modifications and only allows read requests for metadata. Here are a few ways to get into safe mode:

Available space

When the NameNode is started, it retrieves the amount of storage that is actually available on the disk on which the file is located. This is compared to duReserved. It returns true if the storage is greater than duReserved and false if the storage is less. The return value is assigned to the return value HasResourcesAvailable. Dureserved is 100M by default and can be passeddfs.namenode.resource.du.reservedConfigure.

When hasResourcesAvailable is false, less than 100MB of storage space is available, and safe mode is entered.

Block number

When the NameNode starts, it will load metadata from the disk fsimage. He knows how many blocks there are, but he doesn’t know if the DataNode is working properly. So he goes into safe mode and waits for the DataNode to report his block information. When the number of reported blocks reaches a certain threshold, the safe mode will exit. The following is the detailed calculation process.

In HDFS, the state of a block includes both the building state and the complete state, so to get the number of complete blocks, we need to get all of them first, and then subtract the number of blocks we are building, so we can get the number of complete blocks.

The threshold parameter, for example, is 0.9. If the number of blocks is 100, then 90 normal blocks will be sufficient. Because the number of files in the HDFS cluster is quite large, its default value is 0.999, which means that at least 999 of 1000 blocks must be normaldfs.namenode.safemode.threshold-pctConfigure.

If the DataNode reports a block number smaller than the minimum number of HDFS write requests, then enter the safe mode. If the DataNode reports a block number smaller than the threshold, then enter the safe mode.

The number of DataNodes alive

The DataAnodeThreshold is set to 0 by default, indicating that the DataNode is not enabled. If the DataNode number is set, it must be determined with the number of DataNodes. If the number of DataNodes alive is less than the set value, it needs to enter the safe mode.

The command

In addition to the above three automatic access to safe mode, we can also use the command to put HDFS into safe mode:

The command describe
hdfs dfsadmin -safemode get View the status of safe mode
hdfs dfsadmin -safemode enter Go into safe mode
hdfs dfsadmin -safemode wait Go into safe mode
hdfs dfsadmin -safemode leave Out of safe mode

Wait waits for HDFS to enter safe mode, just as if you were executing a GC command in Java without directly executing a GC command.