Commit 6c618a7db35ecc1a757f09869fcb16465f7de465
1 parent
474dc167
Fixed issue with Zookeeper reconnect
Showing
1 changed file
with
30 additions
and
2 deletions
... | ... | @@ -24,6 +24,8 @@ import org.apache.curator.framework.recipes.cache.ChildData; |
24 | 24 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; |
25 | 25 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; |
26 | 26 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; |
27 | +import org.apache.curator.framework.state.ConnectionState; | |
28 | +import org.apache.curator.framework.state.ConnectionStateListener; | |
27 | 29 | import org.apache.curator.retry.RetryForever; |
28 | 30 | import org.apache.curator.utils.CloseableUtils; |
29 | 31 | import org.apache.zookeeper.CreateMode; |
... | ... | @@ -127,12 +129,38 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
127 | 129 | .creatingParentsIfNeeded() |
128 | 130 | .withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(zkNodesDir + "/", SerializationUtils.serialize(self.getServerAddress())); |
129 | 131 | log.info("[{}:{}] Created ZK node for current instance: {}", self.getHost(), self.getPort(), nodePath); |
132 | + client.getConnectionStateListenable().addListener(checkReconnect(self)); | |
130 | 133 | } catch (Exception e) { |
131 | 134 | log.error("Failed to create ZK node", e); |
132 | 135 | throw new RuntimeException(e); |
133 | 136 | } |
134 | 137 | } |
135 | 138 | |
139 | + private ConnectionStateListener checkReconnect(ServerInstance self) { | |
140 | + return (client, newState) -> { | |
141 | + log.info("[{}:{}] ZK state changed: {}", self.getHost(), self.getPort(), newState); | |
142 | + if (newState == ConnectionState.LOST) { | |
143 | + reconnect(); | |
144 | + } | |
145 | + }; | |
146 | + } | |
147 | + | |
148 | + private boolean reconnectInProgress = false; | |
149 | + | |
150 | + private synchronized void reconnect() { | |
151 | + if (!reconnectInProgress) { | |
152 | + reconnectInProgress = true; | |
153 | + try { | |
154 | + client.blockUntilConnected(); | |
155 | + publishCurrentServer(); | |
156 | + } catch (InterruptedException e) { | |
157 | + log.error("Failed to reconnect to ZK: {}", e.getMessage(), e); | |
158 | + } finally { | |
159 | + reconnectInProgress = false; | |
160 | + } | |
161 | + } | |
162 | + } | |
163 | + | |
136 | 164 | @Override |
137 | 165 | public void unpublishCurrentServer() { |
138 | 166 | try { |
... | ... | @@ -156,7 +184,7 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
156 | 184 | .filter(cd -> !cd.getPath().equals(nodePath)) |
157 | 185 | .map(cd -> { |
158 | 186 | try { |
159 | - return new ServerInstance( (ServerAddress) SerializationUtils.deserialize(cd.getData())); | |
187 | + return new ServerInstance((ServerAddress) SerializationUtils.deserialize(cd.getData())); | |
160 | 188 | } catch (NoSuchElementException e) { |
161 | 189 | log.error("Failed to decode ZK node", e); |
162 | 190 | throw new RuntimeException(e); |
... | ... | @@ -198,7 +226,7 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
198 | 226 | } |
199 | 227 | ServerInstance instance; |
200 | 228 | try { |
201 | - ServerAddress serverAddress = SerializationUtils.deserialize(data.getData()); | |
229 | + ServerAddress serverAddress = SerializationUtils.deserialize(data.getData()); | |
202 | 230 | instance = new ServerInstance(serverAddress); |
203 | 231 | } catch (SerializationException e) { |
204 | 232 | log.error("Failed to decode server instance for node {}", data.getPath(), e); | ... | ... |