1. 程式人生 > >hadoop2.0的datanode多目錄資料副本存放策略

hadoop2.0的datanode多目錄資料副本存放策略

在hadoop2.0中,datanode資料副本存放磁碟選擇策略有兩種方式:

第一種是沿用hadoop1.0的磁碟目錄輪詢方式,實現類:RoundRobinVolumeChoosingPolicy.java

第二種是選擇可用空間足夠多的磁碟方式儲存,實現類:AvailableSpaceVolumeChoosingPolicy.java

選擇策略對應的配置項是:

  <property>
    <name>dfs.datanode.fsdataset.volume.choosing.policy</name>
    <value>org.apache.hadoop.hdfs.server.datanode.fsdataset.AvailableSpaceVolumeChoosingPolicy</value>
  </property>

如果不配置,預設使用第一種方式,既輪詢選擇磁碟來儲存資料副本,但是輪詢的方式雖然能夠保證所有磁碟都能夠被使用,但是經常會出現各個磁碟直接資料儲存不均衡問題,有的磁碟儲存得很滿了,而有的磁碟可能還有很多儲存空間沒有得到利用,所有在hadoop2.0叢集中,最好將磁碟選擇策略配置成第二種,根據磁碟空間剩餘量來選擇磁碟儲存資料副本,這樣一樣能保證所有磁碟都能得到利用,還能保證所有磁碟都被利用均衡。

在採用第二種方式時還有另外兩個引數會用到:

dfs.datanode.available-space-volume-choosing-policy.balanced-space-threshold 

預設值是10737418240,既10G,一般使用預設值就行,以下是該選項的官方解釋:

This setting controls how much DN volumes are allowed to differ in terms of bytes of free disk space before they are considered imbalanced. If the free space of all the volumes are within this range of each other, the volumes will be considered balanced and block assignments will be done on a pure round robin basis. 

意思是首先計算出兩個值,一個是所有磁碟中最大可用空間,另外一個值是所有磁碟中最小可用空間,如果這兩個值相差小於該配置項指定的閥值時,則就用輪詢方式的磁碟選擇策略選擇磁碟儲存資料副本。原始碼如下:

public boolean areAllVolumesWithinFreeSpaceThreshold() {
      long leastAvailable = Long.MAX_VALUE;
      long mostAvailable = 0;
      for (AvailableSpaceVolumePair volume : volumes) {
        leastAvailable = Math.min(leastAvailable, volume.getAvailable());
        mostAvailable = Math.max(mostAvailable, volume.getAvailable());
      }
      return (mostAvailable - leastAvailable) < balancedSpaceThreshold;
    }

dfs.datanode.available-space-volume-choosing-policy.balanced-space-preference-fraction

預設值是0.75f,一般使用預設值就行,以下是該選項的官方解釋:
This setting controls what percentage of new block allocations will be sent to volumes with more available disk space than others. This setting should be in the range 0.0 - 1.0, though in practice 0.5 - 1.0, since there should be no reason to prefer that volumes with

意思是有多少比例的資料副本應該儲存到剩餘空間足夠多的磁碟上。該配置項取值範圍是0.0-1.0,一般取0.5-1.0,如果配置太小,會導致剩餘空間足夠的磁碟實際上沒分配足夠的資料副本,而剩餘空間不足的磁碟取需要儲存更多的資料副本,導致磁碟資料儲存不均衡。

參考: