storm shell命令源码分析-shell_submission.clj
当我们在shell里执行storm shell命令时会调用shell_submission.clj里的main函数。shell_submission.clj如下:
shell_submission.clj
(ns backtype.storm.command.shell-submission
;; :import引用backtype.storm中的StormSubmitter类
(:import [backtype.storm StormSubmitter])
;; :use引用backtype.storm thrift命名空间中的thrift、util、config和log,并建立连接,这样调用thrift、util、config和log中函数就可以直接使用函数名称不需要加完全限定名
(:use [backtype.storm thrift util config log])
;; :require引用clojure.string,并使用别名str代替完全限定名clojure.string
(:require [clojure.string :as str])
;; :gen-class生成java类
(:gen-class))
;; storm shell命令所执行的main函数,gen-class的默认前缀"-",-main函数可以看成public函数,^String是类型提示符,用于声明参数tmpjarpath是一个字符串,-main函数可以接受多个实参,第一个参数赋值给tmpjarpath,其他参数全部保存在args中,args一个"序列"
(defn -main [^String tmpjarpath & args]
;; conf绑定集群配置信息map,read-storm-config函数定义在backtype.storm.config命令空间,用于读取集群配置信息,返回包含集群配置信息的map,read-storm-config函数参见其定义部分
(let [conf (read-storm-config)
;; 从集群配置信息中获取nimbus主机
host (conf NIMBUS-HOST)
;; 从集群配置信息中获取nimbus thrift server的端口
port (conf NIMBUS-THRIFT-PORT)
;; 调用StormSubmitter类的静态方法submitJar,将tmpjarpath所标识的jar文件上传到nimbus服务器上,jarpath保存jar文件在nimbus服务器上的路径,submitJar方法参见其定义部分
jarpath (StormSubmitter/submitJar conf tmpjarpath)
;; concat函数将[host port jarpath]和args进行合并,并保存在args中
args (concat args [host port jarpath])]
;; str/join将args中的参数用空格进行连接后,作为参数传递给exec-command!函数,执行jar文件中的main方法
(exec-command! (str/join " " args))
))
当Clojure源文件做为脚本执行时,它们将在运行时被编译为java字节码。它们同样可以提前编译为java字节码(AOT编译)。这改善了Clojure应用的启动时间,并生产了可以运用于java中的.class文件。如果编译过的命名空间中拥有一个叫做-main的函数,那么它就能够作为一个Java应用运行。命令行参数会作为参数传递给这个函数。
read-storm-config函数
(defn read-storm-config
[]
;; readStormConfig方法参见其定义部分
(let [conf (clojurify-structure (Utils/readStormConfig))]
;; 调用validate-configs-with-schemas函数验证配置信息的正确性并删除不正确的配置信息
(validate-configs-with-schemas conf)
conf))
readStormConfig方法
// 调用readDefaultConfig方法从defaults.yaml配置文件读取集群默认配置信息存入一个map对象ret中
Map ret = readDefaultConfig();
// confFile保存系统变量"storm.conf.file"的值,系统变量"storm.conf.file"保存了用户自定义配置文件的路径
String confFile = System.getProperty("storm.conf.file");
Map storm;
// 如果没有用户自定义配置文件,那么调用findAndReadConfigFile方法读取"storm.yaml"配置文件,将配置信息保存在storm中,否则读取用户自定义配置文件
if (confFile==null || confFile.equals("")) {
storm = findAndReadConfigFile("storm.yaml", false);
} else {
storm = findAndReadConfigFile(confFile, true);
}
// 将"storm.yaml"配置文件或用户自定义的配置文件信息覆盖添加到默认配置信息ret中
ret.putAll(storm);
// 读取命令行提供的配置信息,并覆盖添加到之前的map对象中
ret.putAll(readCommandLineOpts());
// 返回保存了配置信息的map对象
return ret;
}
submitJar方法
submitJar方法调用了StormSubmitter类的重载方法submitJar
* Submit jar file
* @param conf the topology-specific configuration. See {@link Config}.
* @param localJar file path of the jar file to submit
* @return the remote location of the submitted jar
*/
public static String submitJar(Map conf, String localJar) {
return submitJar(conf, localJar, null);
}
重载方法submitJar
submitJar通过thrift client调用nimbus thrift server中的beginFileUpload函数获取目标路径,然后将jar上传到nimbus的目标路径上
) break;
// 调用nimbus thrift server中的uploadChunk函数将jar文件上传nimbus服务器,uploadChunk函数参见其定义部分
client.getClient().uploadChunk(uploadLocation, ByteBuffer.wrap(toSubmit));
}
// 调用nimbus thrift server中的finishFileUpload完成jar文件上传,finishFileUpload函数参见其定义部分
client.getClient().finishFileUpload(uploadLocation);
if (listener != null) {
listener.onCompleted(localJar, uploadLocation, totalSize);
}
LOG.info("Successfully uploaded topology jar to assigned location: " + uploadLocation);
// 返回jar文件上传nimbus的路径
return uploadLocation;
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
client.close();
}
}
beginFileUpload函数
;; fileloc就是jar上传到nimbus上的目录"{storm.local.dir}/nimubs/inbox/stormjar-(uuid).jar",storm.local.dir是在配置信息中设置的
(let [fileloc (str (inbox nimbus) "/stormjar-" (uuid) ".jar")]
;; (:uploaders nimbus)获取nimbus元数据中的TimeCacheMap,关于TimeCacheMap将在以后博客详细分析,将fileloc及其对应的FileOutputStream放入TimeCacheMap
(.put (:uploaders nimbus)
fileloc
(Channels/newChannel (FileOutputStream. fileloc)))
(log-message "Uploading file from client to " fileloc)
;; 返回上传路径
fileloc
))
shell_submission.clj就分析到这里了,分析过程只列举了一些重要的函数,还有一些辅助函数没有列出,感兴趣的可以自己查看下。
storm shell命令源码分析-shell_submission.clj的更多相关文章
- Nimbus<二>storm启动nimbus源码分析-nimbus.clj
nimbus是storm集群的"控制器",是storm集群的重要组成部分.我们可以通用执行bin/storm nimbus >/dev/null 2>&1 &a ...
- storm启动nimbus源码分析-nimbus.clj
nimbus是storm集群的"控制器",是storm集群的重要组成部分.我们可以通用执行bin/storm nimbus >/dev/null 2>&1 &a ...
- storm操作zookeeper源码分析-cluster.clj
storm操作zookeeper的主要函数都定义在命名空间backtype.storm.cluster中(即cluster.clj文件中).backtype.storm.cluster定义了两个重要p ...
- storm启动supervisor源码分析-supervisor.clj
supervisor是storm集群重要组成部分,supervisor主要负责管理各个"工作节点".supervisor与zookeeper进行通信,通过zookeeper的&qu ...
- storm定时器timer源码分析-timer.clj
storm定时器与java.util.Timer定时器比较相似.java.util.Timer定时器实际上是个线程,定时调度所拥有的TimerTasks:storm定时器也有一个线程负责调度所拥有的& ...
- supervisor启动worker源码分析-worker.clj
supervisor通过调用sync-processes函数来启动worker,关于sync-processes函数的详细分析请参见"storm启动supervisor源码分析-superv ...
- worker启动executor源码分析-executor.clj
在"supervisor启动worker源码分析-worker.clj"一文中,我们详细讲解了worker是如何初始化的.主要通过调用mk-worker函数实现的.在启动worke ...
- debug:am、cmd命令源码分析
debug:am.cmd命令源码分析 目录 debug:am.cmd命令源码分析 am命令的实现 手机里的am am.jar cmd命令的实现 手机里的cmd cmd activity cmd.cpp ...
- memcached学习笔记——存储命令源码分析下篇
上一篇回顾:<memcached学习笔记——存储命令源码分析上篇>通过分析memcached的存储命令源码的过程,了解了memcached如何解析文本命令和mencached的内存管理机制 ...
随机推荐
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 0424 collections模块、time模块、rondom模块、sys模块
昨日回顾:hashlib 摘要 md5 sha系列 文件的一致性校验 密文的认证 logging 记录日志 两种用法 basicConfig不常用 getLogger()常用 可以通过一个参数去控制全 ...
- mini2440移植uboot 2014.04(三)
我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440.git 参考文档: s3c2440手册(下载地址) ...
- 什么是DDOS攻击?怎么防御?
一.什么是DDOS? DDOS是英文Distributed Denial of Service的缩写,意即"分布式拒绝服务",那么什么又是拒绝服务(Denial of Servic ...
- Android蓝牙串口通讯【转】
本文转载自:http://blog.sina.com.cn/s/blog_631e3f2601012ixi.html Android蓝牙串口通讯 闲着无聊玩起了Android蓝牙模块与单片机蓝牙模块的 ...
- stdcall、cdecl详解(以及WINAPI和CALLBACK之类的宏对应什么)
转自:http://blog.csdn.net/huanjieshuijing/article/details/5822942 对_stdcall 的理解在C语言中,假设我们有这样的一个函数:int ...
- JavaScript 练习,变量,数组,函数,对象, with for 语句
JavaScript 基于对象 和 事件驱动!!! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- codevs1199 开车旅行
[问题描述]小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为H i ,城市 ...
- 访问虚拟机中的架设的Web服务器
环境: 1.虚拟机中安装了CentOS,虚拟机使用NAT的方式 2.在CentOS中安装了APACHE 并且使用 http://127.0.0.1可以正常访问,通过ifconfig查到IP地址是 19 ...
- cocos2d-x3.0rc打包apk遇到的一些问题记录
下载cocos2d-x3.0rc后根据官方教程进行环境配置等等一系列过程没有遇到什么问题 打包apk时出现一些问题: 按照官方教程cmd下运行cocos run -p android -m relea ...