Added new count method for LIMITED services

This commit is contained in:
Adolfo Gómez García 2022-03-23 21:28:49 +01:00
parent 40b9572233
commit 86990638dc
2 changed files with 29 additions and 21 deletions

View File

@ -61,6 +61,7 @@ from uds.models.meta_pool import MetaPoolMember
from uds.core import services, transports from uds.core import services, transports
from uds.core.util import singleton from uds.core.util import singleton
from uds.core.util.stats import events from uds.core.util.stats import events
from uds.web.util.errors import MAX_SERVICES_REACHED
from .userservice import comms from .userservice import comms
from .userservice.opchecker import UserServiceOpChecker from .userservice.opchecker import UserServiceOpChecker
@ -75,15 +76,20 @@ class UserServiceManager(metaclass=singleton.Singleton):
@staticmethod @staticmethod
def manager() -> 'UserServiceManager': def manager() -> 'UserServiceManager':
return UserServiceManager() # Singleton pattern will return always the same instance return (
UserServiceManager()
) # Singleton pattern will return always the same instance
@staticmethod @staticmethod
def getCacheStateFilter(level: int) -> Q: def getCacheStateFilter(servicePool: ServicePool, level: int) -> Q:
return Q(cache_level=level) & UserServiceManager.getStateFilter() return Q(cache_level=level) & UserServiceManager.getStateFilter(servicePool)
@staticmethod @staticmethod
def getStateFilter() -> Q: def getStateFilter(servicePool: ServicePool) -> Q:
if GlobalConfig.MAX_SERVICES_COUNT_NEW.getBool() == False: if (
servicePool.service.getInstance().maxDeployed == services.Service.UNLIMITED
and GlobalConfig.MAX_SERVICES_COUNT_NEW.getBool() is False
):
states = [State.PREPARING, State.USABLE] states = [State.PREPARING, State.USABLE]
else: else:
states = [State.PREPARING, State.USABLE, State.REMOVING, State.REMOVABLE] states = [State.PREPARING, State.USABLE, State.REMOVING, State.REMOVABLE]
@ -522,7 +528,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if serviceType.usesCache: if serviceType.usesCache:
inAssigned = ( inAssigned = (
servicePool.assignedUserServices() servicePool.assignedUserServices()
.filter(UserServiceManager.getStateFilter()) .filter(UserServiceManager.getStateFilter(servicePool))
.count() .count()
) )
if ( if (
@ -921,19 +927,20 @@ class UserServiceManager(metaclass=singleton.Singleton):
meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool) meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool)
# Get pool members. Just pools "visible" and "usable" # Get pool members. Just pools "visible" and "usable"
pools = [ pools = [
p.pool for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable() p.pool
for p in meta.members.all()
if p.pool.isVisible() and p.pool.isUsable()
] ]
# look for an existing user service in the pool # look for an existing user service in the pool
try: try:
return UserService.objects.filter( return UserService.objects.filter(
deployed_service__in=pools, deployed_service__in=pools,
state__in=State.VALID_STATES, state__in=State.VALID_STATES,
user=user, user=user,
cache_level=0, cache_level=0,
).order_by('deployed_service__name')[0] ).order_by('deployed_service__name')[0]
except IndexError: except IndexError:
return None return None
def getMeta( def getMeta(
self, self,
@ -978,14 +985,10 @@ class UserServiceManager(metaclass=singleton.Singleton):
# Remove "full" pools (100%) from result and pools in maintenance mode, not ready pools, etc... # Remove "full" pools (100%) from result and pools in maintenance mode, not ready pools, etc...
sortedPools = sorted(sortPools, key=lambda x: x[0]) sortedPools = sorted(sortPools, key=lambda x: x[0])
pools: typing.List[ServicePool] = [ pools: typing.List[ServicePool] = [
p[1] p[1] for p in sortedPools if p[1].usage() < 100 and p[1].isUsable()
for p in sortedPools
if p[1].usage() < 100 and p[1].isUsable()
] ]
poolsFull: typing.List[ServicePool] = [ poolsFull: typing.List[ServicePool] = [
p[1] p[1] for p in sortedPools if p[1].usage() == 100 and p[1].isUsable()
for p in sortedPools
if p[1].usage() == 100 and p[1].isUsable()
] ]
logger.debug('Pools: %s/%s', pools, poolsFull) logger.debug('Pools: %s/%s', pools, poolsFull)
@ -1020,7 +1023,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
try: try:
# Already assigned should look for in all usable pools, not only "non-full" ones # Already assigned should look for in all usable pools, not only "non-full" ones
alreadyAssigned: UserService = UserService.objects.filter( alreadyAssigned: UserService = UserService.objects.filter(
deployed_service__in=pools+poolsFull, deployed_service__in=pools + poolsFull,
state__in=State.VALID_STATES, state__in=State.VALID_STATES,
user=user, user=user,
cache_level=0, cache_level=0,

View File

@ -127,6 +127,7 @@ class ServiceCacheUpdater(Job):
servicePool.cachedUserServices() servicePool.cachedUserServices()
.filter( .filter(
userServiceManager().getCacheStateFilter( userServiceManager().getCacheStateFilter(
servicePool,
services.UserDeployment.L1_CACHE services.UserDeployment.L1_CACHE
) )
) )
@ -137,6 +138,7 @@ class ServiceCacheUpdater(Job):
servicePool.cachedUserServices() servicePool.cachedUserServices()
.filter( .filter(
userServiceManager().getCacheStateFilter( userServiceManager().getCacheStateFilter(
servicePool,
services.UserDeployment.L2_CACHE services.UserDeployment.L2_CACHE
) )
) )
@ -144,7 +146,7 @@ class ServiceCacheUpdater(Job):
) )
inAssigned: int = ( inAssigned: int = (
servicePool.assignedUserServices() servicePool.assignedUserServices()
.filter(userServiceManager().getStateFilter()) .filter(userServiceManager().getStateFilter(servicePool))
.count() .count()
) )
# if we bypasses max cache, we will reduce it in first place. This is so because this will free resources on service provider # if we bypasses max cache, we will reduce it in first place. This is so because this will free resources on service provider
@ -235,6 +237,7 @@ class ServiceCacheUpdater(Job):
.select_for_update() .select_for_update()
.filter( .filter(
userServiceManager().getCacheStateFilter( userServiceManager().getCacheStateFilter(
servicePool,
services.UserDeployment.L2_CACHE services.UserDeployment.L2_CACHE
) )
) )
@ -308,6 +311,7 @@ class ServiceCacheUpdater(Job):
servicePool.cachedUserServices() servicePool.cachedUserServices()
.filter( .filter(
userServiceManager().getCacheStateFilter( userServiceManager().getCacheStateFilter(
servicePool,
services.UserDeployment.L1_CACHE services.UserDeployment.L1_CACHE
) )
) )
@ -351,6 +355,7 @@ class ServiceCacheUpdater(Job):
servicePool.cachedUserServices() servicePool.cachedUserServices()
.filter( .filter(
userServiceManager().getCacheStateFilter( userServiceManager().getCacheStateFilter(
servicePool,
services.UserDeployment.L2_CACHE services.UserDeployment.L2_CACHE
) )
) )