使用java在aliyun/aws创建E-MapReduce (emr)集群

2024-02-09 15:38

本文主要是介绍使用java在aliyun/aws创建E-MapReduce (emr)集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 背景
    • 功能点
    • 开撸
      • emr接口
      • emr抽象类
      • 阿里云集群的创建

背景

在上个公司,我的 hera 任务调度系统是运行在本地 cdh 机器上的,并没有使用 aws/aliyun 提供的 emr 服务。所以为了使 hera 能够兼容 emr,就需要使用 java 创建 emr 集群.

功能点

既然要创建集群,肯定也要有等待集群创建完成、销毁集群等操作。

所以功能点大概有

  • 判断集群是否已经创建过
  • 创建集群
  • 等待集群创建完成
  • 集群销毁
  • 获得集群的登录脚本
  • 集群弹性伸缩
  • 等等

开撸

emr接口

为了增加程序的可维护性和扩展性,我提供了一个Emr接口类

/*** desc:** @author scx* @create 2019/04/01*/
public interface Emr {/*** 添加任务接口*/void addJob();/*** 移除任务*/void removeJob();/*** 获得登录命令* @return  登录命令*/String getLogin(String user);}

主要有三个方法,其中 addJobremoveJob 的作用是为了后面进行判断集群是否有任务执行来进行关闭集群的操作。而 getLogin 方法是为了获得集群的 ssh 登录命令

emr抽象类

我希望在抽象类中完成所有的同步操作、集群关闭监听事件、等待集群创建完成 等等公共操作,而子类只关注一些简单实现即可。具体请看源码

/*** desc: emr集群 抽象类** @author scx* @create 2019/04/01*/
public abstract class AbstractEmr implements Emr {/*** 缓存的集群IP*/protected volatile String cacheIp = null;/*** 任务数*/private AtomicInteger taskRunning;/*** 任务计数器*/private AtomicLong taskNum;/*** 缓存的集群Id*/private String cacheClusterId;/*** 关闭集群的调度池*/private ScheduledExecutorService pool;/*** 上次的任务数*/private long cacheTaskNum;/*** 集群是否已经关闭字段*/private volatile boolean clusterTerminate = true;/*** check 集群是否需要关闭返回的future*/private ScheduledFuture<?> clusterWatchFuture;/*** 当前应用的环境 设置不同的参数*/private String env = HeraGlobalEnvironment.getEnv() == null ? "daily" : HeraGlobalEnvironment.getEnv();/*** emr集群的前缀*/protected final String clusterPrefix = "hera-schedule-" + env + "-";/*** 自动扩展冷却时间 单位:秒*/private int coolDown = 300;/*** 扩展百分比*/private int scalePercent = 10;/*** 最少实例数*/private int minCapacity = 1;/*** 最大实例数*/private int maxCapacity = 10;public int getCoolDown() {return coolDown;}public int getScalePercent() {return scalePercent;}public int getMinCapacity() {return minCapacity;}public int getMaxCapacity() {return maxCapacity;}protected String getClusterName() {return clusterPrefix + ActionUtil.getCurrDate();}protected String getEnv() {return env;}/*** 创建集群的方法*/protected void createCluster() {//抽象方法留给子类实现init();//dubbo-check 同步,避免多次创建if (clusterTerminate) {synchronized (this) {if (clusterTerminate) {//启动时可能已经有创建好的集群 首先判断if (notAlive()) {//如果没有创建好的集群,发送创建集群的请求,抽象方法具体留给子类实现cacheClusterId = sendCreateRequest();}//参数初始化clusterTerminate = false;taskRunning = new AtomicInteger(0);taskNum = new AtomicLong(0);//等待集群创建完成waitClusterCompletion();//集群关闭的监视器submitClusterWatch();MonitorLog.info("集群创建完成,可以执行任务了.集群ID为:" + cacheClusterId + ".集群IP为:" + getIp());}}} else {//此时虽然集群被判断为活着,但是也有可能被人为关闭,recheckif (notAlive()) {destroyCluster();createCluster();}}}@Overridepublic void addJob() {//每次调用addJob方法都要执行一下createCluster方法createCluster();//正在执行的任务数+1taskRunning.incrementAndGet();//总任务数计数taskNum.incrementAndGet();}@Overridepublic void removeJob() {//正在执行的任务数-1if (taskRunning != null) {taskRunning.decrementAndGet();}}/*** 判断是否有创建好的集群* @return  结果*/private boolean notAlive() {//getAliveId 抽象方法留给具体子类实现,返回已经创建好的集群idreturn StringUtils.isBlank(cacheClusterId = getAliveId());}/*** 获得emr集群的登录ip/域名* @return  ip/域名*/public String getIp() {//同样dubbo-check 避免多次判断if (cacheIp == null) {synchronized (this) {if (cacheIp == null) {createCluster();// Amazon返回的是域名 ,aliyun目前只返回了master的内网ip,可以自己自定义 getMasterIp 的实现cacheIp = getMasterIp(cacheClusterId);if (StringUtils.isBlank(cacheIp)) {throw new NullPointerException("cacheIp can not be null");}}}}return cacheIp;}/*** 循环检测 ,等待集群创建完成*/private void waitClusterCompletion() {long start = System.currentTimeMillis();long sleepTime = 15 * 1000 * 1000000L;// 必须同步 不能异步while (!isCompletion(cacheClusterId)) {LockSupport.parkNanos(sleepTime);}MonitorLog.info("创建集群:" + cacheClusterId + "耗时:" + (System.currentTimeMillis() - start) + "ms");}/*** createCluster 方法已经同步过* 关于为什么为这里使用的11分钟:我的hera任务调度系统在任务失败后有任务重试,* 重试的间隔时间为10分钟,由于创建集群的时间过长,* 避免任务在重试期间集群被关闭,然后又重新创建,* 所以选择了略大于10的值11。具体时间,大家可以自己衡量*/private void submitClusterWatch() {if (clusterWatchFuture == null) {if (pool == null) {pool = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("cluster-destroy-watch", true));}//11分钟前的任务数cacheTaskNum = taskNum.get();clusterWatchFuture = pool.scheduleWithFixedDelay(() -> {MonitorLog.info("正在emr集群运行的任务个数:{},十分钟前运行的总任务个数:{},现在运行的总任务个数:{}", taskRunning.get(), cacheTaskNum, taskNum.get());//如果正在执行的任务数为0并且上次记录的任务总数与当前的任务总数一致,考虑关闭集群if (taskRunning.get() == 0 && cacheTaskNum == taskNum.get()) {terminateCluster(cacheClusterId);MonitorLog.info("集群:" + cacheClusterId + "关闭成功,执行任务数为:" + taskNum.get());//关闭调度clusterWatchFuture.cancel(true);} else {//重新设置任务数cacheTaskNum = taskNum.get();}}, 11, 11, TimeUnit.MINUTES);}}/*** 初始化client操作*/protected abstract void init();/*** 关闭集群操作** @param clusterId clusterId*/protected abstract void terminateCluster(String clusterId);/*** 关闭client*/protected abstract void shutdown();/*** 发送创建集群的请求** @return 返回clusterId*/protected abstract String sendCreateRequest();/*** 判断以clusterPrefix开头的机器是否有存活** @return StringUtils.isBlank() 表示无存活*/protected abstract String getAliveId();/*** 检测集群是否创建完成** @param clusterId clusterId* @return*/protected abstract boolean isCompletion(String clusterId);/*** 获得master的ip** @param clusterId clusterId* @return*/protected abstract String getMasterIp(String clusterId);/*** 销毁集群,以及做一些后置操作*/protected synchronized void destroyCluster() {if (!clusterTerminate) {clusterTerminate = true;cacheIp = null;cacheClusterId = null;clusterWatchFuture = null;pool.shutdown();pool = null;shutdown();}}
}

阿里云集群的创建

这篇关于使用java在aliyun/aws创建E-MapReduce (emr)集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694612

相关文章

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except