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 0eeb11c0f4..750751ab1a 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 @@ -369,6 +369,49 @@ public class HashPartitionServiceTest { }); } + @Test + public void testIsManagedByCurrentService_isolatedProfileWithoutDedicatedServers() { + ServiceInfo commonRuleEngine = ServiceInfo.newBuilder() + .setServiceId("tb-rule-engine-1") + .addAllServiceTypes(List.of(ServiceType.TB_RULE_ENGINE.name())) + .build(); + TenantProfileId profileWithoutDedicatedServers = new TenantProfileId(UUID.randomUUID()); + TenantProfileId profileWithDedicatedServer = new TenantProfileId(UUID.randomUUID()); + ServiceInfo dedicatedRuleEngine = ServiceInfo.newBuilder() + .setServiceId("tb-rule-engine-isolated-1") + .addAllServiceTypes(List.of(ServiceType.TB_RULE_ENGINE.name())) + .addAssignedTenantProfiles(profileWithDedicatedServer.toString()) + .build(); + + TenantId tenantId = TenantId.fromUUID(UUID.randomUUID()); + TenantId otherTenantId = TenantId.fromUUID(UUID.randomUUID()); + mockRoutingInfo(tenantId, profileWithoutDedicatedServers, true); + mockRoutingInfo(otherTenantId, profileWithDedicatedServer, true); + mockQueues(List.of(createQueue(TenantId.SYS_TENANT_ID, 10), createQueue(tenantId, 3), createQueue(otherTenantId, 3))); + when(serviceInfoProvider.isService(eq(ServiceType.TB_RULE_ENGINE))).thenReturn(true); + + when(serviceInfoProvider.getAssignedTenantProfiles()).thenReturn(Collections.emptySet()); + HashPartitionService commonPartitionService = createPartitionService(); + commonPartitionService.recalculatePartitions(commonRuleEngine, Collections.emptyList()); + assertThat(commonPartitionService.isManagedByCurrentService(tenantId)).isTrue(); + assertThat(commonPartitionService.isManagedByCurrentService(otherTenantId)).isTrue(); + + // a dedicated server for another profile joins + commonPartitionService.recalculatePartitions(commonRuleEngine, List.of(dedicatedRuleEngine)); + assertThat(commonPartitionService.resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, tenantId, tenantId).isMyPartition()).isTrue(); + assertThat(commonPartitionService.isManagedByCurrentService(tenantId)).isTrue(); + assertThat(commonPartitionService.isManagedByCurrentService(otherTenantId)).isFalse(); + // stays the same on the next recalculation + commonPartitionService.recalculatePartitions(commonRuleEngine, List.of(dedicatedRuleEngine)); + assertThat(commonPartitionService.isManagedByCurrentService(tenantId)).isTrue(); + + when(serviceInfoProvider.getAssignedTenantProfiles()).thenReturn(Set.of(profileWithDedicatedServer.getId())); + HashPartitionService dedicatedPartitionService = createPartitionService(); + dedicatedPartitionService.recalculatePartitions(dedicatedRuleEngine, List.of(commonRuleEngine)); + assertThat(dedicatedPartitionService.isManagedByCurrentService(tenantId)).isFalse(); + assertThat(dedicatedPartitionService.isManagedByCurrentService(otherTenantId)).isTrue(); + } + @Test public void testPartitionsDistribution_sameTenantDifferentQueues() { List ruleEngines = 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 57a8023548..67ef7e965d 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 @@ -21,7 +21,6 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantProfileId; import org.thingsboard.server.common.data.integration.IntegrationType; import org.thingsboard.server.common.data.job.JobType; -import org.thingsboard.server.common.data.util.CollectionsUtil; import org.thingsboard.server.common.msg.queue.ServiceType; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; import org.thingsboard.server.gen.transport.TransportProtos; @@ -108,6 +107,7 @@ public class HashPartitionService implements PartitionService { private List currentOtherServices; private final Map> tbTransportServicesByType = new HashMap<>(); private volatile Map> responsibleServices = Collections.emptyMap(); + private volatile Set profilesWithDedicatedServices = Collections.emptySet(); private HashFunction hashFunction; @@ -319,7 +319,7 @@ public class HashPartitionService implements PartitionService { TenantRoutingInfo routingInfo = getRoutingInfo(tenantId); if (isRegular) { if (routingInfo.isIsolated()) { - isManaged = hasDedicatedService(routingInfo.getProfileId()); + isManaged = !profilesWithDedicatedServices.contains(routingInfo.getProfileId()); } else { isManaged = true; } @@ -334,10 +334,6 @@ public class HashPartitionService implements PartitionService { return isManaged; } - private boolean hasDedicatedService(TenantProfileId profileId) { - return CollectionsUtil.isEmpty(responsibleServices.get(profileId)); - } - @Override public TopicPartitionInfo resolve(ServiceType serviceType, String queueName, TenantId tenantId, EntityId entityId) { QueueKey queueKey = getQueueKey(serviceType, queueName, tenantId); @@ -447,6 +443,8 @@ public class HashPartitionService implements PartitionService { } queueServicesMap.values().forEach(list -> list.sort(Comparator.comparing(ServiceInfo::getServiceId))); responsibleServices.values().forEach(list -> list.sort(Comparator.comparing(ServiceInfo::getServiceId))); + // resolveByPartitionIdx adds the fallback servers for profiles without dedicated services to responsibleServices + Set profilesWithDedicatedServices = Set.copyOf(responsibleServices.keySet()); final ConcurrentMap> newPartitions = new ConcurrentHashMap<>(); partitionSizesMap.forEach((queueKey, size) -> { @@ -463,6 +461,7 @@ public class HashPartitionService implements PartitionService { } }); this.responsibleServices = responsibleServices; + this.profilesWithDedicatedServices = profilesWithDedicatedServices; final ConcurrentMap> oldPartitions = myPartitions; myPartitions = newPartitions;