GJL commented on a change in pull request #8912: [FLINK-12709] [runtime] 
Implement new generation restart strategy loader which also respects legacy…
URL: https://github.com/apache/flink/pull/8912#discussion_r326166365
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/executiongraph/failover/flip1/RestartBackoffTimeStrategyFactoryLoader.java
 ##########
 @@ -0,0 +1,312 @@
+/*
+ * 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.runtime.executiongraph.failover.flip1;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.RestartStrategyOptions;
+import 
org.apache.flink.runtime.executiongraph.restart.FixedDelayRestartStrategy;
+import 
org.apache.flink.runtime.executiongraph.restart.NoOrFixedIfCheckpointingEnabledRestartStrategyFactory;
+import org.apache.flink.runtime.executiongraph.restart.NoRestartStrategy;
+import org.apache.flink.runtime.executiongraph.restart.RestartStrategy;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A utility class to load {@link RestartBackoffTimeStrategy.Factory} from the 
configuration.
+ * It respects the configs for the legacy {@link RestartStrategy}.
+ */
+public final class RestartBackoffTimeStrategyFactoryLoader {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(RestartBackoffTimeStrategyFactoryLoader.class);
+
+       private static final String CREATE_METHOD = "createFactory";
+
+       private RestartBackoffTimeStrategyFactoryLoader() {
+       }
+
+       /**
+        * Creates a {@link RestartBackoffTimeStrategy.Factory}.
+        * If new version restart strategy is specified, will directly use it.
+        * Otherwise will decide based on legacy restart strategy configs.
+        *
+        * @param jobRestartStrategyConfiguration restart configuration given 
within the job graph
+        * @param clusterConfiguration cluster(server-side) configuration, 
usually represented as jobmanager config
+        * @param isCheckpointingEnabled if checkpointing was enabled for the 
job
+        * @return new version restart strategy factory
+        */
+       public static RestartBackoffTimeStrategy.Factory 
createRestartStrategyFactory(
+                       final RestartStrategies.RestartStrategyConfiguration 
jobRestartStrategyConfiguration,
+                       final Configuration clusterConfiguration,
+                       final boolean isCheckpointingEnabled) {
+
+               checkNotNull(jobRestartStrategyConfiguration);
+               checkNotNull(clusterConfiguration);
+
+               final String restartStrategyClassName = 
clusterConfiguration.getString(
+                       RestartStrategyOptions.RESTART_STRATEGY_CLASS_NAME);
+
+               if (restartStrategyClassName != null) {
+                       // create new version restart strategy directly if it 
is specified in cluster config
+                       return 
createRestartStrategyFactoryInternal(clusterConfiguration);
+               } else {
+                       // adapt the legacy restart strategy configs for new 
version restart strategy
+                       final Configuration adaptedConfiguration = 
getAdaptedConfiguration(
+                               jobRestartStrategyConfiguration,
+                               clusterConfiguration,
+                               isCheckpointingEnabled);
+
+                       // create new version restart strategy from the adapted 
config
+                       return 
createRestartStrategyFactoryInternal(adaptedConfiguration);
+               }
+       }
+
+       private static RestartBackoffTimeStrategy.Factory 
createRestartStrategyFactoryInternal(
+                       final Configuration configuration) {
+
+               final String restartStrategyClassName = configuration.getString(
+                       RestartStrategyOptions.RESTART_STRATEGY_CLASS_NAME);
+
+               if (restartStrategyClassName == null) {
+                       LOG.info("No restart strategy is configured.");
+                       return new 
NoRestartBackoffTimeStrategy.NoRestartBackoffTimeStrategyFactory();
+               }
+
+               try {
+                       final Class<?> clazz = 
Class.forName(restartStrategyClassName);
+                       if (clazz != null && 
RestartBackoffTimeStrategy.class.isAssignableFrom(clazz)) {
+                               final Method method = 
clazz.getMethod(CREATE_METHOD, Configuration.class);
+                               if (method != null) {
+                                       final Object result = 
method.invoke(null, configuration);
+
+                                       if (result != null) {
+                                               return 
(RestartBackoffTimeStrategy.Factory) result;
+                                       }
+                               }
+                       }
+               } catch (ClassNotFoundException cnfe) {
+                       LOG.warn("Could not find restart strategy class {}.", 
restartStrategyClassName);
+               } catch (NoSuchMethodException nsme) {
+                       LOG.warn("Class {} does not have static method {}.", 
restartStrategyClassName, CREATE_METHOD);
+               } catch (InvocationTargetException ite) {
+                       LOG.warn("Cannot call static method {} from class {}.", 
CREATE_METHOD, restartStrategyClassName);
+               } catch (IllegalAccessException iae) {
+                       LOG.warn("Illegal access while calling method {} from 
class {}.", CREATE_METHOD, restartStrategyClassName);
+               }
+
+               // fallback in case of an error
+               LOG.warn("Configured restart strategy {} is not valid. Fall 
back to no restart strategy.",
+                       restartStrategyClassName);
+               return new 
NoRestartBackoffTimeStrategy.NoRestartBackoffTimeStrategyFactory();
+       }
+
+       /**
+        * Decides the {@link RestartBackoffTimeStrategy} (new version restart 
strategy) to use and its params based on
+        * legacy restart strategy configs. Records its class name and the 
params into an adapted configuration.
+        *
+        * <p>The decision making is as follows:
+        * <ol>
+        *     <li>Use the strategy related to {@link 
RestartStrategies.RestartStrategyConfiguration}, unless it's
+        * null or {@link 
RestartStrategies.FallbackRestartStrategyConfiguration}.</li>
+        *     <li>If the strategy is not determined, try adapting the 
cluster(server-side) configs of legacy strategy
+        * for new version, unless the legacy restart strategy is not specified 
or not valid</li>
+        *     <li>If the strategy is still not determined, use {@link 
FixedDelayRestartStrategy} if checkpointing is
+        * enabled. Otherwise use {@link NoRestartStrategy}</li>
+        * </ol>
+        *
+        * @param jobRestartStrategyConfiguration restart configuration given 
within the job graph
+        * @param clusterConfiguration cluster(server-side) configuration, 
usually represented as jobmanager config
+        * @param isCheckpointingEnabled if checkpointing was enabled for the 
job
+        * @return adapted configuration
+        */
+       private static Configuration getAdaptedConfiguration(
+                       final RestartStrategies.RestartStrategyConfiguration 
jobRestartStrategyConfiguration,
+                       final Configuration clusterConfiguration,
+                       final boolean isCheckpointingEnabled) {
+
+               // clone a new config to not modify the existing config
+               final Configuration adaptedConfiguration = new Configuration();
+
+               // try determining restart strategy based on job level restart 
strategy config first
+               if 
(!tryAdaptingJobRestartStrategyConfiguration(jobRestartStrategyConfiguration, 
adaptedConfiguration)) {
 
 Review comment:
   I think we should drop the reflective instantiation (see my other comment) 
and return the `RestartBackoffTimeStrategy.Factory` immediately.  Moreover, I 
would restructure the code a bit to avoid output arguments 
(`adaptedConfiguration`), which are usually discouraged:
   
   ```
        final Optional<RestartBackoffTimeStrategy.Factory> jobRestartStrategy = 
[...];
        final Optional<RestartBackoffTimeStrategy.Factory> 
clusterRestartStrategy = [...];
   
        return Stream.of(jobRestartStrategy, clusterRestartStrategy)
                .filter(Optional::isPresent)
                .findFirst()
                .map(Optional::get)
                .orElse(defaultRestartStrategy(checkpointingEnabled);
   ```
   
   Let me know what you think.

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

Reply via email to