KarmaGYZ commented on a change in pull request #11920: URL: https://github.com/apache/flink/pull/11920#discussion_r420532489
########## File path: flink-external-resource/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUDriver.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.flink.externalresource.gpu; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.externalresource.ExternalResourceDriver; +import org.apache.flink.configuration.ConfigConstants; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.ExternalResourceOptions; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.StringUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static org.apache.flink.configuration.ConfigOptions.key; + +/** + * Driver takes the responsibility to discover GPU resources and provide the GPU resource information. + * It get the GPU information by executing user-defined discovery script. + */ +public class GPUDriver implements ExternalResourceDriver { + + private static final Logger LOG = LoggerFactory.getLogger(GPUDriver.class); + + private static final String RESOURCE_NAME = "gpu"; + + private static final String DISCOVERY_SCRIPT_PATH_SUFFIX = "param.discovery-script.path"; + + private static final String DISCOVERY_SCRIPT_ARGS_SUFFIX = "param.discovery-script.args"; + + @VisibleForTesting + static final ConfigOption<String> DISCOVERY_SCRIPT_PATH = + key(ExternalResourceOptions.keyWithResourceNameAndSuffix(RESOURCE_NAME, DISCOVERY_SCRIPT_PATH_SUFFIX)) + .stringType() + .noDefaultValue(); + + @VisibleForTesting + static final ConfigOption<String> DISCOVERY_SCRIPT_ARG = + key(ExternalResourceOptions.keyWithResourceNameAndSuffix(RESOURCE_NAME, DISCOVERY_SCRIPT_ARGS_SUFFIX)) + .stringType() + .noDefaultValue(); + + @VisibleForTesting + static final ConfigOption<Long> GPU_AMOUNT = + key(ExternalResourceOptions.keyWithResourceNameAndSuffix(RESOURCE_NAME, ExternalResourceOptions.EXTERNAL_RESOURCE_AMOUNT_SUFFIX)) + .longType() + .defaultValue(0L); + + private final Set<GPUInformation> gpuResources; + + public GPUDriver(Configuration config) throws Exception { + final String discoveryScriptPath = config.getString(DISCOVERY_SCRIPT_PATH); + if (StringUtils.isNullOrWhitespaceOnly(discoveryScriptPath)) { + throw new FlinkRuntimeException("Could not find config of the path of gpu discovery script."); + } + + final File discoveryScript = new File(System.getenv().getOrDefault(ConfigConstants.ENV_FLINK_HOME_DIR, ".") + + "/" + discoveryScriptPath); + if (!discoveryScript.exists()) { + throw new FlinkRuntimeException("The gpu discovery script does not exist in path " + discoveryScript.getAbsolutePath()); + } + + final String args = config.getString(DISCOVERY_SCRIPT_ARG); + final long gpuAmount = config.getLong(GPU_AMOUNT); + gpuResources = new HashSet<>(); + + if (gpuAmount <= 0) { + LOG.warn("The amount of GPU should be positive."); + return; + } + + String output = executeDiscoveryScript(discoveryScript, gpuAmount, args); + if (output != null && output != "") { + String[] indexes = output.split(","); + for (String index : indexes) { + gpuResources.add(new GPUInformation(index)); + } + } + LOG.info("Discover the GPU resource {}.", gpuResources); + } + + @Override + public Set<GPUInformation> retrieveResourceInfo(long gpuAmount) { + return Collections.unmodifiableSet(gpuResources); + } + + private String executeDiscoveryScript(File discoveryScript, long gpuAmount, String args) throws Exception { + + final StringBuilder cmdBuilder = new StringBuilder(); + cmdBuilder + .append(discoveryScript.getAbsolutePath()) + .append(" ") + .append(gpuAmount) + .append(" ") + .append(args); + + final Process process = Runtime.getRuntime().exec(cmdBuilder.toString()); + final BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + final BufferedReader stderrReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + final int exitVal = process.waitFor(); + if (exitVal != 0) { + final String stdout = stdoutReader.lines().collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); + final String stderr = stderrReader.lines().collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); + LOG.error("Discovery script exit with {}. With output {} {}", exitVal, stdout, stderr); + throw new FlinkRuntimeException("Discovery script exit with non-zero."); + } + return stdoutReader.readLine(); Review comment: I think it would be good to deduplicate the process execution logic in Flink. However, after a quick search, this logic is involved in a lot of code paths in the current Flink. So, I prefer to move this refactor out of the scope of this PR. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org