hadoop 之源碼 ResourceManager

hadoop

RM

/**
 * The ResourceManager is the main class that is a set of components.
 * "I am the ResourceManager. All your resources belong to us..."
 *
 */
@SuppressWarnings("unchecked")
public class ResourceManager extends CompositeService implements Recoverable {

源碼中關於ResourceManager的註釋只有寥寥幾句。大概意思就是管理集羣全部的資源。
仔細看了下ResourceManager的方法列表,瞅到一mian()方法。web

public static void main(String argv[]) {
   Thread.setDefaultUncaughtExceptionHandler(new YarnUncaughtExceptionHandler());
   StringUtils.startupShutdownMessage(ResourceManager.class, argv, LOG);
   try {
     Configuration conf = new YarnConfiguration();
     // If -format-state-store, then delete RMStateStore; else startup normally
     if (argv.length == 1 && argv[0].equals("-format-state-store")) {
       deleteRMStateStore(conf);
     } else {
       ResourceManager resourceManager = new ResourceManager();
       ShutdownHookManager.get().addShutdownHook(
         new CompositeServiceShutdownHook(resourceManager),
         SHUTDOWN_HOOK_PRIORITY);
       resourceManager.init(conf);
       resourceManager.start();
     }
   } catch (Throwable t) {
     LOG.fatal("Error starting ResourceManager", t);
     System.exit(-1);
   }
 }

這裏有一關鍵點,ResourceManager在啓動時,首先調用父類init()方法,而後調用start().
這裏面有必要說明下繼承關係ResourceManager 繼承了 CompositeService,CompositeService繼承了AbstractService抽象類。
這裏使用了模板方法。AbstractService類中定義了init(),start(),stop(),close()等方法的實現,這裏就不一一列舉其源碼的實現了。oop

在init方法中分別調用了 setConfig 、serviceInit 和notifyListeners三個方法。ResourceManager複寫了serviceInit(Configuration conf)方法。
這個方法主要作如下幾件事情;this

  1. 加載core-site.xml 並 refresh SuperUserGroupsConfiguration
  2. 加載yarn-site.xml 並驗證配置文件
  3. 設置HA配置
  4. 用戶登陸
// Set UGI and do login
// If security is enabled, use login user
// If security is not enabled, use current user
this.rmLoginUGI = UserGroupInformation.getCurrentUser();
try {
  doSecureLogin();
} catch(IOException ie) {
  throw new YarnRuntimeException("Failed to login", ie);
}
  1. 各類服務的註冊(來回翻了下,沒有註釋,實在不知道是幹嗎用的)

下面看看 serviceStart()作了些什麼。code

protected void serviceStart() throws Exception {
  if (this.rmContext.isHAEnabled()) {
    transitionToStandby(true);
  } else {
    transitionToActive();
  }

  startWepApp();
  if (getConfig().getBoolean(YarnConfiguration.IS_MINI_YARN_CLUSTER,
      false)) {
    int port = webApp.port();
    WebAppUtils.setRMWebAppPort(conf, port);
  }
  super.serviceStart();
}
  1. 首先設置下HAServiceState
/**
 * An HA service may be in active or standby state. During startup, it is in
 * an unknown INITIALIZING state. During shutdown, it is in the STOPPING state
 * and can no longer return to active/standby states.
 */
public enum HAServiceState {
  INITIALIZING("initializing"),
  ACTIVE("active"),
  STANDBY("standby"),
  STOPPING("stopping");
  1. 啓動webAPP

以上就是ResourceManager啓動過程。
這裏插一段,hadoop中service的生命週期component

/** Constructed but not initialized */
NOTINITED(0, "NOTINITED"),

/** Initialized but not started or stopped */
INITED(1, "INITED"),

/** started and not stopped */
STARTED(2, "STARTED"),

/** stopped. No further state transitions are permitted */
STOPPED(3, "STOPPED");

未完待續……orm

相關文章
相關標籤/搜索