From 15eae700a3437d82131c6db7a7cb6e890f6d65ee Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 15 Aug 2018 16:04:05 -0400 Subject: [PATCH] Move LeaderInitiators to later phase There is a race condition when Zookeeper `LeaderInitiator` may start earlier than `CuratorFramework` it depends on. This is because both of them are configured for the same default `phase` (`0`) * Make `LeaderInitiator` and `LockRegistryLeaderInitiator` to be started in the `Integer.MAX_VALUE - 1000` phase by default - as late as possible * Make `CuratorFrameworkFactoryBean` to start in the `Integer.MIN_VALUE + 1000` phase - as early as possible * Alight lifecycle properties from the `LeaderInitiatorFactoryBean` with defaults in the `LeaderInitiator` * Simplify a `LeaderListenerParser` to rely on the `LeaderInitiatorFactoryBean` **Cherry-pick to 5.0.x** --- .../leader/LockRegistryLeaderInitiator.java | 2 +- .../config/CuratorFrameworkFactoryBean.java | 19 ++++++------ .../config/LeaderInitiatorFactoryBean.java | 11 +++---- .../config/xml/LeaderListenerParser.java | 30 +++++++------------ .../zookeeper/leader/LeaderInitiator.java | 4 +-- 5 files changed, 30 insertions(+), 36 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java b/spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java index 62dfe16d34..f67403959a 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java @@ -151,7 +151,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe /** * @see SmartLifecycle which is an extension of org.springframework.context.Phased */ - private int phase; + private int phase = Integer.MAX_VALUE - 1000; /** * Flag that indicates whether the leadership election for this {@link #candidate} is diff --git a/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBean.java b/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBean.java index 51d5820147..52a62554f6 100644 --- a/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBean.java +++ b/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,10 +27,11 @@ import org.springframework.context.SmartLifecycle; import org.springframework.util.Assert; /** - * A spring-friendly way to build a {@link CuratorFramework} and implementing {@link SmartLifecycle}. + * A Spring-friendly way to build a {@link CuratorFramework} and implementing {@link SmartLifecycle}. * * @author Gary Russell * @author Artem Bilan + * * @since 4.2 */ public class CuratorFrameworkFactoryBean implements FactoryBean, SmartLifecycle { @@ -42,18 +43,18 @@ public class CuratorFrameworkFactoryBean implements FactoryBean