Flink - InstanceManager

简介:

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;
            }
        }
    }
}
复制代码
相关实践学习
基于Hologres+Flink搭建GitHub实时数据大屏
通过使用Flink、Hologres构建实时数仓,并通过Hologres对接BI分析工具(以DataV为例),实现海量数据实时分析.
实时计算 Flink 实战课程
如何使用实时计算 Flink 搞定数据处理难题?实时计算 Flink 极客训练营产品、技术专家齐上阵,从开源 Flink功能介绍到实时计算 Flink 优势详解,现场实操,5天即可上手! 欢迎开通实时计算 Flink 版: https://cn.aliyun.com/product/bigdata/sc Flink Forward Asia 介绍: Flink Forward 是由 Apache 官方授权,Apache Flink Community China 支持的会议,通过参会不仅可以了解到 Flink 社区的最新动态和发展计划,还可以了解到国内外一线大厂围绕 Flink 生态的生产实践经验,是 Flink 开发者和使用者不可错过的盛会。 去年经过品牌升级后的 Flink Forward Asia 吸引了超过2000人线下参与,一举成为国内最大的 Apache 顶级项目会议。结合2020年的特殊情况,Flink Forward Asia 2020 将在12月26日以线上峰会的形式与大家见面。
相关文章
|
JavaScript 前端开发 Web App开发
带你读《Three. js开发指南: 基于WebGL和HTML5在网页上渲染 3D图形和动画(原书第3版)》之一:使用Three.js创建你的第一个三维场景
本书将介绍如何直在浏览器中创建漂亮的3D场景和动画,并且充分发挥WebGL和现代浏览器的潜能。首先介绍基本概念和基础组件,然后通过逐渐扩展示例代码逐步深讲解更多高级技术。在本书中读者将学到如何从外部加载3D模型和具有真实效果的材质纹理、学习使用Three.js提供的摄像机组件来实现在3D场景中飞行和走动、如何将HTML5视频和画布作为材质贴在3D模型表面。此外还将学习变形动画和骨骼动画,甚至还会涉及在场景中使用物理模拟的方法,例如重力、碰撞检测等等。
|
存储 SQL 分布式计算
AnalyticDB for MySQL最佳实践总结
随着AnalyticDB for MySQL(下文统一简称:ADB)在阿里集团各个业务线、社会上各行各业的推广应用,我们沉淀了一些最佳实践,现在笔者整理在这里,供大家参考,希望对大家有帮助。本篇文章总结了ADB表的设计的最佳经验、数据写入的最佳经验、高效查询的最佳实践,以及一些常见的问题。 说明: 1.在读这篇文章之前,请先了解ADB的产品官方文档,以提前适当了解ADB; 2.本文写的最佳实践主要针对ADB3.0,ADB2.0在原理上也同样适用。
6355 1
AnalyticDB for MySQL最佳实践总结
|
Java Linux Docker
安装 Elasticsearch (本地及 Docker)-Elastic Stack 实战手册
本章介绍 Elasticsearch (简称 ES )的安装和部署
3281 1
安装 Elasticsearch (本地及 Docker)-Elastic Stack 实战手册
|
SQL 机器学习/深度学习 Kubernetes
官宣|Apache Flink 1.13.0 正式发布,流处理应用更加简单高效!
Flink 1.13.0 版本让流处理应用的使用像普通应用一样简单和自然,并且让用户可以更好地理解流作业的性能。
官宣|Apache Flink 1.13.0 正式发布,流处理应用更加简单高效!
|
新零售 存储 关系型数据库
基于TableStore的海量电商订单元数据管理
# 一、背景 订单系统存在于各行各业,如电商订单、银行流水、运营商话费账单等,是一个非常广泛、通用的系统。对于这类系统,在过去十几年发展中已经形成了经典的做法。但是随着互联网的发展,以及各企业对数据的重视,需要存储和持久化的订单量越来越大。
5478 0
|
算法
Matlab算法 图论最短路径问题
图论中的图(Graph)是由若干给定的点及连接两点的线所构成的图形,这种图形通常用来描述某些事物之间的某种特定关系,用点代表事物,用连接两点的线表示相应两个事物间具有这种关系。
755 0
Matlab算法 图论最短路径问题
|
存储 边缘计算 运维
阿里云李克:阿里云边缘云计算的技术和实践
李克:边缘计算的核心目标是推动人、事、物的快速决策。 在4月7日下午举办的边缘计算论坛上,阿里云资深技术专家李克为我们带来了《阿里云边缘云计算的技术和实践》为题的精彩演讲。
5515 0
阿里云李克:阿里云边缘云计算的技术和实践
|
存储 SQL 关系型数据库
【PostgreSQL 创新营】第七课:PostgreSQL性能优化和体系化运维 答疑汇总
【PostgreSQL 创新营】第七课:PostgreSQL性能优化和体系化运维 答疑汇总
721 0
【PostgreSQL 创新营】第七课:PostgreSQL性能优化和体系化运维 答疑汇总
|
机器学习/深度学习 分布式计算 运维
【云栖号案例 | 互联网 】美柚上云 致力成为最懂女人的互联网企业
美柚大数据之路的初期,遇到平台及运维成本高、扩展性低、人手经验不足等问题。通过数加达到0运维、降低大数据使用门槛,实现常用数据源的全覆盖。
【云栖号案例 | 互联网 】美柚上云 致力成为最懂女人的互联网企业
|
存储 SQL 监控
PolarDB PostgreSQL更新路计划
PolarDB PostgreSQL将持续发布对用户有价值的功能。当前我们计划了5个阶段:

热门文章

最新文章