NameNode的启动和停止

1. 安全模式

NameNode启动:
(1)把fsimage加载到内存,已用edits日志。这个过程结束之后,创建一个新的检查点,包括一个新的fsimage和空的edits日志。
(2)监听IPC和HTTP请求。此时只为客户端提供一个只读视图,这种名字节点的只读模式称为安全模式。
(3)获得足够多的DataNode。
(4)当收集到足够多的第二关系信息之后,NameNode会离开安全模式

1.1 安全模式的需求

(1)处于安全模式下,HDFS只提供系统的只读视图,不能进行修改。
(2)NameNode启动时,根据配置,检查第二关系中数据块的副本信息,满足条件时离开安全模式。
(3)通过命令行支持安全模式状态查询、等待和设置。

FSNamesystem.SafeModeInfo

  • 成员变量
 // configuration fields
    /** Safe mode threshold condition %.*/
    private double threshold;
    /** Safe mode extension after the threshold. */
    private int extension;
    /** Min replication required by safe mode. */
    private int safeReplication;
      
    // internal fields
    /** Time when threshold was reached.
     * 
     * 
-1 safe mode is off *
0 safe mode is on, but threshold is not reached yet */ private long reached = -1; /** * 用于计算离开安全模式的副本的比例 **/ /** Total number of blocks. */ int blockTotal; /** Number of safe blocks. */ private int blockSafe; /** time of the last status printout */ private long lastStatusReport = 0;
  • blockTotal和blockSafe成员的修改
	 /**
     * Set total number of blocks.
     */
    synchronized void setBlockTotal(int total) 
    {
    	  	this.blockTotal = total; 
	        checkMode();
    }
  /**
     * Increment number of safe blocks if current block has 
     * reached minimal replication.
     * @param replication current replication 
     */
    synchronized void incrementSafeBlockCount(short replication) 
    {
      //达到最低副本水平数
      if ((int)replication == safeReplication)
        this.blockSafe++;
      checkMode();
    }
      
    /**
     * Decrement number of safe blocks if current block has 
     * fallen below minimal replication.
     * @param replication current replication 
     */
    synchronized void decrementSafeBlockCount(short replication) {
      if (replication == safeReplication-1)
        this.blockSafe--;
      checkMode();
    }
  • checkMode
	 /**
     * Check and trigger safe mode if needed. 
     */
    private void checkMode() 
    {
	      if (needEnter()) 
	      {
		        enter();                     // this.reached = 0;
		        reportStatus("STATE* Safe mode ON.", false);
		        return;
	      }
	      // the threshold is reached(已经满足最小副本水平条件)
	      if (!isOn() ||                           // safe mode is off
	          extension <= 0 || threshold <= 0) {      // don't need to wait
		        this.leave(true);                                 // leave safe mode
		        return;
	      }
	      if (reached > 0) {  // threshold has already been reached before(不是第一次满足最小副本水平条件)
	        reportStatus("STATE* Safe mode ON.", false);
	        return;
	      }
	      
	      /**
	      * start monitor
	      * 创建SageModeMonitor对象和它的运行线程
	      **/ 
	      reached = now();
	      smmthread = new Daemon(new SafeModeMonitor());
	      smmthread.start();
	      reportStatus("STATE* Safe mode extension entered.", true);
    }
  • SafeModeMonitor
class SafeModeMonitor implements Runnable 
{
    /** 
	    interval in msec for checking safe mode 
    	检查的时间间隔
    */
    private static final long recheckInterval = 1000;
      
    /**
     */
    public void run() 
    {
      while (fsRunning && (safeMode != null && !safeMode.canLeave())) 
      {
        try 
        {
           //等待1s
          Thread.sleep(recheckInterval);
        }
         catch (InterruptedException ie) 
         {
        }
      }
      // leave safe mode and stop the monitor
      try
     {
        //离开安全模式
        leaveSafeMode(true);
      } 
      catch(SafeModeException es)   should never happen
      { 
      String msg = "SafeModeMonitor may not run during distributed upgrade.";
      assert false : msg;
        throw new RuntimeException(msg, es);
      }
      //释放SafeModeMonitor所在的线程
      smmthread = null;
    }
  }

你可能感兴趣的:(笔试面试)