1. 程式人生 > >hbase 原始碼分析 (15)compact 過程

hbase 原始碼分析 (15)compact 過程

上一個章節分析了spit過程。當時遺留了compact問題沒有分析。這個章節將重點分析一下。 compact流程:  這個流程沒有寫完,涉及都行太多了,都沒有心情寫了。先留著吧, 入口:HStore.java 結束flush之後,會做這樣一個判斷。
  1. private boolean updateStorefiles(final List<StoreFile> sfs, final long snapshotId)
  2. throws IOException{
  3. return needsCompaction();
  4. }
主要比較
public boolean needsCompaction(final Collection<StoreFile> storeFiles,
    final List<StoreFile> filesCompacting) {
  int numCandidates = storeFiles.size() - filesCompacting.size();
return numCandidates >= comConf.getMinFilesToCompact(); }
minFilesToCompact = Math.max(2, conf.getInt(HBASE_HSTORE_COMPACTION_MIN_KEY,
/*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
public static final String HBASE_HSTORE_COMPACTION_MIN_KEY = "hbase.hstore.compaction.min"
;
當然還有一些守候程序會去觸發compact
  1. //Cache flushing thread.
  2. this.cacheFlusher = new MemStoreFlusher(conf, this);
  3. //Compaction thread
  4. this.compactSplitThread = new CompactSplitThread(this);
    1. //Background thread to check for compactions; needed if region has not gotten updates
  5. //in a while.It will take care of not checking too frequently on store
    -by-store basis.
  6. this.compactionChecker = new CompactionChecker(this, this.threadWakeFrequency, this);
  7. this.periodicFlusher = new PeriodicMemstoreFlusher(this.threadWakeFrequency, this);
1)如果觸發了compact ,需要針對當前region的每個store進行compact,在CompatSpitThread.java 
  1. private List<CompactionRequest> requestCompactionInternal(final Region r, final String why,
  2. int p,List<Pair<CompactionRequest,Store>> requests, boolean selectNow,User user)
  3. throws IOException{
  4. for(Store s : r.getStores()){
  5. CompactionRequest cr = requestCompactionInternal(r, s, why, p, null, selectNow, user);
  6. }
  7. }