Ejemplo n.º 1
0
def getServiceInstance(service_instance_id):
    if service_instance_id == "all":
        LOG.debug("[lifecycle.app_funcs] [getServiceInstance] Call to 'get all' ")
        try:
            obj_response_cimi = common.ResponseCIMI()
            service_instances = data_adapter.get_all_service_instances(obj_response_cimi)

            if not service_instances is None:
                return common.gen_response_ok('Service instances content', 'service_instances', service_instances, "Msg", obj_response_cimi.msj)
            else:
                return common.gen_response(500, "Error in 'get_all' function", "Error_Msg", obj_response_cimi.msj)
        except:
            LOG.exception("[lifecycle.app_funcs] [getServiceInstance] Exception. Returning error 500 ...")
            return common.gen_response(500, 'Exception', 'get_all', "-")
    else:
        LOG.debug("[lifecycle.app_funcs] [getServiceInstance] " + service_instance_id)
        try:
            obj_response_cimi = common.ResponseCIMI()
            service_instance = data_adapter.get_service_instance(service_instance_id, obj_response_cimi)

            if not service_instance is None and service_instance != -1:
                return common.gen_response_ok('Service instance content', 'service_instance_id', service_instance_id, 'service_instance', service_instance)
            elif service_instance == -1:
                return common.gen_response_ok('Service instance not found', 'service_instance_id', service_instance_id, 'service_instance', {})
            else:
                return common.gen_response(500, "Error in 'get' function", "service_instance_id", service_instance_id, "Error_Msg", obj_response_cimi.msj)
        except:
            LOG.exception('[lifecycle.app_funcs] [getServiceInstance] Exception. Returning error 500 ...')
            return common.gen_response(500, 'Exception', 'service_instance_id', service_instance_id)
Ejemplo n.º 2
0
def terminate(service_instance_id):
    LOG.debug("[lifecycle.operations] [terminate] service_instance_id=" +
              service_instance_id)
    try:
        # 1. get service_instance object
        service_instance = data_adapter.get_service_instance(
            service_instance_id)
        if service_instance is None or service_instance == -1:
            return common.gen_response(
                500, 'Error getting service instance object',
                'service_instance_id', service_instance_id)

        # 2. updates service instance status
        service_instance['status'] = STATUS_TERMINATING
        data_adapter.update_service_instance(service_instance_id,
                                             service_instance)

        # 3.1 service instance status = Stopped => stop and terminate
        if service_instance['status'] != "Stopped":
            return operation_service(service_instance_id,
                                     OPERATION_STOP_TERMINATE)
        # 3.2 terminate
        else:
            return operation_service(service_instance_id, OPERATION_TERMINATE)
    except:
        LOG.exception('[lifecycle.operations] [terminate] Exception')
        return common.gen_response(500, 'Exception', 'service_instance_id',
                                   service_instance_id)
Ejemplo n.º 3
0
def start_job(body, service_instance_id):
    LOG.info(
        "########################################################################################"
    )
    LOG.info("######## JOBS: START JOB")
    LOG.debug("[lifecycle.operations] [start_job] body=" + str(body))
    LOG.debug("[lifecycle.operations] [start_job] service_instance_id=" +
              service_instance_id)
    try:
        # service instance
        service_instance = data_adapter.get_service_instance(
            service_instance_id)
        if service_instance is None or service_instance == -1:
            return common.gen_response(
                500, 'Error getting service instance object',
                'service_instance_id', service_instance_id)

        LOG.debug("[lifecycle.operations] [start_job] service_instance=" +
                  str(service_instance))

        # start job in agent(s)
        if len(service_instance['agents']) == 1:
            res = apps_adapter.start_job_compss(service_instance, body)
        elif len(service_instance['agents']) >= 2:
            res = apps_adapter.start_job_compss_multiple_agents(
                service_instance, body)
        else:
            LOG.warning(
                "[lifecycle.operations] [start_job] Execution supported in only 1 or more agents! agents size="
                + str(len(service_instance['agents'])))
            res = None

        if res:
            return common.gen_response_ok('Start job', 'service_id',
                                          service_instance_id, 'res', res)
        else:
            return common.gen_response(500, 'Error when starting job',
                                       'service_instance',
                                       str(service_instance))
    except:
        LOG.exception('[lifecycle.operations] [start_job] Exception')
        return common.gen_response(500, 'Exception', 'data', str(body))
Ejemplo n.º 4
0
def operation_service(service_instance_id, operation):
    LOG.debug("[lifecycle.operations] [operation_service] operation=" +
              operation + ", service_instance_id=" + service_instance_id)
    try:
        # 1. get service_instance object
        service_instance = data_adapter.get_service_instance(
            service_instance_id)
        if service_instance is None or service_instance == -1:
            return common.gen_response(
                500, 'Error getting service instance object',
                'service_instance_id', service_instance_id)

        if operation == OPERATION_STOP_TERMINATE:
            service_instance['status'] = STATUS_TERMINATING
            # execute thread
            t = threading.Thread(target=__thr_stop_terminate_service,
                                 args=(service_instance, ))
            t.start()
        else:
            # submit operation thread
            if operation == OPERATION_START:
                service_instance['status'] = STATUS_STARTING
            elif operation == OPERATION_STOP:
                service_instance['status'] = STATUS_STOPPING
            elif operation == OPERATION_TERMINATE:
                service_instance['status'] = STATUS_TERMINATING
            # execute thread
            t = threading.Thread(target=__thr_operation_service,
                                 args=(
                                     service_instance,
                                     operation,
                                 ))
            t.start()

        # response
        return common.gen_response_ok(
            "Service " + operation + " operation is being processed ...",
            "service_instance", service_instance)
    except:
        LOG.exception('[lifecycle.operations] [operation_service] Exception')
        return common.gen_response(500, 'Exception', 'service_instance_id',
                                   service_instance_id)
Ejemplo n.º 5
0
def thr(notification):
    try:
        global QoS_SERVICE_INSTANCES_LIST

        LOG.debug(
            "[lifecycle.events.handler_qos] [thr] Handling QoS notifications ["
            + str(notification) + "] ...")

        # get notification values
        service_instance_id = notification['service_instance_id']
        service_instance = data_adapter.get_service_instance(
            service_instance_id)
        service = data_adapter.get_service(service_instance['service'])
        current_num_agents = len(
            service_instance['agents'])  #service['num_agents']
        new_num_agents = notification['num_agents']
        appId = data_adapter.serv_instance_get_appid_from_master(
            service_instance)

        if not appId:
            LOG.error(
                "[lifecycle.events.handler_qos] [thr] Service instance reconfiguration: 'appId' not found in service_instance!"
            )
            LOG.error(
                "[lifecycle.events.handler_qos] [thr] service_instance=" +
                str(service_instance))
        else:
            # ADD NEW RESOURCES TO COMPSs MASTER
            if new_num_agents > current_num_agents:
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] new_num_agents > current_num_agents"
                )
                LOG.info(
                    "[lifecycle.events.handler_qos] [thr] Reconfiguring service instance: Adding more nodes to service instance (COMPSs) ..."
                )
                # Call to landscaper/recommender
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] Getting list of available agents ..."
                )
                available_agents_list = agent_decision.get_available_agents_resources(
                    service)  # ==> [{"agent_ip": "192.168.252.41"}, ...]
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] List of available agents: "
                    + str(available_agents_list))
                if len(available_agents_list) > 0:
                    LOG.debug(
                        "[lifecycle.events.handler_qos] [thr] Reconfiguring service instance: Checking available resources ..."
                    )
                    for agent in available_agents_list:
                        LOG.debug(
                            "[lifecycle.events.handler_qos] [thr]  - checking agent ["
                            + str(agent) + "]")
                        # add new resources to master if agent does not belong to current execution
                        if not data_adapter.serv_instance_is_agent_in_service_instance(
                                service_instance, agent["agent_ip"]
                        ) and new_num_agents > current_num_agents:
                            # DEPLOY
                            LOG.debug(
                                "[lifecycle.events.handler_qos] [thr] Deploying COMPSs in agent ["
                                + str(agent) + "] ...")
                            res, new_agent = __deploy_COMPSs_in_agent(
                                service, service_instance, agent["agent_ip"])
                            if res:
                                # ADD TO COMPSs
                                LOG.debug(
                                    "[lifecycle.events.handler_qos] [thr] Adding to COMPSs execution ..."
                                )
                                time.sleep(25)
                                if compss_adpt.add_resources_to_job(
                                        service_instance, appId,
                                        agent["agent_ip"], new_agent['ports']):
                                    service_instance['agents'].append(
                                        new_agent)
                                    data_adapter.update_service_instance(
                                        service_instance['id'],
                                        service_instance)
                                    current_num_agents = current_num_agents + 1
                                else:
                                    LOG.error(
                                        "[lifecycle.events.handler_qos] [thr] Reconfiguring service instance: Error adding new resources / 'appId' not found in service_instance!"
                                    )
                else:
                    LOG.error(
                        "[lifecycle.events.handler_qos] [thr] Handling QoS notifications: available_agents_list is None or is empty "
                    )
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] Waiting 60s to terminate process ..."
                )
                time.sleep(60)

            # REMOVE RESOURCES FROM COMPSs MASTER
            elif new_num_agents < current_num_agents:
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] new_num_agents < current_num_agents"
                )
                LOG.info(
                    "[lifecycle.events.handler_qos] [thr] Reconfiguring service instance: Removing nodes from service instance (COMPSs) ..."
                )
                for agent in service_instance['agents']:
                    # if agent is not the master, it can be removed
                    if not data_adapter.serv_instance_is_master(
                            agent) and new_num_agents < current_num_agents:
                        if compss_adpt.rem_resources_from_job(
                                service_instance, appId, agent["agent_ip"]):
                            current_num_agents = current_num_agents - 1
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] Waiting 60s to terminate process ..."
                )
                time.sleep(60)

            else:
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] new_num_agents = current_num_agents"
                )
                LOG.debug(
                    "[lifecycle.events.handler_qos] [thr] Waiting 10s to terminate process ..."
                )
                time.sleep(10)

        LOG.debug(
            "[lifecycle.events.handler_qos] [thr] Removing service instance id from QoS_SERVICE_INSTANCES_LIST ..."
        )
        QoS_SERVICE_INSTANCES_LIST.remove(notification['service_instance_id'])

        LOG.debug(
            "[lifecycle.events.handler_qos] [thr] QoS notifications handled")
    except:
        LOG.exception('[lifecycle.events.handler_qos] [thr] Exception')
        QoS_SERVICE_INSTANCES_LIST.remove(notification['service_instance_id'])
Ejemplo n.º 6
0
def submit_service_in_agents(service,
                             user_id,
                             service_instance_id,
                             sla_template_id,
                             agents_list,
                             check_service=False):
    if service_instance_id is not None:
        LOG.debug(
            "[lifecycle.deployment] [submit_service_in_agents] Deploying service [name="
            + service['name'] + ", service_instance_id=" +
            service_instance_id + ", user_id=" + user_id +
            ", sla_template_id=" + sla_template_id + ", agents_list=" +
            str(agents_list) + "] in agents ...")
    else:
        LOG.debug(
            "[lifecycle.deployment] [submit_service_in_agents] Deploying service [name="
            + service['name'] + ", user_id=" + user_id + ", sla_template_id=" +
            sla_template_id + ", agents_list=" + str(agents_list) +
            "] in agents ...")

    try:
        # 1. check parameters content
        if check_service and not check_service_content(service):
            return common.gen_response(
                500, 'field(s) category/exec/exec_type not found', 'service',
                str(service))

        # 2.1 FORWARD REQUEST
        if service_instance_id is not None and service_instance_id != "":
            LOG.info(
                "[lifecycle.deployment] [submit_service_in_agents] 'FORWARD REQUEST' ..."
            )
            LOG.info(
                "[lifecycle.deployment] [submit_service_in_agents] Getting service instance ["
                + service_instance_id + "] from cimi ... ")

            service_instance = data_adapter.get_service_instance(
                service_instance_id)
            LOG.info(
                "[lifecycle.deployment] [submit_service_in_agents] service_instance="
                + str(service_instance))
            # try to get the service-instance created in child agent (x3)
            if service_instance is None or service_instance == -1:
                LOG.info(
                    "[lifecycle.deployment] [submit_service_in_agents] Waiting 20s (for synchronization) to try again ..."
                )
                time.sleep(20)
                service_instance = data_adapter.get_service_instance(
                    service_instance_id)
                LOG.info(
                    "[lifecycle.deployment] [submit_service_in_agents] service_instance="
                    + str(service_instance))
                if service_instance is None or service_instance == -1:
                    LOG.info(
                        "[lifecycle.deployment] [submit_service_in_agents] Waiting 40s (for synchronization) to try again ..."
                    )
                    time.sleep(40)
                    service_instance = data_adapter.get_service_instance(
                        service_instance_id)
                    LOG.info(
                        "[lifecycle.deployment] [submit_service_in_agents] service_instance="
                        + str(service_instance))

                    if service_instance is None or service_instance == -1:
                        LOG.error(
                            "[lifecycle.deployment] [submit_service_in_agents] error getting service_instance"
                        )
                        return common.gen_response(
                            500, 'error getting service_instance', 'service',
                            str(service))

            # update service-instance's agents list
            LOG.info(
                "[lifecycle.deployment] [submit_service_in_agents] Updating service instance's agents list ... "
            )
            service_instance = data_adapter.serv_instance_replace_service_instance_agents(
                service_instance, service, user_id, sla_template_id,
                agents_list)
        # 2.2 create new service instance
        else:
            LOG.debug(
                "[lifecycle.deployment] [submit_service_in_agents] Creating service instance ... "
            )
            service_instance = data_adapter.create_service_instance(
                service, agents_list, user_id, "DEFAULT-VALUE")
            if not service_instance or 'id' not in service_instance:
                LOG.error(
                    "[lifecycle.deployment] [submit_service_in_agents] error creating service_instance"
                )
                return common.gen_response(500,
                                           'error creating service_instance',
                                           'service', str(service))

        # 3. select from agents list
        if 'num_agents' not in service or service['num_agents'] == 0:
            num_agents = -1
        else:
            num_agents = service['num_agents']

        LOG.info(
            "[lifecycle.deployment] [submit_service_in_agents] Total agents needed to run the service ['num_agents']: "
            + str(num_agents))
        LOG.info(
            "[lifecycle.deployment] [submit_service_in_agents] Selecting agents ... "
        )

        # get agent type
        if 'agent_type' not in service:
            agent_type = "normal"
        else:
            agent_type = service['agent_type']

        r, m = agent_decision.select_agents(service['exec_type'], agent_type,
                                            num_agents, service_instance)

        if m == "error" or r is None:
            LOG.error(
                "[lifecycle.deployment] [submit_service_in_agents] error when selecting agents. Forwarding to Leader..."
            )
            # forward to parent
            return __forward_submit_request_to_leader(service, user_id,
                                                      sla_template_id,
                                                      service_instance['id'])

        elif m == "not-enough-resources-found" or len(r['agents']) == 0:
            LOG.warning(
                "[lifecycle.deployment] [submit_service_in_agents] Not enough resources (number of agents) found. Forwarding to Leader..."
            )
            # forward to parent
            return __forward_submit_request_to_leader(service, user_id,
                                                      sla_template_id,
                                                      service_instance['id'])

        else:
            LOG.info(
                "######## DEPLOYMENT ###########################################################################"
            )
            service_instance = r
            LOG.debug(
                "[lifecycle.deployment] [submit_service_in_agents] service_instance: "
                + str(service_instance))

            # submit service thread
            service_instance['status'] = STATUS_DEPLOYING
            t = threading.Thread(target=thr_submit_service_in_agents,
                                 args=(
                                     service,
                                     service_instance,
                                     sla_template_id,
                                     user_id,
                                 ))
            t.start()

            return common.gen_response_ok(
                "Service deployment operation is being processed [" +
                service_instance['id'] + "]...", "service_instance",
                service_instance)
    except:
        LOG.exception(
            '[lifecycle.deployment] [submit_service_in_agents] Exception')
        return common.gen_response(500, 'Exception', 'service', str(service))