def test_verify_execute_mistral_v2_action_hash(self): execute_mistral_action_spec_1 = \ ActionSpecs(id='mistmistmist1', type=ActionType.EXECUTE_MISTRAL, targets={}, properties={'workflow': 'wf4', 'input': { 'prop1': 'ppp', 'prop2': 'qqq', 'prop3': 'rrr', }}) execute_mistral_action_spec_2 = \ ActionSpecs(id='mistmistmist2', type=ActionType.EXECUTE_MISTRAL, targets={}, properties={'workflow': 'wf4', 'input': { 'prop2': 'qqq', 'prop3': 'rrr', 'prop1': 'ppp', }}) self.assertEqual( ScenarioEvaluator._generate_action_id( execute_mistral_action_spec_1), ScenarioEvaluator._generate_action_id( execute_mistral_action_spec_2))
def _init_instance(self): actions_callback = VitrageNotifier(conf=self._conf, publisher_id='vitrage_evaluator', topics=[EVALUATOR_TOPIC]).notify self._evaluator = ScenarioEvaluator(self._conf, self._entity_graph, None, actions_callback, enabled=False)
def _init_instance(self): scenario_repo = ScenarioRepository(self.worker_id, self._workers_num) actions_callback = messaging.VitrageNotifier( publisher_id='vitrage_evaluator', topics=[EVALUATOR_TOPIC]).notify self._evaluator = ScenarioEvaluator(self._entity_graph, scenario_repo, actions_callback, enabled=False) self._evaluator.scenario_repo.log_enabled_scenarios()
def start(self): super(EvaluatorWorker, self).start() actions_callback = VitrageNotifier(conf=self._conf, publisher_id='vitrage_evaluator', topic=EVALUATOR_TOPIC).notify self._entity_graph.notifier._subscriptions = [] # Quick n dirty self._evaluator = ScenarioEvaluator(self._conf, self._entity_graph, self._scenario_repo, actions_callback, self._enabled) self.tg.add_thread(self._read_queue) LOG.info("EvaluatorWorkerService - Started!") self._evaluator.scenario_repo.log_enabled_scenarios()
class EvaluatorWorker(GraphCloneWorkerBase): def __init__(self, worker_id, task_queues, workers_num): super(EvaluatorWorker, self).__init__(worker_id, task_queues) self._workers_num = workers_num self._evaluator = None name = 'EvaluatorWorker' def _init_instance(self): scenario_repo = ScenarioRepository(self.worker_id, self._workers_num) actions_callback = messaging.VitrageNotifier( publisher_id='vitrage_evaluator', topics=[EVALUATOR_TOPIC]).notify self._evaluator = ScenarioEvaluator(self._entity_graph, scenario_repo, actions_callback, enabled=False) self._evaluator.scenario_repo.log_enabled_scenarios() def do_task(self, task): super(EvaluatorWorker, self).do_task(task) action = task[0] if action == START_EVALUATION: # fresh init (without snapshot) requires iterating the graph self._evaluator.run_evaluator() elif action == ENABLE_EVALUATION: # init with a snapshot does not require iterating the graph self._evaluator.enabled = True elif action == RELOAD_TEMPLATES: self._reload_templates() elif action == TEMPLATE_ACTION: (action, template_names, action_mode) = task self._template_action(template_names, action_mode) def _reload_templates(self): LOG.info("reloading evaluator scenarios") scenario_repo = ScenarioRepository(self.worker_id, self._workers_num) self._evaluator.scenario_repo = scenario_repo self._evaluator.scenario_repo.log_enabled_scenarios() def _template_action(self, template_names, action_mode): # Here, we create a temporary ScenarioRepo to execute the needed # templates. Once _reload_templates is called, it will create a # non temporary ScenarioRepo, to replace this one scenario_repo = ScenarioRepository() for s in scenario_repo._all_scenarios: s.enabled = False for template_name in template_names: if s.id.startswith(template_name): s.enabled = True self._evaluator.scenario_repo = scenario_repo self._evaluator.scenario_repo.log_enabled_scenarios() self._evaluator.run_evaluator(action_mode)
def setUpClass(cls): super(TestConsistencyFunctional, cls).setUpClass() cls.conf = cfg.ConfigOpts() cls.conf.register_opts(cls.CONSISTENCY_OPTS, group='consistency') cls.conf.register_opts(cls.PROCESSOR_OPTS, group='entity_graph') cls.conf.register_opts(cls.EVALUATOR_OPTS, group='evaluator') cls.conf.register_opts(cls.DATASOURCES_OPTS, group='datasources') cls.add_db(cls.conf) cls.load_datasources(cls.conf) cls.graph = NXGraph("Entity Graph") cls.processor = Processor(cls.conf, cls.graph) cls.event_queue = queue.Queue() def actions_callback(event_type, data): """Mock notify method Mocks vitrage.messaging.VitrageNotifier.notify(event_type, data) :param event_type: is currently always the same and is ignored :param data: """ cls.event_queue.put(data) scenario_repo = ScenarioRepository(cls.conf) cls.evaluator = ScenarioEvaluator(cls.conf, cls.processor.entity_graph, scenario_repo, actions_callback) cls.consistency_enforcer = ConsistencyEnforcer( cls.conf, cls.processor.entity_graph, actions_callback)
def setUp(self): super(TestConsistencyFunctional, self).setUp() self.conf_reregister_opts(self.CONSISTENCY_OPTS, 'consistency') self.conf_reregister_opts(self.EVALUATOR_OPTS, 'evaluator') self.add_db() self.load_datasources() self.graph = NXGraph("Entity Graph") self.processor = Processor(self.graph) self.event_queue = queue.Queue() def actions_callback(event_type, data): """Mock notify method Mocks vitrage.messaging.VitrageNotifier.notify(event_type, data) :param event_type: is currently always the same and is ignored :param data: """ self.event_queue.put(data) scenario_repo = ScenarioRepository() self.evaluator = ScenarioEvaluator(self.processor.entity_graph, scenario_repo, actions_callback) self.consistency_enforcer = ConsistencyEnforcer( self.processor.entity_graph, actions_callback)
class TemplateLoaderWorker(GraphCloneWorkerBase): def __init__(self, worker_id, conf, task_queues, e_graph): super(TemplateLoaderWorker, self).__init__(worker_id, conf, task_queues, e_graph) self._evaluator = None name = 'TemplateLoaderWorker' def _init_instance(self): actions_callback = messaging.VitrageNotifier( conf=self._conf, publisher_id='vitrage_evaluator', topics=[EVALUATOR_TOPIC]).notify self._evaluator = ScenarioEvaluator(self._conf, self._entity_graph, None, actions_callback, enabled=False) def do_task(self, task): super(TemplateLoaderWorker, self).do_task(task) action = task[0] if action == TEMPLATE_ACTION: (action, template_names, action_mode) = task self._template_action(template_names, action_mode) def _template_action(self, template_names, action_mode): self._enable_evaluator_templates(template_names) self._evaluator.run_evaluator(action_mode) self._disable_evaluator() def _enable_evaluator_templates(self, template_names): scenario_repo = ScenarioRepository(self._conf) for s in scenario_repo._all_scenarios: s.enabled = False for template_name in template_names: if s.id.startswith(template_name): s.enabled = True self._evaluator.scenario_repo = scenario_repo self._evaluator.scenario_repo.log_enabled_scenarios() self._evaluator.enabled = True def _disable_evaluator(self): self._entity_graph.notifier._subscriptions = [] # Quick n dirty self._evaluator.enabled = False
def _init_system(self): processor = self._create_processor_with_graph(self.conf) event_queue = queue.Queue() evaluator = ScenarioEvaluator(self.conf, processor.entity_graph, self.scenario_repository, event_queue, enabled=True) return event_queue, processor, evaluator
def init(): conf = service.prepare_service() event_queue = multiprocessing.Queue() e_graph = entity_graph.EntityGraph( 'Entity Graph', '%s:%s' % (EntityCategory.RESOURCE, OPENSTACK_CLUSTER)) scenario_repo = ScenarioRepository(conf) evaluator = ScenarioEvaluator(conf, e_graph, scenario_repo, event_queue) initialization_status = InitializationStatus() return conf, event_queue, evaluator, e_graph, initialization_status
def init(conf): mp_queue = multiprocessing.Queue() evaluator_q = queue.Queue() e_graph = entity_graph.EntityGraph( 'Entity Graph', '%s:%s' % (EntityCategory.RESOURCE, OPENSTACK_CLUSTER)) scenario_repo = ScenarioRepository(conf) evaluator = ScenarioEvaluator(conf, e_graph, scenario_repo, evaluator_q) return mp_queue, evaluator_q, evaluator, e_graph
class EvaluatorWorker(GraphCloneWorkerBase): def __init__(self, worker_id, conf, task_queues, e_graph, workers_num): super(EvaluatorWorker, self).__init__(worker_id, conf, task_queues, e_graph) self._workers_num = workers_num self._evaluator = None name = 'EvaluatorWorker' def _init_instance(self): scenario_repo = ScenarioRepository(self._conf, self.worker_id, self._workers_num) actions_callback = messaging.VitrageNotifier( conf=self._conf, publisher_id='vitrage_evaluator', topics=[EVALUATOR_TOPIC]).notify self._evaluator = ScenarioEvaluator(self._conf, self._entity_graph, scenario_repo, actions_callback, enabled=False) self._evaluator.scenario_repo.log_enabled_scenarios() def do_task(self, task): super(EvaluatorWorker, self).do_task(task) action = task[0] if action == START_EVALUATION: # fresh init (without snapshot) requires iterating the graph self._evaluator.run_evaluator() elif action == ENABLE_EVALUATION: # init with a snapshot does not require iterating the graph self._evaluator.enabled = True elif action == RELOAD_TEMPLATES: self._reload_templates() def _reload_templates(self): LOG.info("reloading evaluator scenarios") scenario_repo = ScenarioRepository(self._conf, self.worker_id, self._workers_num) self._evaluator.scenario_repo = scenario_repo self._evaluator.scenario_repo.log_enabled_scenarios()
def setUpClass(cls): super(TestConsistencyFunctional, cls).setUpClass() cls.initialization_status = InitializationStatus() cls.conf = cfg.ConfigOpts() cls.conf.register_opts(cls.CONSISTENCY_OPTS, group='consistency') cls.conf.register_opts(cls.PROCESSOR_OPTS, group='entity_graph') cls.conf.register_opts(cls.EVALUATOR_OPTS, group='evaluator') cls.conf.register_opts(cls.DATASOURCES_OPTS, group='datasources') cls.load_datasources(cls.conf) cls.processor = Processor(cls.conf, cls.initialization_status) cls.event_queue = queue.Queue() scenario_repo = ScenarioRepository(cls.conf) cls.evaluator = ScenarioEvaluator(cls.conf, cls.processor.entity_graph, scenario_repo, cls.event_queue) cls.consistency_enforcer = ConsistencyEnforcer( cls.conf, cls.event_queue, cls.evaluator, cls.processor.entity_graph, cls.initialization_status)
def test_deduced_state(self): # Test Setup processor = self._create_processor_with_graph(self.conf) event_queue = queue.Queue() ScenarioEvaluator(self.conf, processor.entity_graph, self.scenario_repository, event_queue, enabled=True) target_host = 'host-2' host_v = self._get_host_from_graph(target_host, processor.entity_graph) self.assertEqual('RUNNING', host_v[VProps.AGGREGATED_STATE], 'host should be RUNNING when starting') nagios_event = { 'last_check': '2016-02-07 15:26:04', 'resource_name': target_host, 'resource_type': NOVA_HOST_DATASOURCE, 'service': 'Check_MK', 'status': 'CRITICAL', 'status_info': 'ok', 'sync_mode': 'snapshot', 'sync_type': 'nagios', 'sample_date': '2016-02-07 15:26:04' } processor.process_event(nagios_event) # The set_state action should have added an event to the queue, so processor.process_event(event_queue.get()) host_v = self._get_host_from_graph(target_host, processor.entity_graph) self.assertEqual('SUBOPTIMAL', host_v[VProps.AGGREGATED_STATE], 'host should be SUBOPTIMAL after nagios alarm event') # next disable the alarm nagios_event['status'] = 'OK' processor.process_event(nagios_event) # The set_state action should have added an event to the queue, so processor.process_event(event_queue.get()) host_v = self._get_host_from_graph(target_host, processor.entity_graph) self.assertEqual('RUNNING', host_v[VProps.AGGREGATED_STATE], 'host should be RUNNING when starting')
class EvaluatorWorker(os_service.Service): def __init__(self, conf, task_queue, entity_graph, scenario_repo, enabled=False): super(EvaluatorWorker, self).__init__() self._conf = conf self._task_queue = task_queue self._entity_graph = entity_graph self._scenario_repo = scenario_repo self._enabled = enabled self._evaluator = None def start(self): super(EvaluatorWorker, self).start() actions_callback = VitrageNotifier(conf=self._conf, publisher_id='vitrage_evaluator', topic=EVALUATOR_TOPIC).notify self._entity_graph.notifier._subscriptions = [] # Quick n dirty self._evaluator = ScenarioEvaluator(self._conf, self._entity_graph, self._scenario_repo, actions_callback, self._enabled) self.tg.add_thread(self._read_queue) LOG.info("EvaluatorWorkerService - Started!") self._evaluator.scenario_repo.log_enabled_scenarios() def _read_queue(self): while True: next_task = self._task_queue.get() if next_task is POISON_PILL: self._task_queue.task_done() break try: self._do_task(next_task) except Exception as e: LOG.exception("Graph may not be in sync: exception %s", e) self._task_queue.task_done() # Evaluator queue may have been updated, thus the sleep: time.sleep(0) def _do_task(self, task): (before, current, is_vertex, action) = task if not action: self._graph_update(before, current, is_vertex) elif action == START_EVALUATION: self._evaluator.run_evaluator() def _graph_update(self, before, current, is_vertex): if current: if is_vertex: self._entity_graph.add_vertex(current) else: self._entity_graph.add_edge(current) else: if is_vertex: self._entity_graph.delete_vertex(before) else: self._entity_graph.delete_edge(before) def stop(self, graceful=False): super(EvaluatorWorker, self).stop(graceful) self.tg.stop() LOG.info("EvaluatorWorkerService - Stopped!")