You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by hx...@apache.org on 2023/02/27 02:42:35 UTC

[flink] branch master updated: [FLINK-30277][python]Allow PYTHONPATH of Python Worker configurable

This is an automated email from the ASF dual-hosted git repository.

hxb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e0efe4e072 [FLINK-30277][python]Allow PYTHONPATH of Python Worker configurable
2e0efe4e072 is described below

commit 2e0efe4e0723429e26ca04e2f61fcf89884dd077
Author: Samrat002 <de...@gmail.com>
AuthorDate: Thu Feb 23 16:46:01 2023 +0800

    [FLINK-30277][python]Allow PYTHONPATH of Python Worker configurable
    
    This closes #21770.
---
 .../shortcodes/generated/python_configuration.html      |  6 ++++++
 docs/static/generated/rest_v2_sql_gateway.yml           |  2 +-
 .../org/apache/flink/client/python/PythonEnvUtils.java  | 10 ++++++++++
 .../java/org/apache/flink/python/PythonOptions.java     | 12 ++++++++++++
 .../python/env/AbstractPythonEnvironmentManager.java    |  9 ++++++++-
 .../apache/flink/python/env/PythonDependencyInfo.java   | 17 ++++++++++++++---
 6 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/docs/layouts/shortcodes/generated/python_configuration.html b/docs/layouts/shortcodes/generated/python_configuration.html
index ba15a93112b..eb9c66795c8 100644
--- a/docs/layouts/shortcodes/generated/python_configuration.html
+++ b/docs/layouts/shortcodes/generated/python_configuration.html
@@ -98,6 +98,12 @@
             <td>Boolean</td>
             <td>Specifies whether to enable Python worker profiling. The profile result will be displayed in the log file of the TaskManager periodically. The interval between each profiling is determined by the config options python.fn-execution.bundle.size and python.fn-execution.bundle.time.</td>
         </tr>
+        <tr>
+            <td><h5>python.pythonpath</h5></td>
+            <td style="word-wrap: break-word;">(none)</td>
+            <td>String</td>
+            <td>Specify the path on the Worker Node where the Flink Python Dependencies are installed, which gets added into the PYTHONPATH of the Python Worker. </td>
+        </tr>
         <tr>
             <td><h5>python.requirements</h5></td>
             <td style="word-wrap: break-word;">(none)</td>
diff --git a/docs/static/generated/rest_v2_sql_gateway.yml b/docs/static/generated/rest_v2_sql_gateway.yml
index 887930eb65a..1284d0ae67f 100644
--- a/docs/static/generated/rest_v2_sql_gateway.yml
+++ b/docs/static/generated/rest_v2_sql_gateway.yml
@@ -6,7 +6,7 @@ info:
   license:
     name: Apache 2.0
     url: https://www.apache.org/licenses/LICENSE-2.0.html
-  version: v2/1.17-SNAPSHOT
+  version: v2/1.18-SNAPSHOT
 paths:
   /api_versions:
     get:
diff --git a/flink-python/src/main/java/org/apache/flink/client/python/PythonEnvUtils.java b/flink-python/src/main/java/org/apache/flink/client/python/PythonEnvUtils.java
index f018703dcdc..6a59511388d 100644
--- a/flink-python/src/main/java/org/apache/flink/client/python/PythonEnvUtils.java
+++ b/flink-python/src/main/java/org/apache/flink/client/python/PythonEnvUtils.java
@@ -70,6 +70,7 @@ import java.util.stream.Collectors;
 import static org.apache.flink.python.PythonOptions.PYTHON_ARCHIVES;
 import static org.apache.flink.python.PythonOptions.PYTHON_CLIENT_EXECUTABLE;
 import static org.apache.flink.python.PythonOptions.PYTHON_FILES;
+import static org.apache.flink.python.PythonOptions.PYTHON_PATH;
 import static org.apache.flink.python.util.PythonDependencyUtils.FILE_DELIMITER;
 
 /** The util class help to prepare Python env and run the python process. */
@@ -212,6 +213,15 @@ final class PythonEnvUtils {
                             });
         }
 
+        // 4. append configured python.pythonpath to the PYTHONPATH.
+        if (config.getOptional(PYTHON_PATH).isPresent()) {
+            env.pythonPath =
+                    String.join(
+                            File.pathSeparator,
+                            config.getOptional(PYTHON_PATH).get(),
+                            env.pythonPath);
+        }
+
         if (entryPointScript != null) {
             addToPythonPath(env, Collections.singletonList(new Path(entryPointScript)));
         }
diff --git a/flink-python/src/main/java/org/apache/flink/python/PythonOptions.java b/flink-python/src/main/java/org/apache/flink/python/PythonOptions.java
index 3f0b90a91af..3a83fd76105 100644
--- a/flink-python/src/main/java/org/apache/flink/python/PythonOptions.java
+++ b/flink-python/src/main/java/org/apache/flink/python/PythonOptions.java
@@ -122,6 +122,18 @@ public class PythonOptions {
                                     + "optional parameter exists. The option is equivalent to the command line option "
                                     + "\"-pyreq\".");
 
+    /** The configuration allows user to define python path for client and workers. */
+    public static final ConfigOption<String> PYTHON_PATH =
+            ConfigOptions.key("python.pythonpath")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription(
+                            Description.builder()
+                                    .text(
+                                            "Specify the path on the Worker Node where the Flink Python Dependencies are installed, which "
+                                                    + "gets added into the PYTHONPATH of the Python Worker. ")
+                                    .build());
+
     public static final ConfigOption<String> PYTHON_ARCHIVES =
             ConfigOptions.key("python.archives")
                     .stringType()
diff --git a/flink-python/src/main/java/org/apache/flink/python/env/AbstractPythonEnvironmentManager.java b/flink-python/src/main/java/org/apache/flink/python/env/AbstractPythonEnvironmentManager.java
index 2ab0facb313..70992e6ddbb 100644
--- a/flink-python/src/main/java/org/apache/flink/python/env/AbstractPythonEnvironmentManager.java
+++ b/flink-python/src/main/java/org/apache/flink/python/env/AbstractPythonEnvironmentManager.java
@@ -43,6 +43,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -166,6 +167,13 @@ public abstract class AbstractPythonEnvironmentManager implements PythonEnvironm
 
         constructFilesDirectory(env, baseDirectory);
 
+        if (dependencyInfo.getPythonPath().isPresent()) {
+            appendToPythonPath(
+                    env, Collections.singletonList(dependencyInfo.getPythonPath().get()));
+        }
+
+        LOG.info("PYTHONPATH of python worker: {}", env.get("PYTHONPATH"));
+
         constructRequirementsDirectory(env, baseDirectory);
 
         constructArchivesDirectory(env, baseDirectory);
@@ -294,7 +302,6 @@ public abstract class AbstractPythonEnvironmentManager implements PythonEnvironm
             pythonFilePaths.add(pythonPath);
         }
         appendToPythonPath(env, pythonFilePaths);
-        LOG.info("PYTHONPATH of python worker: {}", env.get("PYTHONPATH"));
     }
 
     private void constructRequirementsDirectory(Map<String, String> env, String baseDirectory)
diff --git a/flink-python/src/main/java/org/apache/flink/python/env/PythonDependencyInfo.java b/flink-python/src/main/java/org/apache/flink/python/env/PythonDependencyInfo.java
index 80f570b2714..2f65e5f7be7 100644
--- a/flink-python/src/main/java/org/apache/flink/python/env/PythonDependencyInfo.java
+++ b/flink-python/src/main/java/org/apache/flink/python/env/PythonDependencyInfo.java
@@ -37,6 +37,7 @@ import static org.apache.flink.python.PythonOptions.PYTHON_ARCHIVES_DISTRIBUTED_
 import static org.apache.flink.python.PythonOptions.PYTHON_EXECUTABLE;
 import static org.apache.flink.python.PythonOptions.PYTHON_EXECUTION_MODE;
 import static org.apache.flink.python.PythonOptions.PYTHON_FILES_DISTRIBUTED_CACHE_INFO;
+import static org.apache.flink.python.PythonOptions.PYTHON_PATH;
 import static org.apache.flink.python.PythonOptions.PYTHON_REQUIREMENTS_FILE_DISTRIBUTED_CACHE_INFO;
 
 /** PythonDependencyInfo contains the information of third-party dependencies. */
@@ -63,6 +64,8 @@ public final class PythonDependencyInfo {
      */
     @Nullable private final String requirementsCacheDir;
 
+    @Nullable private final String pythonPath;
+
     /**
      * The python archives uploaded by TableEnvironment#add_python_archive() or command line option
      * "-pyarch". The key is the path of the archive file and the value is the name of the directory
@@ -91,7 +94,8 @@ public final class PythonDependencyInfo {
                 requirementsCacheDir,
                 archives,
                 pythonExec,
-                PYTHON_EXECUTION_MODE.defaultValue());
+                PYTHON_EXECUTION_MODE.defaultValue(),
+                PYTHON_PATH.defaultValue());
     }
 
     public PythonDependencyInfo(
@@ -100,13 +104,15 @@ public final class PythonDependencyInfo {
             @Nullable String requirementsCacheDir,
             @Nonnull Map<String, String> archives,
             @Nonnull String pythonExec,
-            @Nonnull String executionMode) {
+            @Nonnull String executionMode,
+            @Nullable String pythonPath) {
         this.pythonFiles = Objects.requireNonNull(pythonFiles);
         this.requirementsFilePath = requirementsFilePath;
         this.requirementsCacheDir = requirementsCacheDir;
         this.pythonExec = Objects.requireNonNull(pythonExec);
         this.archives = Objects.requireNonNull(archives);
         this.executionMode = Objects.requireNonNull(executionMode);
+        this.pythonPath = pythonPath;
     }
 
     public Map<String, String> getPythonFiles() {
@@ -133,6 +139,10 @@ public final class PythonDependencyInfo {
         return executionMode;
     }
 
+    public Optional<String> getPythonPath() {
+        return Optional.ofNullable(pythonPath);
+    }
+
     /**
      * Creates PythonDependencyInfo from GlobalJobParameters and DistributedCache.
      *
@@ -190,6 +200,7 @@ public final class PythonDependencyInfo {
                 requirementsCacheDir,
                 archives,
                 pythonExec,
-                config.get(PYTHON_EXECUTION_MODE));
+                config.get(PYTHON_EXECUTION_MODE),
+                config.get(PYTHON_PATH));
     }
 }