Skip to content

Commit

Permalink
Fixed service pools info
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmstr committed Nov 6, 2023
1 parent 8997d2f commit 3451719
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
3 changes: 3 additions & 0 deletions server/src/uds/core/consts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
TRUE_STR: typing.Final[str] = 'true'
FALSE_STR: typing.Final[str] = 'false'

# Constant to mark an "UNLIMITED" value
UNLIMITED: typing.Final[int] = -1

# Default length for Gui Text Fields
DEFAULT_TEXT_LENGTH: typing.Final[int] = 64

Expand Down
3 changes: 2 additions & 1 deletion server/src/uds/core/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from . import actor
from . import auth
from . import validation
from . import service
from . import validation

# Common exceptions inserted here
from .common import UDSException, BlockAccess
Expand Down
6 changes: 3 additions & 3 deletions server/src/uds/core/managers/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def maximumUserServicesDeployed(self, service: 'models.Service') -> bool:
"""
serviceInstance = service.getInstance()
# Early return, so no database count is needed
if serviceInstance.maxUserServices == services.Service.UNLIMITED:
if serviceInstance.maxUserServices == consts.UNLIMITED:
return False

if self.getExistingUserServices(service) >= (serviceInstance.maxUserServices or 1):
Expand Down Expand Up @@ -604,7 +604,7 @@ def reset(self, userService: UserService) -> None:
def notifyPreconnect(self, userService: UserService, info: types.connections.ConnectionData) -> None:
try:
comms.notifyPreconnect(userService, info)
except exceptions.NoActorComms: # If no comms url for userService, try with service
except exceptions.actor.NoActorComms: # If no comms url for userService, try with service
userService.deployed_service.service.notifyPreconnect(userService, info)

def checkUuid(self, userService: UserService) -> bool:
Expand Down Expand Up @@ -918,7 +918,7 @@ def getMeta(
if meta.policy == types.pools.LoadBalancingPolicy.PRIORITY:
sortPools = [(p.priority, p.pool) for p in poolMembers]
elif meta.policy == types.pools.LoadBalancingPolicy.GREATER_PERCENT_FREE:
sortPools = [(p.pool.usage(), p.pool) for p in poolMembers]
sortPools = [(p.pool.usage()[0], p.pool) for p in poolMembers]
else:
sortPools = [
(
Expand Down
11 changes: 4 additions & 7 deletions server/src/uds/core/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from uds.core.util.state import State
from uds.core.util import log

from uds.core import types
from uds.core import types, consts


if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -93,9 +93,6 @@ class Service(Module):
"""

# : Constant for indicating that max elements this service can deploy is unlimited.
UNLIMITED: int = -1

# : Name of type, used at administration interface to identify this
# : service (i.e. Xen server, oVirt Server, ...)
# : This string will be translated when provided to admin interface
Expand Down Expand Up @@ -125,7 +122,7 @@ class Service(Module):
# : Normally set to UNLIMITED. This attribute indicates if the service has some "limitation"
# : for providing deployed services to users. This attribute can be set here or
# : modified at instance level, core will access always to it using an instance object.
maxUserServices: int = UNLIMITED # : If the service provides more than 1 "provided service" (-1 = no limit, 0 = ???? (do not use it!!!), N = max number to deploy
maxUserServices: int = consts.UNLIMITED # : If the service provides more than 1 "provided service" (-1 = no limit, 0 = ???? (do not use it!!!), N = max number to deploy

# : If this item "has constains", on deployed service edition, defined keys will overwrite defined ones
# : That is, this Dicionary will OVERWRITE fields ON ServicePool (normally cache related ones) dictionary from a REST api save invocation!!
Expand Down Expand Up @@ -271,10 +268,10 @@ def unmarshal(self, data: bytes) -> None:
try:
self.maxUserServices = getattr(self, 'maxServices').num()
except Exception:
self.maxUserServices = Service.UNLIMITED
self.maxUserServices = consts.UNLIMITED

if self.maxUserServices < 1:
self.maxUserServices = Service.UNLIMITED
self.maxUserServices = consts.UNLIMITED

# Keep untouched if maxServices is not present

Expand Down
12 changes: 7 additions & 5 deletions server/src/uds/models/meta_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from django.db.models import QuerySet, signals
from django.utils.translation import gettext_noop as _

from uds.core import types
from uds.core import consts, types
from uds.core.util import log, states
from uds.core.util.calendar import CalendarChecker

Expand Down Expand Up @@ -178,6 +178,7 @@ def usage(self, cachedValue=-1) -> typing.Tuple[int, int, int]:
)
.prefetch_related(
'service',
'service__provider',
)
)

Expand All @@ -186,13 +187,14 @@ def usage(self, cachedValue=-1) -> typing.Tuple[int, int, int]:
for pool in query:
p, u, m = pool.usage(pool.usage_count) # type:ignore # Anotated field
usage_count += u
if max_count <= 0 or m <= 0:
max_count = -1
# If any of the pools has no max, then max is -1
if max_count == consts.UNLIMITED or m == consts.UNLIMITED:
max_count = consts.UNLIMITED
else:
max_count += m

if max_count <= 0:
return (0, usage_count, max_count)
if max_count == 0 or max_count == consts.UNLIMITED:
return (0, usage_count, consts.UNLIMITED)

return (usage_count * 100 // max_count, usage_count, max_count)

Expand Down
5 changes: 3 additions & 2 deletions server/src/uds/models/service_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

from ..core.consts import NEVER
from ..core.util.model import getSqlDatetime
from uds.core import consts


# Not imported at runtime, just for type checking
Expand Down Expand Up @@ -643,8 +644,8 @@ def usage(self, cachedValue=-1) -> typing.Tuple[int, int, int]:
if cachedValue == -1:
cachedValue = self.assignedUserServices().filter(state__in=states.userService.VALID_STATES).count()

if maxs <= 0:
return 0, cachedValue, maxs
if maxs == 0 or max == consts.UNLIMITED:
return 0, cachedValue, consts.UNLIMITED

return 100 * cachedValue // maxs, cachedValue, maxs

Expand Down

0 comments on commit 3451719

Please sign in to comment.