From 2e00a3e00b281a304eeb668b923a671ccfbeaa8e Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Wed, 8 Jul 2026 16:03:20 +0200 Subject: [PATCH] Add cheap isKnownServiceId membership check and size-bounded SetCache getAllServiceIds() rebuilds two HashSets just to test membership of a single id - PartitionService#isKnownServiceId scans the live service lists directly instead, for the hot path in DefaultTbClusterService's per-notification unknown-service guard. SetCache.boundedBySize() adds an LRU-by-size variant alongside the existing TTL-based constructor. --- .../discovery/HashPartitionServiceTest.java | 9 +++++++++ .../queue/discovery/HashPartitionService.java | 17 +++++++++++++++++ .../queue/discovery/PartitionService.java | 6 ++++++ .../org/thingsboard/common/util/SetCache.java | 16 +++++++++++++--- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java b/application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java index 888a0b31bb..8c73569128 100644 --- a/application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java @@ -83,6 +83,7 @@ public class HashPartitionServiceTest { .setServiceId("tb-core-0") .addAllServiceTypes(Collections.singletonList(ServiceType.TB_CORE.name())) .build(); + when(serviceInfoProvider.getServiceInfo()).thenReturn(currentServer); List otherServers = new ArrayList<>(); for (int i = 1; i < SERVER_COUNT; i++) { otherServers.add(ServiceInfo.newBuilder() @@ -94,6 +95,14 @@ public class HashPartitionServiceTest { partitionService.recalculatePartitions(currentServer, otherServers); } + @Test + public void testIsKnownServiceId() { + assertThat(partitionService.isKnownServiceId(ServiceType.TB_CORE, "tb-core-0")).isTrue(); // current server + assertThat(partitionService.isKnownServiceId(ServiceType.TB_CORE, "tb-rule-1")).isTrue(); // other server + assertThat(partitionService.isKnownServiceId(ServiceType.TB_CORE, "unknown-id")).isFalse(); + assertThat(partitionService.isKnownServiceId(ServiceType.TB_RULE_ENGINE, "tb-core-0")).isFalse(); // wrong type + } + @Test public void testDispersionOnMillionDevices() { List devices = new ArrayList<>(); diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java index c2974fd78b..ff1746d54c 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java +++ b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java @@ -537,6 +537,23 @@ public class HashPartitionService implements PartitionService { return getAllServices(serviceType).stream().map(ServiceInfo::getServiceId).collect(Collectors.toSet()); } + @Override + public boolean isKnownServiceId(ServiceType serviceType, String serviceId) { + ServiceInfo current = serviceInfoProvider.getServiceInfo(); + if (serviceId.equals(current.getServiceId()) && current.getServiceTypesList().contains(serviceType.name())) { + return true; + } + List others = currentOtherServices; + if (others != null) { + for (ServiceInfo serviceInfo : others) { + if (serviceId.equals(serviceInfo.getServiceId()) && serviceInfo.getServiceTypesList().contains(serviceType.name())) { + return true; + } + } + } + return false; + } + @Override public Set getAllServices(ServiceType serviceType) { Set result = getOtherServices(serviceType); diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java index 73e585568b..73ccf32373 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java +++ b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java @@ -48,6 +48,12 @@ public interface PartitionService { */ Set getAllServiceIds(ServiceType serviceType); + /** + * Cheaper alternative to {@link #getAllServiceIds} when only membership of a single id needs to be checked - + * avoids building the full service id set. + */ + boolean isKnownServiceId(ServiceType serviceType, String serviceId); + Set getAllServices(ServiceType serviceType); Set getOtherServices(ServiceType serviceType); diff --git a/common/util/src/main/java/org/thingsboard/common/util/SetCache.java b/common/util/src/main/java/org/thingsboard/common/util/SetCache.java index b191709740..5322ebc4ef 100644 --- a/common/util/src/main/java/org/thingsboard/common/util/SetCache.java +++ b/common/util/src/main/java/org/thingsboard/common/util/SetCache.java @@ -14,9 +14,19 @@ public class SetCache { private final Cache cache; public SetCache(long valueTtlMs) { - this.cache = Caffeine.newBuilder() - .expireAfterWrite(valueTtlMs, TimeUnit.MILLISECONDS) - .build(); + this(Caffeine.newBuilder().expireAfterWrite(valueTtlMs, TimeUnit.MILLISECONDS)); + } + + private SetCache(Caffeine builder) { + this.cache = builder.build(); + } + + /** + * Bounded by entry count with LRU-ish eviction, instead of a TTL - for callers that want to remember + * up to N recently seen keys rather than expire entries after a fixed duration. + */ + public static SetCache boundedBySize(int maximumSize) { + return new SetCache<>(Caffeine.newBuilder().maximumSize(maximumSize)); } public void add(K key) {