def test_demand(self): cal = self._get_calendar() rset = ReservationSet() for i in range(5): r = self._make_reservation(id=str(i)) # add to the list rset.add(reservation=r) cal.add_demand(reservation=r) # get the list and check it temp = cal.get_demand() self.check_set(rset=rset, check=temp) # remove from the returned set temp.remove(reservation=r) # make sure this did not affect the parent data structure temp = cal.get_demand() self.check_set(rset=rset, check=temp) for i in range(5): r = self._make_reservation(id=str(i)) # add to the list rset.remove(reservation=r) cal.remove_demand(reservation=r) # get the list and check it temp = cal.get_demand() self.check_set(rset=rset, check=temp)
def test_pending(self): cal = self._get_calendar() rset = ReservationSet() for i in range(5): r = self._make_reservation(id=str(i)) rset.add(reservation=r) cal.add_pending(reservation=r) temp = cal.get_pending() self.check_set(rset=rset, check=temp) temp.remove(reservation=r) temp = cal.get_pending() self.check_set(rset=rset, check=temp) for i in range(5): r = self._make_reservation(id=str(i)) rset.remove(reservation=r) cal.remove_pending(reservation=r) temp = cal.get_pending() self.check_set(rset=rset, check=temp)
class ReservationHoldings: """ This class maintains a collection of reservations. Each reservation is associated with a validity interval. The class allows to answer intersection queries: what reservations are valid at a given time instance. As time goes by, the class can be purged from irrelevant reservation records by invoking tick(). Purging is strongly recommended as it reduces the cost of intersection queries. An attempt has been made to optimize the cost of using this data structure. Inserts are O(log(n)). Queries, however, may take between O(log(n)) and O(n). """ def __init__(self): # List of reservation wrappers sorted by increasing end time. self.list = [] # All reservations stored in this collection. self.reservation_set = ReservationSet() # Map of reservations to ReservationWrappers. Needed when removing a reservation. self.map = {} def add_reservation(self, *, reservation: ABCReservationMixin, start: int, end: int): """ Adds a reservation to the collection for the specified period of time. The interval is closed on both sides. @params reservation : reservation to add @params start : start time @params end : end time """ # If this is an extended reservation, we may already have it in the # list (with potentially different start and end times). Remove the # previous entry if this is the case. my_start = start entry = None if reservation.get_reservation_id() in self.map: entry = self.map[reservation.get_reservation_id()] if entry is not None: assert (start - entry.end) <= 1 my_start = entry.start self.remove_reservation(reservation=reservation) entry = ReservationWrapper(reservation=reservation, start=my_start, end=end) self.add_to_list(entry=entry) self.reservation_set.add(reservation=reservation) self.map[reservation.get_reservation_id()] = entry def add_to_list(self, *, entry: ReservationWrapper): """ Adds the entry to the linked list. Maintains the list in sorted order. Cost: O(log(n)). @params entry : entry to add """ bisect.insort_left(self.list, entry) def clear(self): """ Clears the collection. """ self.map.clear() self.list.clear() self.reservation_set.clear() def get_reservations(self, *, time: int = None, rtype: ResourceType = None) -> ReservationSet: """ Performs an intersection query: returns all reservations from the specified resource type present in the collection that are active at the specified time instance. @params time : time instance @params rtype : resource type @returns reservations set containing active reservations """ if time is None and rtype is None: return self.reservation_set result = ReservationSet() key = ReservationWrapper(reservation=None, start=time, end=time) # Find the location of key in the list. index = binary_search(a=self.list, x=key) if index < 0: index = -index - 1 # Scan the upper part of the list. We need to scan the whole list. i = index count = self.size() while i < count: entry = self.list[i] if rtype is None or rtype == entry.reservation.getType(): if entry.start <= time <= entry.end: result.add(reservation=entry.reservation) i += 1 # Scan the lower part of the list until no further intersections are possible i = index - 1 while i >= 0: entry = self.list[i] if entry.end < time: break if entry.start <= time and rtype is None or entry.reservation.getType( ) == rtype: result.add(reservation=entry.reservation) i -= 1 return result def remove_from_list(self, *, entry: ReservationWrapper): """ Removes a reservation from the collection. @params reservation : reservation to remove """ index = binary_search(a=self.list, x=entry) if index >= 0: self.list.pop(index) def remove_reservation(self, *, reservation: ABCReservationMixin): """ Removes a reservation from the collection. @params reservation : reservation to remove """ if reservation.get_reservation_id() in self.map: entry = self.map[reservation.get_reservation_id()] self.map.pop(reservation.get_reservation_id()) self.reservation_set.remove(reservation=reservation) self.remove_from_list(entry=entry) def size(self) -> int: """ Returns the size of the collection. @returns size of the collection """ return self.reservation_set.size() def tick(self, *, time: int): """ Removes all reservations that have end time not after the given cycle. @params time : time """ while True: if len(self.list) > 0: entry = self.list[0] if entry.end <= time: self.list.remove(entry) self.reservation_set.remove(reservation=entry.reservation) self.map.pop(entry.reservation.get_reservation_id()) else: break else: break
class ControllerTicketReviewPolicy(ControllerSimplePolicy): """ This implementation of a Controller policy is almost identical to the parent ControllerSimplePolicy. The only real difference is that it addresses that Tickets should not be redeemed if any reservations are currently Failed or Nascent. This effectively acts as a "gate" between the Controller and AM. All reservations must be Ticketed, before any reservations are allowed to be redeemed. """ def __init__(self): super().__init__() self.pending_redeem = ReservationSet() def __getstate__(self): state = self.__dict__.copy() del state['logger'] del state['actor'] del state['clock'] del state['initialized'] del state['pending_notify'] del state['lazy_close'] del state['pending_redeem'] return state def __setstate__(self, state): self.__dict__.update(state) self.logger = None self.actor = None self.clock = None self.initialized = False self.pending_notify = ReservationSet() self.lazy_close = False self.pending_redeem = ReservationSet() def check_pending(self): """ Check to make sure all reservations are Ticketed (not Failed or Nascent) before calling the parent method. @throws Exception in case of error """ # add all of our pendingRedeem, so they can be checked for reservation in self.pending_redeem.values(): self.calendar.add_pending(reservation=reservation) # get set of reservations that need to be redeemed my_pending = self.calendar.get_pending() # keep track of status of the slice containing each reservation slice_status_map = {} # nothing to do! if my_pending is None: return # check the status of the Slice of each reservation for reservation in my_pending.values(): slice_obj = reservation.get_slice() slice_id = slice_obj.get_slice_id() # only want to do this for 'new' tickets if reservation.is_failed() or reservation.is_ticketed(): # check if we've examined this slice already if slice_id not in slice_status_map: # set the default status slice_status_map[ slice_id] = TicketReviewSliceState.Redeemable # examine every reservation contained within the slice, # looking for either a Failed or Nascent reservation # we have to look at everything in a slice once, to determine all/any Sites with failures for slice_reservation in slice_obj.get_reservations( ).values(): # If any Reservations that are being redeemed, that means the # slice has already cleared TicketReview. if slice_reservation.is_redeeming(): if slice_status_map[ slice_id] == TicketReviewSliceState.Nascent: # There shouldn't be any Nascent reservations, if a reservation is being Redeemed. self.logger.error( "Nascent reservation found while Reservation {} in slice {} is redeeming" .format( slice_reservation.get_reservation_id(), slice_obj.get_name())) # We may have previously found a Failed Reservation, # but if a ticketed reservation is being redeemed, # the failure _should_ be from the AM, not Controller # so it should be ignored by TicketReview slice_status_map[ slice_id] = TicketReviewSliceState.Redeemable # we don't need to look at any other reservations in this slice break # if any tickets are Nascent, # as soon as we remove the Failed reservation, # those Nascent tickets might get redeemed. # we must wait to Close any failed reservations # until all Nascent tickets are either Ticketed or Failed if slice_reservation.is_nascent(): self.logger.debug( "Found Nascent Reservation {} in slice {} when check_pending for {}" .format(slice_reservation.get_reservation_id(), slice_obj.get_name(), reservation.get_reservation_id())) slice_status_map[ slice_id] = TicketReviewSliceState.Nascent # once we have found a Nascent reservation, that is what we treat the entire slice break # track Failed reservations, but need to keep looking for Nascent or Redeemable. if slice_reservation.is_failed(): self.logger.debug( "Found failed reservation {} in slice {} when check_pending for {}" .format(slice_reservation.get_reservation_id(), slice_obj.get_name(), reservation.get_reservation_id())) slice_status_map[ slice_id] = TicketReviewSliceState.Failing # take action on the current reservation if slice_status_map[ slice_id] == TicketReviewSliceState.Failing: if reservation.get_resources( ) is not None and reservation.get_resources().get_type( ) is not None: msg = f"TicketReviewPolicy: Closing reservation {reservation.get_reservation_id()} due to " \ f"failure in slice {slice_obj.get_name()}" self.logger.info(msg) if not reservation.is_failed(): update_data = UpdateData() update_data.failed = True update_data.message = Constants.CLOSURE_BY_TICKET_REVIEW_POLICY reservation.mark_close_by_ticket_review( update_data=update_data) self.actor.close(reservation=reservation) self.calendar.remove_pending(reservation=reservation) self.pending_notify.remove(reservation=reservation) elif slice_status_map[ slice_id] == TicketReviewSliceState.Nascent: self.logger.info( "Moving reservation {} to pending redeem list due to nascent reservation in slice {}" .format(reservation.get_reservation_id(), slice_obj.get_name())) self.pending_redeem.add(reservation=reservation) self.calendar.remove_pending(reservation=reservation) else: # we don't need to look at any other reservations in this slice self.logger.debug( "Removing from pendingRedeem: {}".format(reservation)) self.pending_redeem.remove(reservation=reservation) else: # Remove active or close reservations self.logger.debug( "Removing from pendingRedeem: {}".format(reservation)) self.pending_redeem.remove(reservation=reservation) super().check_pending()
class ClientCalendar(BaseCalendar): """ This a client-side calendar to be used by brokers or service managers. A client calendar maintains the following lists: - demand: a list of reservations representing demand for resources - pending: a list of reservations with pending operations - renewing: a list of reservations organized by the time they must be renewed (cycle) - holdings: a list of active/granted reservations associated with their lease term. This list should be maintained using real time, not cycles. The renewing and holding lists are automatically purged by the implementation as time advances. The demand and pending list, however, must be purged manually by the user of this class. """ def __init__(self, *, clock: ActorClock): """ Constructor @params clock: clock factory """ super().__init__(clock=clock) # Set of reservations representing the current demand. Callers are # responsible for removing serviced reservations self.demand = ReservationSet() # Set of reservations for which a request has been issued but no # confirmation has been received. Callers are responsible for removing # acknowledged reservations. self.pending = ReservationSet() # Set of reservations grouped by renewing time. self.renewing = ReservationList() # Set of active reservations. self.holdings = ReservationHoldings() self.lock = threading.Lock() def __getstate__(self): state = self.__dict__.copy() del state['lock'] return state def __setstate__(self, state): self.__dict__.update(state) self.lock = threading.Lock() def remove(self, *, reservation: ABCReservationMixin): """ Removes the specified reservation from all internal calendar data structures @params reservation: reservation to remove """ self.remove_demand(reservation=reservation) self.remove_pending(reservation=reservation) self.remove_renewing(reservation=reservation) self.remove_holdings(reservation=reservation) def remove_scheduled_or_in_progress(self, *, reservation: ABCReservationMixin): """ Removes the specified reservations from all internal calendar data structures that represent operations to be scheduled in the future or operations that are currently in progress. Does not remove the reservation from the holdings list. @params reservation : reservation to remove """ self.remove_demand(reservation=reservation) self.remove_pending(reservation=reservation) self.remove_renewing(reservation=reservation) def get_demand(self) -> ReservationSet: """ Returns the known demand. Can be for resources starting at different times. @returns the set of demanded reservations. """ try: self.lock.acquire() return self.demand.clone() finally: self.lock.release() def add_demand(self, *, reservation: ABCReservationMixin): """ Adds a reservation to the demand list. @params reservation: reservation to add """ try: self.lock.acquire() self.demand.add(reservation=reservation) finally: self.lock.release() def remove_demand(self, *, reservation: ABCReservationMixin): """ Removes a reservation to the demand list. @params reservation: reservation to remove """ try: self.lock.acquire() self.demand.remove(reservation=reservation) finally: self.lock.release() def get_pending(self) -> ReservationSet: """ Returns the known pending. Can be for resources starting at different times. @returns the set of pending reservations. """ try: self.lock.acquire() return self.pending.clone() finally: self.lock.release() def add_pending(self, *, reservation: ABCReservationMixin): """ Adds a reservation to the pending list. @params reservation: reservation to add """ try: self.lock.acquire() self.pending.add(reservation=reservation) finally: self.lock.release() def remove_pending(self, *, reservation: ABCReservationMixin): """ Removes a reservation to the pending list. @params reservation: reservation to remove """ try: self.lock.acquire() self.pending.remove(reservation=reservation) finally: self.lock.release() def get_renewing(self, *, cycle: int) -> ReservationSet: """ Returns the reservations that need to be renewed on the specified cycle. @params cycle : cycle number @returns reservation set with reservations to be renewed on the specified cycle """ try: self.lock.acquire() return self.renewing.get_all_reservations(cycle=cycle) finally: self.lock.release() def add_renewing(self, *, reservation: ABCReservationMixin, cycle: int): """ Adds a reservation to the renewing list at the given cycle. @params reservation : reservation to add @params cycle : cycle number """ try: self.lock.acquire() self.renewing.add_reservation(reservation=reservation, cycle=cycle) finally: self.lock.release() def remove_renewing(self, *, reservation: ABCReservationMixin): """ Removes the reservation from the renewing list. @params reservation : reservation to remove """ try: self.lock.acquire() self.renewing.remove_reservation(reservation=reservation) finally: self.lock.release() def get_holdings(self, *, d: datetime = None, type: ResourceType = None) -> ReservationSet: """ Returns the resources of the specified type held by the client that are active at the specified time instance. @params d : datetime instance. @params type : resource type @returns st of reservations of the specified type that are active at the specified time """ try: self.lock.acquire() when = None if d is not None: when = ActorClock.to_milliseconds(when=d) return self.holdings.get_reservations(time=when, rtype=type) finally: self.lock.release() def add_holdings(self, *, reservation: ABCReservationMixin, start: datetime, end: datetime): """ Adds a reservation to the holdings list. @params reservation : reservation to add @params start : start time @params end : end time """ try: self.lock.acquire() self.holdings.add_reservation( reservation=reservation, start=ActorClock.to_milliseconds(when=start), end=ActorClock.to_milliseconds(when=end)) finally: self.lock.release() def remove_holdings(self, *, reservation: ABCReservationMixin): """ Removes the reservation from the renewing list. @params reservation : reservation to remove """ try: self.lock.acquire() self.holdings.remove_reservation(reservation=reservation) finally: self.lock.release() def tick(self, *, cycle: int): try: self.lock.acquire() super().tick(cycle=cycle) self.renewing.tick(cycle=cycle) ms = self.clock.cycle_end_in_millis(cycle=cycle) self.holdings.tick(time=ms) finally: self.lock.release()
class ControllerCalendarPolicy(Policy, ABCControllerPolicy): """ The base class for all calendar-based service manager policy implementations. """ def __init__(self): super().__init__() # calendar self.calendar = None # Contains reservations for which we may have completed performing # bookkeeping actions but may need to wait for some other event to take # place before we raise the corresponding event. self.pending_notify = ReservationSet() # If the actor is initialized self.initialized = False # If true, the orchestrator will close reservations lazily: it will not # issue a close and will wait until the site terminates the lease. The # major drawback is that leave actions will not be able to connect to the # resources, since the resources will not exist at this time. self.lazy_close = False def check_pending(self): """ Checks pending operations, and installs successfully completed requests in the holdings calendar. Note that the policy module must add bids to the pending set, or they may not install in the calendar. @raises Exception in case of error """ rvset = self.calendar.get_pending() if rvset is None: return for reservation in rvset.values(): if reservation.is_failed(): # This reservation has failed. Remove it from the list. This is # a separate case, because we may fail but not satisfy the # condition of the else statement. self.logger.debug( "Removing failed reservation from the pending list: {}". format(reservation)) self.calendar.remove_pending(reservation=reservation) self.pending_notify.remove(reservation=reservation) elif reservation.is_no_pending( ) and not reservation.is_pending_recover(): # No pending operation and we are not about the reissue a recovery operation on this reservation. self.logger.debug( "Controller pending request completed {}".format( reservation)) if reservation.is_closed(): # do nothing handled by close(IReservation) self.logger.debug("No op") elif reservation.is_active_ticketed(): # An active reservation extended its ticket. # cancel the current close self.calendar.remove_closing(reservation=reservation) # schedule a new close self.calendar.add_closing(reservation=reservation, cycle=self.get_close( reservation=reservation, term=reservation.get_term())) # Add from start to end instead of close. It is possible # that holdings may not accurately reflect the actual # number of resources towards the end of a lease. This is # because we assume that we still have resources even after # an advanceClose. When looking at this value, see if the # reservation has closed. self.calendar.add_holdings( reservation=reservation, start=reservation.get_term().get_new_start_time(), end=reservation.get_term().get_end_time()) self.calendar.add_redeeming( reservation=reservation, cycle=self.get_redeem(reservation=reservation)) if reservation.is_renewable(): cycle = self.get_renew(reservation=reservation) reservation.set_renew_time(time=cycle) reservation.set_dirty() self.calendar.add_renewing(reservation=reservation, cycle=cycle) self.pending_notify.remove(reservation=reservation) elif reservation.is_ticketed(): # The reservation obtained a ticket for the first time self.calendar.add_holdings( reservation=reservation, start=reservation.get_term().get_new_start_time(), end=reservation.get_term().get_end_time()) self.calendar.add_redeeming( reservation=reservation, cycle=self.get_redeem(reservation=reservation)) self.calendar.add_closing(reservation=reservation, cycle=self.get_close( reservation=reservation, term=reservation.get_term())) if reservation.is_renewable(): cycle = self.get_renew(reservation=reservation) reservation.set_renew_time(time=cycle) reservation.set_dirty() self.calendar.add_renewing(reservation=reservation, cycle=cycle) self.pending_notify.remove(reservation=reservation) elif reservation.get_state() == ReservationStates.Active: if self.pending_notify.contains(reservation=reservation): # We are waiting for transfer in operations to complete # so that we can raise the lease complete event. if reservation.is_active_joined(): self.pending_notify.remove(reservation=reservation) else: # Just completed a lease call (redeem or extendLease). # We need to remove this reservation from closing, # because we added it using r.getTerm(), and add this # reservation to closing using r.getLeasedTerm() [the # site could have changed the term of the reservation]. # This assumes that r.getTerm has not changed in the # mean time. This is true now, since the state machine # does not allow more than one pending operation. # Should we change this, we will need to update the # code below. self.calendar.remove_closing(reservation=reservation) self.calendar.add_closing( reservation=reservation, cycle=self.get_close( reservation=reservation, term=reservation.get_lease_term())) if reservation.get_renew_time() == 0: reservation.set_renew_time( time=(self.actor.get_current_cycle() + 1)) reservation.set_dirty() self.calendar.add_renewing( reservation=reservation, cycle=reservation.get_renew_time()) if not reservation.is_active_joined(): # add to the pending notify list so that we can raise the event # when transfer in operations complete. self.pending_notify.add(reservation=reservation) elif reservation.get_state() == ReservationStates.CloseWait or \ reservation.get_state() == ReservationStates.Failed: self.pending_notify.remove(reservation=reservation) else: self.logger.warning( "Invalid state on reservation. We may be still recovering: {}" .format(reservation)) continue if not self.pending_notify.contains(reservation=reservation): self.logger.debug( "Removing from pending: {}".format(reservation)) self.calendar.remove_pending(reservation=reservation) def close(self, *, reservation: ABCReservationMixin): # ignore any scheduled/in progress operations self.calendar.remove_scheduled_or_in_progress(reservation=reservation) def closed(self, *, reservation: ABCReservationMixin): # remove the reservation from all calendar structures self.calendar.remove_holdings(reservation=reservation) self.calendar.remove_redeeming(reservation=reservation) self.calendar.remove_renewing(reservation=reservation) self.calendar.remove_closing(reservation=reservation) self.pending_notify.remove(reservation=reservation) def demand(self, *, reservation: ABCClientReservation): if not reservation.is_nascent(): self.logger.error("demand reservation is not fresh") else: self.calendar.add_demand(reservation=reservation) def extend(self, *, reservation: ABCReservationMixin, resources: ResourceSet, term: Term): # cancel any previously scheduled extends self.calendar.remove_renewing(reservation=reservation) # do not cancel the close: the extend may fail cancel any pending redeem: we will redeem after the extension self.calendar.remove_redeeming(reservation=reservation) # There should be no pending operations for this reservation at this time # Add to the pending list so that we can track the progress of the reservation self.calendar.add_pending(reservation=reservation) def finish(self, *, cycle: int): super().finish(cycle=cycle) self.calendar.tick(cycle=cycle) @abstractmethod def get_close(self, *, reservation: ABCClientReservation, term: Term) -> int: """ Returns the time that a reservation should be closed. @params reservation reservation @params term term @returns the close time of the reservation (cycle) @raises Exception in case of error """ def get_closing(self, *, cycle: int) -> ReservationSet: closing = self.calendar.get_closing(cycle=cycle) result = ReservationSet() for reservation in closing.values(): if not reservation.is_failed(): self.calendar.add_pending(reservation=reservation) result.add(reservation=reservation) else: self.logger.warning( "Removing failed reservation from the closing list: {}". format(reservation)) return result @abstractmethod def get_redeem(self, *, reservation: ABCClientReservation) -> int: """ Returns the time when the reservation should be redeemed. @params reservation the reservation @returns the redeem time of the reservation (cycle) @raises Exception in case of error """ def get_redeeming(self, *, cycle: int) -> ReservationSet: redeeming = self.calendar.get_redeeming(cycle=cycle) for reservation in redeeming.values(): self.calendar.add_pending(reservation=reservation) return redeeming @abstractmethod def get_renew(self, *, reservation: ABCClientReservation) -> int: """ Returns the time when the reservation should be renewed. @params reservation the reservation @returns the renew time of the reservation (cycle) @raises Exception in case of error """ def initialize(self): if not self.initialized: super().initialize() self.calendar = ControllerCalendar(clock=self.clock) self.initialized = True def is_expired(self, *, reservation: ABCReservationMixin): """ Checks if the reservation has expired. @params reservation reservation to check @returns true or false """ term = reservation.get_term() end = self.clock.cycle(when=term.get_end_time()) return self.actor.get_current_cycle() > end def remove(self, *, reservation: ABCReservationMixin): # remove the reservation from the calendar self.calendar.remove(reservation=reservation) def revisit(self, *, reservation: ABCReservationMixin): super().revisit(reservation=reservation) if reservation.get_state() == ReservationStates.Nascent: self.calendar.add_pending(reservation=reservation) elif reservation.get_state() == ReservationStates.Ticketed: if reservation.get_pending_state( ) == ReservationPendingStates.None_: if reservation.is_pending_recover(): self.calendar.add_pending(reservation=reservation) else: self.calendar.add_redeeming( reservation=reservation, cycle=self.get_redeem(reservation=reservation)) self.calendar.add_holdings( reservation=reservation, start=reservation.get_term().get_new_start_time(), end=reservation.get_term().get_end_time()) self.calendar.add_closing(reservation=reservation, cycle=self.get_close( reservation=reservation, term=reservation.get_term())) if reservation.is_renewable( ) and reservation.get_renew_time() != 0: # Scheduling renewal is a bit tricky, since it may # involve communication with the upstream broker. # However, in some recovery cases, typical in one # container deployment, the broker and the service # manager will be recovering at the same time. In # this case the query may fail and we will have to # fail the reservation. # Our approach here is as follows: we cache the # renew time in the reservation class and persist # it in the database. When we recover, we will # check the renewTime field of the reservation if # it is non-zero, we will use it, otherwise we will # schedule the renew after we get the lease back # from the authority. self.calendar.add_renewing( reservation=reservation, cycle=reservation.get_renew_time()) elif reservation.get_pending_state( ) == ReservationPendingStates.Redeeming: raise ControllerException(Constants.INVALID_RECOVERY_STATE) elif reservation.get_state() == ReservationStates.Active: if reservation.get_pending_state( ) == ReservationPendingStates.None_: # pending list if reservation.is_pending_recover(): self.calendar.add_pending(reservation=reservation) # renewing if reservation.is_renewable(): self.calendar.add_renewing( reservation=reservation, cycle=reservation.get_renew_time()) # holdings self.calendar.add_holdings( reservation=reservation, start=reservation.get_term().get_new_start_time(), end=reservation.get_term().get_end_time()) # closing self.calendar.add_closing( reservation=reservation, cycle=self.get_close(reservation=reservation, term=reservation.get_lease_term())) elif reservation.get_pending_state( ) == ReservationPendingStates.ExtendingTicket: raise ControllerException(Constants.INVALID_RECOVERY_STATE) elif reservation.get_state() == ReservationStates.ActiveTicketed: if reservation.get_pending_state( ) == ReservationPendingStates.None_: if reservation.is_pending_recover(): self.calendar.add_pending(reservation=reservation) else: self.calendar.add_redeeming( reservation=reservation, cycle=self.get_redeem(reservation=reservation)) # holdings self.calendar.add_holdings( reservation=reservation, start=reservation.get_term().get_new_start_time(), end=reservation.get_term().get_end_time()) # closing self.calendar.add_closing( reservation=reservation, cycle=self.get_close(reservation=reservation, term=reservation.get_lease_term())) # renewing if reservation.is_renewable(): self.calendar.add_renewing( reservation=reservation, cycle=reservation.get_renew_time()) elif reservation.get_pending_state( ) == ReservationPendingStates.ExtendingLease: raise ControllerException(Constants.INVALID_RECOVERY_STATE) def ticket_satisfies(self, *, requested_resources: ResourceSet, actual_resources: ResourceSet, requested_term: Term, actual_term: Term): return def update_ticket_complete(self, *, reservation: ABCClientReservation): return def update_delegation_complete(self, *, delegation: ABCDelegation, reclaim: bool = False): return def lease_satisfies(self, *, request_resources: ResourceSet, actual_resources: ResourceSet, requested_term: Term, actual_term: Term): return
class Slice(ABCKernelSlice): """ Slice implementation. A slice has a globally unique identifier, name, description, property list, an owning identity, an access control list, and a set of reservations. This class is used within the Service Manager, which may hold reservations on many sites; on the Broker, which may have provided tickets to the slice for reservations at many sites; and on the site Authority, where each slice may hold multiple reservations for resources at that site. """ def __init__(self, *, slice_id: ID = None, name: str = "unspecified"): # Globally unique identifier. self.guid = slice_id # Slice name. Not required to be globally or locally unique. self.name = name # Description string. Has only local meaning. self.description = "no description" # The slice type: inventory or client. self.type = SliceTypes.ClientSlice # The owner of the slice. self.owner = None # Resource type associated with this slice. Used when the slice is used to # represent an inventory pool. self.resource_type = None # The reservations in this slice. self.reservations = ReservationSet() self.delegations = {} # Neo4jGraph Id self.graph_id = None self.graph = None self.state_machine = SliceStateMachine(slice_id=slice_id) self.dirty = False self.config_properties = None self.lock = threading.Lock() self.lease_end = None self.lease_start = None def __getstate__(self): state = self.__dict__.copy() del state['reservations'] del state['delegations'] del state['graph'] del state['lock'] return state def __setstate__(self, state): self.__dict__.update(state) self.reservations = ReservationSet() self.graph = None self.delegations = {} self.lock = threading.Lock() def set_graph_id(self, graph_id: str): self.graph_id = graph_id def get_graph_id(self) -> str: return self.graph_id def set_graph(self, *, graph: ABCPropertyGraph): self.graph = graph self.set_graph_id(graph_id=self.graph.get_graph_id()) def get_graph(self) -> ABCPropertyGraph: return self.graph def clone_request(self) -> ABCSlice: result = Slice() result.name = self.name result.guid = self.guid return result def get_lease_end(self) -> datetime: return self.lease_end def get_lease_start(self) -> datetime: return self.lease_start def get_description(self) -> str: return self.description def get_name(self) -> str: return self.name def get_owner(self) -> AuthToken: return self.owner def get_reservations(self) -> ReservationSet: return self.reservations def get_delegations(self) -> Dict[str, ABCDelegation]: return self.delegations def get_reservations_list(self) -> list: return self.reservations.values() def get_resource_type(self) -> ResourceType: return self.resource_type def get_slice_id(self) -> ID: return self.guid def is_broker_client(self) -> bool: return self.type == SliceTypes.BrokerClientSlice def is_client(self) -> bool: return not self.is_inventory() def is_inventory(self) -> bool: return self.type == SliceTypes.InventorySlice def is_empty(self) -> bool: return self.reservations.is_empty() def prepare(self): self.reservations.clear() self.delegations.clear() self.state_machine.clear() self.transition_slice(operation=SliceStateMachine.CREATE) def register(self, *, reservation: ABCKernelReservation): if self.reservations.contains(rid=reservation.get_reservation_id()): raise SliceException( "Reservation #{} already exists in slice".format( reservation.get_reservation_id())) self.reservations.add(reservation=reservation) def register_delegation(self, *, delegation: ABCDelegation): if delegation.get_delegation_id() in self.delegations: raise SliceException( "Delegation #{} already exists in slice".format( delegation.get_delegation_id())) self.delegations[delegation.get_delegation_id()] = delegation def set_lease_end(self, *, lease_end: datetime): self.lease_end = lease_end def set_lease_start(self, *, lease_start: datetime): self.lease_start = lease_start def set_broker_client(self): self.type = SliceTypes.BrokerClientSlice def set_client(self): self.type = SliceTypes.ClientSlice def set_description(self, *, description: str): self.description = description def set_inventory(self, *, value: bool): if value: self.type = SliceTypes.InventorySlice else: self.type = SliceTypes.ClientSlice def get_slice_type(self) -> SliceTypes: return self.type def set_name(self, *, name: str): self.name = name def set_owner(self, *, owner: AuthToken): self.owner = owner def set_resource_type(self, *, resource_type: ResourceType): self.resource_type = resource_type def soft_lookup(self, *, rid: ID) -> ABCKernelReservation: return self.reservations.get(rid=rid) def soft_lookup_delegation(self, *, did: str) -> ABCDelegation: return self.delegations.get(did, None) def __str__(self): msg = "{}({})".format(self.name, str(self.guid)) if self.graph_id is not None: msg += " Graph Id:{}".format(self.graph_id) if self.owner is not None: if self.owner.get_email() is not None: msg += " Owner:{}".format(self.owner.get_email()) else: msg += " Owner:{}".format(self.owner) ''' if self.state_machine is not None: msg += " State:{}".format(self.state_machine.get_state()) ''' return msg def unregister(self, *, reservation: ABCKernelReservation): self.reservations.remove(reservation=reservation) def unregister_delegation(self, *, delegation: ABCDelegation): if delegation.get_delegation_id() in self.delegations: self.delegations.pop(delegation.get_delegation_id()) def get_state(self) -> SliceState: return self.state_machine.get_state() def set_dirty(self): self.dirty = True def clear_dirty(self): self.dirty = False def is_dirty(self) -> bool: return self.dirty def transition_slice(self, *, operation: SliceOperation) -> Tuple[bool, SliceState]: return self.state_machine.transition_slice( operation=operation, reservations=self.reservations) def is_stable_ok(self) -> bool: state_changed, slice_state = self.transition_slice( operation=SliceStateMachine.REEVALUATE) if slice_state == SliceState.StableOk: return True return False def is_stable_error(self) -> bool: state_changed, slice_state = self.transition_slice( operation=SliceStateMachine.REEVALUATE) if slice_state == SliceState.StableError: return True return False def is_stable(self) -> bool: state_changed, slice_state = self.transition_slice( operation=SliceStateMachine.REEVALUATE) if slice_state == SliceState.StableError or slice_state == SliceState.StableOk: return True return False def is_dead_or_closing(self) -> bool: state_changed, slice_state = self.transition_slice( operation=SliceStateMachine.REEVALUATE) if slice_state == SliceState.Dead or slice_state == SliceState.Closing: return True return False def is_dead(self) -> bool: state_changed, slice_state = self.transition_slice( operation=SliceStateMachine.REEVALUATE) if slice_state == SliceState.Dead: return True return False def set_config_properties(self, *, value: dict): self.config_properties = value def get_config_properties(self) -> dict: return self.config_properties def update_slice_graph(self, sliver: BaseSliver, rid: str, reservation_state: str) -> BaseSliver: try: self.lock.acquire() # Update for Orchestrator for Active / Ticketed Reservations if sliver is not None and self.graph_id is not None: if sliver.reservation_info is None: sliver.reservation_info = ReservationInfo() sliver.reservation_info.reservation_id = rid sliver.reservation_info.reservation_state = reservation_state FimHelper.update_node(graph_id=self.graph_id, sliver=sliver) return sliver finally: self.lock.release()