@ -46,8 +46,8 @@ import org.thingsboard.server.dao.tenant.TenantService;
import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper ;
import java.util.HashSet ;
import java.util.Optional ;
import java.util.Set ;
import java.util.UUID ;
@Slf4j
public class AppActor extends ContextAwareActor {
@ -124,14 +124,13 @@ public class AppActor extends ContextAwareActor {
try {
if ( systemContext . isTenantComponentsInitEnabled ( ) ) {
PageDataIterable < Tenant > tenantIterator = new PageDataIterable < > ( tenantService : : findTenants , ENTITY_PACK_LIMIT ) ;
Set < UUID > assignedProfiles = systemContext . getServiceInfoProvider ( ) . getAssignedTenantProfiles ( ) ;
for ( Tenant tenant : tenantIterator ) {
if ( ! assignedProfiles . isEmpty ( ) & & ! assignedProfiles . contains ( tenant . getTenantProfileId ( ) . getId ( ) ) ) {
continue ;
}
log . debug ( "[{}] Creating tenant actor" , tenant . getId ( ) ) ;
getOrCreateTenantActor ( tenant . getId ( ) ) ;
log . debug ( "[{}] Tenant actor created." , tenant . getId ( ) ) ;
getOrCreateTenantActor ( tenant . getId ( ) ) . ifPresentOrElse ( tenantActor - > {
log . debug ( "[{}] Tenant actor created." , tenant . getId ( ) ) ;
} , ( ) - > {
log . debug ( "[{}] Skipped actor creation" , tenant . getId ( ) ) ;
} ) ;
}
}
log . info ( "Main system actor started." ) ;
@ -145,7 +144,9 @@ public class AppActor extends ContextAwareActor {
msg . getMsg ( ) . getCallback ( ) . onFailure ( new RuleEngineException ( "Message has system tenant id!" ) ) ;
} else {
if ( ! deletedTenants . contains ( msg . getTenantId ( ) ) ) {
getOrCreateTenantActor ( msg . getTenantId ( ) ) . tell ( msg ) ;
getOrCreateTenantActor ( msg . getTenantId ( ) ) . ifPresentOrElse ( actor - > {
actor . tell ( msg ) ;
} , ( ) - > msg . getMsg ( ) . getCallback ( ) . onSuccess ( ) ) ;
} else {
msg . getMsg ( ) . getCallback ( ) . onSuccess ( ) ;
}
@ -165,12 +166,13 @@ public class AppActor extends ContextAwareActor {
log . info ( "[{}] Handling tenant deleted notification: {}" , msg . getTenantId ( ) , msg ) ;
deletedTenants . add ( tenantId ) ;
ctx . stop ( new TbEntityActorId ( tenantId ) ) ;
} else {
target = getOrCreateTenantActor ( msg . getTenantId ( ) ) ;
return ;
}
} else {
target = getOrCreateTenantActor ( msg . getTenantId ( ) ) ;
}
target = getOrCreateTenantActor ( msg . getTenantId ( ) ) . orElseGet ( ( ) - > {
log . debug ( "Ignoring component lifecycle msg for tenant {} because it is not managed by this service" , msg . getTenantId ( ) ) ;
return null ;
} ) ;
}
if ( target ! = null ) {
target . tellWithHighPriority ( msg ) ;
@ -181,12 +183,13 @@ public class AppActor extends ContextAwareActor {
private void onToDeviceActorMsg ( TenantAwareMsg msg , boolean priority ) {
if ( ! deletedTenants . contains ( msg . getTenantId ( ) ) ) {
TbActorRef tenantActor = getOrCreateTenantActor ( msg . getTenantId ( ) ) ;
if ( priority ) {
tenantActor . tellWithHighPriority ( msg ) ;
} else {
tenantActor . tell ( msg ) ;
}
getOrCreateTenantActor ( msg . getTenantId ( ) ) . ifPresent ( tenantActor - > {
if ( priority ) {
tenantActor . tellWithHighPriority ( msg ) ;
} else {
tenantActor . tell ( msg ) ;
}
} ) ;
} else {
if ( msg instanceof TransportToDeviceActorMsgWrapper ) {
( ( TransportToDeviceActorMsgWrapper ) msg ) . getCallback ( ) . onSuccess ( ) ;
@ -194,10 +197,15 @@ public class AppActor extends ContextAwareActor {
}
}
private TbActorRef getOrCreateTenantActor ( TenantId tenantId ) {
return ctx . getOrCreateChildActor ( new TbEntityActorId ( tenantId ) ,
( ) - > DefaultActorService . TENANT_DISPATCHER_NAME ,
( ) - > new TenantActor . ActorCreator ( systemContext , tenantId ) ) ;
private Optional < TbActorRef > getOrCreateTenantActor ( TenantId tenantId ) {
if ( systemContext . getServiceInfoProvider ( ) . isService ( ServiceType . TB_CORE ) | |
systemContext . getPartitionService ( ) . isManagedByCurrentService ( tenantId ) ) {
return Optional . of ( ctx . getOrCreateChildActor ( new TbEntityActorId ( tenantId ) ,
( ) - > DefaultActorService . TENANT_DISPATCHER_NAME ,
( ) - > new TenantActor . ActorCreator ( systemContext , tenantId ) ) ) ;
} else {
return Optional . empty ( ) ;
}
}
private void onToEdgeSessionMsg ( EdgeSessionMsg msg ) {
@ -205,7 +213,7 @@ public class AppActor extends ContextAwareActor {
if ( ModelConstants . SYSTEM_TENANT . equals ( msg . getTenantId ( ) ) ) {
log . warn ( "Message has system tenant id: {}" , msg ) ;
} else {
target = getOrCreateTenantActor ( msg . getTenantId ( ) ) ;
target = getOrCreateTenantActor ( msg . getTenantId ( ) ) . orElse ( null ) ;
}
if ( target ! = null ) {
target . tellWithHighPriority ( msg ) ;