wuchong commented on code in PR #19849:
URL: https://github.com/apache/flink/pull/19849#discussion_r913726636


##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/endpoint/SqlGatewayEndpointFactoryUtils.java:
##########
@@ -0,0 +1,153 @@
+/*
+ *  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.api.endpoint;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.configuration.ConfigOptions;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.DelegatingConfiguration;
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.factories.FactoryUtil;
+import org.apache.flink.table.factories.FactoryUtil.FactoryHelper;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** Util to discover the {@link SqlGatewayEndpoint}. */
+@PublicEvolving
+public class SqlGatewayEndpointFactoryUtils {
+
+    private static final String GATEWAY_ENDPOINT_PREFIX = 
"sql-gateway.endpoint";
+
+    public static final ConfigOption<List<String>> SQL_GATEWAY_ENDPOINT_TYPE =
+            ConfigOptions.key(String.format("%s.type", 
GATEWAY_ENDPOINT_PREFIX))
+                    .stringType()
+                    .asList()
+                    .noDefaultValue()
+                    .withDescription("Specify the endpoints that are used.");
+
+    /**
+     * Attempts to discover the appropriate endpoint factory and creates the 
instance of the
+     * endpoints.
+     */
+    public static List<SqlGatewayEndpoint> createSqlGatewayEndpoint(
+            SqlGatewayService service, Configuration configuration) {
+        List<String> identifiers = 
configuration.get(SQL_GATEWAY_ENDPOINT_TYPE);
+
+        if (identifiers == null || identifiers.isEmpty()) {
+            throw new ValidationException(
+                    String.format(
+                            "Endpoint options do not contain an option key 
'%s' for discovering an endpoint.",
+                            SQL_GATEWAY_ENDPOINT_TYPE.key()));
+        }
+        validateSpecifiedEndpointsAreUnique(identifiers);
+
+        List<SqlGatewayEndpoint> endpoints = new ArrayList<>();
+        for (String identifier : identifiers) {
+            final SqlGatewayEndpointFactory factory =
+                    FactoryUtil.discoverFactory(
+                            Thread.currentThread().getContextClassLoader(),
+                            SqlGatewayEndpointFactory.class,
+                            identifier);
+            Configuration endpointConfig =
+                    new DelegatingConfiguration(

Review Comment:
   Shall we need to delegate all configurations with the endpoint prefix? This 
disallows the endpoint to access session configurations, such as options in 
`SqlGatewayServiceConfigOptions` which might be helpful in some cases. 



##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/endpoint/SqlGatewayEndpointFactoryUtils.java:
##########
@@ -0,0 +1,153 @@
+/*
+ *  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.api.endpoint;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.configuration.ConfigOptions;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.DelegatingConfiguration;
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.factories.FactoryUtil;
+import org.apache.flink.table.factories.FactoryUtil.FactoryHelper;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** Util to discover the {@link SqlGatewayEndpoint}. */
+@PublicEvolving
+public class SqlGatewayEndpointFactoryUtils {
+
+    private static final String GATEWAY_ENDPOINT_PREFIX = 
"sql-gateway.endpoint";
+
+    public static final ConfigOption<List<String>> SQL_GATEWAY_ENDPOINT_TYPE =
+            ConfigOptions.key(String.format("%s.type", 
GATEWAY_ENDPOINT_PREFIX))
+                    .stringType()
+                    .asList()
+                    .noDefaultValue()
+                    .withDescription("Specify the endpoints that are used.");
+
+    /**
+     * Attempts to discover the appropriate endpoint factory and creates the 
instance of the
+     * endpoints.
+     */
+    public static List<SqlGatewayEndpoint> createSqlGatewayEndpoint(
+            SqlGatewayService service, Configuration configuration) {
+        List<String> identifiers = 
configuration.get(SQL_GATEWAY_ENDPOINT_TYPE);
+
+        if (identifiers == null || identifiers.isEmpty()) {
+            throw new ValidationException(
+                    String.format(
+                            "Endpoint options do not contain an option key 
'%s' for discovering an endpoint.",
+                            SQL_GATEWAY_ENDPOINT_TYPE.key()));
+        }
+        validateSpecifiedEndpointsAreUnique(identifiers);
+
+        List<SqlGatewayEndpoint> endpoints = new ArrayList<>();
+        for (String identifier : identifiers) {
+            final SqlGatewayEndpointFactory factory =
+                    FactoryUtil.discoverFactory(
+                            Thread.currentThread().getContextClassLoader(),
+                            SqlGatewayEndpointFactory.class,
+                            identifier);
+            Configuration endpointConfig =
+                    new DelegatingConfiguration(
+                            configuration,
+                            String.format("%s.%s.", GATEWAY_ENDPOINT_PREFIX, 
identifier));
+            endpoints.add(
+                    factory.createSqlGatewayEndpoint(
+                            new 
DefaultSqlGatewayEndpointFactoryContext(service, endpointConfig)));
+        }
+        return endpoints;
+    }
+
+    /**
+     * Creates a utility that helps to validate options for a {@link 
SqlGatewayEndpointFactory}.
+     *
+     * <p>Note: This utility checks for left-over options in the final step.
+     */
+    public static SqlGatewayEndpointFactoryHelper 
createSqlGatewayEndpointFactoryHelper(
+            SqlGatewayEndpointFactory endpointFactory, 
SqlGatewayEndpointFactory.Context context) {
+        return new SqlGatewayEndpointFactoryHelper(endpointFactory, 
context.getOptions());
+    }
+
+    // 
----------------------------------------------------------------------------------------
+
+    /**
+     * Helper utility for validating all options for a {@link 
SqlGatewayEndpointFactory}.
+     *
+     * @see #createSqlGatewayEndpointFactoryHelper(SqlGatewayEndpointFactory,
+     *     SqlGatewayEndpointFactory.Context)
+     */
+    public static class SqlGatewayEndpointFactoryHelper
+            extends FactoryHelper<SqlGatewayEndpointFactory> {
+
+        private SqlGatewayEndpointFactoryHelper(
+                SqlGatewayEndpointFactory factory, Map<String, String> 
configOptions) {
+            super(factory, configOptions);
+        }
+    }
+
+    private static class DefaultSqlGatewayEndpointFactoryContext
+            implements SqlGatewayEndpointFactory.Context {
+
+        private final SqlGatewayService service;
+        private final Configuration configuration;
+
+        public DefaultSqlGatewayEndpointFactoryContext(
+                SqlGatewayService service, Configuration configuration) {
+            this.service = service;
+            this.configuration = configuration;
+        }
+
+        @Override
+        public SqlGatewayService getSqlGatewayService() {
+            return service;
+        }
+
+        @Override
+        public ReadableConfig getConfiguration() {
+            return configuration;
+        }
+
+        @Override
+        public Map<String, String> getOptions() {
+            return configuration.toMap();
+        }

Review Comment:
   This looks like a design flaw that if `getConfiguration` and `getOptions` 
are equivalent, then why do we need to provide both of them? 
   
   In DynamicTableFactory, the configuration represents the configs of the 
session, and options represent the table WITH options. I think we can change 
the methods to `getFlinkConfiguration()` and `getEndpointOptions()` to make it 
clear. 
   
   
   
   



-- 
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]

Reply via email to