shangeyao commented on code in PR #4418: URL: https://github.com/apache/streampark/pull/4418#discussion_r3540606078
########## streampark-flink/streampark-flink-shims/streampark-flink-shims-base/src/main/java/org/apache/streampark/flink/core/conf/ParameterCli.java: ########## @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.streampark.flink.core.conf; + +import org.apache.streampark.common.conf.ConfigKeys; +import org.apache.streampark.common.util.PropertiesUtils; + +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; + +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Parses Flink application CLI parameters from configuration files. */ +public final class ParameterCli { + + private static final String PROPERTY_PREFIX = ConfigKeys.KEY_FLINK_PROPERTY_PREFIX(); + private static final String OPTION_PREFIX = ConfigKeys.KEY_FLINK_OPTION_PREFIX(); + private static final String OPTION_MAIN = PROPERTY_PREFIX + "$internal.application.main"; + + private static final Options FLINK_OPTIONS = FlinkRunOption.allOptions(); + private static final DefaultParser PARSER = new DefaultParser(); + + private ParameterCli() { + } + + public static void main(String[] args) { + System.out.print(read(args)); + } + + public static String read(String[] args) { + switch (args[0]) { + case "--vmopt": + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + if (classLoader instanceof URLClassLoader) { + return ""; + } + return "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED " + + "--add-opens jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED"; + default: + String action = args[0]; + String conf = args[1]; + Map<String, String> map; + try { + String extension = conf.substring(conf.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case "yml": + case "yaml": + map = PropertiesUtils.fromYamlFile(conf); + break; + case "conf": + map = PropertiesUtils.fromHoconFile(conf); + break; + case "properties": + map = PropertiesUtils.fromPropertiesFile(conf); + break; + default: + throw new IllegalArgumentException( + "[StreamPark] Usage:flink.conf file error,must be (yml|conf|properties)"); + } + } catch (Exception e) { + map = Collections.emptyMap(); + } + String[] programArgs = new String[args.length - 2]; + System.arraycopy(args, 2, programArgs, 0, programArgs.length); + switch (action) { + case "--option": + String[] option = getOption(map, programArgs); + StringBuilder buffer = new StringBuilder(); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FLINK_OPTIONS, option, false); + for (org.apache.commons.cli.Option x : line.getOptions()) { + buffer.append(" -").append(x.getOpt()); + if (x.hasArg()) { + buffer.append(" ").append(x.getValue()); + } + } + } catch (Exception exception) { + exception.printStackTrace(); Review Comment: Fixed in 752fa8a68: removed printStackTrace. ########## streampark-flink/streampark-flink-packer/src/main/java/org/apache/streampark/flink/packer/pipeline/impl/FlinkYarnApplicationBuildPipeline.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.streampark.flink.packer.pipeline.impl; + +import org.apache.streampark.common.conf.Workspace; +import org.apache.streampark.common.enums.FlinkJobType; +import org.apache.streampark.common.fs.FsOperator; +import org.apache.streampark.common.fs.HdfsOperator; +import org.apache.streampark.common.fs.LfsOperator; +import org.apache.streampark.flink.packer.maven.MavenTool; +import org.apache.streampark.flink.packer.pipeline.*; + +import org.apache.commons.codec.digest.DigestUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class FlinkYarnApplicationBuildPipeline extends BuildPipeline { + private final FlinkYarnApplicationBuildRequest request; + public FlinkYarnApplicationBuildPipeline(FlinkYarnApplicationBuildRequest request) { this.request = request; } + public static FlinkYarnApplicationBuildPipeline of(FlinkYarnApplicationBuildRequest request) { return new FlinkYarnApplicationBuildPipeline(request); } + @Override public PipelineTypeEnum getPipeType() { return PipelineTypeEnum.FLINK_YARN_APPLICATION; } + @Override protected BuildParam offerBuildParam() { return request; } + @Override protected BuildResult buildProcess() throws Throwable { + execStep(1, () -> { + if (request.flinkJobType() == FlinkJobType.FLINK_SQL || request.flinkJobType() == FlinkJobType.PYFLINK) { + LfsOperator.getInstance().mkCleanDirs(request.localWorkspace()); + HdfsOperator.getInstance().mkCleanDirs(request.yarnProvidedPath()); + } + return null; + }).orElseThrow(() -> getError().exception()); + List<String> mavenJars = execStep(2, () -> { + if (request.flinkJobType() == FlinkJobType.FLINK_SQL || request.flinkJobType() == FlinkJobType.PYFLINK) { + List<String> paths = new ArrayList<>(); + MavenTool.resolveArtifacts(request.dependencyInfo().mavenArts()).forEach(f -> paths.add(f.getAbsolutePath())); + paths.addAll(request.dependencyInfo().extJarLibs()); + return paths; + } + return Collections.<String>emptyList(); + }).orElseThrow(() -> getError().exception()); + execStep(3, () -> { + for (String jar : mavenJars) { + uploadJarToHdfsOrLfs(FsOperator.lfs(), jar, request.localWorkspace()); + uploadJarToHdfsOrLfs(FsOperator.hdfs(), jar, request.yarnProvidedPath()); + } + return null; + }).orElseThrow(() -> getError().exception()); + return new SimpleBuildResponse(); + } + + private void uploadJarToHdfsOrLfs(FsOperator fsOperator, String origin, String target) throws Exception { + File originFile = new File(origin); + if (!fsOperator.exists(target)) fsOperator.mkdirs(target); + if (originFile.isFile()) { + if (fsOperator == FsOperator.lfs()) { + fsOperator.copy(originFile.getAbsolutePath(), target); + } else { + String uploadFile = Workspace.remote().APP_UPLOADS() + "/" + originFile.getName(); + if (fsOperator.exists(uploadFile)) { + try (FileInputStream in = new FileInputStream(originFile)) { + if (!DigestUtils.md5Hex(in).equals(fsOperator.fileMd5(uploadFile))) { Review Comment: Fixed in `752fa8a68`: MD5 for upload integrity only; added `// NOSONAR java:S4790`. ########## streampark-flink/streampark-flink-packer/src/main/java/org/apache/streampark/flink/packer/pipeline/impl/SparkYarnBuildPipeline.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.streampark.flink.packer.pipeline.impl; + +import org.apache.streampark.common.conf.Workspace; +import org.apache.streampark.common.enums.SparkJobType; +import org.apache.streampark.common.fs.FsOperator; +import org.apache.streampark.common.fs.HdfsOperator; +import org.apache.streampark.common.fs.LfsOperator; +import org.apache.streampark.flink.packer.maven.MavenTool; +import org.apache.streampark.flink.packer.pipeline.BuildParam; +import org.apache.streampark.flink.packer.pipeline.BuildPipeline; +import org.apache.streampark.flink.packer.pipeline.BuildResult; +import org.apache.streampark.flink.packer.pipeline.PipelineTypeEnum; +import org.apache.streampark.flink.packer.pipeline.SimpleBuildResponse; +import org.apache.streampark.flink.packer.pipeline.SparkYarnBuildRequest; + +import org.apache.commons.codec.digest.DigestUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** Building pipeline for spark yarn application mode. */ +public class SparkYarnBuildPipeline extends BuildPipeline { + + private final SparkYarnBuildRequest request; + + public SparkYarnBuildPipeline(SparkYarnBuildRequest request) { + this.request = request; + } + + public static SparkYarnBuildPipeline of(SparkYarnBuildRequest request) { + return new SparkYarnBuildPipeline(request); + } + + @Override + public PipelineTypeEnum getPipeType() { + return PipelineTypeEnum.SPARK_CLUSTER; + } + + @Override + protected BuildParam offerBuildParam() { + return request; + } + + @Override + protected BuildResult buildProcess() throws Throwable { + execStep( + 1, + () -> { + if (request.jobType() == SparkJobType.SPARK_SQL) { + LfsOperator.getInstance().mkCleanDirs(request.localWorkspace()); + HdfsOperator.getInstance().mkCleanDirs(request.yarnProvidedPath()); + } + return null; + }) + .orElseThrow(() -> getError().exception()); + + List<String> mavenJars = + execStep( + 2, + () -> { + if (request.jobType() == SparkJobType.SPARK_SQL) { + List<String> paths = new ArrayList<>(); + MavenTool.resolveArtifacts(request.dependencyInfo().mavenArts()) + .forEach(f -> paths.add(f.getAbsolutePath())); + paths.addAll(request.dependencyInfo().extJarLibs()); + return paths; + } + return Collections.<String>emptyList(); + }) + .orElseThrow(() -> getError().exception()); + + execStep( + 3, + () -> { + for (String jar : mavenJars) { + uploadJarToHdfsOrLfs( + FsOperator.lfs(), jar, request.localWorkspace()); + uploadJarToHdfsOrLfs( + FsOperator.hdfs(), jar, request.yarnProvidedPath()); + } + return null; + }) + .orElseThrow(() -> getError().exception()); + return new SimpleBuildResponse(); + } + + private void uploadJarToHdfsOrLfs(FsOperator fsOperator, String origin, String target) + throws Exception { + File originFile = new File(origin); + if (!fsOperator.exists(target)) { + fsOperator.mkdirs(target); + } + if (originFile.isFile()) { + if (fsOperator == FsOperator.lfs()) { + fsOperator.copy(originFile.getAbsolutePath(), target); + } else { + String uploadFile = + Workspace.remote().APP_UPLOADS() + "/" + originFile.getName(); + if (fsOperator.exists(uploadFile)) { + try (FileInputStream in = new FileInputStream(originFile)) { + if (!DigestUtils.md5Hex(in).equals(fsOperator.fileMd5(uploadFile))) { Review Comment: Fixed in `752fa8a68`: MD5 for upload integrity only; added `// NOSONAR java:S4790`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
