InstanceManager用于管理JobManager申请到的taskManager和slots资源

/**
* Simple manager that keeps track of which TaskManager are available and alive.
*/
public class InstanceManager { // ------------------------------------------------------------------------
// Fields
// ------------------------------------------------------------------------ //分别以InstanceId和ResourceId来索引Instance
/** Set of hosts known to run a task manager that are thus able to execute tasks (by ID). */
private final Map<InstanceID, Instance> registeredHostsById;
/** Set of hosts known to run a task manager that are thus able to execute tasks (by ResourceID). */
private final Map<ResourceID, Instance> registeredHostsByResource; /** Set of hosts that were present once and have died */
private final Set<ResourceID> deadHosts; /** Listeners that want to be notified about availability and disappearance of instances */
private final List<InstanceListener> instanceListeners = new ArrayList<>(); //Instance资源发生变化时,需要通知谁,如Scheduler /** The total number of task slots that the system has */
private int totalNumberOfAliveTaskSlots;

 

关键的操作,

registerTaskManager

/**
* Registers a task manager. Registration of a task manager makes it available to be used
* for the job execution.
*
* @param taskManagerGateway gateway to the task manager
* @param taskManagerLocation Location info of the TaskManager
* @param resources Hardware description of the TaskManager
* @param numberOfSlots Number of available slots on the TaskManager
* @return The assigned InstanceID of the registered task manager
*/
public InstanceID registerTaskManager(
TaskManagerGateway taskManagerGateway,
TaskManagerLocation taskManagerLocation,
HardwareDescription resources,
int numberOfSlots) { synchronized (this.lock) {
InstanceID instanceID = new InstanceID(); Instance host = new Instance( //创建新的instance
taskManagerGateway,
taskManagerLocation,
instanceID,
resources,
numberOfSlots); registeredHostsById.put(instanceID, host); //register
registeredHostsByResource.put(taskManagerLocation.getResourceID(), host); totalNumberOfAliveTaskSlots += numberOfSlots; host.reportHeartBeat(); // notify all listeners (for example the scheduler)
notifyNewInstance(host); return instanceID;
}
}

其中,notifyNewInstance

private void notifyNewInstance(Instance instance) {
synchronized (this.instanceListeners) {
for (InstanceListener listener : this.instanceListeners) {
try {
listener.newInstanceAvailable(instance); //调用listener的newInstanceAvailable
}
catch (Throwable t) {
LOG.error("Notification of new instance availability failed.", t);
}
}
}
}

 

Instance

看注释,instance就是一种抽象

用于描述注册到JobManager,并准备接受work的TaskManager

/**
* An instance represents a {@link org.apache.flink.runtime.taskmanager.TaskManager}
* registered at a JobManager and ready to receive work.
*/
public class Instance implements SlotOwner { /** The instance gateway to communicate with the instance */
private final TaskManagerGateway taskManagerGateway; /** The instance connection information for the data transfer. */
private final TaskManagerLocation location; /** A description of the resources of the task manager */
private final HardwareDescription resources; /** The ID identifying the taskManager. */
private final InstanceID instanceId; /** The number of task slots available on the node */
private final int numberOfSlots; /** A list of available slot positions */
private final Queue<Integer> availableSlots; //注意这里记录的不是slot,而是position,因为slot是在用的时候创建的 /** Allocated slots on this taskManager */
private final Set<Slot> allocatedSlots = new HashSet<Slot>(); /** A listener to be notified upon new slot availability */
private SlotAvailabilityListener slotAvailabilityListener; //listener用于通知当slot状态发生变化 /** Time when last heat beat has been received from the task manager running on this taskManager. */
private volatile long lastReceivedHeartBeat = System.currentTimeMillis();

核心的操作,

申请slot

/**
* Allocates a simple slot on this TaskManager instance. This method returns {@code null}, if no slot
* is available at the moment.
*
* @param jobID The ID of the job that the slot is allocated for.
*
* @return A simple slot that represents a task slot on this TaskManager instance, or null, if the
* TaskManager instance has no more slots available.
*
* @throws InstanceDiedException Thrown if the instance is no longer alive by the time the
* slot is allocated.
*/
public SimpleSlot allocateSimpleSlot(JobID jobID) throws InstanceDiedException { synchronized (instanceLock) {
Integer nextSlot = availableSlots.poll(); //看看有没有available的slot position
if (nextSlot == null) {
return null;
}
else {
SimpleSlot slot = new SimpleSlot(jobID, this, location, nextSlot, taskManagerGateway);
allocatedSlots.add(slot);
return slot;
}
}
}

 

归还slot

/**
* Returns a slot that has been allocated from this instance. The slot needs have been canceled
* prior to calling this method.
*
* <p>The method will transition the slot to the "released" state. If the slot is already in state
* "released", this method will do nothing.</p>
*
* @param slot The slot to return.
* @return True, if the slot was returned, false if not.
*/
@Override
public boolean returnAllocatedSlot(Slot slot) { if (slot.markReleased()) {
LOG.debug("Return allocated slot {}.", slot);
synchronized (instanceLock) { if (this.allocatedSlots.remove(slot)) {
this.availableSlots.add(slot.getSlotNumber()); if (this.slotAvailabilityListener != null) {
this.slotAvailabilityListener.newSlotAvailable(this); //通知有个slot可以用
} return true;
}
}
}
}

Flink - InstanceManager的更多相关文章

  1. Flink 1.1 – ResourceManager

    Flink resource manager的作用如图,   FlinkResourceManager /** * * <h1>Worker allocation steps</h1 ...

  2. Flink - FLIP

    https://cwiki.apache.org/confluence/display/FLINK/Flink+Improvement+Proposals FLIP-1 : Fine Grained ...

  3. 追源索骥:透过源码看懂Flink核心框架的执行流程

    li,ol.inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-bottom:20px}dt, ...

  4. Flink - Scheduler

    Job资源分配的过程, 在submitJob中,会生成ExecutionGraph 最终调用到, executionGraph.scheduleForExecution(scheduler) 接着,E ...

  5. Flink BLOB架构

    Flink中支持的BLOB文件类型 jar包 被user classloader使用的jar包 高负荷RPC消息 1. RPC消息长度超出了akka.framesize的大小 2. 在HA摸式中,利用 ...

  6. Flink源码阅读(1.7.2)

    目录 Client提交任务 flink的图结构 StreamGraph OptimizedPlan JobGraph ExecutionGraph flink部署与执行模型 Single Job Jo ...

  7. 透过源码看懂Flink核心框架的执行流程

    前言 Flink是大数据处理领域最近很火的一个开源的分布式.高性能的流式处理框架,其对数据的处理可以达到毫秒级别.本文以一个来自官网的WordCount例子为引,全面阐述flink的核心架构及执行流程 ...

  8. apache flink 入门

    配置环境 包括 JAVA_HOME jobmanager.rpc.address jobmanager.heap.mb 和 taskmanager.heap.mb taskmanager.number ...

  9. Apache Flink初接触

    Apache Flink闻名已久,一直没有亲自尝试一把,这两天看了文档,发现在real-time streaming方面,Flink提供了更多高阶的实用函数. 用Apache Flink实现WordC ...

随机推荐

  1. poj1988_Cube Stacking

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 24130   Accepted: 8468 Ca ...

  2. 使用emIDE创建STM32项目

    emIDE是一个开源的嵌入式集成开发环境,基于Code::Blocks开发,能够支持多个平台和多个厂家的嵌入式硬件,继承了Code::Blocks的有点. 下载emIDE并安装,也可选择绿色版.若需要 ...

  3. Android 动态生成 EditTest

    这几天,开始一个项目,需要一个小效果,就是一个输入邮箱地址的EditTest 需要动态的添加.默认有两个,然后最多5个,手机的屏幕总是有限的 好的 ,这就上代码了 布局文件 <LinearLay ...

  4. VR的国内研究现状及发展趋势

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 一.国内研究现状 我国虚拟现实技术研究起步较晚,与发达国家还有一定的差距. 随着计算机图形学.计算机系统 ...

  5. 《C#本质论》读书笔记(16)构建自定义集合

    16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...

  6. HTML行为元素和块级元素及语义化

    块级元素 div - dl - form 交互表单h1 - h6 标题 hr 水平分割线p 段落ul 非排序列表table 表格 行内元素 a 链接br 换行em 强调i 斜体img 图片input ...

  7. [BI项目记]-搭建代码管理环境之服务端

    上一篇介绍如何搭建环境进行文档版本的管理,这篇主要介绍搭建环境进行代码版本的管理. 即使是BI项目也要进行代码版本管理.代码版本管理的工具有很多,VSS, SVN等都是当下大家经常提起的,这里主要介绍 ...

  8. 速度最快的Json序列框架Jil,入门动手实录

    好吧,我又先要贴出跑分图了,出处 Jil是一个面向Json的序列化框架,在Nuget上可以下载到 支持数据类型 值得一提的是,Guid指定带破折号格式(44B2673B-B5CA-477B-A8EA- ...

  9. 8-04流程控制语句BEGIN ..END

     流程控制语句: 是用来控制程序流程的语句. 常用的流程控制语句的分类: 顺序结构:BEGIN...END 分支结构: IF ..ELSE 或CASE ..END 循环结构:WHILE 顺序结构 语法 ...

  10. error C2144: 语法错误:“int”的前面应有“;”

    百度网上都说是中文输入的问题. 但我的错误是函数声明时后面忘了加 :真是要死......