hadoop 中的DRF算法

DRF算法主要目的是按按照主資源進(jìn)行公平分配

考慮一個(gè)有9個(gè)cpu和18GB的系統(tǒng)凌停,有兩個(gè)用戶:用戶A的每個(gè)任務(wù)都請求(1CPU饿凛,4GB)資源;用戶B的每個(gè)任務(wù)都請求(3CPU瞬欧,1GB)資源颜凯。如何為這種情況構(gòu)建一個(gè)公平分配策略?

假設(shè)A任務(wù)分配X個(gè)實(shí)例暑竟,B任務(wù)分配Y個(gè)實(shí)例斋射,有
X+3Y \leq 9
4X+Y \leq 18

A任務(wù)每個(gè)實(shí)例需要的cup占總資源的比例為1/9,內(nèi)存占總資源的比例為4/18但荤,4/18>1/9,所以A的主資源為內(nèi)存罗岖,同理可得B的主資源為CPU,現(xiàn)在讓A任務(wù)分配的主資源內(nèi)存和B任務(wù)分配的主資源CPU 公平腹躁,則由:
\frac {4X} {18}=\frac {3Y} 9

通過三個(gè)方程桑包,解得X=3,Y=2
即A類任務(wù)可以啟動3個(gè) 纺非,占用資源為(3,12)
B類任務(wù)可以啟動2個(gè)哑了,占用資源為(6,2)

hadoop對DRF的應(yīng)用

hadoop 2.7 中使用DominantResourceCalculator實(shí)現(xiàn)了cup赘方、memory這兩種主資源的公平調(diào)度
代碼如下

  /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  package org.apache.hadoop.yarn.util.resource;
  
  import org.apache.hadoop.classification.InterfaceAudience.Private;
  import org.apache.hadoop.classification.InterfaceStability.Unstable;
  import org.apache.hadoop.yarn.api.records.Resource;
  
  /**
   * A {@link ResourceCalculator} which uses the concept of  
   * <em>dominant resource</em> to compare multi-dimensional resources.
   *
   * Essentially the idea is that the in a multi-resource environment, 
   * the resource allocation should be determined by the dominant share 
   * of an entity (user or queue), which is the maximum share that the 
   * entity has been allocated of any resource. 
   * 
   * In a nutshell, it seeks to maximize the minimum dominant share across 
   * all entities. 
   * 
   * For example, if user A runs CPU-heavy tasks and user B runs
   * memory-heavy tasks, it attempts to equalize CPU share of user A 
   * with Memory-share of user B. 
   * 
   * In the single resource case, it reduces to max-min fairness for that resource.
   * 
   * See the Dominant Resource Fairness paper for more details:
   * www.cs.berkeley.edu/~matei/papers/2011/nsdi_drf.pdf
   */
  @Private
  @Unstable
  public class DominantResourceCalculator extends ResourceCalculator {
    
    @Override
    public int compare(Resource clusterResource, Resource lhs, Resource rhs) {
      
      if (lhs.equals(rhs)) {
        return 0;
      }
      
      if (isInvalidDivisor(clusterResource)) {
        //除數(shù)為0,cup和memory一大一小
        if ((lhs.getMemory() < rhs.getMemory() && lhs.getVirtualCores() > rhs
            .getVirtualCores())
            || (lhs.getMemory() > rhs.getMemory() && lhs.getVirtualCores() < rhs
                .getVirtualCores())) {
          return 0;
        } else if (lhs.getMemory() > rhs.getMemory()
            || lhs.getVirtualCores() > rhs.getVirtualCores()) {
          return 1;
        } else if (lhs.getMemory() < rhs.getMemory()
            || lhs.getVirtualCores() < rhs.getVirtualCores()) {
          return -1;
        }
      }
      //比較主資源
      float l = getResourceAsValue(clusterResource, lhs, true);
      float r = getResourceAsValue(clusterResource, rhs, true);
      
      if (l < r) {
        return -1;
      } else if (l > r) {
        return 1;
      } else {
        //主資源相等弱左,比較副資源占比
        l = getResourceAsValue(clusterResource, lhs, false);
        r = getResourceAsValue(clusterResource, rhs, false);
        if (l < r) {
          return -1;
        } else if (l > r) {
          return 1;
        }
      }
      
      return 0;
    }
  
    /**
     * Use 'dominant' for now since we only have 2 resources - gives us a slight
     * performance boost.
     * 
     * Once we add more resources, we'll need a more complicated (and slightly
     * less performant algorithm).
     */
    protected float getResourceAsValue(
        Resource clusterResource, Resource resource, boolean dominant) {
      // Just use 'dominant' resource
      return (dominant) ?
          Math.max(
              (float)resource.getMemory() / clusterResource.getMemory(), 
              (float)resource.getVirtualCores() / clusterResource.getVirtualCores()
              ) 
          :
            Math.min(
                (float)resource.getMemory() / clusterResource.getMemory(), 
                (float)resource.getVirtualCores() / clusterResource.getVirtualCores()
                ); 
    }
    
    @Override
    public int computeAvailableContainers(Resource available, Resource required) {
      return Math.min(
          available.getMemory() / required.getMemory(), 
          available.getVirtualCores() / required.getVirtualCores());
    }
  
    @Override
    public float divide(Resource clusterResource, 
        Resource numerator, Resource denominator) {
      return 
          getResourceAsValue(clusterResource, numerator, true) / 
          getResourceAsValue(clusterResource, denominator, true);
    }
    
    @Override
    public boolean isInvalidDivisor(Resource r) {
      if (r.getMemory() == 0.0f || r.getVirtualCores() == 0.0f) {
        return true;
      }
      return false;
    }
  
    @Override
    public float ratio(Resource a, Resource b) {
      return Math.max(
          (float)a.getMemory()/b.getMemory(), 
          (float)a.getVirtualCores()/b.getVirtualCores()
          );
    }
  
    @Override
    public Resource divideAndCeil(Resource numerator, int denominator) {
      return Resources.createResource(
          divideAndCeil(numerator.getMemory(), denominator),
          divideAndCeil(numerator.getVirtualCores(), denominator)
          );
    }
  
    @Override
    public Resource normalize(Resource r, Resource minimumResource,
                              Resource maximumResource, Resource stepFactor) {
      int normalizedMemory = Math.min(
        roundUp(
          Math.max(r.getMemory(), minimumResource.getMemory()),
          stepFactor.getMemory()),
        maximumResource.getMemory());
      int normalizedCores = Math.min(
        roundUp(
          Math.max(r.getVirtualCores(), minimumResource.getVirtualCores()),
          stepFactor.getVirtualCores()),
        maximumResource.getVirtualCores());
      return Resources.createResource(normalizedMemory,
        normalizedCores);
    }
  
    @Override
    public Resource roundUp(Resource r, Resource stepFactor) {
      return Resources.createResource(
          roundUp(r.getMemory(), stepFactor.getMemory()), 
          roundUp(r.getVirtualCores(), stepFactor.getVirtualCores())
          );
    }
  
    @Override
    public Resource roundDown(Resource r, Resource stepFactor) {
      return Resources.createResource(
          roundDown(r.getMemory(), stepFactor.getMemory()),
          roundDown(r.getVirtualCores(), stepFactor.getVirtualCores())
          );
    }
  
    @Override
    public Resource multiplyAndNormalizeUp(Resource r, double by,
        Resource stepFactor) {
      return Resources.createResource(
          roundUp(
              (int)Math.ceil(r.getMemory() * by), stepFactor.getMemory()),
          roundUp(
              (int)Math.ceil(r.getVirtualCores() * by), 
              stepFactor.getVirtualCores())
          );
    }
  
    @Override
    public Resource multiplyAndNormalizeDown(Resource r, double by,
        Resource stepFactor) {
      return Resources.createResource(
          roundDown(
              (int)(r.getMemory() * by), 
              stepFactor.getMemory()
              ),
          roundDown(
              (int)(r.getVirtualCores() * by), 
              stepFactor.getVirtualCores()
              )
          );
    }
  
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窄陡,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子拆火,更是在濱河造成了極大的恐慌跳夭,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件榜掌,死亡現(xiàn)場離奇詭異,居然都是意外死亡乘综,警方通過查閱死者的電腦和手機(jī)憎账,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卡辰,“玉大人胞皱,你說我怎么就攤上這事【怕瑁” “怎么了反砌?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長萌朱。 經(jīng)常有香客問我宴树,道長,這世上最難降的妖魔是什么晶疼? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任酒贬,我火速辦了婚禮,結(jié)果婚禮上翠霍,老公的妹妹穿的比我還像新娘锭吨。我一直安慰自己,他們只是感情好寒匙,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布零如。 她就那樣靜靜地躺著,像睡著了一般锄弱。 火紅的嫁衣襯著肌膚如雪考蕾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天会宪,我揣著相機(jī)與錄音辕翰,去河邊找鬼。 笑死狈谊,一個(gè)胖子當(dāng)著我的面吹牛喜命,可吹牛的內(nèi)容都是我干的沟沙。 我是一名探鬼主播,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼壁榕,長吁一口氣:“原來是場噩夢啊……” “哼矛紫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起牌里,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤颊咬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后牡辽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體喳篇,經(jīng)...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年态辛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了麸澜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,650評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奏黑,死狀恐怖炊邦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情熟史,我是刑警寧澤馁害,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站蹂匹,受9級特大地震影響碘菜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜限寞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一炉媒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昆烁,春花似錦吊骤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鼠渺,卻和暖如春鸭巴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拦盹。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工鹃祖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人普舆。 一個(gè)月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓恬口,卻偏偏與公主長得像校读,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子祖能,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評論 2 349

推薦閱讀更多精彩內(nèi)容