示例#1
0
    def __init__(self, exchange=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
        """
        Creates a TaskInfoCache instance, which listens for OTDB task status events, and then fetches and caches relevant info for the current active task(s).

        :param exchange:
        :param broker:
        """

        # the internal cache is a dict with a mapping of otdb_id->TaskInfo
        self._cache = {}

        # the internal project cache is a dict with a mapping of project_name->project_info_dict
        self._project_cache = {}

        # the internal stations cache is a list of the currently used stations
        self._stations_cache = []

        # internal rpc's to fetch the needed information
        self._otdbrpc = OTDBRPC.create(exchange=exchange,
                                       broker=broker,
                                       timeout=DEFAULT_RPC_TIMEOUT)
        self._momrpc = MoMQueryRPC.create(exchange=exchange,
                                          broker=broker,
                                          timeout=DEFAULT_RPC_TIMEOUT)
        self._radbrpc = RADBRPC.create(exchange=exchange,
                                       broker=broker,
                                       timeout=DEFAULT_RPC_TIMEOUT)
示例#2
0
 def __init__(self, exchange=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
     """
     :param exchange: name of the exchange to listen on.
     :param broker: name of the broker to connect to.
     """
     super().__init__()
     self.otdbrpc = OTDBRPC.create(exchange=exchange, broker=broker)
     self.radbrpc = RADBRPC.create(exchange=exchange, broker=broker)
     self.momrpc = MoMQueryRPC.create(exchange=exchange, broker=broker)
     self.send_bus = ToBus(exchange=exchange, broker=broker)
示例#3
0
    def __init__(self, exchange=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
        """
        RAtoOTDBPropagator updates tasks in the OTDB after the ResourceAssigner is done with them.
        :param exchange: exchange on which the services listen (default: lofar)
        :param broker: Valid Qpid broker host (default: None, which means localhost)
        """

        self.radbrpc = RADBRPC.create(exchange=exchange, broker=broker)
        self.otdbrpc = OTDBRPC.create(exchange=exchange, broker=broker)
        self.momrpc = MoMQueryRPC.create(exchange=exchange, broker=broker)
        self.translator = RAtoOTDBTranslator()
示例#4
0
 def __init__(self, exchange=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
     """
     """
     self._thread = None
     self._running = False
     self._radbrpc = RADBRPC.create(exchange=exchange, broker=broker)
     self._momrpc = MoMQueryRPC.create(exchange=exchange, broker=broker)
     self._curpc = CleanupRPC.create(exchange=exchange, broker=broker)
     self._otdbrpc = OTDBRPC.create(exchange=exchange,
                                    broker=broker,
                                    timeout=180)
示例#5
0
class TaskManagementHandler(ServiceMessageHandler):
    def handle_message(self, msg):
        pass

    def __init__(self):
        super(TaskManagementHandler, self).__init__()

        self.radb = RADBRPC()
        self.otdb = OTDBRPC()
        self.obs_ctrl = ObservationControlRPCClient()

    def AbortTask(self, otdb_id):
        """aborts tasks based on otdb id
        :param otdb_id:
        :return: dict with aborted key saying if aborting was succesful and otdb_id key
        """
        if self._is_active_observation(otdb_id):
            aborted = self._abort_active_observation(otdb_id)
        else:
            aborted = self._abort_inactive_task(otdb_id)

        return {"aborted": aborted, "otdb_id": otdb_id}

    def _is_active_observation(self, otdb_id):
        task_type, task_status = self._get_task_type_and_status(otdb_id)

        return task_type == "observation" and (task_status == "running"
                                               or task_status == "queued")

    def _abort_inactive_task(self, otdb_id):
        logger.info("Aborting inactive task: %s", otdb_id)

        try:
            self.otdb.taskSetStatus(otdb_id=otdb_id, new_status="aborted")
            aborted = True
        except OTDBPRCException:
            aborted = False
        return aborted

    def _abort_active_observation(self, otdb_id):
        logger.info("Aborting active task: %s", otdb_id)

        result = self.obs_ctrl.abort_observation(otdb_id)
        aborted = result["aborted"] is True
        return aborted

    def _get_task_type_and_status(self, otdb_id):
        task = self.radb.getTask(otdb_id)
        task_type = task["type"]
        task_status = task['status']
        return task_type, task_status
示例#6
0
    def __init__(self,
                 mountpoint=CEP4_DATA_MOUNTPOINT,
                 exchange=DEFAULT_BUSNAME,
                 broker=DEFAULT_BROKER):

        self.mountpoint = mountpoint
        self.projects_path = os.path.join(
            self.mountpoint,
            'projects' if isProductionEnvironment() else 'test-projects')
        self.scratch_path = os.path.join(self.mountpoint, 'scratch',
                                         'pipeline')
        self.share_path = os.path.join(self.mountpoint, 'share', 'pipeline')

        self.radbrpc = RADBRPC.create(exchange=exchange, broker=broker)
        self.momrpc = MoMQueryRPC.create(exchange=exchange, broker=broker)
示例#7
0
    def __init__(self,
                 radb_busname=RADB_BUSNAME,
                 radb_servicename=RADB_SERVICENAME,
                 radb_broker=None,
                 otdb_busname=DEFAULT_OTDB_SERVICE_BUSNAME,
                 otdb_servicename=DEFAULT_OTDB_SERVICENAME,
                 mom_busname=DEFAULT_MOMQUERY_BUSNAME,
                 mom_servicename=DEFAULT_MOMQUERY_SERVICENAME,
                 otdb_broker=None,
                 mom_broker=None,
                 broker=None):
        """
        RAtoOTDBPropagator updates tasks in the OTDB after the ResourceAssigner is done with them.
        :param radb_busname: busname on which the radb service listens (default: lofar.ra.command)
        :param radb_servicename: servicename of the radb service (default: RADBService)
        :param radb_broker: valid Qpid broker host (default: None, which means localhost)
        :param otdb_busname: busname on which the OTDB service listens (default: lofar.otdb.command)
        :param otdb_servicename: servicename of the OTDB service (default: OTDBService)
        :param otdb_broker: valid Qpid broker host (default: None, which means localhost)
        :param broker: if specified, overrules radb_broker and otdb_broker. Valid Qpid broker host (default: None, which means localhost)
        """
        if broker:
            radb_broker = broker
            otdb_broker = broker
            mom_broker = broker

        self.radbrpc = RADBRPC(
            busname=radb_busname,
            servicename=radb_servicename,
            broker=radb_broker
        )  ## , ForwardExceptions=True hardcoded in RPCWrapper right now
        self.otdbrpc = OTDBRPC(
            busname=otdb_busname,
            servicename=otdb_servicename,
            broker=otdb_broker
        )  ## , ForwardExceptions=True hardcoded in RPCWrapper right now
        self.momrpc = MoMQueryRPC(busname=mom_busname,
                                  servicename=mom_servicename,
                                  broker=mom_broker)
        self.translator = RAtoOTDBTranslator()
示例#8
0
    def __init__(self):
        super(TaskManagementHandler, self).__init__()

        self.radb = RADBRPC()
        self.otdb = OTDBRPC()
        self.obs_ctrl = ObservationControlRPCClient()
示例#9
0
 def init_tobus(self,
                exchange: str = DEFAULT_BUSNAME,
                broker: str = DEFAULT_BROKER):
     super().init_tobus(exchange, broker)
     self.otdb = OTDBRPC.create(exchange=exchange, broker=broker)
     self.radb = RADBRPC.create(exchange=exchange, broker=broker)
示例#10
0
    def test(self):
        '''basic test '''

        with TemporaryExchange(__name__) as tmp_exchange:

            # the system under test is the service and the rpc, not the RADatabase
            # so, patch (mock) the RADatabase class during these tests.
            # when the service instantiates an RADatabase it will get the mocked class.
            with patch('lofar.sas.resourceassignment.database.radb.RADatabase',
                       autospec=True) as MockRADatabase:
                mock = MockRADatabase.return_value
                # modify the return values of the various RADatabase methods with pre-cooked answers
                mock.getTaskStatuses.return_value = [{
                    'id': 1,
                    'name': 'opened'
                }, {
                    'id': 2,
                    'name': 'scheduled'
                }]
                mock.getTaskTypes.return_value = [{
                    'id': 0,
                    'name': 'OBSERVATION'
                }, {
                    'id': 1,
                    'name': 'PIPELINE'
                }]
                mock.getResourceClaimStatuses.return_value = [{
                    'id':
                    0,
                    'name':
                    'TENTATIVE'
                }, {
                    'id': 1,
                    'name': 'CLAIMED'
                }, {
                    'id':
                    2,
                    'name':
                    'CONFLICT'
                }]
                mock.getUnits.return_value = [{
                    'units': 'rsp_channel_bit',
                    'id': 0
                }, {
                    'units': 'bytes',
                    'id': 1
                }, {
                    'units': 'rcu_board',
                    'id': 2
                }, {
                    'units': 'bits/second',
                    'id': 3
                }, {
                    'units': 'cores',
                    'id': 4
                }]
                mock.getResourceTypes.return_value = [{
                    'unit_id': 0,
                    'id': 0,
                    'unit': 'rsp_channel_bit',
                    'name': 'rsp'
                }, {
                    'unit_id': 1,
                    'id': 1,
                    'unit': 'bytes',
                    'name': 'tbb'
                }, {
                    'unit_id': 2,
                    'id': 2,
                    'unit': 'rcu_board',
                    'name': 'rcu'
                }, {
                    'unit_id': 3,
                    'id': 3,
                    'unit': 'bits/second',
                    'name': 'bandwidth'
                }, {
                    'unit_id': 4,
                    'id': 4,
                    'unit': 'cores',
                    'name': 'processor'
                }, {
                    'unit_id': 1,
                    'id': 5,
                    'unit': 'bytes',
                    'name': 'storage'
                }]
                mock.getResourceGroupTypes.return_value = [{
                    'id': 0,
                    'name': 'instrument'
                }, {
                    'id': 1,
                    'name': 'cluster'
                }, {
                    'id':
                    2,
                    'name':
                    'station_group'
                }, {
                    'id': 3,
                    'name': 'station'
                }, {
                    'id': 4,
                    'name': 'node_group'
                }, {
                    'id': 5,
                    'name': 'node'
                }]
                mock.getResources.return_value = [{
                    'type_id': 0,
                    'type': 'rsp',
                    'id': 0,
                    'unit': 'rsp_channel_bit',
                    'name': 'rsp'
                }, {
                    'type_id': 1,
                    'type': 'tbb',
                    'id': 1,
                    'unit': 'bytes',
                    'name': 'tbb'
                }, {
                    'type_id': 2,
                    'type': 'rcu',
                    'id': 2,
                    'unit': 'rcu_board',
                    'name': 'rcu'
                }, {
                    'type_id': 3,
                    'type': 'bandwidth',
                    'id': 3,
                    'unit': 'bits/second',
                    'name': 'bandwidth'
                }]
                mock.getResourceGroups.return_value = [{
                    'type_id': 0,
                    'type': 'instrument',
                    'id': 0,
                    'name': 'LOFAR'
                }, {
                    'type_id': 1,
                    'type': 'cluster',
                    'id': 1,
                    'name': 'CEP2'
                }]
                mock.getTasks.return_value = [{
                    'status':
                    'prepared',
                    'type_id':
                    1,
                    'status_id':
                    200,
                    'specification_id':
                    1,
                    'starttime':
                    datetime.datetime(2015, 11, 30, 12, 0),
                    'mom_id':
                    -1,
                    'endtime':
                    datetime.datetime(2015, 11, 30, 15, 0),
                    'type':
                    'PIPELINE',
                    'id':
                    5,
                    'otdb_id':
                    -1
                }]
                mock.getTask.return_value = mock.getTasks.return_value[0]
                mock.getTask.side_effect = lambda x: mock.getTasks.return_value[
                    0] if x == 5 else None
                mock.getResourceClaims.return_value = [{
                    'username':
                    '******',
                    'status':
                    'CLAIMED',
                    'user_id':
                    1,
                    'task_id':
                    5,
                    'status_id':
                    1,
                    'resource_id':
                    1,
                    'session_id':
                    1,
                    'claim_size':
                    10,
                    'starttime':
                    datetime.datetime(2015, 11, 30, 12, 0),
                    'endtime':
                    datetime.datetime(2015, 11, 30, 12, 0),
                    'id':
                    5
                }]

                # create and run the service
                with createService(exchange=tmp_exchange.address):
                    with RADBRPC.create(exchange=tmp_exchange.address,
                                        timeout=1) as rpc:
                        self.assertEqual(mock.getTaskStatuses.return_value,
                                         rpc.getTaskStatuses())
                        self.assertEqual(mock.getTaskTypes.return_value,
                                         rpc.getTaskTypes())
                        self.assertEqual(
                            mock.getResourceClaimStatuses.return_value,
                            rpc.getResourceClaimStatuses())
                        self.assertEqual(mock.getUnits.return_value,
                                         rpc.getUnits())
                        self.assertEqual(mock.getResourceTypes.return_value,
                                         rpc.getResourceTypes())
                        self.assertEqual(
                            mock.getResourceGroupTypes.return_value,
                            rpc.getResourceGroupTypes())
                        self.assertEqual(mock.getResources.return_value,
                                         rpc.getResources())
                        self.assertEqual(mock.getResourceGroups.return_value,
                                         rpc.getResourceGroups())
                        self.assertEqual(mock.getTasks.return_value,
                                         rpc.getTasks())
                        self.assertEqual(mock.getResourceClaims.return_value,
                                         rpc.getResourceClaims())

                        #TODO: fix this test
                        #self.assertEqual(None, rpc.getTask(1))
                        #self.assertEqual(mock.getTask.return_value, rpc.getTask(5))

                        # test non existing service method, should raise
                        with self.assertRaises(Exception) as e:
                            rpc.execute('foo')
示例#11
0
def main():
    # make sure we run in UTC timezone
    import os
    os.environ['TZ'] = 'UTC'

    # Check the invocation arguments
    parser = OptionParser(
        '%prog [options]',
        description='run the resource assignment editor web service')
    parser.add_option(
        '--webserver_port',
        dest='webserver_port',
        type='int',
        default=7412,
        help='port number on which to host the webservice, default: %default')
    parser.add_option('-q',
                      '--broker',
                      dest='broker',
                      type='string',
                      default=DEFAULT_BROKER,
                      help='Address of the qpid broker, default: %default')
    parser.add_option(
        '--exchange',
        dest='exchange',
        type='string',
        default=DEFAULT_BUSNAME,
        help='Name of the bus exchange on the qpid broker, default: %default')
    parser.add_option('-V',
                      '--verbose',
                      dest='verbose',
                      action='store_true',
                      help='verbose logging')
    parser.add_option_group(dbcredentials.options_group(parser))
    parser.set_defaults(dbcredentials="RADB")
    (options, args) = parser.parse_args()

    logging.basicConfig(
        format='%(asctime)s %(levelname)s %(message)s',
        level=logging.DEBUG if options.verbose else logging.INFO)

    global _radb_dbcreds
    _radb_dbcreds = dbcredentials.parse_options(options)
    if _radb_dbcreds.database:
        logger.info("Using dbcreds for direct RADB access: %s" %
                    _radb_dbcreds.stringWithHiddenPassword())
    else:
        _radb_dbcreds = None

    global rarpc
    rarpc = RADBRPC.create(exchange=options.exchange, broker=options.broker)
    global otdbrpc
    otdbrpc = OTDBRPC.create(exchange=options.exchange, broker=options.broker)
    global curpc
    curpc = CleanupRPC.create(exchange=options.exchange, broker=options.broker)
    global sqrpc
    sqrpc = StorageQueryRPC.create(exchange=options.exchange,
                                   timeout=10,
                                   broker=options.broker)
    global momqueryrpc
    momqueryrpc = MoMQueryRPC.create(exchange=options.exchange,
                                     timeout=10,
                                     broker=options.broker)
    global changeshandler
    changeshandler = ChangesHandler(exchange=options.exchange,
                                    broker=options.broker,
                                    momqueryrpc=momqueryrpc,
                                    radbrpc=rarpc,
                                    sqrpc=sqrpc)

    with changeshandler, rarpc, otdbrpc, curpc, sqrpc, momqueryrpc:
        '''Start the webserver'''
        app.run(debug=options.verbose,
                threaded=True,
                host='0.0.0.0',
                port=options.webserver_port)
示例#12
0
 def __init__(self, exchange=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
     self.rarpc = RADBRPC.create(exchange=exchange, broker=broker)
     logger.info('PipelineDependencies busname=%s', exchange)
     self.otdbrpc = OTDBRPC.create(exchange=exchange, broker=broker)
示例#13
0
def main(args):
    import os
    os.environ['TZ'] = 'UTC'  # LOFAR observatory software uses UTC

    options, resource_updates, print_help_func = parseArgs(args)

    status = 0
    radb = None
    try:
        radb = RADBRPC.create(exchange=options.busname, broker=options.broker)

        db_resource_list = radb.getResources(resource_types=options.resource_type, include_availability=True)

        if options.timestart is None:
            options.timestart = datetime(1970, 1, 1)
        if options.timestop is None:
            options.timestop = datetime(2100, 1, 1)

        # Filter resource list via resource root group option
        db_resource_group_mships = radb.getResourceGroupMemberships()
        db_rgp2rgp = db_resource_group_mships['groups']  # resource-group-to-resource-group relations
        group_id = getResourceGroupIdByName(db_rgp2rgp, options.resource_group_root)
        if group_id is None:
            print_help_func()
            print("")
            logger.error("could not find resource group '{}'. You may want to (correct the) use (of) the -G/--resource-group-root option to widen the resource scope, e.g. -G CEP4|DRAGNET|LOFAR".format(options.resource_group_root))
            return 1
        resource_id_list = getSubtreeResourceIdList(db_rgp2rgp, group_id)
        if not resource_id_list:
            print_help_func()
            print("")
            logger.error("no resources found under resource group '{}' and its (grand)children".format(options.resource_group_root))
            return 1
        resources = [res for res in db_resource_list if res['id'] in resource_id_list]  # SQL could have done this better

        if options.end_storage_claims:
            try:
                storage_resource_type_id = next((res['type_id'] for res in resources))
            except StopIteration:
                print_help_func()
                print("")
                logger.error("-E/--end-past-tasks-storage-claims used, but no storage resources found under resource group '{}' and its (grand)children".format(options.resource_group_root))
                return 1

            status |= updateStorageClaimsEndTime(radb, resources, storage_resource_type_id, lower_bound=options.timestart, upper_bound=options.timestop)

        if options.update_avail:
            status |= updateAvailableStorageCapacities(radb, resources)

        if resource_updates:
            status |= updateSpecifiedCapacities(radb, resources, resource_updates)

        # If no specific action requested, print list of resources and claims
        if not options.end_storage_claims and not options.update_avail and not resource_updates:
            resource_ids = [res['id'] for res in resources]
            claims = radb.getResourceClaims(lower_bound=options.timestart, upper_bound=options.timestop,
                                            resource_ids=resource_ids, extended=True)

            # A small downside of querying RADB again is that the claimable capacities might be inconsistent with claims just retrieved.
            # We could derive it ourselves or stick it in a transaction, but this is good enough for the overview.
            for res in resources:
                res['claimable_capacity'] = radb.get_resource_claimable_capacity(resource_id=res['id'],
                                                                                 lower_bound=options.timestart,
                                                                                 upper_bound=options.timestop)

            printResources(resources, not options.no_scaled_units)
            print("")
            printClaims(claims, not options.no_scaled_units)

    #except Exception:  # disabled: prefer default stacktrace on bug here
    finally:
        if radb is not None:
            radb.close()

    return status