Пример #1
0
    def resolve_location(self, ctx, arg):

        log_util.setLoggerFilter(LOG, ctx.get('keyspace'), ctx.get('plan_id'))

        error = False
        resolved_location = None

        host_name = arg.get('host_name')
        clli_code = arg.get('clli_code')

        if host_name:
            results = self.ip_ext_manager.map_method('resolve_host_location',
                                                     host_name)

        elif clli_code:
            results = self.ip_ext_manager.map_method('resolve_clli_location',
                                                     clli_code)
        else:
            results = None
            # unknown location response
            LOG.error(
                _LE("Unknown location type from the input template."
                    "Expected location types are host_name"
                    " or clli_code."))

        if results and len(results) > 0:
            resolved_location = results[0]
        else:
            error = True
        return {
            'response': {
                'resolved_location': resolved_location
            },
            'error': error
        }
Пример #2
0
    def resolve_demands(self, ctx, arg):

        log_util.setLoggerFilter(LOG, ctx.get('keyspace'), ctx.get('plan_id'))

        error = False
        demands = arg.get('demands')
        plan_info = arg.get('plan_info')
        triage_translator_data = arg.get('triage_translator_data')
        resolved_demands = None
        results = self.ip_ext_manager.map_method('resolve_demands', demands,
                                                 plan_info,
                                                 triage_translator_data)
        if results and len(results) > 0:
            resolved_demands = results[0]
            if self.triage_data_trans['plan_id'] == None:
                self.triage_data_trans['plan_name'] = triage_translator_data[
                    'plan_name']
                self.triage_data_trans['plan_id'] = triage_translator_data[
                    'plan_id']
                self.triage_data_trans['translator_triage'].append(
                    triage_translator_data['dropped_candidates'])
            elif (not self.triage_data_trans['plan_id']
                  == triage_translator_data['plan_id']):
                self.triage_data_trans = {
                    'plan_id': None,
                    'plan_name': None,
                    'translator_triage': []
                }
                self.triage_data_trans['plan_name'] = triage_translator_data[
                    'plan_name']
                self.triage_data_trans['plan_id'] = triage_translator_data[
                    'plan_id']
                self.triage_data_trans['translator_triage'].append(
                    triage_translator_data['dropped_candidates'])
            else:
                self.triage_data_trans['translator_triage'].append(
                    triage_translator_data['dropped_candidates'])
        else:
            error = True

        return {
            'response': {
                'resolved_demands': resolved_demands,
                'trans': self.triage_data_trans
            },
            'error': error
        }
Пример #3
0
    def run(self):
        """Run"""
        LOG.debug("%s" % self.__class__.__name__)
        # TODO(snarayanan): This is really meant to be a control loop
        # As long as self.running is true, we process another request.

        while self.running:

            # Delay time (Seconds) for MUSIC requests.
            time.sleep(self.conf.delay_time)

            # plans = Plan.query().all()
            # Find the first plan with a status of SOLVED.
            # Change its status to RESERVING.

            solution = None
            translation = None
            p = None
            # requests_to_reserve = dict()

            # Instead of using the query.all() method, now creating an index for 'status'
            # field in conductor.plans table, and query plans by status columns
            solved_plans = self.Plan.query.get_plan_by_col(
                "status", self.Plan.SOLVED)
            reserving_plans = self.Plan.query.get_plan_by_col(
                "status", self.Plan.RESERVING)

            # combine the plans with status = 'solved' and 'reserving' together
            plans = solved_plans + reserving_plans

            found_solved_template = False

            for p in plans:
                # when a plan is in RESERVING status more than timeout value
                if p.status == self.Plan.RESERVING and \
                (self.current_time_seconds() - self.millisec_to_sec(p.updated)) > self.conf.reservation.timeout:
                    # change the plan status to SOLVED for another VM to reserve
                    p.status = self.Plan.SOLVED
                    p.update(condition=self.reservating_status_condition)
                    break
                elif p.status == self.Plan.SOLVED:
                    solution = p.solution
                    translation = p.translation
                    found_solved_template = True
                    break

            if not solution:
                if found_solved_template:
                    message = _LE("Plan {} status is solved, yet "
                                  "the solution wasn't found").format(p.id)
                    LOG.error(message)
                    p.status = self.Plan.ERROR
                    p.message = message
                    p.update(condition=self.solved_status_condition)
                continue

            if found_solved_template and p and p.reservation_counter >= self.conf.reservation.max_reservation_counter:
                message = _LE("Tried {} times. Plan {} is unable to reserve") \
                    .format(self.conf.reservation.max_reservation_counter, p.id)
                LOG.error(message)
                p.status = self.Plan.ERROR
                p.message = message
                p.update(condition=self.solved_status_condition)
                continue

            log_util.setLoggerFilter(LOG, self.conf.keyspace, p.id)

            # update status to reserving
            p.status = self.Plan.RESERVING

            p.reservation_counter += 1
            p.reservation_owner = socket.gethostname()
            _is_updated = p.update(condition=self.solved_status_condition)

            if not _is_updated:
                continue

            if 'FAILURE' in _is_updated:
                continue

            LOG.info(
                _LI("Reservation starts, changing the template status from solved to reserving, "
                    "atomic update response from MUSIC {}").format(
                        _is_updated))
            LOG.info(
                _LI("Plan {} with request id {} is reserving by machine {}. Tried to reserve it for {} times."
                    ).format(p.id, p.name, p.reservation_owner,
                             p.reservation_counter))

            # begin reservations
            # if plan needs reservation proceed with reservation
            # else set status to done.
            reservations = None
            _is_success = "FAILURE"

            if translation:
                conductor_solver = translation.get("conductor_solver")
                if conductor_solver:
                    reservations = conductor_solver.get("reservations")
                else:
                    LOG.error("no conductor_solver in "
                              "translation for Plan {}".format(p.id))

            if reservations:

                recommendations = solution.get("recommendations")
                reservation_list = list()
                # TODO(larry) combine the two reservation logic as one, make the code service independent
                sdwan_candidate_list = list()
                service_model = reservations.get("service_model")

                for reservation, resource in reservations.get("demands",
                                                              {}).items():
                    candidates = list()
                    reservation_demand = resource.get("demands")
                    reservation_name = resource.get("name")
                    reservation_type = resource.get("type")

                    reservation_properties = resource.get("properties")
                    if reservation_properties:
                        controller = reservation_properties.get("controller")
                        request = reservation_properties.get("request")

                    for recommendation in recommendations:
                        for demand, r_resource in recommendation.items():
                            if demand == reservation_demand:
                                # get selected candidate from translation
                                selected_candidate_id = r_resource.get(
                                    "candidate").get("candidate_id")
                                demands = translation.get(
                                    "conductor_solver").get("demands")
                                for demand_name, d_resource in demands.items():
                                    if demand_name == demand:

                                        for candidate in d_resource.get(
                                                "candidates"):
                                            if candidate.get(
                                                    "candidate_id"
                                            ) == selected_candidate_id:
                                                candidate['request'] = request
                                                candidates.append(candidate)
                                                sdwan_candidate_list.append(
                                                    candidate)

                    #TODO(larry) combine the two reservation logic as one, make the code service independent
                    if service_model == "ADIOD":
                        is_success = self.try_reservation_call(
                            method="reserve",
                            candidate_list=candidates,
                            reservation_type=service_model,
                            controller=controller,
                            request=request,
                            reservation_name=None)

                        # if reservation succeed continue with next candidate
                        if is_success:
                            curr_reservation = dict()
                            curr_reservation['candidate_list'] = candidates
                            curr_reservation[
                                'reservation_name'] = reservation_name
                            curr_reservation[
                                'reservation_type'] = reservation_type
                            curr_reservation['controller'] = controller
                            curr_reservation['request'] = request
                            reservation_list.append(curr_reservation)

                        else:
                            # begin roll back of all reserved resources on
                            # the first failed reservation
                            rollback_status = \
                                self.rollback_reservation(reservation_list)

                            # order_lock spin-up rollback
                            for decision in solution.get('recommendations'):

                                candidate = list(decision.values())[0].get(
                                    'candidate'
                                )  # Python 3 Conversion -- dict object to list object
                                if candidate.get('inventory_type') == 'cloud':
                                    # TODO(larry) change the code to get('conflict_id') instead of 'location_id'
                                    conflict_id = candidate.get('conflict_id')
                                    order_record = self.OrderLock.query.get_plan_by_col(
                                        "id", conflict_id)[0]
                                    if order_record:
                                        order_record.delete()
                            # statuses
                            if rollback_status:
                                # released all reservations,
                                # move plan to translated
                                if p.reservation_counter >= self.conf.reservation.max_reservation_counter:
                                    p.status = self.Plan.ERROR
                                    p.message = _LE(
                                        "Tried {} times. Plan {} is unable to reserve"
                                    ).format(
                                        self.conf.reservation.
                                        max_reservation_counter, p.id)
                                    LOG.error(p.message)
                                else:
                                    p.status = self.Plan.TRANSLATED
                                # TODO(larry): Should be replaced by the new api from MUSIC
                                while 'FAILURE' in _is_success:
                                    _is_success = p.update(
                                        condition=self.
                                        reservation_owner_condition)
                                    LOG.info(
                                        _LI("Rolling back the template from reserving to {} status, "
                                            "atomic update response from MUSIC {}"
                                            ).format(p.status, _is_success))
                                del reservation_list[:]
                            else:
                                LOG.error("Reservation rollback failed")
                                p.status = self.Plan.ERROR
                                p.message = "Reservation release failed"
                                # TODO(larry): Should be replaced by the new api from MUSIC
                                while 'FAILURE' in _is_success:
                                    _is_success = p.update(
                                        condition=self.
                                        reservation_owner_condition)
                                    LOG.info(
                                        _LI("Rollback Failed, Changing the template status from reserving to error, "
                                            "atomic update response from MUSIC {}"
                                            ).format(_is_success))
                            break  # reservation failed

                    continue
                    # continue with reserving the next candidate

                # TODO(larry) combine the two reservation logic as one, make the code service independent
                if service_model == "DHV":
                    is_success = self.try_reservation_call(
                        method="reserve",
                        candidate_list=sdwan_candidate_list,
                        reservation_type=service_model,
                        controller=controller,
                        request=request,
                        reservation_name=None)

                    if not is_success:
                        # order_lock spin-up rollback
                        for decision in solution.get('recommendations'):

                            candidate = list(decision.values())[0].get(
                                'candidate'
                            )  # Python 3 Conversion -- dict object to list object
                            if candidate.get('inventory_type') == 'cloud':
                                conflict_id = candidate.get('conflict_id')
                                order_record = self.OrderLock.query.get_plan_by_col(
                                    "id", conflict_id)[0]
                                if order_record:
                                    order_record.delete()

                        if p.reservation_counter >= self.conf.reservation.max_reservation_counter:
                            p.status = self.Plan.ERROR
                            p.message = _LE(
                                "Tried {} times. Plan {} is unable to reserve"
                            ).format(
                                self.conf.reservation.max_reservation_counter,
                                p.id)
                            LOG.error(p.message)
                        else:
                            p.status = self.Plan.TRANSLATED

                        # TODO(larry): Should be replaced by the new api from MUSIC
                        while 'FAILURE' in _is_success:
                            _is_success = p.update(
                                condition=self.reservation_owner_condition)
                            LOG.info(
                                _LI("Rolling back the template from reserving to {} status, "
                                    "atomic update response from MUSIC {}").
                                format(p.status, _is_success))
                        del reservation_list[:]

            # verify if all candidates have been reserved
            if p.status == self.Plan.RESERVING:
                # all reservations succeeded.
                LOG.info(
                    _LI("Plan {} with request id {} Reservation complete").
                    format(p.id, p.name))
                LOG.debug("Plan {} Reservation complete".format(p.id))
                p.status = self.Plan.DONE

                while 'FAILURE' in _is_success and (
                        self.current_time_seconds() - self.millisec_to_sec(
                            p.updated)) <= self.conf.reservation.timeout:
                    _is_success = p.update(
                        condition=self.reservation_owner_condition)
                    LOG.info(
                        _LI("Reservation is complete, changing the template status from reserving to done, "
                            "atomic update response from MUSIC {}").format(
                                _is_success))
            continue
Пример #4
0
    def run(self):
        """Run"""
        LOG.debug("{}".format(self.__class__.__name__))
        # TODO(snarayanan): This is really meant to be a control loop
        # As long as self.running is true, we process another request.

        while self.running:

            # Delay time (Seconds) for MUSIC requests.
            time.sleep(self.conf.delay_time)

            # plans = Plan.query().all()
            # Find the first plan with a status of TRANSLATED.
            # Change its status to SOLVING.
            # Then, read the "translated" field as "template".
            json_template = None
            p = None

            requests_to_solve = dict()
            regions_maps = dict()
            country_groups = list()

            # Instead of using the query.all() method, now creating an index for 'status'
            # field in conductor.plans table, and query plans by status columns
            translated_plans = self.Plan.query.get_plan_by_col("status", self.Plan.TRANSLATED)
            solving_plans = self.Plan.query.get_plan_by_col("status", self.Plan.SOLVING)

            # combine the plans with status = 'translated' and 'solving' together
            plans = translated_plans + solving_plans

            found_translated_template = False

            for p in plans:
                if p.status == self.Plan.TRANSLATED:
                    json_template = p.translation
                    found_translated_template = True
                    break
                elif p.status == self.Plan.SOLVING and (self.current_time_seconds()
                                                        - self.millisec_to_sec(p.updated)) > self.conf.solver.timeout:
                    p.status = self.Plan.TRANSLATED
                    p.update(condition=self.solving_status_condition)
                    break

            if not json_template:
                if found_translated_template:
                    message = _LE("Plan {} status is translated, yet "
                                  "the translation wasn't found").format(p.id)
                    LOG.error(message)
                    p.status = self.Plan.ERROR
                    p.message = message
                    p.update(condition=self.translated_status_condition)
                continue

            if found_translated_template and p and p.solver_counter >= self.conf.solver.max_solver_counter:
                message = _LE("Tried {} times. Plan {} is unable to solve").format(self.conf.solver.max_solver_counter,
                                                                                   p.id)
                LOG.error(message)
                p.status = self.Plan.ERROR
                p.message = message
                p.update(condition=self.translated_status_condition)
                continue

            log_util.setLoggerFilter(LOG, self.conf.keyspace, p.id)

            p.status = self.Plan.SOLVING
            p.solver_counter += 1
            p.solver_owner = socket.gethostname()

            _is_updated = p.update(condition=self.translated_status_condition)
            if not _is_updated:
                continue

            # other VMs have updated the status and start solving the plan
            if 'FAILURE' in _is_updated:
                continue

            LOG.info(_LI("Sovling starts, changing the template status from translated to solving, "
                         "atomic update response from MUSIC {}").format(_is_updated))

            LOG.info(_LI("Plan {} with request id {} is solving by machine {}. Tried to solve it for {} times.").
                     format(p.id, p.name, p.solver_owner, p.solver_counter))

            _is_success = "FAILURE"
            request = parser.Parser()
            request.cei = self.cei
            request.request_id = p.name
            request.plan_id = p.id
            # getting the number of solutions need to provide
            num_solution = getattr(p, 'recommend_max', '1')
            if num_solution.isdigit():
                num_solution = int(num_solution)

            # TODO(inam/larry): move this part of logic inside of parser and don't apply it to distance_between
            try:
                # getting region placeholders from database and insert/put into regions_maps dictionary
                region_placeholders = self.RegionPlaceholders.query.all()
                for region in region_placeholders:
                    regions_maps.update(region.countries)

                # getting country groups from database and insert into the country_groups list
                customer_loc = ''
                location_list = json_template["conductor_solver"]["locations"]
                for location_id, location_info in location_list.items():
                    customer_loc = location_info['country']

                countries = self.CountryLatency.query.get_plan_by_col("country_name", customer_loc)
                LOG.info("Customer Location for Latency Reduction " + customer_loc)

                if len(countries) == 0:
                    LOG.info("country is not present is country latency table, looking for * wildcard entry")
                    countries = self.CountryLatency.query.get_plan_by_col("country_name", "*")
                if len(countries) != 0:
                    LOG.info("Found '*' wild card entry in country latency table")
                else:
                    msg = "No '*' wild card entry found in country latency table. No solution will be provided"
                    LOG.info(msg)
                    p.message = msg

                for country in countries:
                    country_groups = country.groups

                LOG.info("Done getting Latency Country DB Groups ")
            except Exception as error_msg:
                LOG.error("Exception thrown while reading region_placeholders and country groups information "
                          "from database. Exception message: {}".format(error_msg))

            try:
                request.parse_template(json_template, country_groups, regions_maps)
                request.assgin_constraints_to_demands()
                requests_to_solve[p.id] = request
                opt = optimizer.Optimizer(self.conf, _requests=requests_to_solve)
                solution_list = opt.get_solution(num_solution)

            except Exception as err:
                message = _LE("Plan {} status encountered a "
                              "parsing error: {}").format(p.id, err)
                LOG.error(traceback.print_exc())
                p.status = self.Plan.ERROR
                p.message = message
                while 'FAILURE' in _is_success:
                    _is_success = p.update(condition=self.solver_owner_condition)
                    LOG.info(_LI("Encountered a parsing error, changing the template status from solving to error, "
                                 "atomic update response from MUSIC {}").format(_is_success))

                continue

            LOG.info("Preparing the recommendations ")
            # checking if the order is 'initial' or 'speed changed' one
            is_speed_change = False
            if request and request.request_type == 'speed changed':
                is_speed_change = True

            recommendations = []
            if not solution_list or len(solution_list) < 1:
                # when order takes too much time to solve
                if (int(round(time.time())) - self.millisec_to_sec(p.updated)) > self.conf.solver.solver_timeout:
                    message = _LI("Plan {} is timed out, exceed the expected "
                                  "time {} seconds").format(p.id, self.conf.solver.timeout)

                # when no solution found
                else:
                    message = _LI("Plan {} search failed, no "
                                  "recommendations found by machine {}").format(p.id, p.solver_owner)
                LOG.info(message)
                # Update the plan status
                p.status = self.Plan.NOT_FOUND
                p.message = message

                # Metrics to Prometheus
                m_svc_name = p.template.get('parameters', {}).get('service_name', 'N/A')
                PC.VNF_FAILURE.labels('ONAP', m_svc_name).inc()

                while 'FAILURE' in _is_success:
                    _is_success = p.update(condition=self.solver_owner_condition)
                    LOG.info(_LI("Plan serach failed, changing the template status from solving to not found, "
                                 "atomic update response from MUSIC {}").format(_is_success))
            else:
                # Assemble recommendation result JSON
                for solution in solution_list:
                    current_rec = dict()
                    for demand_name in solution:
                        resource = solution[demand_name]

                        if not is_speed_change:
                            is_rehome = "false"
                        else:
                            is_rehome = "false" if resource.get("existing_placement") == 'true' else "true"

                        location_id = "" if resource.get("cloud_region_version") == '2.5' \
                                      else resource.get("location_id")

                        rec = {
                            # FIXME(shankar) A&AI must not be hardcoded here.
                            # Also, account for more than one Inventory Provider.
                            "inventory_provider": "aai",
                            "service_resource_id":
                                resource.get("service_resource_id"),
                            "candidate": {
                                "candidate_id": resource.get("candidate_id"),
                                "inventory_type": resource.get("inventory_type"),
                                "cloud_owner": resource.get("cloud_owner"),
                                "location_type": resource.get("location_type"),
                                "location_id": location_id,
                                "is_rehome": is_rehome},
                            "attributes": {
                                "physical-location-id":
                                    resource.get("physical_location_id"),
                                "cloud_owner": resource.get("cloud_owner"),
                                'aic_version': resource.get("cloud_region_version")},
                        }

                        if rec["candidate"]["inventory_type"] in ["nssi", "nsi", "slice_profiles"]:
                            rec["candidate"] = resource

                        if resource.get('vim-id'):
                            rec["candidate"]['vim-id'] = resource.get('vim-id')

                        if rec["candidate"]["inventory_type"] == "service":
                            rec["attributes"]["host_id"] = resource.get("host_id")
                            rec["attributes"]["service_instance_id"] = resource.get("candidate_id")
                            rec["candidate"]["host_id"] = resource.get("host_id")

                            if resource.get('vlan_key'):
                                rec["attributes"]['vlan_key'] = resource.get('vlan_key')
                            if resource.get('port_key'):
                                rec["attributes"]['port_key'] = resource.get('port_key')

                        if rec["candidate"]["inventory_type"] == "vfmodule":
                            rec["attributes"]["host_id"] = resource.get("host_id")
                            rec["attributes"]["service_instance_id"] = resource.get("service_instance_id")
                            rec["candidate"]["host_id"] = resource.get("host_id")

                            if resource.get('vlan_key'):
                                rec["attributes"]['vlan_key'] = resource.get('vlan_key')
                            if resource.get('port_key'):
                                rec["attributes"]['port_key'] = resource.get('port_key')

                            vf_module_data = rec["attributes"]
                            vf_module_data['nf-name'] = resource.get("nf-name")
                            vf_module_data['nf-id'] = resource.get("nf-id")
                            vf_module_data['nf-type'] = resource.get("nf-type")
                            vf_module_data['vnf-type'] = resource.get("vnf-type")
                            vf_module_data['vf-module-id'] = resource.get("vf-module-id")
                            vf_module_data['vf-module-name'] = resource.get("vf-module-name")
                            vf_module_data['ipv4-oam-address'] = resource.get("ipv4-oam-address")
                            vf_module_data['ipv6-oam-address'] = resource.get("ipv6-oam-address")
                            vf_module_data['vservers'] = resource.get("vservers")

                        elif rec["candidate"]["inventory_type"] == "cloud":
                            if resource.get("all_directives") and resource.get("flavor_map"):
                                rec["attributes"]["directives"] = \
                                    self.set_flavor_in_flavor_directives(
                                        resource.get("flavor_map"), resource.get("all_directives"))

                                # Metrics to Prometheus
                                m_vim_id = resource.get("vim-id")
                                m_hpa_score = resource.get("hpa_score", 0)
                                m_svc_name = p.template['parameters'].get(
                                    'service_name', 'N/A')
                                for vnfc, flavor in resource.get("flavor_map").items():
                                    PC.VNF_COMPUTE_PROFILES.labels('ONAP',
                                                                   m_svc_name,
                                                                   demand_name,
                                                                   vnfc,
                                                                   flavor,
                                                                   m_vim_id).inc()

                                PC.VNF_SCORE.labels('ONAP', m_svc_name,
                                                    demand_name,
                                                    m_hpa_score).inc()

                            if resource.get('conflict_id'):
                                rec["candidate"]["conflict_id"] = resource.get("conflict_id")

                        if resource.get('passthrough_attributes'):
                            for key, value in resource.get('passthrough_attributes').items():
                                if key in rec["attributes"]:
                                    LOG.error('Passthrough attribute {} in demand {} already exist for candidate {}'.
                                              format(key, demand_name, rec['candidate_id']))
                                else:
                                    rec["attributes"][key] = value
                        # TODO(snarayanan): Add total value to recommendations?
                        # msg = "--- total value of decision = {}"
                        # LOG.debug(msg.format(_best_path.total_value))
                        # msg = "--- total cost of decision = {}"
                        # LOG.debug(msg.format(_best_path.total_cost))
                        current_rec[demand_name] = rec

                    recommendations.append(current_rec)

                # Update the plan with the solution
                p.solution = {
                    "recommendations": recommendations
                }

                # multiple spin-ups logic
                '''
                go through list of recommendations in the solution
                for cloud candidates, check if (cloud-region-id + e2evnfkey) is in the order_locks table
                if so, insert the row with status 'parked' in order_locks, changes plan status to 'pending' in plans
                table (or other status value)
                otherwise, insert the row with status 'locked' in order_locks, and change status to 'solved' in plans
                table - continue reservation
                '''

                # clean up the data/record in order_locks table, deleting all records that failed from MSO
                order_locks = self.OrderLock.query.all()
                for order_lock_record in order_locks:

                    plans = getattr(order_lock_record, 'plans')
                    for plan_id, plan_attributes in plans.items():
                        plan_dict = json.loads(plan_attributes)

                        if plan_dict.get('status', None) == OrderLock.FAILED:
                            order_lock_record.delete()
                            LOG.info(_LI("The order lock record {} with status {} is deleted (due to failure"
                                         " spinup from MSO) from order_locks table").
                                     format(order_lock_record, plan_dict.get('status')))
                            break

                inserted_order_records_dict = dict()
                available_dependenies_set = set()

                is_inserted_to_order_locks = True
                is_conflict_id_missing = False
                is_order_translated_before_spinup = False

                for solution in solution_list:

                    for demand_name, candidate in solution.items():
                        if candidate.get('inventory_type') == 'cloud':
                            conflict_id = candidate.get('conflict_id')
                            service_resource_id = candidate.get('service_resource_id')
                            # TODO(larry): add more logic for missing conflict_id in template
                            if not conflict_id:
                                is_conflict_id_missing = True
                                break

                            available_dependenies_set.add(conflict_id)
                            # check if conflict_id exists in order_locks table
                            order_lock_record = self.OrderLock.query.get_plan_by_col("id", conflict_id)
                            if order_lock_record:
                                is_spinup_completed = getattr(order_lock_record[0], 'is_spinup_completed')
                                spinup_completed_timestamp = getattr(order_lock_record[0],
                                                                     'spinup_completed_timestamp')
                                if is_spinup_completed and spinup_completed_timestamp > p.translation_begin_timestamp:
                                    is_order_translated_before_spinup = True
                                    break
                                elif not is_spinup_completed:
                                    inserted_order_records_dict[conflict_id] = service_resource_id

                if is_conflict_id_missing:
                    message = _LE("Missing conflict identifier field for cloud candidates in the template, "
                                  "could not insert into order_locks table")
                    LOG.debug(message)
                    p.status = self.Plan.SOLVED

                elif is_order_translated_before_spinup:
                    message = _LE("Retriggering Plan {} due to the new order arrives before the "
                                  "spinup completion of the old order ").format(p.id)
                    LOG.debug(message)
                    p.rehome_plan()

                elif len(inserted_order_records_dict) > 0:

                    new_dependenies_set = available_dependenies_set - set(inserted_order_records_dict.keys())
                    dependencies = ','.join(str(s) for s in new_dependenies_set)

                    for conflict_id, service_resource_id in inserted_order_records_dict.items():
                        plan = {
                            p.id: {
                                "status": OrderLock.UNDER_SPIN_UP,
                                "created": self.current_time_millis(),
                                "updated": self.current_time_millis(),
                                "service_resource_id": service_resource_id
                            }
                        }

                        if dependencies:
                            plan[p.id]['dependencies'] = dependencies

                        order_lock_row = self.OrderLock(id=conflict_id, plans=plan)
                        response = order_lock_row.insert()

                        # TODO(larry): add more logs for inserting order lock record (insert/update)
                        LOG.info(_LI("Inserting the order lock record to order_locks table in MUSIC, "
                                     "conditional insert operation response from MUSIC {}").format(response))
                        if response and response.status_code == 200:
                            body = response.json()
                            LOG.info("Succcessfully inserted the record in order_locks table with "
                                     "the following response message {}".format(body))
                        else:
                            is_inserted_to_order_locks = False
                else:
                    for solution in solution_list:
                        for demand_name, candidate in solution.items():
                            if candidate.get('inventory_type') == 'cloud':
                                conflict_id = candidate.get('conflict_id')
                                service_resource_id = candidate.get('service_resource_id')

                                order_lock_record = self.OrderLock.query.get_plan_by_col("id", conflict_id)
                                if order_lock_record:
                                    deleting_record = order_lock_record[0]
                                    plans = getattr(deleting_record, 'plans')
                                    is_spinup_completed = getattr(deleting_record, 'is_spinup_completed')
                                    spinup_completed_timestamp = getattr(deleting_record, 'spinup_completed_timestamp')

                                    if is_spinup_completed:
                                        # persist the record in order_locks_history table
                                        order_lock_history_record = \
                                            self.OrderLockHistory(conflict_id=conflict_id, plans=plans,
                                                                  is_spinup_completed=is_spinup_completed,
                                                                  spinup_completed_timestamp=spinup_completed_timestamp
                                                                  )
                                        LOG.debug("Inserting the history record with conflict id {}"
                                                  " to order_locks_history table".format(conflict_id))
                                        order_lock_history_record.insert()
                                        # remove the older record
                                        LOG.debug("Deleting the order lock record {} from order_locks table"
                                                  .format(deleting_record))
                                        deleting_record.delete()

                                plan = {
                                    p.id: {
                                        "status": OrderLock.UNDER_SPIN_UP,
                                        "created": self.current_time_millis(),
                                        "updated": self.current_time_millis(),
                                        "service_resource_id": service_resource_id
                                    }
                                }
                                order_lock_row = self.OrderLock(id=conflict_id, plans=plan)
                                response = order_lock_row.insert()
                                # TODO(larry): add more logs for inserting order lock record (insert/update)
                                LOG.info(_LI("Inserting the order lock record to order_locks table in MUSIC, "
                                             "conditional insert operation response from MUSIC {}").format(response))
                                if response and response.status_code == 200:
                                    body = response.json()
                                    LOG.info("Succcessfully inserted the record in order_locks table "
                                             "with the following response message {}".format(body))
                                else:
                                    is_inserted_to_order_locks = False

                if not is_inserted_to_order_locks:
                    message = _LE("Plan {} status encountered an "
                                  "error while inserting order lock message to MUSIC.").format(p.id)
                    LOG.error(message)
                    p.status = self.Plan.ERROR
                    p.message = message

                elif p.status == self.Plan.SOLVING:
                    if len(inserted_order_records_dict) > 0:
                        LOG.info(_LI("The plan with id {} is parked in order_locks table,"
                                     "waiting for MSO release calls").format(p.id))
                        p.status = self.Plan.WAITING_SPINUP
                    else:
                        LOG.info(_LI("The plan with id {} is inserted in order_locks table.").
                                 format(p.id))
                        p.status = self.Plan.SOLVED

            while 'FAILURE' in _is_success \
                  and (self.current_time_seconds() - self.millisec_to_sec(p.updated)) <= self.conf.solver.timeout:
                _is_success = p.update(condition=self.solver_owner_condition)
                LOG.info(_LI("Plan search complete, changing the template status from solving to {}, "
                             "atomic update response from MUSIC {}").format(p.status, _is_success))

            LOG.info(_LI("Plan {} search complete, {} solution(s) found by machine {}").
                     format(p.id, len(solution_list), p.solver_owner))
            LOG.debug("Plan {} detailed solution: {}".
                      format(p.id, p.solution))
            LOG.info("Plan name: {}".format(p.name))
Пример #5
0
    def __check_for_templates(self):
        """Wait for the polling interval, then do the real template check."""

        # Wait for at least poll_interval sec
        polling_interval = self.conf.controller.polling_interval
        time.sleep(polling_interval)
        # Look for plans with the status set to TEMPLATE

        # Instead of using the query.all() method, now creating an index for 'status'
        # field in conductor.plans table, and query plans by status columns
        template_plans = self.Plan.query.get_plan_by_col(
            "status", self.Plan.TEMPLATE)
        translating_plans = self.Plan.query.get_plan_by_col(
            "status", self.Plan.TRANSLATING)

        # combine the plans with status = 'template' and 'translating' together
        plans = template_plans + translating_plans

        for plan in plans:
            # If there's a template to be translated, do it!
            if plan.status == self.Plan.TEMPLATE:
                if plan.translation_counter >= self.conf.controller.max_translation_counter:
                    message = _LE("Tried {} times. Plan {} is unable to translate") \
                        .format(self.conf.controller.max_translation_counter, plan.id)
                    plan.message = message
                    plan.status = self.Plan.ERROR
                    plan.update(condition=self.template_status_condition)
                    LOG.error(message)
                    break
                else:
                    # change the plan status to "translating" and assign the current machine as translation owner
                    plan.status = self.Plan.TRANSLATING
                    plan.translation_counter += 1
                    plan.translation_owner = socket.gethostname()
                    plan.translation_begin_timestamp = int(
                        round(time.time() * 1000))
                    _is_updated = plan.update(
                        condition=self.template_status_condition)
                    log_util.setLoggerFilter(LOG, self.conf.keyspace, plan.id)
                    LOG.info(
                        _LE("Plan {} is trying to update the status from 'template' to 'translating',"
                            " get {} response from MUSIC").format(
                                plan.id, _is_updated))
                    if not _is_updated:
                        break

                    if _is_updated and 'SUCCESS' in _is_updated:
                        self.translate(plan)
                break

            # TODO(larry): sychronized clock among Conducotr VMs, or use an offset
            elif plan.status == self.Plan.TRANSLATING and (self.current_time_seconds()
                                                           - self.millisec_to_sec(plan.updated)) \
                    > self.conf.messaging_server.timeout:
                plan.status = self.Plan.TEMPLATE
                plan.update(condition=self.translating_status_condition)
                break

            elif plan.timedout:
                # TODO(jdandrea): How to tell all involved to stop working?
                # Not enough to just set status.
                continue