def createAssignedFor(self, servicePool: ServicePool, user: User) -> UserService: """ Creates a new assigned deployed service for the current publication (if any) of service pool and user indicated """ # First, honor maxPreparingServices if self.canInitiateServiceFromDeployedService(servicePool) is False: # Cannot create new logger.info( 'Too many preparing services. Creation of assigned service denied by max preparing services parameter. (login storm with insufficient cache?).' ) raise ServiceNotReadyError() if servicePool.service.getType().publicationType is not None: publication = servicePool.activePublication() logger.debug( 'Creating a new assigned element for user %s por publication %s', user, publication) if publication: assigned = self.__createAssignedAtDb(publication, user) else: raise Exception( 'Invalid publication creating service assignation: {} {}'. format(servicePool, user)) else: logger.debug('Creating a new assigned element for user %s', user) assigned = self.__createAssignedAtDbForNoPublication( servicePool, user) assignedInstance = assigned.getInstance() state = assignedInstance.deployForUser(user) UserServiceOpChecker.makeUnique(assigned, assignedInstance, state) return assigned
def createFromAssignable(self, servicePool: ServicePool, user: User, assignableId: str) -> UserService: """ Creates an assigned service from an "assignable" id """ serviceInstance = servicePool.service.getInstance() if not serviceInstance.canAssign(): raise Exception('This service type cannot assign asignables') if servicePool.service.getType().publicationType is not None: publication = servicePool.activePublication() logger.debug('Creating an assigned element from assignable %s for user %s por publication %s', user, assignableId, publication) if publication: assigned = self.__createAssignedAtDb(publication, user) else: raise Exception('Invalid publication creating service assignation: {} {}'.format(servicePool, user)) else: logger.debug('Creating an assigned element from assignable %s for user %s', assignableId, user) assigned = self.__createAssignedAtDbForNoPublication(servicePool, user) # Now, get from serviceInstance the data assignedInstance = assigned.getInstance() state = serviceInstance.assignFromAssignables(assignableId, user, assignedInstance) # assigned.updateData(assignedInstance) UserServiceOpChecker.makeUnique(assigned, assignedInstance, state) return assigned
def growL1Cache(self, servicePool: ServicePool, cacheL1: int, cacheL2: int, assigned: int) -> None: """ This method tries to enlarge L1 cache. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache """ logger.debug('Growing L1 cache creating a new service for %s', servicePool.name) # First, we try to assign from L2 cache if cacheL2 > 0: valid = None with transaction.atomic(): for n in servicePool.cachedUserServices().select_for_update().filter(userServiceManager().getCacheStateFilter(services.UserDeployment.L2_CACHE)).order_by('creation_date'): if n.needsOsManager(): if State.isUsable(n.state) is False or State.isUsable(n.os_state): valid = n break else: valid = n break if valid is not None: valid.moveToLevel(services.UserDeployment.L1_CACHE) return try: # This has a velid publication, or it will not be here userServiceManager().createCacheFor( typing.cast(ServicePoolPublication, servicePool.activePublication()), services.UserDeployment.L1_CACHE ) except MaxServicesReachedError: log.doLog(servicePool, log.ERROR, 'Max number of services reached for this service', log.INTERNAL) logger.warning('Max user services reached for %s: %s. Cache not created', servicePool.name, servicePool.max_srvs) except Exception: logger.exception('Exception')
def continueRemovalOf(self, servicePool: ServicePool): # Recheck that there is no publication created in "bad moment" try: for pub in servicePool.publications.filter(state=State.PREPARING): pub.cancel() except Exception: pass try: # Now all publications are canceling, let's try to cancel cache and assigned also uServices: typing.Iterable[ UserService] = servicePool.userServices.filter( state=State.PREPARING) for userService in uServices: logger.debug('Canceling %s', userService) userService.cancel() except Exception: pass # First, we remove all publications and user services in "info_state" with transaction.atomic(): servicePool.userServices.select_for_update().filter( state__in=State.INFO_STATES).delete() # Mark usable user services as removable now = getSqlDatetime() with transaction.atomic(): servicePool.userServices.select_for_update().filter( state=State.USABLE).update(state=State.REMOVABLE, state_date=now) # When no service is at database, we start with publications if servicePool.userServices.all().count() == 0: try: logger.debug( 'All services removed, checking active publication') if servicePool.activePublication() is not None: logger.debug('Active publication found, unpublishing it') servicePool.unpublish() else: logger.debug( 'No active publication found, removing info states and checking if removal is done' ) servicePool.publications.filter( state__in=State.INFO_STATES).delete() if servicePool.publications.count() == 0: servicePool.removed( ) # Mark it as removed, clean later from database except Exception: logger.exception( 'Cought unexpected exception at continueRemovalOf: ')
def growL2Cache(self, servicePool: ServicePool, cacheL1: int, cacheL2: int, assigned: int) -> None: """ Tries to grow L2 cache of service. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache """ logger.debug("Growing L2 cache creating a new service for %s", servicePool.name) try: # This has a velid publication, or it will not be here userServiceManager().createCacheFor( typing.cast(ServicePoolPublication, servicePool.activePublication()), services.UserDeployment.L2_CACHE ) except MaxServicesReachedError: logger.warning('Max user services reached for %s: %s. Cache not created', servicePool.name, servicePool.max_srvs)