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_r323600583
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/executiongraph/failover/flip1/RestartBackoffTimeStrategyFactoryLoader.java
 ##########
 @@ -0,0 +1,393 @@
+/*
+ * 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.AkkaOptions;
+import org.apache.flink.configuration.ConfigConstants;
+import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.RestartBackoffTimeStrategyOptions;
+import 
org.apache.flink.runtime.executiongraph.restart.FixedDelayRestartStrategy;
+import org.apache.flink.runtime.executiongraph.restart.NoRestartStrategy;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.concurrent.TimeUnit;
+
+import scala.concurrent.duration.Duration;
+
+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.
+ */
+public class RestartBackoffTimeStrategyFactoryLoader {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(RestartBackoffTimeStrategyFactoryLoader.class);
+
+       private static final String CREATE_METHOD = "createFactory";
+
+       /**
+        * Creates proper {@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) throws Exception {
+
+               checkNotNull(jobRestartStrategyConfiguration);
+               checkNotNull(clusterConfiguration);
+
+               final String restartStrategyClassName = 
clusterConfiguration.getString(
+                       
RestartBackoffTimeStrategyOptions.RESTART_BACKOFF_TIME_STRATEGY_CLASS_NAME);
+
+               if (restartStrategyClassName != null) {
+                       // create new restart strategy directly if it is 
specified in cluster config
+                       return 
createRestartStrategyFactoryInternal(clusterConfiguration);
+               } else {
+                       // adapt the legacy restart strategy configs as new 
restart strategy configs
+                       final Configuration adaptedConfiguration = 
getAdaptedConfiguration(
+                               jobRestartStrategyConfiguration,
+                               clusterConfiguration,
+                               isCheckpointingEnabled);
+
+                       // create new restart strategy from the adapted config
+                       return 
createRestartStrategyFactoryInternal(adaptedConfiguration);
+               }
+       }
+
+       /**
+        * Decides the {@link RestartBackoffTimeStrategy} to use and its params 
based on legacy configs,
+        * and records its class name and the params into a adapted 
configuration.
+        *
+        * <p>The decision making is as follows:
+        * <ol>
+        *     <li>Use strategy of {@link 
RestartStrategies.RestartStrategyConfiguration}, unless it's a
+        * {@link RestartStrategies.FallbackRestartStrategyConfiguration} or 
not valid.</li>
+        *     <li>If strategy is not decided, use legacy strategy specified in 
cluster(server-side) config,
+        * unless it is not set or not valid</li>
+        *     <li>If strategy is not decided, 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) throws Exception {
+
+               // clone a new config to not modify existing one
+               final Configuration adaptedConfiguration = new Configuration();
+
+               // try determining restart strategy based on job restart 
strategy config first
+               if 
(!tryAdaptingJobRestartStrategyConfiguration(jobRestartStrategyConfiguration, 
adaptedConfiguration)) {
+
+                       // if job restart strategy config does not help
+                       // try determining restart strategy based on cluster 
config as fallback
+                       if 
(!tryAdaptingClusterRestartStrategyConfiguration(clusterConfiguration, 
adaptedConfiguration)) {
+
+                               // if the restart strategy is not decided yet
+                               // determine the strategy based on whether 
checkpointing is enabled
+                               
setDefaultRestartStrategyToConfiguration(isCheckpointingEnabled, 
adaptedConfiguration);
+                       }
+               }
+
+               return adaptedConfiguration;
+       }
+
+       private static RestartBackoffTimeStrategy.Factory 
createRestartStrategyFactoryInternal(
+                       final Configuration configuration) {
+
+               final String restartStrategyClassName = configuration.getString(
+                       
RestartBackoffTimeStrategyOptions.RESTART_BACKOFF_TIME_STRATEGY_CLASS_NAME);
+
+               if (restartStrategyClassName == null) {
+                       LOG.info("No restart strategy is configured. Using no 
restart strategy.");
+                       return new 
NoRestartBackoffTimeStrategy.NoRestartBackoffTimeStrategyFactory();
+               }
+
+               try {
+                       final Class<?> clazz = 
Class.forName(restartStrategyClassName);
 
 Review comment:
   As long as there are unit tests, it is probably reasonably safe to keep 
instantiating by reflection.

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

Reply via email to