Browse Source

Merge pull request #16345 from smatvienko-tb/fix/isolated-profile-fallback-managed-status

Fixed Rule Engine processing stall for isolated tenant profiles without dedicated servers
pull/16361/head
Viacheslav Klimov 2 months ago
committed by GitHub
parent
commit
57d877c5a2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 43
      application/src/test/java/org/thingsboard/server/queue/discovery/HashPartitionServiceTest.java
  2. 13
      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 testPartitionChangeEvent_affectedTenantsIncludeIsolatedTenantWhenItsDedicatedServerChanges() {
ServiceInfo commonRuleEngine = ServiceInfo.newBuilder()

13
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;
@ -107,7 +106,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 +318,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 +333,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 +442,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) -> {
@ -462,7 +459,7 @@ public class HashPartitionService implements PartitionService {
}
}
});
this.responsibleServices = responsibleServices;
this.profilesWithDedicatedServices = profilesWithDedicatedServices;
final ConcurrentMap<QueueKey, List<Integer>> oldPartitions = myPartitions;
myPartitions = newPartitions;

Loading…
Cancel
Save