HuangXingBo commented on a change in pull request #11448: [FLINK-16666][python][table] Support new Python dependency configuration options in flink-java, flink-streaming-java and flink-table. URL: https://github.com/apache/flink/pull/11448#discussion_r394966087
########## File path: flink-java/src/main/java/org/apache/flink/api/java/utils/PythonDependencyManager.java ########## @@ -0,0 +1,282 @@ +/* + * 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.api.java.utils; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.cache.DistributedCache; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.ConfigOptions; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.PythonOptions; +import org.apache.flink.configuration.ReadableConfig; +import org.apache.flink.util.Preconditions; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.apache.flink.configuration.PythonOptions.PYTHON_CLIENT_EXECUTABLE; +import static org.apache.flink.configuration.PythonOptions.PYTHON_EXECUTABLE; + +/** + * Utility class for Python dependency management. The dependencies will be registered at the distributed + * cache. + */ +@Internal +public class PythonDependencyManager { + + public static final String PYTHON_FILE_PREFIX = "python_file"; + public static final String PYTHON_REQUIREMENTS_FILE_PREFIX = "python_requirements_file"; + public static final String PYTHON_REQUIREMENTS_CACHE_PREFIX = "python_requirements_cache"; + public static final String PYTHON_ARCHIVE_PREFIX = "python_archive"; + public static final String CLUSTER = "cluster"; + public static final String FILE = "file"; + public static final String CACHE = "cache"; + public static final String FILE_DELIMITER = ","; + public static final String PARAM_DELIMITER = "#"; + + // Metadata of Python dependencies. + + public static final ConfigOption<Map<String, String>> PYTHON_FILES = + ConfigOptions.key("python.internal.files-key-map").mapType().noDefaultValue(); + public static final ConfigOption<Map<String, String>> PYTHON_REQUIREMENTS_FILE = + ConfigOptions.key("python.internal.requirements-file-key").mapType().noDefaultValue(); + public static final ConfigOption<Map<String, String>> PYTHON_ARCHIVES = + ConfigOptions.key("python.internal.archives-key-map").mapType().noDefaultValue(); + + private final Configuration config; + private final List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cachedFiles; + private int counter = -1; + + public PythonDependencyManager( + Configuration config, + List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cachedFiles) { + this.config = Preconditions.checkNotNull(config); + this.cachedFiles = cachedFiles; + } + + /** + * Adds a Python dependency which could be .py files, Python packages(.zip, .egg etc.) or + * local directories. The dependencies will be added to the PYTHONPATH of the Python UDF worker + * and the local Py4J python client. + * + * @param filePath The path of the Python dependency. + */ + public void addPythonFile(String filePath) { + Preconditions.checkNotNull(filePath); + if (fileAlreadyRegistered(filePath)) { + return; + } + String fileKey = generateUniqueFileKey(PYTHON_FILE_PREFIX); + registerCachedFile(filePath, fileKey); + if (!config.contains(PYTHON_FILES)) { + config.set(PYTHON_FILES, new HashMap<>()); + } + config.get(PYTHON_FILES).put(fileKey, new File(filePath).getName()); + } + + /** + * Specifies the third-party dependencies via a requirements file. These dependencies will be + * installed by the command "pip install -r [requirements file]" before launching the Python + * UDF worker. + * + * @param requirementsFilePath The path of the requirements file. + */ + public void setPythonRequirements(String requirementsFilePath) { + setPythonRequirements(requirementsFilePath, null); + } + + /** + * Specifies the third-party dependencies via a requirements file. The `requirementsCachedDir` + * will be uploaded to support offline installation. These dependencies will be installed by + * the command "pip install -r [requirements file] --find-links [requirements cached dir]" + * before launching the Python UDF worker. + * + * @param requirementsFilePath The path of the requirements file. + * @param requirementsCachedDir The path of the requirements cached directory. + */ + public void setPythonRequirements(String requirementsFilePath, String requirementsCachedDir) { + Preconditions.checkNotNull(requirementsFilePath); + if (!config.contains(PYTHON_REQUIREMENTS_FILE)) { + config.set(PYTHON_REQUIREMENTS_FILE, new HashMap<>()); + } + config.get(PYTHON_REQUIREMENTS_FILE).forEach((k, fileKey) -> removeCachedFileIfExists(fileKey)); + config.get(PYTHON_REQUIREMENTS_FILE).clear(); + + String fileKey = generateUniqueFileKey(PYTHON_REQUIREMENTS_FILE_PREFIX); + registerCachedFile(requirementsFilePath, fileKey); + config.get(PYTHON_REQUIREMENTS_FILE).put(FILE, fileKey); + + if (requirementsCachedDir != null) { + String cacheDirKey = generateUniqueFileKey(PYTHON_REQUIREMENTS_CACHE_PREFIX); + registerCachedFile(requirementsCachedDir, cacheDirKey); + config.get(PYTHON_REQUIREMENTS_FILE).put(CACHE, cacheDirKey); + } + } + + /** + * Adds a Python archive file (zip format). The file will be extracted and move to a dedicated + * directory under the working directory of the Python UDF workers. The name of the dedicated + * directory is the same as the archive file name. The Python UDFs and the config option + * "python.executable" could access the extracted files via relative path. + * + * @param archivePath The path of the archive file. + */ + public void addPythonArchive(String archivePath) { + addPythonArchive(archivePath, null); + } + + /** + * Adds a Python archive file (zip format). The file will be extracted and move to a dedicated Review comment: ditto ---------------------------------------------------------------- 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 With regards, Apache Git Services