Browse Source

Create the current node ZK registration in protected mode

The current node ZK node is created with Curator protection, so a create
retried after a connection loss reuses the node already created by the
lost attempt, and a node left by a failed create is deleted in the
background instead of staying registered for the lifetime of the session.
pull/16263/head
Oleksandra Matviienko 2 months ago
parent
commit
a823acd9ac
  1. 3
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java
  2. 4
      common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java

3
common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java

@ -167,7 +167,8 @@ public class ZkDiscoveryService implements DiscoveryService {
log.info("[{}] Creating ZK node for current instance", self.getServiceId());
nodePath = client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(zkNodesDir + "/", self.toByteArray());
.withProtection()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(zkNodesDir + "/node-", self.toByteArray());
log.info("[{}] Created ZK node for current instance: {}", self.getServiceId(), nodePath);
client.getConnectionStateListenable().addListener(checkReconnect(self));
} catch (Exception e) {

4
common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java

@ -217,7 +217,7 @@ public class ZkDiscoveryServiceTest {
when(existsBuilder.forPath(currentData.getPath())).thenReturn(null);
CreateBuilder createBuilder = mock(CreateBuilder.class, RETURNS_DEEP_STUBS);
when(client.create()).thenReturn(createBuilder);
when(createBuilder.creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/thingsboard/nodes/", currentInfo.toByteArray()))
when(createBuilder.creatingParentsIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/thingsboard/nodes/node-", currentInfo.toByteArray()))
.thenReturn(newPath);
when(client.getConnectionStateListenable()).thenReturn(mock(Listenable.class));
@ -235,7 +235,7 @@ public class ZkDiscoveryServiceTest {
mockSessionId();
CreateBuilder createBuilder = mock(CreateBuilder.class, RETURNS_DEEP_STUBS);
when(client.create()).thenReturn(createBuilder);
when(createBuilder.creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/thingsboard/nodes/", currentInfo.toByteArray()))
when(createBuilder.creatingParentsIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/thingsboard/nodes/node-", currentInfo.toByteArray()))
.thenReturn(newPath);
when(client.getConnectionStateListenable()).thenReturn(mock(Listenable.class));

Loading…
Cancel
Save