From 7bd156b555ec779934daf18916ee01d318550b51 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Fri, 16 Oct 2026 11:25:37 +0300 Subject: [PATCH 1/3] Fix license headers --- .../src/main/data/upgrade/lts/4.4.1.0/schema_update.sql | 5 ++--- .../server/service/install/lts/V4_4_1_0Migration.java | 5 ++--- tools/src/main/python/license-headers/curations.json | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql b/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql index 758854f92b..7d857eb6b2 100644 --- a/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql +++ b/application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql @@ -1,7 +1,6 @@ -- --- SPDX-FileCopyrightText: Copyright The ThingsBoard Authors --- SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. --- SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 +-- SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +-- SPDX-License-Identifier: BUSL-1.1 -- -- Tenant-scoped paging over calculated fields (WHERE tenant_id = ? ORDER BY id LIMIT ?), issued for every tenant diff --git a/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java b/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java index 1d91bc0e82..7b79bc59be 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java +++ b/application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java @@ -1,6 +1,5 @@ -// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. -// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 package org.thingsboard.server.service.install.lts; import org.springframework.stereotype.Component; diff --git a/tools/src/main/python/license-headers/curations.json b/tools/src/main/python/license-headers/curations.json index ee19d99f35..ecc5bb95de 100644 --- a/tools/src/main/python/license-headers/curations.json +++ b/tools/src/main/python/license-headers/curations.json @@ -137,7 +137,7 @@ "path": "application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java", "header": "ce-modified", "reason": "CE test application/src/test/java/org/thingsboard/server/service/install/lts/LtsMigrationIntegrationTest.java renamed in 7a926f2c1f2 when the Lts prefix was dropped from migrations, with PE changes on top. Reviewed against CE application/src/test/java/org/thingsboard/server/service/install/lts/LtsMigrationIntegrationTest.java at 5954190c32f (blob cf60ec2e4b5fce7a55862805e9bd959c125ce17e); a renamed CE file stays CE-derived.", - "contentHash": "sha256:cbe953023311f4f98e57ac04bdae7f851ef28cdc4938c31526af68ec30d2b4c2" + "contentHash": "sha256:24cb205ad1c7a7577284aa2fca5b245d60e0c554accfe9f26021a6d60e0807ce" }, { "path": "application/src/test/java/org/thingsboard/server/service/install/lts/MigrationServiceTest.java", From b3e77c6c9f9886f813dc9fb248eba29d506dec63 Mon Sep 17 00:00:00 2001 From: Oleksandra Matviienko Date: Fri, 16 Oct 2026 11:05:58 +0200 Subject: [PATCH 2/3] Pick the latest ZK registration of a service by creation zxid instead of mtime --- .../queue/discovery/ZkDiscoveryService.java | 5 +++- .../discovery/ZkDiscoveryServiceTest.java | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java index 34e30bc8a2..1233e2878a 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java +++ b/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) diff --git a/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java b/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java index 5dfa660649..37af643ff0 100644 --- a/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java +++ b/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; } From 65417760f43fad95570f7bbaf81f0d824c163f70 Mon Sep 17 00:00:00 2001 From: Oleksandra Matviienko Date: Fri, 16 Oct 2026 11:05:58 +0200 Subject: [PATCH 3/3] Pick the latest ZK registration of a service by creation zxid instead of mtime --- .../queue/discovery/ZkDiscoveryService.java | 5 +++- .../discovery/ZkDiscoveryServiceTest.java | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java index 3a3a161ef2..67ec2f4c02 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java +++ b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java @@ -119,8 +119,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) diff --git a/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java b/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java index 5649a637d5..da0635e65c 100644 --- a/common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java +++ b/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; }