def assert_ticketed(self, r: ABCReservationMixin, units: int,
                     rtype: ResourceType, start: datetime, end: datetime):
     self.assertIsNotNone(r)
     self.assertFalse(r.is_failed())
     self.assertEqual(units, r.get_resources().get_units())
     self.assertEqual(rtype, r.get_resources().get_type())
     self.assertIsNotNone(r.get_term())
     self.assertEqual(start, r.get_term().get_new_start_time())
     self.assertEqual(end, r.get_term().get_end_time())
Example #2
0
    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
Example #3
0
    def is_expired(*, reservation: ABCReservationMixin) -> bool:
        """
        See if a reservation has expired

        @params reservation: reservation
        @return true if the reservation expired; otherwise, return false
        @raises Exception in case of error
        """
        now = datetime.utcnow()
        end = reservation.get_term().get_end_time()

        return now > end
    def check_incoming_extend_lease(self, request: ABCAuthorityReservation, incoming: ABCReservationMixin):
        self.assertIsNotNone(incoming)
        self.assertEqual(ReservationStates.Active, incoming.get_state())
        self.assertEqual(ReservationPendingStates.None_, incoming.get_pending_state())
        rset = incoming.get_resources()
        self.assertIsNotNone(rset)
        self.assertEqual(request.get_requested_units(), rset.get_units())
        self.assertEqual(incoming.get_term(), request.get_requested_term())

        uset = rset.get_resources()
        self.assertIsNotNone(uset)
        self.assertEqual(self.TicketUnits, uset.get_units())

        u = uset.get_set().values().__iter__().__next__()
        self.assertEqual(self.my_unit, u)
    def check_incoming_lease(self, request: ABCAuthorityReservation, incoming: ABCReservationMixin):
        self.assertIsNotNone(incoming)

        rset = incoming.get_resources()

        self.assertIsNotNone(rset)

        self.assertEqual(request.get_requested_units(), rset.get_units())

        self.assertEqual(incoming.get_term(), request.get_requested_term())

        uset = rset.get_resources()
        self.assertIsNotNone(uset)
        self.assertEqual(self.TicketUnits, uset.get_units())

        u = uset.get_set().values().__iter__().__next__()
        self.my_unit = u
Example #6
0
    def revisit(self, *, reservation: ABCReservationMixin):
        super().revisit(reservation=reservation)
        if isinstance(reservation, ABCAuthorityReservation):
            term = reservation.get_term()
            if term is None:
                term = reservation.get_requested_term()
            self.calendar.add_closing(reservation=reservation,
                                      cycle=self.get_close(term=term))
            approved = reservation.get_approved_resources()
            if approved is None:
                self.logger.debug(
                    "Reservation has no approved resources. Nothing is allocated to it."
                )
                return

            rtype = approved.get_type()
            self.logger.debug(
                f"Resource type for recovered reservation: {rtype}")
            control = self.get_control_by_type(rtype=rtype)
            if control is None:
                raise AuthorityException("Missing resource control")
            control.revisit(reservation=reservation)
Example #7
0
    def fill_reservation(*, reservation: ABCReservationMixin,
                         full: bool) -> ReservationMng:
        rsv_mng = None
        if isinstance(reservation, ABCControllerReservation):
            rsv_mng = LeaseReservationAvro()
            if reservation.get_redeem_predecessors() is not None and len(
                    reservation.get_redeem_predecessors()) > 0:
                rsv_mng.redeem_processors = []
                for p in reservation.get_redeem_predecessors():
                    pred = Converter.fill_reservation_predecessor(pred=p)
                    rsv_mng.redeem_processors.append(pred)

        elif isinstance(reservation, ABCClientReservation):
            rsv_mng = TicketReservationAvro()
        else:
            rsv_mng = ReservationMng()

        rsv_mng.set_reservation_id(str(reservation.get_reservation_id()))
        rsv_mng.set_slice_id(str(reservation.get_slice_id()))

        if reservation.get_type() is not None:
            rsv_mng.set_resource_type(str(reservation.get_type()))

        rsv_mng.set_units(reservation.get_units())
        rsv_mng.set_state(reservation.get_state().value)
        rsv_mng.set_pending_state(reservation.get_pending_state().value)

        if isinstance(reservation, ABCControllerReservation):
            rsv_mng.set_leased_units(reservation.get_leased_abstract_units())
            rsv_mng.set_join_state(reservation.get_join_state().value)
            authority = reservation.get_authority()

            if authority is not None:
                rsv_mng.set_authority(str(authority.get_guid()))

        if isinstance(reservation, ABCClientReservation):
            broker = reservation.get_broker()
            if broker is not None:
                rsv_mng.set_broker(str(broker.get_guid()))
            rsv_mng.set_renewable(reservation.is_renewable())
            rsv_mng.set_renew_time(reservation.get_renew_time())

        if reservation.get_term() is not None:
            rsv_mng.set_start(
                ActorClock.to_milliseconds(
                    when=reservation.get_term().get_start_time()))
            rsv_mng.set_end(
                ActorClock.to_milliseconds(
                    when=reservation.get_term().get_end_time()))
        else:
            if reservation.get_requested_term() is not None:
                rsv_mng.set_start(
                    ActorClock.to_milliseconds(
                        when=reservation.get_requested_term().get_start_time())
                )
                rsv_mng.set_end(
                    ActorClock.to_milliseconds(
                        when=reservation.get_requested_term().get_end_time()))

        if reservation.get_requested_term() is not None:
            rsv_mng.set_requested_end(
                ActorClock.to_milliseconds(
                    when=reservation.get_requested_term().get_end_time()))

        rsv_mng.set_notices(reservation.get_notices())

        if full:
            rsv_mng = Converter.attach_res_properties(mng=rsv_mng,
                                                      reservation=reservation)

        return rsv_mng
Example #8
0
    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)