原文链接 http://www.iteblog.com/archives/977
 
 在《Hadoop 1.x中fsimage和edits合并实现》文章中提到,Hadoop的NameNode在重启的时候,将会进入到安全模式。而在安全模式,HDFS只支持访问元数据的操作才会返回成功,其他的操作诸如创建、删除文件等操作都会导致失败。   NameNode在重启的时候,DataNode需要向NameNode发送块的信息,NameNode只有获取到整个文件系统中有99.9%(可以配置的)的块满足最小副本才会自动退出安全模式。最小副本和那个99.9%阀值可以通过下面配置来设定:
| 02 |   <name>dfs.namenode.replication.min</name> | 
 
| 04 |   <description>Minimal block replication. | 
 
| 09 |   <name>dfs.namenode.safemode.threshold-pct</name> | 
 
| 12 |     Specifies the percentage of blocks that should satisfy the minimal | 
 
| 13 |     replication requirement defined by dfs.namenode.replication.min. | 
 
| 14 |     Values less than or equal to 0mean not to wait forany particular | 
 
| 15 |     percentage of blocks before exiting safemode. | 
 
| 16 |     Values greater than 1will make safe mode permanent. | 
 
 
 
  Hadoop中每个块的默认最小副本为1;dfs.namenode.safemode.threshold-pct参数的意思是指定达到最小副本数的数据块的百分比。这个值小等于0表示无须等待就可以退出安全模式;而如果这个值大于1表示永远处于安全模式。除了上面两个参数对安全模式有影响之外,下面几个参数也会对安全模式有影响
| 02 |   <name>dfs.namenode.safemode.min.datanodes</name> | 
 
| 05 |     Specifies the number of datanodes that must be considered alive | 
 
| 06 |     before the name node exits safemode. | 
 
| 07 |     Values less than or equal to 0mean not to take the number of live | 
 
| 08 |     datanodes into account when deciding whether to remain in safe mode | 
 
| 10 |     Values greater than the number of datanodes in the cluster | 
 
| 11 |     will make safe mode permanent. | 
 
| 16 |   <name>dfs.namenode.safemode.extension</name> | 
 
| 19 |     Determines extension of safe mode in milliseconds | 
 
| 20 |     after the threshold level is reached. | 
 
 
 
  dfs.namenode.safemode.min.datanodes的意思指namenode退出安全模式之前有效的(活着的)datanode的数量。这个值小等于0表示在退出安全模式之前无须考虑有效的datanode节点个数,值大于集群中datanode节点总数则表示永远处于安全模式;dfs.namenode.safemode.extension表示在满足dfs.namenode.safemode.threshold-pct值之后,NameNode还需要处于安全模式的时间(单位是秒)。来看看代码是怎么弄的:
| 02 |  * Creates SafeModeInfo when the name node enters | 
 
| 03 |  * automatic safe mode at startup. | 
 
| 05 |  * @param conf configuration | 
 
| 07 | privateSafeModeInfo(Configuration conf) { | 
 
| 08 |       this.threshold = conf.getFloat(DFS_NAMENODE_SAFEMODE_THRESHOLD_PCT_KEY, | 
 
| 09 |           DFS_NAMENODE_SAFEMODE_THRESHOLD_PCT_DEFAULT); | 
 
| 11 |         LOG.warn("The threshold value should't be greater | 
 
| 12 |                                           than 1, threshold: " + threshold); | 
 
| 14 |       this.datanodeThreshold = conf.getInt( | 
 
| 15 |         DFS_NAMENODE_SAFEMODE_MIN_DATANODES_KEY, | 
 
| 16 |         DFS_NAMENODE_SAFEMODE_MIN_DATANODES_DEFAULT); | 
 
| 17 |       this.extension = conf.getInt(DFS_NAMENODE_SAFEMODE_EXTENSION_KEY, 0); | 
 
| 18 |       this.safeReplication =conf.getInt(DFS_NAMENODE_REPLICATION_MIN_KEY, | 
 
| 19 |                                         DFS_NAMENODE_REPLICATION_MIN_DEFAULT); | 
 
| 21 |       LOG.info(DFS_NAMENODE_SAFEMODE_THRESHOLD_PCT_KEY + " = "+ threshold); | 
 
| 22 |       LOG.info(DFS_NAMENODE_SAFEMODE_MIN_DATANODES_KEY+"="+datanodeThreshold); | 
 
| 23 |       LOG.info(DFS_NAMENODE_SAFEMODE_EXTENSION_KEY + "     = "+ extension); | 
 
| 25 |       this.replQueueThreshold = | 
 
| 26 |         conf.getFloat(DFS_NAMENODE_REPL_QUEUE_THRESHOLD_PCT_KEY, | 
 
 
 
上述代码从配置文件中获取dfs.namenode.safemode.min.datanodes、dfs.namenode.safemode.threshold-pct、dfs.namenode.replication.min和dfs.namenode.safemode.extension。   我们需要知道,当Resource is low时,只有Active NameNode才会进入到安全模式(HDFS-2914)。   当然,如果我们也可以手动进入安全模式
| 1 | hadoop fs –safemode enter | 
 
 
 
手动进入安全模式对于集群维护或者升级的时候非常有用,因为这时候HDFS上的数据是只读的。手动退出安全模式可以用下面命令:
| 1 | hadoop fs –safemode leave | 
 
 
 
如果你想获取到集群是否处于安全模式,可以用下面的命令获取:
| 1 | hadoop fs –safemode get | 
 
 
 
当然,你也可以在NameNode的50070端口的WEB页面上看到集群是否处于安全模式。
本博客文章除特别声明,全部都是原创! 
尊重原创,转载请注明: 转载自过往记忆(http://www.iteblog.com/) 
本文链接地址: 《Hadoop安全模式详解及配置》(http://www.iteblog.com/archives/977)