def attach_res_properties(*, mng: ReservationMng, reservation: ABCReservationMixin): sliver = None if isinstance(reservation, ABCControllerReservation): if reservation.is_active(): sliver = reservation.get_leased_resources().get_sliver() else: sliver = reservation.get_resources().get_sliver() else: rset = reservation.get_resources() if rset is not None: sliver = rset.get_sliver() ticket = None rset = reservation.get_resources() if rset is not None: cs = rset.get_resources() if cs is not None and isinstance(cs, Ticket): ticket = cs.get_properties() mng.set_sliver(sliver=sliver) if isinstance(mng, TicketReservationAvro): mng.set_ticket_properties(ticket) return mng
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 update_reservation(*, res_obj: ABCReservationMixin, rsv_mng: ReservationMng) -> ABCReservationMixin: if isinstance(res_obj, ABCClientReservation): res_obj.set_renewable(renewable=rsv_mng.is_renewable()) return Converter.absorb_res_properties(rsv_mng=rsv_mng, res_obj=res_obj)
def assert_failed(self, r: ABCReservationMixin, update_data: UpdateData = None): self.assertIsNotNone(r) if update_data is None: self.assertTrue(r.is_failed()) else: self.assertTrue(update_data.failed) self.assertIsNotNone(r.get_notices())
def remove(self, *, reservation: ABCReservationMixin): """ Removes the specified reservation. Args: reservation: reservation to remove """ if reservation.get_reservation_id() in self.reservations: self.reservations.pop(reservation.get_reservation_id())
def close(self, *, reservation: ABCReservationMixin): self.calendar.remove_schedule_or_in_progress(reservation=reservation) if reservation.get_type() is not None: rc = self.get_control_by_type(rtype=reservation.get_type()) if rc is not None: rc.close(reservation=reservation) else: raise AuthorityException( Constants.UNSUPPORTED_RESOURCE_TYPE.format( reservation.get_type()))
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 close(self, *, reservation: ABCReservationMixin): if reservation is not None: if not self.recovered: self.logger.debug( "Adding reservation: {} to closing list".format( reservation.get_reservation_id())) self.closing.add(reservation=reservation) else: self.logger.debug("Closing reservation: {}".format( reservation.get_reservation_id())) self.wrapper.close(rid=reservation.get_reservation_id())
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
def add(self, *, reservation: ABCReservationMixin): """ Adds the reservation to the set Args: reservation: reservation to be added """ self.reservations[reservation.get_reservation_id()] = reservation
def redeem(self, *, reservation: ABCReservationMixin, callback: ABCControllerCallbackProxy = None, caller: AuthToken = None): if callback is None and caller is None: if not self.recovered: self.redeeming.add(reservation=reservation) else: self.wrapper.redeem_request(reservation=reservation, caller=reservation.get_client_auth_token(), callback=reservation.get_callback(), compare_sequence_numbers=False) else: if not self.is_recovered() or self.is_stopped(): raise AuthorityException(Constants.INVALID_ACTOR_STATE) if self.plugin.validate_incoming(reservation=reservation, auth=caller): self.wrapper.redeem_request(reservation=reservation, caller=caller, callback=callback, compare_sequence_numbers=True) else: self.logger.error("the redeem request is invalid") self.logger.debug("Completed processing Redeem Request")
def remove(self, *, reservation: ABCReservationMixin): super().remove(reservation=reservation) self.remove_closing(reservation=reservation) if isinstance(reservation, ABCBrokerReservation): self.remove_request(reservation=reservation) source = reservation.get_source() if source is not None: self.remove_request(reservation=reservation, source=source) self.remove_outlay(source=source, client=reservation)
def remove_reservation(self, *, reservation: ABCReservationMixin = None, rid: ID = None): if reservation is not None: self.wrapper.remove_reservation( rid=reservation.get_reservation_id()) if rid is not None: self.wrapper.remove_reservation(rid=rid)
def restore(self, *, plugin: ABCBasePlugin, reservation: ABCReservationMixin): """ Restore post stateful restart @param plugin plugin @param reservation reservation """ if reservation is not None: self.rid = reservation.get_reservation_id() if self.resources is not None: self.resources.restore(plugin=plugin, reservation=reservation)
def remove_scheduled_or_in_progress(self, *, reservation: ABCReservationMixin): super().remove_scheduled_or_in_progress(reservation=reservation) self.remove_closing(reservation=reservation) if isinstance(reservation, ABCBrokerReservation): self.remove_request(reservation=reservation) source = reservation.get_source() if source is not None: self.remove_request(reservation=reservation, source=source)
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 set_reservation(self, *, reservation: ABCReservationMixin): """ Set Reservation @param reservation reservation """ try: self.lock.acquire() self.reservation = reservation self.reservation_id = reservation.get_reservation_id() finally: self.lock.release()
def unregister(self, *, reservation: ABCReservationMixin, rid: ID): """ Unregister reservation @param reservation reservation @param rid reservation id """ if reservation is not None: self.wrapper.unregister_reservation( rid=reservation.get_reservation_id()) if rid is not None: self.wrapper.unregister_reservation(rid=rid)
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_close_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()) uset = rset.get_resources() self.assertIsNotNone(uset) self.assertEqual(0, uset.get_units())
def pass_authority_reservation(reservation: ABCReservationMixin, caller: AuthToken) -> ReservationAvro: concrete = reservation.get_resources().get_resources() if concrete is None: raise ProxyException( Constants.NOT_SPECIFIED_PREFIX.format("ticket")) avro_reservation = ReservationAvro() avro_reservation.slice = Translate.translate_slice_to_avro( slice_obj=reservation.get_slice()) avro_reservation.term = Translate.translate_term( term=reservation.get_requested_term()) avro_reservation.reservation_id = str(reservation.get_reservation_id()) avro_reservation.sequence = reservation.get_lease_sequence_out() rset = Translate.translate_resource_set( resource_set=reservation.get_resources()) if concrete is not None and isinstance(concrete, Ticket): rset.ticket = Translate.translate_ticket(ticket=concrete) if concrete is not None and isinstance(concrete, UnitSet): rset.unit_set = Translate.translate_unit_set(unit_set=concrete) avro_reservation.resource_set = rset return avro_reservation
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)
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())
def contains(self, *, reservation: ABCReservationMixin = None, rid: ID = None): """ Checks if the reservation is part of the set Args: reservation: reservation to check rid: reservation id Returns: true if the set contains the specified reservation; false otherwise """ if reservation is not None and reservation.get_reservation_id() in self.reservations: return True if rid is not None and rid in self.reservations: return True return False
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
def remove_reservation(self, *, reservation: ABCReservationMixin): """ Removes a reservation from the list. @params reservation: reservation to remove """ rid = reservation.get_reservation_id() if rid in self.reservation_id_to_cycle: cycle = self.reservation_id_to_cycle[rid] self.reservation_id_to_cycle.pop(rid) if cycle in self.cycle_to_rset: reservation_set = self.cycle_to_rset[cycle] reservation_set.remove(reservation=reservation) if reservation_set.size() == 0: self.cycle_to_rset.pop(cycle) self.count -= 1
def recover_reservation(self, *, r: ABCReservationMixin, slice_obj: ABCSlice): """ Recover reservation @param r reservation @param slice_obj slice object """ try: r.restore(actor=self, slice_obj=slice_obj) self.logger.info("Found reservation # {} in state {}".format( r.get_reservation_id(), r.get_reservation_state())) if r.is_closed(): self.logger.info( "Reservation #{} is closed. Nothing to recover.".format( r.get_reservation_id())) return self.logger.info("Recovering reservation #{}".format( r.get_reservation_id())) self.logger.debug("Recovering reservation object r={}".format(r)) self.logger.debug("Registering the reservation with the actor") self.re_register(reservation=r) self.logger.info(r) self.logger.debug("Revisiting with the Plugin") self.plugin.revisit(reservation=r) self.logger.info(r) self.logger.debug("Revisiting with the actor policy") self.policy.revisit(reservation=r) self.logger.info("Recovered reservation #{}".format( r.get_reservation_id())) except Exception as e: self.logger.error(traceback.format_exc()) self.logger.error( "Exception occurred in recovering reservation e={}".format(e)) raise ActorException("Could not recover Reservation #{}".format(r))
def fill_reservation_state( *, res: ABCReservationMixin) -> ReservationStateAvro: result = None if isinstance(res, ABCControllerReservation): result = LeaseReservationStateAvro() result.set_reservation_id(rid=str(res.get_reservation_id())) result.set_state(res.get_state().value) result.set_pending_state(res.get_pending_state().value) result.set_joining(res.get_join_state().value) else: result = ReservationStateAvro() result.set_reservation_id(rid=str(res.get_reservation_id())) result.set_state(res.get_state().value) result.set_pending_state(res.get_pending_state().value) return result
def pass_broker_reservation(reservation: ABCReservationMixin, auth: AuthToken) -> ReservationAvro: avro_reservation = ReservationAvro() avro_reservation.slice = Translate.translate_slice_to_avro(slice_obj=reservation.get_slice()) avro_reservation.term = Translate.translate_term(term=reservation.get_requested_term()) avro_reservation.reservation_id = str(reservation.get_reservation_id()) avro_reservation.sequence = reservation.get_ticket_sequence_out() rset = Translate.translate_resource_set(resource_set=reservation.get_requested_resources()) if reservation.get_requested_resources() is not None: cset = reservation.get_requested_resources().get_resources() if cset is not None and isinstance(cset, Ticket): rset.ticket = Translate.translate_ticket(ticket=cset) if cset is not None and isinstance(cset, UnitSet): rset.units = Translate.translate_unit_set(unit_set=cset) avro_reservation.resource_set = rset return avro_reservation