def _iterate_body(self, containers, platform, tasks_data, common_labels): """Detector callback body.""" # Call Detector's detect function. detection_start = time.time() anomalies, extra_metrics = self._detector.detect(platform, tasks_data) detection_duration = time.time() - detection_start profiler.register_duration('detect', detection_duration) log.debug('Anomalies detected: %d', len(anomalies)) # Prepare anomaly metrics anomaly_metrics = convert_anomalies_to_metrics(anomalies, tasks_data) update_anomalies_metrics_with_task_information(anomaly_metrics, tasks_data) # Prepare and send all output (anomalies) metrics. anomalies_package = MetricPackage(self._anomalies_storage) anomalies_package.add_metrics( anomaly_metrics, extra_metrics, self._anomalies_statistics.get_metrics(anomalies)) anomalies_package.send(common_labels)
def _iterate_body(self, containers, platform, tasks_measurements, tasks_resources, tasks_labels, common_labels): """Allocator callback body.""" current_allocations = _get_tasks_allocations(containers) # Allocator callback allocate_start = time.time() new_allocations, anomalies, extra_metrics = self._allocator.allocate( platform, tasks_measurements, tasks_resources, tasks_labels, current_allocations) allocate_duration = time.time() - allocate_start # Validate callback output _validate_allocate_return_vals(new_allocations, anomalies, extra_metrics) log.debug('Anomalies detected: %d', len(anomalies)) log.debug('Current allocations: %s', current_allocations) # Create context aware allocations objects for current allocations. current_allocations_values = TasksAllocationsValues.create( self._rdt_enabled, current_allocations, self._containers_manager.containers, platform) # Handle allocations: calculate changeset and target allocations. allocations_changeset_values = None target_allocations_values = current_allocations_values try: # Special validation step needed for Kubernetes. validate_shares_allocation_for_kubernetes(tasks=containers.keys(), allocations=new_allocations) # Create and validate context aware allocations objects for new allocations. log.debug('New allocations: %s', new_allocations) new_allocations_values = TasksAllocationsValues.create( self._rdt_enabled, new_allocations, self._containers_manager.containers, platform) new_allocations_values.validate() # Calculate changeset and target_allocations. if new_allocations_values is not None: target_allocations_values, allocations_changeset_values = \ new_allocations_values.calculate_changeset(current_allocations_values) target_allocations_values.validate() self._allocations_counter += len(new_allocations) except InvalidAllocations as e: # Handle any allocation validation error. # Log errors and restore current to generate proper metrics. log.error('Invalid allocations: %s', str(e)) log.warning('Ignoring all allocations in this iteration due to validation error!') self._allocations_errors += 1 target_allocations_values = current_allocations_values # Handle allocations: perform allocations based on changeset. if allocations_changeset_values: log.debug('Allocations changeset: %s', allocations_changeset_values) log.info('Performing allocations on %d tasks.', len( allocations_changeset_values)) allocations_changeset_values.perform_allocations() # Prepare anomaly metrics. anomaly_metrics = convert_anomalies_to_metrics(anomalies, tasks_labels) update_anomalies_metrics_with_task_information(anomaly_metrics, tasks_labels) # Store anomalies information anomalies_package = MetricPackage(self._anomalies_storage) anomalies_package.add_metrics( anomaly_metrics, extra_metrics, self._anomalies_statistics.get_metrics(anomalies) ) anomalies_package.send(common_labels) # Prepare allocations metrics. allocations_metrics = target_allocations_values.generate_metrics() allocations_statistic_metrics = _get_allocations_statistics_metrics( self._allocations_counter, self._allocations_errors, allocate_duration) # Store allocations metrics. allocations_package = MetricPackage(self._allocations_storage) allocations_package.add_metrics( allocations_metrics, extra_metrics, allocations_statistic_metrics, ) allocations_package.send(common_labels)
def test_convert_anomalies_to_metrics(anomalies, tasks_labels, expected_metrics): metrics_got = convert_anomalies_to_metrics(anomalies, tasks_labels) assert metrics_got == expected_metrics