EvenScheduler同DefaultScheduler一样,同样实现了IScheduler接口, 
由下面代码可以看出:

(ns backtype.storm.scheduler.EvenScheduler
(:use [backtype.storm util log config])
(:require [clojure.set :as set])
(:import [backtype.storm.scheduler IScheduler Topologies
Cluster TopologyDetails WorkerSlot ExecutorDetails])
(:gen-class
:implements [backtype.storm.scheduler.IScheduler])) EvenScheduler是一个对资源进行均匀分配的调度器:
(defn -prepare [this conf]
) (defn -schedule [this ^Topologies topologies ^Cluster cluster]
(schedule-topologies-evenly topologies cluster))

它是通过调用schedule-topologies-evenly方法来完成任务分配的. 
schedule-topologies-evenly方法的具体定义如下:

(defn schedule-topologies-evenly [^Topologies topologies ^Cluster cluster]
;;通过调用cluster对象的needsSchedulingTopologies方法来获取所有需要进行任务调度的Topology集合,
;;needsSchedulingTopologies方法具体定义如fn1所示.
;;判断Topology是否需要进行任务调度的依据在fn2中有说明.
(let [needs-scheduling-topologies (.needsSchedulingTopologies cluster topologies)]
(doseq [^TopologyDetails topology needs-scheduling-topologies
;;对需要进行任务调度的Topology中的每一个,首先获取它的topology-id,
:let [topology-id (.getId topology)
;;调用schedule-topology方法获取计算得到的<executor,node+port>类型集合new-assignment
;;schedule-topology方法具体定义如fn3所示.
new-assignment (schedule-topology topology cluster)
;;将new-assignment的键和值颠倒获取<node+port,executors>集合.
node+port->executors (reverse-map new-assignment)]]
;;对于前面获取的<node+port,executors>集合中的每一项进行以下操作.
(doseq [[node+port executors] node+port->executors
;;用node和port信息构造WorkerSlot对象,并将其作为slot
:let [^WorkerSlot slot (WorkerSlot. (first node+port) (last node+port))
;;下面两行代码:对于executors集合中的每一项,构造ExecutorDetail对象,
;;并返回一个ExecutorDetails集合作为executors
executors (for [[start-task end-task] executors]
(ExecutorDetails. start-task end-task))]]
;;调用cluster的assign方法将计算出来的slot分配给与该Topology相对应的executors
(.assign cluster slot topology-id executors)))))

fn1:

/**
* 获取所有需要调度的Topology,并以集合的形式返回
*/
public List<TopologyDetails> needsSchedulingTopologies(Topologies topologies) {
List<TopologyDetails> ret = new ArrayList<TopologyDetails>();
for (TopologyDetails topology : topologies.getTopologies()) {
if (needsScheduling(topology)) {
ret.add(topology);
}
}
return ret;
}

fn2:

/**
* 判断Topology是否需要进行任务调度的依据有两个:
* 1.Topology设置的NumWorkers数目是否大于已经分配给Topology的Worker数目
* 2.该Topology尚未分配的Executor的数目是否大于0
*/
public boolean needsScheduling(TopologyDetails topology) {
int desiredNumWorkers = topology.getNumWorkers();
int assignedNumWorkers = this.getAssignedNumWorkers(topology); if (desiredNumWorkers > assignedNumWorkers) {
return true;
} return this.getUnassignedExecutors(topology).size() > 0;
}

fn3:

;;该方法会根据集群当前的可用资源对Topology进行任务分配
(defn- schedule-topology [^TopologyDetails topology ^Cluster cluster]
;;获取topology-id
(let [topology-id (.getId topology)
;;调用cluster的getAvailableSlots方法获取集群当前可用的slot资源,
;;将其转换为<node,port>集合并赋值给available-slots
;;getAvailableSlots主要负责计算当前集群中还没有使用的Supervisor端口
available-slots (->> (.getAvailableSlots cluster)
(map #(vector (.getNodeId %) (.getPort %))))
;;调用getExecutors获取Topology的所有Executor信息,
;;将其转换为<start-task-id,end-task-id>集合,
;;然后赋值给all-executors并返回
all-executors (->> topology
.getExecutors
(map #(vector (.getStartTask %) (.getEndTask %)))
set)
;;调用get-alive-assigned-node+port->executors方法(具体定义如fn3_1)
;;计算当前该Topology已经分得的资源情况,
;;最后返回一个<node+port,executors>集合并将其赋值给变量alive-assigned
;;参数为cluster信息和topology-id
alive-assigned (get-alive-assigned-node+port->executors cluster topology-id)
;;计算当前Topology可以使用的slot数目,并将其赋予total-slots-to-use,
;;该值的具体内容为下面两个值的最小值:
;;1.Topology中设置的Worker数目
;;2.当前available-slots加上alive-assigned数目
total-slots-to-use (min (.getNumWorkers topology)
(+ (count available-slots) (count alive-assigned)))
;;对available-slots进行排序,计算需要分配的slot数目(total-slots-to-use减去alive-assigned)
;;最后从排序后的available-slots集合中按顺序去除这些slot并赋值给reassign-slots
reassign-slots (take (- total-slots-to-use (count alive-assigned))
(sort-slots available-slots))
;;通过比较all-executors跟已经分配的Executor集合间的差异,获取需要进行分配的Executor集合
reassign-executors (sort (set/difference all-executors (set (apply concat (vals alive-assigned)))))
;;将上述计算得到的reassign-executors与reassign-slots进行关联,转换为<executor,slot>映射集合,
;;并赋值给reassignment,此时有两种情况:
;;1.reassign-executors数目少于reassign-slots数目:意味着当前集群中的可用资源比较多,
;;eg.reassign-executors为(e1,e2,e3),reassign-slots为(s1,s2,s3,s4,s5),
;;那么匹配结果为{[e1,s1],[e2,s2],[e3,s3]}
;;2.reassign-executors数目多于reassign-slots数目:意味着当前集群的可用资源非常有限,
;;eg.reassign-executors为(e1,e2,e3,e4,e5,e6),reassign-slots为(s1,s2),
;;此时会有多个Executor被分配到同一个slot上,返回的结果可能是:
;;{[e1,s1],[e2,s1],[e3,s2],[e4,s1],[e5,s2],[e6,s2]}
reassignment (into {}
(map vector
reassign-executors
;; for some reason it goes into infinite loop without limiting the repeat-seq
(repeat-seq (count reassign-executors) reassign-slots)))]
;;判断reassignment是否为空,若不为空则打印内容为可用的slot信息的日志
(when-not (empty? reassignment)
(log-message "Available slots: " (pr-str available-slots))
)
;;返回计算得到类型为<executor,[node,port]>的集合reassignment,
reassignment))

fn3_1:

;;该方法用于获取Topology当前已经分配得到的资源
(defn get-alive-assigned-node+port->executors [cluster topology-id]
;;调用cluster的getAssignmentById获取该Topology当前的assignment
(let [existing-assignment (.getAssignmentById cluster topology-id)
;;判断当前的assignment是否为空,若不为空,则获取其中的<executor,slot>信息
executor->slot (if existing-assignment
(.getExecutorToSlot existing-assignment)
{})
;;将前面获取到的<executor,slot>转换为<executor,[node+port]>集合
executor->node+port (into {} (for [[^ExecutorDetails executor ^WorkerSlot slot] executor->slot
:let [executor [(.getStartTask executor) (.getEndTask executor)]
node+port [(.getNodeId slot) (.getPort slot)]]]
{executor node+port}))
;;将前面的<executor,[node+port]>集合转换为<[node+port],executors>集合
alive-assigned (reverse-map executor->node+port)]
;;返回得到的<[node+port],executors>集合
alive-assigned))

注:学习李明老师等Storm源码分析和陈敏敏老师等Storm技术内幕与大数据实践的笔记整理。 
欢迎关注下面二维码进行技术交流: 

JStorm与Storm源码分析(四)--均衡调度器,EvenScheduler的更多相关文章

  1. JStorm与Storm源码分析(一)--nimbus-data

    Nimbus里定义了一些共享数据结构,比如nimbus-data. nimbus-data结构里定义了很多公用的数据,请看下面代码: (defn nimbus-data [conf inimbus] ...

  2. JStorm与Storm源码分析(三)--Scheduler,调度器

    Scheduler作为Storm的调度器,负责为Topology分配可用资源. Storm提供了IScheduler接口,用户可以通过实现该接口来自定义Scheduler. 其定义如下: public ...

  3. JStorm与Storm源码分析(二)--任务分配,assignment

    mk-assignments主要功能就是产生Executor与节点+端口的对应关系,将Executor分配到某个节点的某个端口上,以及进行相应的调度处理.代码注释如下: ;;参数nimbus为nimb ...

  4. JStorm与Storm源码分析(五)--SpoutOutputCollector与代理模式

    本文主要是解析SpoutOutputCollector源码,顺便分析该类中所涉及的设计模式–代理模式. 首先介绍一下Spout输出收集器接口–ISpoutOutputCollector,该接口主要声明 ...

  5. scrapy 源码解析 (四):启动流程源码分析(四) Scheduler调度器

    Scheduler调度器 对ExecutionEngine执行引擎篇出现的Scheduler进行展开.Scheduler用于控制Request对象的存储和获取,并提供了过滤重复Request的功能. ...

  6. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备四)

    接下来,分析uilib.h中的WinImplBase.h和UIManager.h: WinImplBase.h:窗口实现基类,已实现大部分的工作,基本上窗口类均可直接继承该类,可发现该类继承于多个类, ...

  7. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备二)

    接下来,我们继续分析UIlib.h文件中余下的文件,当然部分文件可能顺序错开分析,这样便于从简单到复杂的整个过程的里面,而避免一开始就出现各种不理解的地方. 1. UIManager.h:UI管理器, ...

  8. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备一)

    上节中提到在遍历创建控件树后,执行了以下操作:      1. CDialogBuilder构建各控件对象并形成控件树,并返回第一个控件对象pRoot:     2. m_pm.AttachDialo ...

  9. Duilib源码分析(四)绘制管理器—CPaintManagerUI

    接下来,分析uilib.h中的UIManager.h,在正式分析CPaintManagerUI前先了解前面的一些宏.结构: 枚举类型EVENTTYPE_UI:定义了UIManager.h中事件通告类型 ...

随机推荐

  1. nginx四层负载均衡配置

    nginx四层负载均衡配置代理Mysql集群 环境如下: ip 192.168.6.203 Nginx ip 192.168.6.*(多台) Mysql 步骤一 查看Nginx是否安装stream模块 ...

  2. spring boot + mybatis + druid

    因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能时数据库连接的问题,所以我打算 ...

  3. R语言重要数据集分析研究——R语言数据集的字段含义

    R语言数据集的字段含义 作者:马文敏 选择一种数据结构来储存数据 将数据输入或导入到这个数据结构中 数据集的概念 数据集通常是有数据结构的一个矩形数组,行表示规则,列表示变量. 不同的行业对数据集的行 ...

  4. 关于HTML学习整理(一)

    新人,自己整理,第一次发,以后慢慢整理,欢迎指点,那些链接怎么做的,希望有人告知一下,谢谢! HTML页面写法,标签成对出现,可嵌套使用 <html> <head> <t ...

  5. jQuery – 鼠标经过(hover)事件的延时处理

    一.关于鼠标hover事件及延时 鼠标经过事件为web页面上非常常见的事件之一.简单的hover可以用CSS :hover伪类实现,复杂点的用js. 一般情况下,我们是不对鼠标hover事件进行延时处 ...

  6. Java数值避免浮点型计算丢失精度问题

    问题描述及方案 假设我们在做电商项目,在进行计算时这个丢失精度在产品价格计算就会出现问题,很有可能造成我们手里有9.99元然后后面会有一堆9,但是呢这些钱无法购买一个10元的商品. 在某些编程语言中有 ...

  7. 无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6”

    "System.InvalidOperationException"类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 无法为具有固定名称"MySql. ...

  8. php隔行换色输出表格

    <?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...

  9. Python基础-类变量和实例变量

    Python基础-类变量和实例变量 写在前面 如非特别说明,下文均基于Python3 大纲: 1. 类变量和实例变量 在Python Tutorial中对于类变量和实例变量是这样描述的: Genera ...

  10. 【Android Developers Training】 16. 暂停和恢复一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...