Exemple #1
0
    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()
Exemple #2
0
 def __init__(self, *, identity: AuthToken = None, clock: ActorClock = None):
     super().__init__(auth=identity, clock=clock)
     # Recovered reservations that need to obtain tickets (both server and client roles).
     self.ticketing = ReservationSet()
     # Recovered reservations that need to extend tickets (both server and client roles).
     self.extending = ReservationSet()
     # The peer registry.
     self.registry = PeerRegistry()
     # Initialization status.
     self.initialized = False
     self.type = ActorType.Broker
Exemple #3
0
 def __init__(self, *, identity: AuthToken = None, clock: ActorClock = None):
     super().__init__(auth=identity, clock=clock)
     self.type = ActorType.Authority
     # Initialization status.
     self.initialized = False
     # Reservations to redeem once the actor recovers.
     self.redeeming = ReservationSet()
     # Reservations to extendLease for once the actor recovers.
     self.extending_lease = ReservationSet()
     # Reservations to modifyLease for once the actor recovers
     self.modifying_lease = ReservationSet()
Exemple #4
0
    def map(self, *, reservation: ABCAuthorityReservation,
            node_id_to_reservations: dict) -> dict:
        """
        Maps a reservation. Indicates we will approve the request: update its
        expire time in the calendar, and issue a map probe. The map probe will
        result in a retry of the mapper request through bind or extend
        above, which will release the request to the associated mapper.

        @param reservation: the reservation
        @param node_id_to_reservations: node_id_to_reservations
        @throws Exception in case of error
        """
        assigned = self.assign_reservation(
            reservation=reservation,
            node_id_to_reservations=node_id_to_reservations)
        if assigned is not None:
            approved = reservation.get_requested_term()
            reservation.set_approved(term=approved,
                                     approved_resources=assigned)
            reservation.set_bid_pending(value=False)
            node_id = assigned.get_sliver().get_node_map()[1]

            if node_id_to_reservations.get(node_id, None) is None:
                node_id_to_reservations[node_id] = ReservationSet()
            node_id_to_reservations[node_id].add(reservation=reservation)
        else:
            if not reservation.is_terminal():
                self.logger.debug(
                    f"Deferring reservation {reservation} for the next cycle: "
                    f"{self.actor.get_current_cycle() + 1}")
                self.reschedule(reservation=reservation)

        return node_id_to_reservations
Exemple #5
0
    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)
Exemple #6
0
 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 = {}
Exemple #7
0
 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 __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()
Exemple #9
0
 def __init__(self,
              *,
              identity: AuthToken = None,
              clock: ActorClock = None):
     super().__init__(auth=identity, clock=clock)
     # Recovered reservations that need to obtain tickets.
     self.ticketing = ReservationSet()
     # Recovered reservations that need to extend tickets.
     self.extending_ticket = ReservationSet()
     # Recovered reservations that need to be redeemed.
     self.redeeming = ReservationSet()
     # Recovered reservations that need to extend leases.
     self.extending_lease = ReservationSet()
     # Recovered reservations that need to modify leases
     self.modifying_lease = ReservationSet()
     # Peer registry.
     self.registry = PeerRegistry()
     # initialization status
     self.initialized = False
     self.type = ActorType.Orchestrator
Exemple #10
0
 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
Exemple #11
0
    def __setstate__(self, state):
        self.__dict__.update(state)
        self.recovered = False
        self.wrapper = None
        self.logger = None
        self.clock = None
        self.current_cycle = -1
        self.first_tick = True
        self.stopped = False
        self.initialized = False
        self.thread = None
        self.thread_lock = threading.Lock()
        self.timer_queue = queue.Queue()
        self.event_queue = queue.Queue()
        self.reservation_tracker = None
        self.subscription_id = None
        self.actor_main_lock = threading.Condition()
        self.closing = ReservationSet()
        self.message_service = None

        self.ticketing = ReservationSet()
        self.extending_ticket = ReservationSet()
        self.redeeming = ReservationSet()
        self.extending_lease = ReservationSet()
        self.modifying_lease = ReservationSet()
        self.registry = PeerRegistry()
    def get_reservations(self, *, cycle: int) -> ReservationSet:
        """
        Returns all reservations associated with the specified cycle.
        @params cycle : cycle
        @return a set of reservations associated with the specified cycle. Note
        that removing from the set will not affect the ReservationList
        """
        result = ReservationSet()

        if cycle in self.cycle_to_rset:
            result = self.cycle_to_rset[cycle].clone()

        return result
 def get_all_reservations(self, *, cycle: int) -> ReservationSet:
     """
     Returns all reservations associated with cycles up to and including the specified cycle
     @params cycle : cycle
     @returns a set of reservations associated with the cycles up to and including specified cycle.
     Note that removing from the set will not affect the ReservationList
     """
     result = ReservationSet()
     for entry in self.rset_wrapper_list:
         if entry.cycle <= cycle:
             for reservation in entry.reservation_set.values():
                 result.add(reservation=reservation)
         else:
             break
     return result
Exemple #14
0
 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
Exemple #15
0
    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
Exemple #16
0
    def __init__(self, *, auth: AuthToken = None, clock: ActorClock = None):
        # Globally unique identifier for this actor.
        self.guid = ID()
        # Actor name.
        self.name = None
        # Actor type code.
        self.type = ActorType.All
        # Actor description.
        self.description = self.DefaultDescription
        # Identity object representing this actor.
        self.identity = auth
        # Actor policy object.
        self.policy = None
        # Actor plugin
        self.plugin = None
        # True if this actor has completed the recovery phase.
        self.recovered = False
        # The kernel wrapper.
        self.wrapper = None
        # logger
        self.logger = None
        # Factory for term.
        self.clock = clock
        # current cycle
        self.current_cycle = -1
        # True if the current tick is the first tick this actor has received.
        self.first_tick = True
        # Set to true when the actor is stopped.
        self.stopped = False
        # Initialization status.
        self.initialized = False
        # Contains a reference to the thread currently executing the timer handler.
        # This field is set at the entry to and clear at the exit.
        # The primary use of the field is to handle correctly stopping the actor.
        self.thread = None
        # A queue of timers that have fired and need to be processed.
        self.timer_queue = queue.Queue()
        self.event_queue = queue.Queue()
        self.reservation_tracker = None
        self.subscription_id = None
        # Reservations to close once recovery is complete.
        self.closing = ReservationSet()

        self.thread_lock = threading.Lock()
        self.actor_main_lock = threading.Condition()
        self.message_service = None
Exemple #17
0
 def __setstate__(self, state):
     self.__dict__.update(state)
     self.recovered = False
     self.wrapper = None
     self.logger = None
     self.clock = None
     self.current_cycle = -1
     self.first_tick = True
     self.stopped = False
     self.initialized = False
     self.thread = None
     self.thread_lock = threading.Lock()
     self.timer_queue = queue.Queue()
     self.event_queue = queue.Queue()
     self.subscription_id = None
     self.actor_main_lock = threading.Condition()
     self.closing = ReservationSet()
     self.message_service = None
     self.policy.set_actor(actor=self)
Exemple #18
0
    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)
Exemple #19
0
    def process_renewing(self, *, renewing: ReservationSet) -> ReservationSet:
        """
        Performs checks on renewing reservations. Updates the terms to
        suggest new terms, stores the extend on the pending list. Returns a
        fresh ReservationSet of expiring reservations to try to renew in this
        bidding cycle.

        @param renewing collection of the renewing reservations

        @return non-null set of renewals
        """
        result = ReservationSet()
        if renewing is None:
            return None

        #self.logger.debug("Expiring = {}".format(renewing.size()))

        for reservation in renewing.values():
            self.logger.debug("Expiring res: {}".format(reservation))

            if reservation.is_renewable():
                self.logger.debug("This is a renewable expiring reservation")

                term = reservation.get_term()

                term = term.extend()

                reservation.set_approved(term=term,
                                         approved_resources=reservation.
                                         get_resources().abstract_clone())

                result.add(reservation=reservation)
                self.calendar.add_pending(reservation=reservation)
            else:
                self.logger.debug(
                    "This is not a renewable expiring reservation")

        return result
    def add_reservation(self, *, reservation: ABCReservationMixin, cycle: int):
        """
        Adds a reservation associated with a given cycle.
        @params reservation:  the reservation
        @params cycle: the cycle with which to associate the reservation
        """
        if reservation is None or cycle < 0 or reservation.get_reservation_id(
        ) is None:
            raise FrameworkException(Constants.INVALID_ARGUMENT)

        if reservation.get_reservation_id() in self.reservation_id_to_cycle:
            existing_cycle = self.reservation_id_to_cycle[
                reservation.get_reservation_id()]
            if existing_cycle == cycle:
                return
            else:
                raise RuntimeError(
                    "Reservation: #{} is already in the list at a different cycle. Please remove it first, "
                    "before adding to a different cycle".format(
                        reservation.get_reservation_id()))

        reservation_set = None
        if cycle in self.cycle_to_rset:
            reservation_set = self.cycle_to_rset[cycle]
        else:
            reservation_set = ReservationSet()

        if reservation_set.contains(reservation=reservation) is False:
            reservation_set.add(reservation=reservation)
            self.count += 1

        if cycle not in self.cycle_to_rset:
            self.add_to_list(reservation_set=reservation_set, cycle=cycle)
            self.cycle_to_rset[cycle] = reservation_set

        self.reservation_id_to_cycle[reservation.get_reservation_id()] = cycle
Exemple #21
0
    def ticket_inventory(
            self, *, reservation: ABCBrokerReservation, inv: InventoryForType,
            term: Term,
            node_id_to_reservations: dict) -> Tuple[bool, dict, Any]:
        error_msg = None
        try:
            rset = reservation.get_requested_resources()
            needed = rset.get_units()

            # for network node slivers
            # find a list of candidate worker nodes that satisfy the requirements based on delegated
            # capacities within the site
            # for network link slivers
            # orchestrator needs to provide a map to CBM guid of the node representing the
            # intended link (and possibly interfaces connected to it)

            res_sliver = rset.get_sliver()
            delegation_id = None
            sliver = None

            if isinstance(res_sliver, NodeSliver):
                delegation_id, sliver, error_msg = self.__allocate_nodes(
                    reservation=reservation,
                    inv=inv,
                    sliver=res_sliver,
                    node_id_to_reservations=node_id_to_reservations)

            elif isinstance(res_sliver, NetworkServiceSliver):
                delegation_id, sliver, error_msg = self.__allocate_services(
                    rid=reservation.get_reservation_id(),
                    inv=inv,
                    sliver=res_sliver,
                    node_id_to_reservations=node_id_to_reservations)
            else:
                self.logger.error(
                    f'Reservation {reservation} sliver type is neither Node, nor NetworkServiceSliver'
                )
                raise BrokerException(
                    msg=f"Reservation sliver type is neither Node "
                    f"nor NetworkLink for reservation# {reservation}")

            if delegation_id is not None:
                delegation = self.actor.get_delegation(did=delegation_id)
                reservation = self.issue_ticket(reservation=reservation,
                                                units=needed,
                                                rtype=rset.get_type(),
                                                term=term,
                                                source=delegation,
                                                sliver=sliver)

                node_map = sliver.get_node_map()
                node_id = node_map[1]
                if node_id_to_reservations.get(node_id, None) is None:
                    node_id_to_reservations[node_id] = ReservationSet()
                node_id_to_reservations[node_id].add(reservation=reservation)
                return True, node_id_to_reservations, error_msg
        except Exception as e:
            self.logger.error(traceback.format_exc())
            self.logger.error(e)
            reservation.fail(message=str(e))
        return False, node_id_to_reservations, error_msg
Exemple #22
0
 def formulate_bids(self, *, cycle: int) -> Bids:
     renewing = self.calendar.get_renewing(cycle=cycle)
     extending = self.process_renewing(renewing=renewing)
     return Bids(ticketing=ReservationSet(), extending=extending)
Exemple #23
0
class Controller(ActorMixin, ABCController):
    """
    Implements Controller
    """
    saved_extended_renewable = ReservationSet()

    def __init__(self,
                 *,
                 identity: AuthToken = None,
                 clock: ActorClock = None):
        super().__init__(auth=identity, clock=clock)
        # Recovered reservations that need to obtain tickets.
        self.ticketing = ReservationSet()
        # Recovered reservations that need to extend tickets.
        self.extending_ticket = ReservationSet()
        # Recovered reservations that need to be redeemed.
        self.redeeming = ReservationSet()
        # Recovered reservations that need to extend leases.
        self.extending_lease = ReservationSet()
        # Recovered reservations that need to modify leases
        self.modifying_lease = ReservationSet()
        # Peer registry.
        self.registry = PeerRegistry()
        # initialization status
        self.initialized = False
        self.type = ActorType.Orchestrator

    def __getstate__(self):
        state = self.__dict__.copy()
        del state['recovered']
        del state['wrapper']
        del state['logger']
        del state['clock']
        del state['current_cycle']
        del state['first_tick']
        del state['stopped']
        del state['initialized']
        del state['thread_lock']
        del state['thread']
        del state['timer_queue']
        del state['event_queue']
        del state['reservation_tracker']
        del state['subscription_id']
        del state['actor_main_lock']
        del state['closing']
        del state['message_service']

        del state['ticketing']
        del state['extending_ticket']
        del state['redeeming']
        del state['extending_lease']
        del state['modifying_lease']
        del state['registry']
        return state

    def __setstate__(self, state):
        self.__dict__.update(state)
        self.recovered = False
        self.wrapper = None
        self.logger = None
        self.clock = None
        self.current_cycle = -1
        self.first_tick = True
        self.stopped = False
        self.initialized = False
        self.thread = None
        self.thread_lock = threading.Lock()
        self.timer_queue = queue.Queue()
        self.event_queue = queue.Queue()
        self.reservation_tracker = None
        self.subscription_id = None
        self.actor_main_lock = threading.Condition()
        self.closing = ReservationSet()
        self.message_service = None

        self.ticketing = ReservationSet()
        self.extending_ticket = ReservationSet()
        self.redeeming = ReservationSet()
        self.extending_lease = ReservationSet()
        self.modifying_lease = ReservationSet()
        self.registry = PeerRegistry()

    def actor_added(self):
        super().actor_added()
        self.registry.actor_added()

    def add_broker(self, *, broker: ABCBrokerProxy):
        self.registry.add_broker(broker=broker)

    def bid(self):
        """
        Bids for resources as dictated by the plugin bidding policy for the
        current cycle.

        @throws Exception in case of error
        """
        # Invoke policy module to select candidates for ticket and extend. Note
        # that candidates structure is discarded when we're done.
        candidates = self.policy.formulate_bids(cycle=self.current_cycle)
        if candidates is not None:
            # Issue new ticket requests.
            ticketing = candidates.get_ticketing()
            if ticketing is not None:
                for ticket in ticketing.values():
                    try:
                        self.wrapper.ticket(reservation=ticket,
                                            destination=self)
                    except Exception as e:
                        self.logger.error(
                            "unexpected ticket failure for #{} {}".format(
                                ticket.get_reservation_id(), e))
                        ticket.fail(
                            message="unexpected ticket failure {}".format(e))

            extending = candidates.get_extending()
            if extending is not None:
                for extend in extending.values():
                    try:
                        self.wrapper.extend_ticket(reservation=extend)
                    except Exception as e:
                        self.logger.error(
                            "unexpected extend failure for #{} {}".format(
                                extend.get_reservation_id(), e))
                        extend.fail(
                            message="unexpected extend failure {}".format(e))

    def claim_delegation_client(self,
                                *,
                                delegation_id: str = None,
                                slice_object: ABCSlice = None,
                                broker: ABCBrokerProxy = None,
                                id_token: str = None) -> ABCDelegation:
        raise ControllerException("Not implemented")

    def reclaim_delegation_client(self,
                                  *,
                                  delegation_id: str = None,
                                  slice_object: ABCSlice = None,
                                  broker: ABCBrokerProxy = None,
                                  id_token: str = None) -> ABCDelegation:
        raise ControllerException("Not implemented")

    def close_expiring(self):
        """
        Issues close requests on all reservations scheduled for closing on the
        current cycle
        """
        rset = self.policy.get_closing(cycle=self.current_cycle)

        if rset is not None and rset.size() > 0:
            self.logger.info(
                "SlottedSM close expiring for cycle {} expiring {}".format(
                    self.current_cycle, rset))
            self.close_reservations(reservations=rset)

    def demand(self, *, rid: ID):
        if rid is None:
            raise ControllerException("Invalid argument")

        reservation = self.get_reservation(rid=rid)

        if reservation is None:
            raise ControllerException("Unknown reservation {}".format(rid))

        self.policy.demand(reservation=reservation)
        reservation.set_policy(policy=self.policy)

    def extend_lease_reservation(self, *,
                                 reservation: ABCControllerReservation):
        """
        Extend Lease for a reservation
        @param reservation reservation
        """
        if not self.recovered:
            self.extending_lease.add(reservation=reservation)
        else:
            self.wrapper.extend_lease(reservation=reservation)

    def extend_lease(self,
                     *,
                     reservation: ABCControllerReservation = None,
                     rset: ReservationSet = None):
        if reservation is not None and rset is not None:
            raise ControllerException(
                "Invalid Arguments: reservation and rset can not be both not None"
            )
        if reservation is None and rset is None:
            raise ControllerException(
                "Invalid Arguments: reservation and rset can not be both None")

        if reservation is not None:
            self.extend_lease_reservation(reservation=reservation)

        if rset is not None:
            for r in rset.values():
                try:
                    if isinstance(r, ABCControllerReservation):
                        self.extend_lease_reservation(reservation=r)
                    else:
                        self.logger.warning(
                            "Reservation #{} cannot extendLease".format(
                                r.get_reservation_id()))
                except Exception as e:
                    self.logger.error(
                        "Could not extend_lease for #{} e={}".format(
                            r.get_reservation_id(), e))

    def extend_ticket_client(self, *, reservation: ABCClientReservation):
        if not self.recovered:
            self.extending_ticket.add(reservation=reservation)
        else:
            self.wrapper.extend_ticket(reservation=reservation)

    def extend_tickets_client(self, *, rset: ReservationSet):
        for reservation in rset.values():
            try:
                if isinstance(reservation, ABCClientReservation):
                    self.extend_ticket_client(reservation=reservation)
                else:
                    self.logger.warning(
                        "Reservation # {} cannot be ticketed".format(
                            reservation.get_reservation_id()))
            except Exception as e:
                self.logger.error("Could not ticket for #{} e: {}".format(
                    reservation.get_reservation_id(), e))

    def get_broker(self, *, guid: ID) -> ABCBrokerProxy:
        return self.registry.get_broker(guid=guid)

    def get_brokers(self) -> list:
        return self.registry.get_brokers()

    def get_default_broker(self) -> ABCBrokerProxy:
        return self.registry.get_default_broker()

    def initialize(self):
        if not self.initialized:
            super().initialize()

            self.registry.set_slices_plugin(plugin=self.plugin)
            self.registry.initialize()

            self.initialized = True

    def process_redeeming(self):
        """
        Issue redeem requests on all reservations scheduled for redeeming on the current cycle
        """
        rset = self.policy.get_redeeming(cycle=self.current_cycle)

        if rset is not None and rset.size() > 0:
            self.logger.info(
                "SlottedController redeem for cycle {} redeeming {}".format(
                    self.current_cycle, rset))

            self.redeem_reservations(rset=rset)

    def redeem(self, *, reservation: ABCControllerReservation):
        if not self.recovered:
            self.redeeming.add(reservation=reservation)
        else:
            self.wrapper.redeem(reservation=reservation)

    def redeem_reservations(self, *, rset: ReservationSet):
        for reservation in rset.values():
            try:
                if isinstance(reservation, ABCControllerReservation):
                    self.redeem(reservation=reservation)
                else:
                    self.logger.warning(
                        "Reservation #{} cannot be redeemed".format(
                            reservation.get_reservation_id()))
            except Exception as e:
                self.logger.error(traceback.format_exc())
                self.logger.error("Could not redeem for #{} {}".format(
                    reservation.get_reservation_id(), e))

    def ticket_client(self, *, reservation: ABCClientReservation):
        if not self.recovered:
            self.ticketing.add(reservation=reservation)
        else:
            self.wrapper.ticket(reservation=reservation, destination=self)

    def tickets_client(self, *, rset: ReservationSet):
        for reservation in rset.values():
            try:
                if isinstance(reservation, ABCClientReservation):
                    self.ticket_client(reservation=reservation)
                else:
                    self.logger.warning(
                        "Reservation #{} cannot be ticketed".format(
                            reservation.get_reservation_id()))
            except Exception as e:
                self.logger.error("Could not ticket for #{} e: {}".format(
                    reservation.get_reservation_id(), e))

    def tick_handler(self):
        self.close_expiring()
        self.process_redeeming()
        self.bid()

    def update_lease(self, *, reservation: ABCReservationMixin, update_data,
                     caller: AuthToken):
        if not self.is_recovered() or self.is_stopped():
            raise ControllerException("This actor cannot receive calls")

        self.wrapper.update_lease(reservation=reservation,
                                  update_data=update_data,
                                  caller=caller)

    def update_ticket(self, *, reservation: ABCReservationMixin, update_data,
                      caller: AuthToken):
        if not self.is_recovered() or self.is_stopped():
            raise ControllerException("This actor cannot receive calls")

        self.wrapper.update_ticket(reservation=reservation,
                                   update_data=update_data,
                                   caller=caller)

    def update_delegation(self, *, delegation: ABCDelegation, update_data,
                          caller: AuthToken):
        raise ControllerException("Not supported in controller")

    def modify(self, *, reservation_id: ID, modify_properties: dict):
        if reservation_id is None or modify_properties is None:
            self.logger.error(
                "modifyProperties argument is null or non-existing reservation"
            )

        rc = None
        try:
            rc = self.get_reservation(rid=reservation_id)
        except Exception as e:
            self.logger.error("Could not find reservation #{} e: {}".format(
                reservation_id, e))

        if rc is None:
            raise ControllerException(
                "Unknown reservation: {}".format(reservation_id))

        if rc.get_resources() is not None:
            # TODO
            print("TODO")
        else:
            self.logger.warning(
                "There are no approved resources for {}, no modify properties will be added"
                .format(reservation_id))

        if not self.recovered:
            self.modifying_lease.add(reservation=rc)
        else:
            self.wrapper.modify_lease(reservation=rc)

    def save_extending_renewable(self):
        """
        For recovery, mark extending reservations renewable or the opposite
        and save this, then restore afterwards
        """
        for reservation in self.extending_ticket.values():
            try:
                if isinstance(reservation, ABCClientReservation):
                    if not reservation.get_renewable():
                        reservation.set_renewable(renewable=True)
                        self.saved_extended_renewable.add(
                            reservation=reservation)
                else:
                    self.logger.warning(
                        "Reservation #{} cannot be remarked".format(
                            reservation.get_reservation_id()))
            except Exception as e:
                self.logger.error(
                    "Could not mark ticket renewable for #{} e: {}".format(
                        reservation.get_reservation_id(), e))

    def restore_extending_renewable(self):
        """
        Restore the value of renewable field after recovery if we changed it
        """
        for reservation in self.saved_extended_renewable.values():
            try:
                reservation.set_renewable(renewable=False)
            except Exception as e:
                self.logger.error(
                    "Could not remark ticket non renewable for #{} e: {}".
                    format(reservation.get_reservation_id(), e))

    def issue_delayed(self):
        super().issue_delayed()
        self.tickets_client(rset=self.ticketing)
        self.ticketing.clear()

        self.save_extending_renewable()
        self.extend_tickets_client(rset=self.extending_ticket)
        self.extending_ticket.clear()

        self.redeem_reservations(rset=self.redeeming)
        self.redeeming.clear()

        self.extend_lease(rset=self.extending_lease)
        self.extending_lease.clear()

    @staticmethod
    def get_management_object_class() -> str:
        return ControllerManagementObject.__name__

    @staticmethod
    def get_management_object_module() -> str:
        return ControllerManagementObject.__module__

    @staticmethod
    def get_kafka_service_class() -> str:
        return ControllerService.__name__

    @staticmethod
    def get_kafka_service_module() -> str:
        return ControllerService.__module__

    @staticmethod
    def get_mgmt_kafka_service_class() -> str:
        return KafkaControllerService.__name__

    @staticmethod
    def get_mgmt_kafka_service_module() -> str:
        return KafkaControllerService.__module__
Exemple #24
0
 def __setstate__(self, state):
     self.__dict__.update(state)
     self.reservations = ReservationSet()
     self.graph = None
     self.delegations = {}
     self.lock = threading.Lock()
Exemple #25
0
 def __init__(self):
     super().__init__()
     self.pending_redeem = ReservationSet()