/*
*当前类:org.apache.flink.client.program.PackagedProgram
*Clifrontend.main(args)->cli.parseAndRun(args)->run->ClientUtils.executeProgram(effectiveConfiguration, program)->program.invokeInteractiveModeForExecution()->callMainMethod(mainClass, args)
*/privatestaticvoidcallMainMethod(Class<?> entryClass,String[] args)throwsProgramInvocationException{Method mainMethod;if(!Modifier.isPublic(entryClass.getModifiers())){thrownewProgramInvocationException("The class "+ entryClass.getName()+" must be public.");}try{
mainMethod = entryClass.getMethod("main",String[].class);}catch(NoSuchMethodException e){thrownewProgramInvocationException("The class "+ entryClass.getName()+" has no main(String[]) method.");}catch(Throwable t){thrownewProgramInvocationException("Could not look up the main(String[]) method from the class "+
entryClass.getName()+": "+ t.getMessage(), t);}if(!Modifier.isStatic(mainMethod.getModifiers())){thrownewProgramInvocationException("The class "+ entryClass.getName()+" declares a non-static main method.");}if(!Modifier.isPublic(mainMethod.getModifiers())){thrownewProgramInvocationException("The class "+ entryClass.getName()+" declares a non-public main method.");}try{/*TODO 调用用户代码的main方法*/
mainMethod.invoke(null,(Object) args);}catch(IllegalArgumentException e){thrownewProgramInvocationException("Could not invoke the main method, arguments are not matching.", e);}catch(IllegalAccessException e){thrownewProgramInvocationException("Access to the main method was denied: "+ e.getMessage(), e);}catch(InvocationTargetException e){Throwable exceptionInMethod = e.getTargetException();if(exceptionInMethod instanceofError){throw(Error) exceptionInMethod;}elseif(exceptionInMethod instanceofProgramParametrizationException){throw(ProgramParametrizationException) exceptionInMethod;}elseif(exceptionInMethod instanceofProgramInvocationException){throw(ProgramInvocationException) exceptionInMethod;}else{thrownewProgramInvocationException("The main method caused an error: "+ exceptionInMethod.getMessage(), exceptionInMethod);}}catch(Throwable t){thrownewProgramInvocationException("An error occurred while invoking the program's main method: "+ t.getMessage(), t);}}//到mainMethod.invoke(null, (Object) args);开始调用用户的main方法,从execute()方法开始,用户的代码和依赖以及flink的配置文件,各种依赖包将被读取,封装,然后启动yarn任务,部署flink集群的各种组件,提交用户代码,jar包依赖和配置文件……
2、执行用户的代码的main方法,触发execute()方法
/*
*当前类:org.apache.flink.streaming.api.environment.StreamingExecutionEnvironment
*StreamingExecutionEnvironment.execute(jobName)
*/publicJobExecutionResultexecute()throwsException{returnexecute(getJobName());}publicJobExecutionResultexecute(String jobName)throwsException{Preconditions.checkNotNull(jobName,"Streaming Job name should not be null.");/*TODO 获取StreamGraph,并接着执行*/returnexecute(getStreamGraph(jobName));}publicJobExecutionResultexecute(StreamGraph streamGraph)throwsException{finalJobClient jobClient =executeAsync(streamGraph);try{finalJobExecutionResult jobExecutionResult;if(configuration.getBoolean(DeploymentOptions.ATTACHED)){
jobExecutionResult = jobClient.getJobExecutionResult().get();}else{
jobExecutionResult =newDetachedJobExecutionResult(jobClient.getJobID());}
jobListeners.forEach(jobListener -> jobListener.onJobExecuted(jobExecutionResult,null));return jobExecutionResult;}catch(Throwable t){// get() on the JobExecutionResult Future will throw an ExecutionException. This// behaviour was largely not there in Flink versions before the PipelineExecutor// refactoring so we should strip that exception.Throwable strippedException =ExceptionUtils.stripExecutionException(t);
jobListeners.forEach(jobListener ->{
jobListener.onJobExecuted(null, strippedException);});ExceptionUtils.rethrowException(strippedException);// never reached, only make javac happyreturnnull;}}publicJobClientexecuteAsync(StreamGraph streamGraph)throwsException{checkNotNull(streamGraph,"StreamGraph cannot be null.");checkNotNull(configuration.get(DeploymentOptions.TARGET),"No execution.target specified in your configuration file.");finalPipelineExecutorFactory executorFactory =
executorServiceLoader.getExecutorFactory(configuration);checkNotNull(
executorFactory,"Cannot find compatible factory for specified execution.target (=%s)",
configuration.get(DeploymentOptions.TARGET));CompletableFuture<JobClient> jobClientFuture = executorFactory
.getExecutor(configuration).execute(streamGraph, configuration, userClassloader);try{JobClient jobClient = jobClientFuture.get();
jobListeners.forEach(jobListener -> jobListener.onJobSubmitted(jobClient,null));return jobClient;}catch(ExecutionException executionException){finalThrowable strippedException =ExceptionUtils.stripExecutionException(executionException);
jobListeners.forEach(jobListener -> jobListener.onJobSubmitted(null, strippedException));thrownewFlinkException(String.format("Failed to execute job '%s'.", streamGraph.getJobName()),
strippedException);}}