Commit f6b00b35db3b14d40c99f55adcb0eebc8d4289d6
1 parent
3235e570
Improve docker compose. Improve TB shutdown logic.
Showing
3 changed files
with
29 additions
and
0 deletions
... | ... | @@ -21,6 +21,7 @@ import org.apache.commons.lang3.SerializationException; |
21 | 21 | import org.apache.commons.lang3.SerializationUtils; |
22 | 22 | import org.apache.curator.framework.CuratorFramework; |
23 | 23 | import org.apache.curator.framework.CuratorFrameworkFactory; |
24 | +import org.apache.curator.framework.imps.CuratorFrameworkState; | |
24 | 25 | import org.apache.curator.framework.recipes.cache.ChildData; |
25 | 26 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; |
26 | 27 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; |
... | ... | @@ -98,6 +99,8 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
98 | 99 | private PathChildrenCache cache; |
99 | 100 | private String nodePath; |
100 | 101 | |
102 | + private volatile boolean stopped = false; | |
103 | + | |
101 | 104 | @PostConstruct |
102 | 105 | public void init() { |
103 | 106 | log.info("Initializing..."); |
... | ... | @@ -118,6 +121,7 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
118 | 121 | cache.start(); |
119 | 122 | } catch (Exception e) { |
120 | 123 | log.error("Failed to connect to ZK: {}", e.getMessage(), e); |
124 | + CloseableUtils.closeQuietly(cache); | |
121 | 125 | CloseableUtils.closeQuietly(client); |
122 | 126 | throw new RuntimeException(e); |
123 | 127 | } |
... | ... | @@ -125,7 +129,9 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
125 | 129 | |
126 | 130 | @PreDestroy |
127 | 131 | public void destroy() { |
132 | + stopped = true; | |
128 | 133 | unpublishCurrentServer(); |
134 | + CloseableUtils.closeQuietly(cache); | |
129 | 135 | CloseableUtils.closeQuietly(client); |
130 | 136 | log.info("Stopped discovery service"); |
131 | 137 | } |
... | ... | @@ -228,6 +234,14 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
228 | 234 | |
229 | 235 | @Override |
230 | 236 | public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { |
237 | + if (stopped) { | |
238 | + log.debug("Ignoring application ready event. Service is stopped."); | |
239 | + return; | |
240 | + } | |
241 | + if (client.getState() != CuratorFrameworkState.STARTED) { | |
242 | + log.debug("Ignoring application ready event, ZK client is not started, ZK client state [{}]", client.getState()); | |
243 | + return; | |
244 | + } | |
231 | 245 | publishCurrentServer(); |
232 | 246 | getOtherServers().forEach( |
233 | 247 | server -> log.info("Found active server: [{}:{}]", server.getHost(), server.getPort()) |
... | ... | @@ -236,6 +250,14 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi |
236 | 250 | |
237 | 251 | @Override |
238 | 252 | public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { |
253 | + if (stopped) { | |
254 | + log.debug("Ignoring {}. Service is stopped.", pathChildrenCacheEvent); | |
255 | + return; | |
256 | + } | |
257 | + if (client.getState() != CuratorFrameworkState.STARTED) { | |
258 | + log.debug("Ignoring {}, ZK client is not started, ZK client state [{}]", pathChildrenCacheEvent, client.getState()); | |
259 | + return; | |
260 | + } | |
239 | 261 | ChildData data = pathChildrenCacheEvent.getData(); |
240 | 262 | if (data == null) { |
241 | 263 | log.debug("Ignoring {} due to empty child data", pathChildrenCacheEvent); | ... | ... |
... | ... | @@ -18,6 +18,7 @@ package org.thingsboard.server.kafka; |
18 | 18 | import lombok.Builder; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | 20 | import org.apache.kafka.clients.consumer.ConsumerRecords; |
21 | +import org.apache.kafka.common.errors.InterruptException; | |
21 | 22 | import org.apache.kafka.common.header.Header; |
22 | 23 | import org.apache.kafka.common.header.internals.RecordHeader; |
23 | 24 | |
... | ... | @@ -127,6 +128,10 @@ public class TbKafkaResponseTemplate<Request, Response> extends AbstractTbKafkaT |
127 | 128 | log.warn("[{}] Failed to process the request: {}", requestId, request, e); |
128 | 129 | } |
129 | 130 | }); |
131 | + } catch (InterruptException ie) { | |
132 | + if (!stopped) { | |
133 | + log.warn("Fetching data from kafka was interrupted.", ie); | |
134 | + } | |
130 | 135 | } catch (Throwable e) { |
131 | 136 | log.warn("Failed to obtain messages from queue.", e); |
132 | 137 | try { | ... | ... |
... | ... | @@ -64,6 +64,7 @@ services: |
64 | 64 | depends_on: |
65 | 65 | - kafka |
66 | 66 | - redis |
67 | + - tb-js-executor | |
67 | 68 | tb2: |
68 | 69 | restart: always |
69 | 70 | image: "${DOCKER_REPO}/${TB_NODE_DOCKER_NAME}:${TB_VERSION}" |
... | ... | @@ -84,6 +85,7 @@ services: |
84 | 85 | depends_on: |
85 | 86 | - kafka |
86 | 87 | - redis |
88 | + - tb-js-executor | |
87 | 89 | tb-mqtt-transport1: |
88 | 90 | restart: always |
89 | 91 | image: "${DOCKER_REPO}/${MQTT_TRANSPORT_DOCKER_NAME}:${TB_VERSION}" | ... | ... |