xintongsong commented on a change in pull request #11920:
URL: https://github.com/apache/flink/pull/11920#discussion_r421922013



##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUInfo.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.api.common.externalresource.ExternalResourceInfo;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Information for GPU resource. Currently only including the GPU index.
+ */
+public class GPUInfo implements ExternalResourceInfo {
+
+       private static final String PROPERTY = "index";

Review comment:
       ```suggestion
        private static final String PROPERTY_KEY_INDEX = "index";
   ```

##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUInfo.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.api.common.externalresource.ExternalResourceInfo;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Information for GPU resource. Currently only including the GPU index.
+ */
+public class GPUInfo implements ExternalResourceInfo {
+
+       private static final String PROPERTY = "index";
+
+       private final String index;
+
+       GPUInfo(String index) {
+               this.index = Preconditions.checkNotNull(index);
+       }
+
+       @Override
+       public String toString() {
+               return String.format("GPU Device(%s)", index);
+       }
+
+       @Override
+       public int hashCode() {
+               return 47 + index.hashCode();

Review comment:
       nit: This may not be a problem, but it would be good to follow a common 
hash code equation.
   ```
   hashCode = field1.hashCode * prime^0 + field2.hashCode * prime^1 + 
field3.hashCode * prime^2 ...
   ```

##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUInfo.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.api.common.externalresource.ExternalResourceInfo;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Information for GPU resource. Currently only including the GPU index.
+ */
+public class GPUInfo implements ExternalResourceInfo {
+
+       private static final String PROPERTY = "index";
+
+       private final String index;
+
+       GPUInfo(String index) {
+               this.index = Preconditions.checkNotNull(index);

Review comment:
       I think we should also check for empty or whitespace only strings.

##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUDriver.java
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.util.FlinkException;
+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.
+ */
+class GPUDriver implements ExternalResourceDriver {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(GPUDriver.class);
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_PATH =
+               key("discovery-script.path")
+                       .stringType()
+                       .noDefaultValue();
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_ARG =
+               key("discovery-script.args")
+                       .stringType()
+                       .noDefaultValue();
+
+       private final File discoveryScript;
+       private final String args;
+
+       GPUDriver(Configuration config) throws Exception {
+               final String discoveryScriptPath = 
config.getString(DISCOVERY_SCRIPT_PATH);
+               if (StringUtils.isNullOrWhitespaceOnly(discoveryScriptPath)) {
+                       throw new FlinkException("Could not find config of the 
path of gpu discovery script.");

Review comment:
       Here `IllegalConfigurationException` might be more descriptive.

##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUDriver.java
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.util.FlinkException;
+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.
+ */
+class GPUDriver implements ExternalResourceDriver {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(GPUDriver.class);
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_PATH =
+               key("discovery-script.path")
+                       .stringType()
+                       .noDefaultValue();
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_ARG =
+               key("discovery-script.args")
+                       .stringType()
+                       .noDefaultValue();
+
+       private final File discoveryScript;
+       private final String args;
+
+       GPUDriver(Configuration config) throws Exception {
+               final String discoveryScriptPath = 
config.getString(DISCOVERY_SCRIPT_PATH);
+               if (StringUtils.isNullOrWhitespaceOnly(discoveryScriptPath)) {
+                       throw new FlinkException("Could not find config of the 
path of gpu discovery script.");
+               }
+
+               discoveryScript = new 
File(System.getenv().getOrDefault(ConfigConstants.ENV_FLINK_HOME_DIR, ".") +
+                       "/" + discoveryScriptPath);
+               if (!discoveryScript.exists()) {
+                       throw new FlinkException("The gpu discovery script does 
not exist in path " + discoveryScript.getAbsolutePath());

Review comment:
       Here `FileNotFoundException` might be more descriptive.

##########
File path: 
flink-external-resources/flink-external-resource-gpu/src/main/java/org/apache/flink/externalresource/gpu/GPUDriver.java
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.util.FlinkException;
+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.
+ */
+class GPUDriver implements ExternalResourceDriver {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(GPUDriver.class);
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_PATH =
+               key("discovery-script.path")
+                       .stringType()
+                       .noDefaultValue();
+
+       @VisibleForTesting
+       static final ConfigOption<String> DISCOVERY_SCRIPT_ARG =
+               key("discovery-script.args")
+                       .stringType()
+                       .noDefaultValue();
+
+       private final File discoveryScript;
+       private final String args;
+
+       GPUDriver(Configuration config) throws Exception {
+               final String discoveryScriptPath = 
config.getString(DISCOVERY_SCRIPT_PATH);
+               if (StringUtils.isNullOrWhitespaceOnly(discoveryScriptPath)) {
+                       throw new FlinkException("Could not find config of the 
path of gpu discovery script.");
+               }
+
+               discoveryScript = new 
File(System.getenv().getOrDefault(ConfigConstants.ENV_FLINK_HOME_DIR, ".") +
+                       "/" + discoveryScriptPath);
+               if (!discoveryScript.exists()) {
+                       throw new FlinkException("The gpu discovery script does 
not exist in path " + discoveryScript.getAbsolutePath());
+               }
+

Review comment:
       We should also check `discoveryScript.canExecute()`.

##########
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:
       This makes sense. Then let's open a follow-up ticket to track this.




----------------------------------------------------------------
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:
[email protected]


Reply via email to