Flink源码专辑:
Flink源码分析: 广播状态流实现规格更新或字段参数变更
版权说明:本专栏是作者在日常工作期间对技术的不断深入研究后的沉淀,辛苦码字总结而成。严禁转载,严禁作为商业用途转发。欢迎同行加入大数据技术群QQ:427560730
版本号:Flink1.10.0
重启策略概览:
public static RestartStrategies.RestartStrategyConfiguration noRestart() {
return new RestartStrategies.NoRestartStrategyConfiguration();
}
public static RestartStrategies.RestartStrategyConfiguration fallBackRestart() {
return new RestartStrategies.FallbackRestartStrategyConfiguration();
}
public static RestartStrategies.RestartStrategyConfiguration fixedDelayRestart(int restartAttempts, long delayBetweenAttempts) {
return fixedDelayRestart(restartAttempts, Time.of(delayBetweenAttempts, TimeUnit.MILLISECONDS));
}
public static RestartStrategies.RestartStrategyConfiguration fixedDelayRestart(int restartAttempts, Time delayInterval) {
return new RestartStrategies.FixedDelayRestartStrategyConfiguration(restartAttempts, delayInterval);
}
public static RestartStrategies.FailureRateRestartStrategyConfiguration failureRateRestart(int failureRate, Time failureInterval, Time delayInterval) {
return new RestartStrategies.FailureRateRestartStrategyConfiguration(failureRate, failureInterval, delayInterval);
}
Flink支持多种重启策略,可以在程序中发生故障时选择不同的重启模式进行重启。常用的重启策略可以从源码中看书:
- noRestart
- fallBackRestart
- fixedDelayRestart
- failureRateRestart
parseConfiguration方法
parseConfiguration方法输入两个参数:String类型的restartstrategyKind和ReadableConfig类型的configuration,从源码中可以看出根据var2=restartstrategyKind.toLowerCase()的hashCode值进行匹配给变量var3进行赋值,这样可以有效的防止hash冲突的问题。
- 当restartstrategyKind为"none",“off”,“disable"时var3等于"0”,“1”,“2”,此时重启策略为noRestart。
- 当restartstrategyKind为"fixed-delay",“fixeddelay"时,var3等于"3”,“4”。
case 3:
case 4:
int attempts = (Integer)configuration.get(RestartStrategyOptions.RESTART_STRATEGY_FIXED_DELAY_ATTEMPTS);
Duration delay = (Duration)configuration.get(RestartStrategyOptions.RESTART_STRATEGY_FIXED_DELAY_DELAY);
return fixedDelayRestart(attempts, delay.toMillis());
int类型的attempts表示flink作业失败前的重启次数。Duration类型的delay表示两次重启之间的延迟时间
这里都调用的是RestartStrategyOptions这个类。
- 当restartstrategyKind为"failure-rate",“failurerate"时,var3等于"5”,“6”。
case 5:
case 6:
int maxFailures = (Integer)configuration.get(RestartStrategyOptions.RESTART_STRATEGY_FAILURE_RATE_MAX_FAILURES_PER_INTERVAL);
Duration failureRateInterval = (Duration)configuration.get(RestartStrategyOptions.RESTART_STRATEGY_FAILURE_RATE_FAILURE_RATE_INTERVAL);
Duration failureRateDelay = (Duration)configuration.get(RestartStrategyOptions.RESTART_STRATEGY_FAILURE_RATE_DELAY);
return failureRateRestart(maxFailures, Time.milliseconds(failureRateInterval.toMillis()), Time.milliseconds(failureRateDelay.toMillis()));
int类型的maxFailures表示flink作业最大失败次数,failureRateInterval表示flink故障率的时间间隔,failureRateDelay表示两个重启之间的延迟时间。
代码测试
// set up the execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
// operate in Event-time
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
// create a checkpoint every 5 seconds
env.enableCheckpointing(5000)
// try to restart 60 times with 10 seconds delay (10 Minutes)
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(60, Time.of(10, TimeUnit.SECONDS)))
欢迎加群,一起学习,群号:427560730