FSEditLog之核心類分析

一、JournalSetjava

JournalSet是維護Journals的集合,FSEditLog經過initJournals(List dirs)填充該集合。node

private synchronized void initJournals(List<URI> dirs) {
    int minimumRedundantJournals = conf.getInt(
        DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_MINIMUM_KEY,
        DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_MINIMUM_DEFAULT);

    synchronized(journalSetLock) {
      journalSet = new JournalSet(minimumRedundantJournals);

      for (URI u : dirs) {
        boolean required = FSNamesystem.getRequiredNamespaceEditsDirs(conf)
            .contains(u);
        //本地文件
        if (u.getScheme().equals(NNStorage.LOCAL_URI_SCHEME)) {
          StorageDirectory sd = storage.getStorageDirectory(u);
          if (sd != null) {
            journalSet.add(new FileJournalManager(conf, sd, storage),
                required, sharedEditsDirs.contains(u));
          }
        } else {
          //jnode
          journalSet.add(createJournal(u), required,
              sharedEditsDirs.contains(u));
        }
      }
    }

    if (journalSet.isEmpty()) {
      LOG.error("No edits directories configured!");
    } 
  }

入參dirs包含了dfs.namenode.shared.edits.dir 和 dfs.namenode.edits.dir 對應jnode集羣地址和本地edits保存地址(若是沒有配置dfs.namenode.edits.dir,則使用dfs.namenode.name.dir)。對dirs中的每一個地址,根據其schema建立不一樣的JournalManager,而後存入JournalSet。apache

  • dfs.namenode.shared.edits.dir的value一般爲qjournal://jn1:8485;jn2:8485;jn3:8485/nsInfo, 根據前綴DFS_NAMENODE_EDITS_PLUGIN_PREFIX和schema qjournal 肯定對應JournalManager爲QuorumJournalManager;
  • dfs.namenode.edits.dir的value爲file:///local_path,對應FileJournalManager。
public static final String  DFS_NAMENODE_EDITS_PLUGIN_PREFIX = "dfs.namenode.edits.journal-plugin";

<property>
  <name>dfs.namenode.edits.journal-plugin.qjournal</name>
  <value>org.apache.hadoop.hdfs.qjournal.client.QuorumJournalManager</value>
</property>

JournalSet在添加JournalManager時會用兩類JournalManager構建出兩個JournalAndStream對象保存在集合中。oop

private final List<JournalAndStream> journals = new CopyOnWriteArrayList<JournalSet.JournalAndStream>();  

void add(JournalManager j, boolean required, boolean shared) {
    JournalAndStream jas = new JournalAndStream(j, required, shared);
    journals.add(jas);
}

二、JournalAndStream 每一個JournalAndStream維護一個JournalManager,其startLogSegment()方法用於產生一個新的editlog segment,並建立一個新的EditLogOutputStream 用於向新的segment中寫入操做記錄。ui

JournalAndStream

public void startLogSegment(long txId, int layoutVersion) throws IOException {
      Preconditions.checkState(stream == null);
      disabled = false;
      stream = journal.startLogSegment(txId, layoutVersion);
    }

 

三、QuorumJournalManagerspa

QuorumJournalManager負責將edit寫入JournalNodes,後續會專門分析。.net

四、FileJournalManagercode

ActiveNamenode上的FileJournalManager 負責將edit寫入本地文件(edits__${beg_txid}_${end_txid})。當每次滾動一個新的edit log segment時,其startLogSegment( )方法會被觸發,該方法建立一個新的EditLogFileOutputStream對象用於edit的寫入。xml

相關文章
相關標籤/搜索