dianfu commented on a change in pull request #11448: [FLINK-16666][python][table] Support new Python dependency configuration options in flink-table. URL: https://github.com/apache/flink/pull/11448#discussion_r404754393
########## File path: flink-python/src/main/java/org/apache/flink/client/python/PythonDependencyUtils.java ########## @@ -0,0 +1,280 @@ +/* + * 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.client.python; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.cache.DistributedCache; +import org.apache.flink.api.java.ExecutionEnvironment; +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.ReadableConfig; +import org.apache.flink.python.PythonOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.util.Preconditions; + +import javax.annotation.Nullable; +import javax.xml.bind.DatatypeConverter; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.flink.python.PythonOptions.PYTHON_CLIENT_EXECUTABLE; +import static org.apache.flink.python.PythonOptions.PYTHON_EXECUTABLE; + +/** + * Utility class for Python dependency management. The dependencies will be registered at the distributed + * cache. + */ +public class PythonDependencyUtils { + + public static final String FILE = "file"; + public static final String CACHE = "cache"; + public static final String FILE_DELIMITER = ","; + public static final String PARAM_DELIMITER = "#"; + private static final String HASH_ALGORITHM = "SHA-256"; + + // Internal Python Config Options. + + 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(); + + public static Configuration configurePythonDependencies(ExecutionEnvironment env, Configuration config) { + PythonDependencyManager pythonDependencyManager; + try { + Field cacheFile = ExecutionEnvironment.class.getDeclaredField("cacheFile"); + cacheFile.setAccessible(true); + pythonDependencyManager = new PythonDependencyManager( + (List<Tuple2<String, DistributedCache.DistributedCacheEntry>>) cacheFile.get(env)); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + return configurePythonDependenciesInternal(pythonDependencyManager, env.getConfiguration(), config); + } + + public static Configuration configurePythonDependencies(StreamExecutionEnvironment env, Configuration config) { + PythonDependencyManager pythonDependencyManager = new PythonDependencyManager(env.getCachedFiles()); + Configuration envConfiguration; + try { + Method getConfiguration = StreamExecutionEnvironment.class.getDeclaredMethod("getConfiguration"); + getConfiguration.setAccessible(true); + envConfiguration = (Configuration) getConfiguration.invoke(env); + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new RuntimeException(e); + } + return configurePythonDependenciesInternal(pythonDependencyManager, envConfiguration, config); + } + + private static Configuration configurePythonDependenciesInternal( + PythonDependencyManager pythonDependencyManager, + Configuration envConfiguration, + Configuration config) { + Configuration internalConfig = pythonDependencyManager.getConfigWithPythonDependencyOptions(); Review comment: The following code is very confusing. Is it possible to improve it? ---------------------------------------------------------------- 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] With regards, Apache Git Services
