Browse Source

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.
pull/15729/head
Sergey Matvienko 5 months ago
parent
commit
2e00a3e00b
  1. 9
      application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java
  2. 17
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java
  3. 6
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java
  4. 16
      common/util/src/main/java/org/thingsboard/common/util/SetCache.java

9
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<ServiceInfo> 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<DeviceId> devices = new ArrayList<>();

17
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<ServiceInfo> 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<ServiceInfo> getAllServices(ServiceType serviceType) {
Set<ServiceInfo> result = getOtherServices(serviceType);

6
common/queue/src/main/java/org/thingsboard/server/queue/discovery/PartitionService.java

@ -48,6 +48,12 @@ public interface PartitionService {
*/
Set<String> 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<TransportProtos.ServiceInfo> getAllServices(ServiceType serviceType);
Set<TransportProtos.ServiceInfo> getOtherServices(ServiceType serviceType);

16
common/util/src/main/java/org/thingsboard/common/util/SetCache.java

@ -14,9 +14,19 @@ public class SetCache<K> {
private final Cache<K, Object> cache;
public SetCache(long valueTtlMs) {
this.cache = Caffeine.newBuilder()
.expireAfterWrite(valueTtlMs, TimeUnit.MILLISECONDS)
.build();
this(Caffeine.newBuilder().expireAfterWrite(valueTtlMs, TimeUnit.MILLISECONDS));
}
private SetCache(Caffeine<Object, Object> 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 <T> SetCache<T> boundedBySize(int maximumSize) {
return new SetCache<>(Caffeine.newBuilder().maximumSize(maximumSize));
}
public void add(K key) {

Loading…
Cancel
Save