Browse Source

rule-engine: keep isolated tenants without dedicated servers managed by regular servers

HashPartitionService.resolveByPartitionIdx caches the fallback (regular)
servers of an isolated profile without dedicated servers in
responsibleServices. isManagedByCurrentService then saw a non-empty list
for that profile and reported its tenants as not managed on the regular
servers, although those servers own the tenants' partitions. This happens
as soon as any dedicated server exists in the cluster.

Remember which profiles have dedicated servers before the fallback entries
are added, and use that set in isManagedByCurrentService.
pull/16345/head
Sergey Matvienko 2 months ago
parent
commit
4418f99dda
  1. 43
      application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java
  2. 11
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java

43
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<ServiceInfo> ruleEngines = new ArrayList<>();

11
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<ServiceInfo> currentOtherServices;
private final Map<String, List<ServiceInfo>> tbTransportServicesByType = new HashMap<>();
private volatile Map<TenantProfileId, List<ServiceInfo>> responsibleServices = Collections.emptyMap();
private volatile Set<TenantProfileId> 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<TenantProfileId> profilesWithDedicatedServices = Set.copyOf(responsibleServices.keySet());
final ConcurrentMap<QueueKey, List<Integer>> newPartitions = new ConcurrentHashMap<>();
partitionSizesMap.forEach((queueKey, size) -> {
@ -463,6 +461,7 @@ public class HashPartitionService implements PartitionService {
}
});
this.responsibleServices = responsibleServices;
this.profilesWithDedicatedServices = profilesWithDedicatedServices;
final ConcurrentMap<QueueKey, List<Integer>> oldPartitions = myPartitions;
myPartitions = newPartitions;

Loading…
Cancel
Save