fsk119 commented on code in PR #19823: URL: https://github.com/apache/flink/pull/19823#discussion_r894322834
########## flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/context/SessionContext.java: ########## @@ -0,0 +1,303 @@ +/* + * 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.table.gateway.service.context; + +import org.apache.flink.client.ClientUtils; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.CoreOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.EnvironmentSettings; +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl; +import org.apache.flink.table.api.internal.TableEnvironmentInternal; +import org.apache.flink.table.catalog.CatalogManager; +import org.apache.flink.table.catalog.FunctionCatalog; +import org.apache.flink.table.catalog.GenericInMemoryCatalog; +import org.apache.flink.table.delegation.Executor; +import org.apache.flink.table.delegation.ExecutorFactory; +import org.apache.flink.table.delegation.Planner; +import org.apache.flink.table.factories.FactoryUtil; +import org.apache.flink.table.factories.PlannerFactoryUtil; +import org.apache.flink.table.gateway.common.endpoint.EndpointVersion; +import org.apache.flink.table.gateway.common.session.SessionHandle; +import org.apache.flink.table.gateway.service.operation.OperationManager; +import org.apache.flink.table.module.ModuleManager; +import org.apache.flink.util.TemporaryClassLoaderContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; + +/** + * Context describing a session, it's mainly used for user to open a new session in the backend. If + * client request to open a new session, the backend {@code Executor} will maintain the session + * context map util users close it. + */ +public class SessionContext { + + private static final Logger LOG = LoggerFactory.getLogger(SessionContext.class); + + private final SessionHandle sessionId; + private final EndpointVersion endpointVersion; + + // store all options and use Configuration to build SessionState and TableConfig. + private final Configuration sessionConf; + private final SessionState sessionState; + private final URLClassLoader userClassloader; + + private final OperationManager operationManager; + + private SessionContext( + SessionHandle sessionId, + EndpointVersion endpointVersion, + Configuration sessionConf, + URLClassLoader classLoader, + SessionState sessionState, + OperationManager operationManager) { + this.sessionId = sessionId; + this.endpointVersion = endpointVersion; + this.sessionConf = sessionConf; + this.userClassloader = classLoader; + this.sessionState = sessionState; + this.operationManager = operationManager; + } + + // -------------------------------------------------------------------------------------------- + // Getter method + // -------------------------------------------------------------------------------------------- + + public SessionHandle getSessionId() { + return this.sessionId; + } + + public Map<String, String> getConfigMap() { + return sessionConf.toMap(); + } + + // -------------------------------------------------------------------------------------------- + // Method to execute commands + // -------------------------------------------------------------------------------------------- + + /** Close resources, e.g. catalogs. */ + public void close() { + try (TemporaryClassLoaderContext ignored = + TemporaryClassLoaderContext.of(userClassloader)) { + for (String name : sessionState.catalogManager.listCatalogs()) { + sessionState.catalogManager.unregisterCatalog(name, true); + } + } + try { + userClassloader.close(); + } catch (IOException e) { + LOG.debug("Error while closing class loader.", e); + } + + operationManager.close(); + } + + // -------------------------------------------------------------------------------------------- + // Utilities + // -------------------------------------------------------------------------------------------- + + public static SessionContext create( + DefaultContext defaultContext, + SessionHandle sessionId, + EndpointVersion endpointVersion, + Configuration sessionConf, + ExecutorService operationExecutorService) { + // -------------------------------------------------------------------------------------------------------------- + // Init config + // -------------------------------------------------------------------------------------------------------------- + + Configuration configuration = defaultContext.getFlinkConfig().clone(); + configuration.addAll(sessionConf); + + // -------------------------------------------------------------------------------------------------------------- + // Init classloader + // -------------------------------------------------------------------------------------------------------------- + + URLClassLoader classLoader = + buildClassLoader(Collections.emptySet(), Collections.emptySet(), configuration); Review Comment: It's just placeholder for now. When support add jar, we shoud build a new classloader. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
