Browse Source

Merge pull request #16333 from zzzeebra/fix/zk-discovery-creation-order

Fixed a stale ZooKeeper registration of a node being preferred over its re-registration
pull/16346/head
Viacheslav Klimov 1 month ago
committed by GitHub
parent
commit
d069f126fa
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 5
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java
  2. 29
      common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java

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

@ -118,8 +118,11 @@ public class ZkDiscoveryService implements DiscoveryService {
}
})
.filter(r -> !r.info().getServiceId().equals(currentServiceId))
// Orders ZK registrations by creation, so that a re-registration wins over a stale node of the same service.
// Uses the creation zxid: ZooKeeper's transaction id, which grows across leader changes and is the same
// on every client. ctime/mtime are not used, as they come from the leader's wall clock and may go backwards.
.collect(Collectors.toMap(r -> r.info().getServiceId(), Function.identity(),
(a, b) -> b.data().getStat().getMtime() > a.data().getStat().getMtime() ? b : a,
(a, b) -> b.data().getStat().getCzxid() > a.data().getStat().getCzxid() ? b : a,
LinkedHashMap::new))
.values().stream()
.map(Registration::info)

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

@ -260,7 +260,7 @@ public class ZkDiscoveryServiceTest {
@Test
public void getOtherServersIgnoresNodesWithCurrentServiceIdTest() {
dataList.add(new ChildData("/thingsboard/nodes/0000000011", statWithMtime(1), currentInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000011", statWithCzxid(1), currentInfo.toByteArray()));
dataList.add(childData);
assertEquals(List.of(childInfo), zkDiscoveryService.getOtherServers());
@ -269,8 +269,8 @@ public class ZkDiscoveryServiceTest {
@Test
public void getOtherServersKeepsLatestNodeForDuplicatedServiceIdTest() {
TransportProtos.ServiceInfo latestInfo = childInfo.toBuilder().setReady(true).build();
dataList.add(new ChildData("/thingsboard/nodes/0000000020", statWithMtime(1), childInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000021", statWithMtime(2), latestInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000020", statWithCzxid(1), childInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000021", statWithCzxid(2), latestInfo.toByteArray()));
assertEquals(List.of(latestInfo), zkDiscoveryService.getOtherServers());
@ -279,9 +279,22 @@ public class ZkDiscoveryServiceTest {
assertEquals(List.of(latestInfo), zkDiscoveryService.getOtherServers());
}
@Test
public void getOtherServersKeepsLatestCreatedNodeEvenIfItsMtimeIsOlderTest() {
TransportProtos.ServiceInfo latestInfo = childInfo.toBuilder().setReady(true).build();
Stat staleStat = statWithCzxid(1);
staleStat.setMtime(2000);
Stat latestStat = statWithCzxid(2);
latestStat.setMtime(1000);
dataList.add(new ChildData("/thingsboard/nodes/0000000020", staleStat, childInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000021", latestStat, latestInfo.toByteArray()));
assertEquals(List.of(latestInfo), zkDiscoveryService.getOtherServers());
}
@Test
public void nodeWithCurrentServiceIdDoesNotTriggerRecalculationTest() {
ChildData orphanData = new ChildData("/thingsboard/nodes/0000000011", statWithMtime(1), currentInfo.toByteArray());
ChildData orphanData = new ChildData("/thingsboard/nodes/0000000011", statWithCzxid(1), currentInfo.toByteArray());
startNode(orphanData);
stopNode(orphanData);
@ -293,8 +306,8 @@ public class ZkDiscoveryServiceTest {
@Test
public void removalOfDuplicatedNodeDoesNotPublishShutdownEventTest() {
ChildData staleData = new ChildData("/thingsboard/nodes/0000000019", statWithMtime(1), childInfo.toByteArray());
ChildData liveData = new ChildData("/thingsboard/nodes/0000000020", statWithMtime(2), childInfo.toByteArray());
ChildData staleData = new ChildData("/thingsboard/nodes/0000000019", statWithCzxid(1), childInfo.toByteArray());
ChildData liveData = new ChildData("/thingsboard/nodes/0000000020", statWithCzxid(2), childInfo.toByteArray());
startNode(staleData);
startNode(liveData);
@ -328,9 +341,9 @@ public class ZkDiscoveryServiceTest {
return stat;
}
private static Stat statWithMtime(long mtime) {
private static Stat statWithCzxid(long czxid) {
Stat stat = new Stat();
stat.setMtime(mtime);
stat.setCzxid(czxid);
return stat;
}

Loading…
Cancel
Save