文章目录
针对flink-1.7-release
1. 引言
Flink客户端通过命令行的形式提交任务时,即执行./flink run xxxx.jar
时,真正执行的run逻辑如下:
protected void run(String[] args) throws Exception {
LOG.info("Running 'run' command.");
final Options commandOptions = CliFrontendParser.getRunCommandOptions();
final Options commandLineOptions = CliFrontendParser.mergeOptions(commandOptions, customCommandLineOptions);
final CommandLine commandLine = CliFrontendParser.parse(commandLineOptions, args, true);
// 运行参数
final RunOptions runOptions = new RunOptions(commandLine);
// 判断下是否是help操作
if (runOptions.isPrintHelp()) {
CliFrontendParser.printHelpForRun(customCommandLines);
return;
}
// 必须要指定任务的jar包路径
if (runOptions.getJarFilePath() == null) {
throw new CliArgsException("The program JAR file was not specified.");
}
// 初始化打包的任务执行程序
final PackagedProgram program;
try {
LOG.info("Building program from JAR file");
program = buildProgram(runOptions);
}
catch (FileNotFoundException e) {
throw new CliArgsException("Could not build the program from JAR file.", e);
}
final CustomCommandLine<?> customCommandLine = getActiveCustomCommandLine(commandLine);
try {
// 执行任务程序
runProgram(customCommandLine, commandLine, runOptions, program);
} finally {
program.deleteExtractedLibraries();
}
}
这段逻辑主要做了两件事情,首先,就是构建执行程序包;然后执行任务程序包。我们本文就重点分析下:program = buildProgram(runOptions);
这段逻辑。怎样通过run的命令行参数来打包程序。
2. buildProgram的执行逻辑
继续分析 buildProgram(runOptions)的逻辑:
/**
* 从给定的命令行参数中创建打包程序
*
* @return A PackagedProgram (upon success)
*/
PackagedProgram buildProgram(ProgramOptions options) throws FileNotFoundException, ProgramInvocationException {
// 程序运行参数
String[] programArgs = options.getProgramArgs();
// 程序jar包路径
String jarFilePath = options.getJarFilePath();
// 依赖的classpaths
List<URL> classpaths = options.getClasspaths();
// 程序jar包不能为空
if (jarFilePath == null) {
throw new IllegalArgumentException("The program JAR file was not specified.");
}
File jarFile = new File(jarFilePath);
// 程序jar包路径必须存在
if (!jarFile.exists()) {
throw new FileNotFoundException("JAR file does not exist: " + jarFile);
}
else if (!jarFile.isFile()) {
throw new FileNotFoundException("JAR file is not a file: " + jarFile);
}
// 程序执行入口函数
String entryPointClass = options.getEntryPointClassName();
// 打包
PackagedProgram program = entryPointClass == null ?
new PackagedProgram(jarFile, classpaths, programArgs) :
new PackagedProgram(jarFile, classpaths, entryPointClass, programArgs);
// 设置包的SavePoint
program.setSavepointRestoreSettings(options.