1. 程式人生 > >hadoop2.6.0原始碼剖析-客戶端(第二部分--Configuration)

hadoop2.6.0原始碼剖析-客戶端(第二部分--Configuration)

我們來講講Configuration類,從名字可以看出,這個類跟配置檔案有關,它的繼承關係如下:

這個類比較大,我們先從一個點切入,後面有需要會追加其他部分,我們的切入點是getDefaultUri函式,但如下:

public static URI getDefaultUri(Configuration conf) {
    return URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
}

這個函式用來建立一個URI類物件,我們先進入conf.get呼叫中,這個函式程式碼如下:

/** 
   * Get the value of the <code>name</code>. If the key is deprecated,
   * it returns the value of the first key which replaces the deprecated key
   * and is not null.
   * If no such property exists,
   * then <code>defaultValue</code> is returned.
   * 
   * @param name property name, will be trimmed before get value.
   * @param defaultValue default value.
   * @return property value, or <code>defaultValue</code> if the property 
   *         doesn't exist.                    
   */
  public String get(String name, String defaultValue) {
    String[] names = handleDeprecation(deprecationContext.get(), name);
    String result = null;
    for(String n : names) {
      result = substituteVars(getProps().getProperty(n, defaultValue));
    }
    return result;
  }

這裡面getprecationContext是什麼類物件呢?這個物件的建立程式碼如下:

/**
   * The global DeprecationContext.
   */
  private static AtomicReference<DeprecationContext> deprecationContext =
                                               new AtomicReference<DeprecationContext>(new DeprecationContext(null, defaultDeprecations));
可以看到,這個類物件是一個原子引用(詳細請自己查閱相關資料,這裡不再贅述),真正是DeprecationContext類物件,這個類到底有什麼用呢?根據相關描述,這個類的物件是不可變的,類物件中儲存了所有被廢棄的key的集合。它的成員變數如下:

/**
     * Stores the deprecated keys, the new keys which replace the deprecated keys
     * and custom message(if any provided).

     //儲存廢棄的key,代替廢棄key的新key,以及相關描述,意思是說,這個map中儲存了廢棄的key以及代替該廢棄key的新key,同時包       //括一些描述資訊
     */
    private final Map<String, DeprecatedKeyInfo> deprecatedKeyMap;

    /**
     * Stores a mapping from superseding keys to the keys which they deprecate.

     //儲存廢棄的key以及取代廢棄key的新key
     */
    private final Map<String, String> reverseDeprecatedKeyMap;

建構函式如下:

/**
     * Create a new DeprecationContext by copying a previous DeprecationContext
     * and adding some deltas.
     *//建立一個新的DeprecationContext類物件,將other物件中的資料拷貝到該物件中,同時新增一些deltas
     * @param other   The previous deprecation context to copy, or null to start
     *                from nothing.
     * @param deltas  The deltas to apply.
     */
    @SuppressWarnings("unchecked")
    DeprecationContext(DeprecationContext other, DeprecationDelta[] deltas) {

      //建立HashMap類物件
      HashMap<String, DeprecatedKeyInfo> newDeprecatedKeyMap = new HashMap<String, DeprecatedKeyInfo>();

      //建立HashMap類物件
      HashMap<String, String> newReverseDeprecatedKeyMap = new HashMap<String, String>();

      //如果other不為null,那麼就將other物件成員變數deprecatedKeyMap中的key-value拷貝到newDeprecatedKeyMap中,同理將

      //other物件成員變數reverseDeprecatedKeyMap的key-value拷貝到newReverseDeprecatedKeyMap中。
      if (other != null) {
        for (Entry<String, DeprecatedKeyInfo> entry : other.deprecatedKeyMap.entrySet()) {
          newDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
        }
        for (Entry<String, String> entry : other.reverseDeprecatedKeyMap.entrySet()) {
          newReverseDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
        }
      }

      //開始遍歷deltas
      for (DeprecationDelta delta : deltas) {

        //如果newDeprecatedKeyMap不包含delta中的key,那麼就將delta中的資料新增到newDeprecatedKeyMap中,同時將key為新                      //key,value為廢棄key新增到newReverseDeprecatedKeyMap中.
        if (!newDeprecatedKeyMap.containsKey(delta.getKey())) {
          DeprecatedKeyInfo newKeyInfo = new DeprecatedKeyInfo(delta.getNewKeys(), delta.getCustomMessage());
          newDeprecatedKeyMap.put(delta.key, newKeyInfo);
          for (String newKey : delta.getNewKeys()) {
            newReverseDeprecatedKeyMap.put(newKey, delta.key);
          }
        }
      }

      //至此,newDeprecatedKeyMap和newReverseDeprecatedKeyMap資料就賦值成功了,總結一下,newDeprecatedKeyMap中                   //key為廢棄的key,value為DeprecatedKeyInfo類物件,這個物件儲存了新key和相應的關於key之間代替描述資訊,

     //而newReverseDeprecatedKeyMap這個物件中儲存了key為新key,value為老key的資訊。

     //接下來分別建立兩個UnmodifiableMap類物件,這個物件中的map物件分別為newDeprecatedKeyMap和                                                     //newReverseDeprecatedKeyMap,同時UnmodifiableMap類物件實現了Map介面,同時裡面的map變數時不可修改的。
      this.deprecatedKeyMap = UnmodifiableMap.decorate(newDeprecatedKeyMap);
      this.reverseDeprecatedKeyMap = UnmodifiableMap.decorate(newReverseDeprecatedKeyMap);
    }

另外,defaultDeprecations賦值程式碼如下:

private static DeprecationDelta[] defaultDeprecations = 
    new DeprecationDelta[] {
      new DeprecationDelta("topology.script.file.name", 
        CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY),
      new DeprecationDelta("topology.script.number.args", 
        CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_NUMBER_ARGS_KEY),
      new DeprecationDelta("hadoop.configured.node.mapping", 
        CommonConfigurationKeys.NET_TOPOLOGY_CONFIGURED_NODE_MAPPING_KEY),
      new DeprecationDelta("topology.node.switch.mapping.impl", 
        CommonConfigurationKeys.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY),
      new DeprecationDelta("dfs.df.interval", 
        CommonConfigurationKeys.FS_DF_INTERVAL_KEY),
      new DeprecationDelta("hadoop.native.lib", 
        CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_KEY),
      new DeprecationDelta("fs.default.name", 
        CommonConfigurationKeys.FS_DEFAULT_NAME_KEY),
      new DeprecationDelta("dfs.umaskmode",
        CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY),
      new DeprecationDelta("dfs.nfs.exports.allowed.hosts",
          CommonConfigurationKeys.NFS_EXPORTS_ALLOWED_HOSTS_KEY)
    };