|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 2020 FABRIC Testbed |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +# |
| 24 | +# |
| 25 | +# Author: Komal Thareja ([email protected]) |
| 26 | +import queue |
| 27 | +import threading |
| 28 | +import traceback |
| 29 | + |
| 30 | +from fabric_cf.actor.core.kernel.reservation_states import ReservationStates |
| 31 | +from fabric_cf.actor.core.util.id import ID |
| 32 | +from fabric_cf.actor.core.util.iterable_queue import IterableQueue |
| 33 | +from fabric_cf.orchestrator.core.exceptions import OrchestratorException |
| 34 | +from fabric_cf.orchestrator.core.orchestrator_slice_wrapper import OrchestratorSliceWrapper |
| 35 | +from fabric_cf.orchestrator.core.reservation_status_update import ReservationStatusUpdate |
| 36 | + |
| 37 | + |
| 38 | +class SliceDemandThread: |
| 39 | + """ |
| 40 | + This runs as a standalone thread started by Orchestrator and deals with issuing demand for the slivers for |
| 41 | + the newly created slices. The purpose of this thread is to help orchestrator respond back to the create |
| 42 | + without waiting for the slivers to be demanded |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, *, kernel): |
| 46 | + self.slice_queue = queue.Queue() |
| 47 | + self.slice_avail_condition = threading.Condition() |
| 48 | + self.thread_lock = threading.Lock() |
| 49 | + self.thread = None |
| 50 | + self.stopped = False |
| 51 | + from fabric_cf.actor.core.container.globals import GlobalsSingleton |
| 52 | + self.logger = GlobalsSingleton.get().get_logger() |
| 53 | + self.mgmt_actor = kernel.get_management_actor() |
| 54 | + self.sut = kernel.get_sut() |
| 55 | + |
| 56 | + def queue_slice(self, *, controller_slice: OrchestratorSliceWrapper): |
| 57 | + """ |
| 58 | + Queue a slice |
| 59 | + :param controller_slice: |
| 60 | + :return: |
| 61 | + """ |
| 62 | + with self.slice_avail_condition: |
| 63 | + self.slice_queue.put_nowait(controller_slice) |
| 64 | + self.logger.debug(f"Added slice to slices queue {controller_slice.get_slice_id()}") |
| 65 | + self.slice_avail_condition.notify_all() |
| 66 | + |
| 67 | + def start(self): |
| 68 | + """ |
| 69 | + Start thread |
| 70 | + :return: |
| 71 | + """ |
| 72 | + try: |
| 73 | + self.thread_lock.acquire() |
| 74 | + if self.thread is not None: |
| 75 | + raise OrchestratorException("This SliceDemandThread has already been started") |
| 76 | + |
| 77 | + self.thread = threading.Thread(target=self.run) |
| 78 | + self.thread.setName(self.__class__.__name__) |
| 79 | + self.thread.setDaemon(True) |
| 80 | + self.thread.start() |
| 81 | + |
| 82 | + finally: |
| 83 | + self.thread_lock.release() |
| 84 | + |
| 85 | + def stop(self): |
| 86 | + """ |
| 87 | + Stop thread |
| 88 | + :return: |
| 89 | + """ |
| 90 | + self.stopped = True |
| 91 | + try: |
| 92 | + self.thread_lock.acquire() |
| 93 | + temp = self.thread |
| 94 | + self.thread = None |
| 95 | + if temp is not None: |
| 96 | + self.logger.warning("It seems that the SliceDemandThread is running. Interrupting it") |
| 97 | + try: |
| 98 | + # TODO find equivalent of interrupt |
| 99 | + with self.slice_avail_condition: |
| 100 | + self.slice_avail_condition.notify_all() |
| 101 | + temp.join() |
| 102 | + except Exception as e: |
| 103 | + self.logger.error(f"Could not join SliceDemandThread thread {e}") |
| 104 | + finally: |
| 105 | + self.thread_lock.release() |
| 106 | + finally: |
| 107 | + if self.thread_lock is not None and self.thread_lock.locked(): |
| 108 | + self.thread_lock.release() |
| 109 | + |
| 110 | + def run(self): |
| 111 | + """ |
| 112 | + Thread main loop |
| 113 | + :return: |
| 114 | + """ |
| 115 | + self.logger.debug("SliceDemandThread started") |
| 116 | + while True: |
| 117 | + slices = [] |
| 118 | + with self.slice_avail_condition: |
| 119 | + |
| 120 | + while self.slice_queue.empty() and not self.stopped: |
| 121 | + try: |
| 122 | + self.slice_avail_condition.wait() |
| 123 | + except InterruptedError as e: |
| 124 | + self.logger.info("Slice Demand thread interrupted. Exiting") |
| 125 | + return |
| 126 | + |
| 127 | + if self.stopped: |
| 128 | + self.logger.info("Slice Demand Thread exiting") |
| 129 | + return |
| 130 | + |
| 131 | + if not self.slice_queue.empty(): |
| 132 | + try: |
| 133 | + for s in IterableQueue(source_queue=self.slice_queue): |
| 134 | + slices.append(s) |
| 135 | + except Exception as e: |
| 136 | + self.logger.error(f"Error while adding slice to slice queue! e: {e}") |
| 137 | + self.logger.error(traceback.format_exc()) |
| 138 | + |
| 139 | + self.slice_avail_condition.notify_all() |
| 140 | + |
| 141 | + if len(slices) > 0: |
| 142 | + self.logger.debug(f"Processing {len(slices)} slices") |
| 143 | + for s in slices: |
| 144 | + try: |
| 145 | + # Process the Slice i.e. Demand the computed reservations i.e. Add them to the policy |
| 146 | + # Once added to the policy; Actor Tick Handler will do following asynchronously: |
| 147 | + # 1. Ticket message exchange with broker and |
| 148 | + # 2. Redeem message exchange with AM once ticket is granted by Broker |
| 149 | + self.demand_slice(controller_slice=s) |
| 150 | + except Exception as e: |
| 151 | + self.logger.error(f"Error while processing slice {type(s)}, {e}") |
| 152 | + self.logger.error(traceback.format_exc()) |
| 153 | + |
| 154 | + def demand_slice(self, *, controller_slice: OrchestratorSliceWrapper): |
| 155 | + """ |
| 156 | + Demand slice reservations. |
| 157 | + :param controller_slice: |
| 158 | + """ |
| 159 | + computed_reservations = controller_slice.get_computed_reservations() |
| 160 | + |
| 161 | + try: |
| 162 | + controller_slice.lock() |
| 163 | + for reservation in computed_reservations: |
| 164 | + self.logger.debug(f"Issuing demand for reservation: {reservation.get_reservation_id()}") |
| 165 | + |
| 166 | + if reservation.get_state() != ReservationStates.Unknown.value: |
| 167 | + self.logger.debug(f"Reservation not in {reservation.get_state()} state, ignoring it") |
| 168 | + continue |
| 169 | + |
| 170 | + if not self.mgmt_actor.demand_reservation(reservation=reservation): |
| 171 | + raise OrchestratorException(f"Could not demand resources: {self.mgmt_actor.get_last_error()}") |
| 172 | + self.logger.debug(f"Reservation #{reservation.get_reservation_id()} demanded successfully") |
| 173 | + |
| 174 | + for r in controller_slice.computed_l3_reservations: |
| 175 | + res_status_update = ReservationStatusUpdate(logger=self.logger) |
| 176 | + self.sut.add_active_status_watch(watch=ID(uid=r.get_reservation_id()), |
| 177 | + callback=res_status_update) |
| 178 | + except Exception as e: |
| 179 | + self.logger.error(traceback.format_exc()) |
| 180 | + self.logger.error("Unable to get orchestrator or demand reservation: {}".format(e)) |
| 181 | + finally: |
| 182 | + controller_slice.unlock() |
0 commit comments