def generate_command(self, command_header): cluster_id = str(command_header['clusterId']) if cluster_id != '-1' and cluster_id != 'null': service_name = command_header['serviceName'] component_name = command_header['role'] else: cluster_id = None service_name = None component_name = None required_config_timestamp = command_header[ 'requiredConfigTimestamp'] if 'requiredConfigTimestamp' in command_header else None command_dict = self.configuration_builder.get_configuration( cluster_id, service_name, component_name, required_config_timestamp) # remove data populated from topology to avoid merge and just override if 'clusterHostInfo' in command_header: del command_dict['clusterHostInfo'] command = Utils.update_nested(Utils.get_mutable_copy(command_dict), command_header) # topology needs to be decompressed if and only if it originates from command header if 'clusterHostInfo' in command_header and command_header[ 'clusterHostInfo']: command['clusterHostInfo'] = self.decompress_cluster_host_info( command['clusterHostInfo']) return command
def __load_definitions(self): """ Loads all alert definitions from a file. All clusters are stored in a single file. This wil also populate the cluster-to-hash dictionary. :return: """ definitions = [] for cluster_id, command_json in self.alert_definitions_cache.iteritems(): clusterName = '' if not 'clusterName' in command_json else command_json['clusterName'] hostName = '' if not 'hostName' in command_json else command_json['hostName'] publicHostName = '' if not 'publicHostName' in command_json else command_json['publicHostName'] clusterHash = None if not 'hash' in command_json else command_json['hash'] # cache the cluster and cluster hash after loading the JSON if clusterName != '' and clusterHash is not None: logger.info('[AlertScheduler] Caching cluster {0} with alert hash {1}'.format(clusterName, clusterHash)) for definition in command_json['alertDefinitions']: alert = self.__json_to_callable(clusterName, hostName, publicHostName, Utils.get_mutable_copy(definition)) if alert is None: continue alert.set_helpers(self._collector, self._cluster_configuration, self.configuration_builder) definitions.append(alert) return definitions
def is_json_equal(): #json_topology = json.dumps(self.initializer_module.topology_cache, indent=2, sort_keys=True) #json_excepted_lopology = json.dumps(self.get_dict_from_file("topology_cache_expected.json"), indent=2, sort_keys=True) #print json_topology #print json_excepted_lopology self.assertEquals( Utils.get_mutable_copy(self.initializer_module.topology_cache), self.get_dict_from_file("topology_cache_expected.json"))
def is_json_equal(): #json_alert_definitions = json.dumps(self.initializer_module.alert_definitions_cache, indent=2, sort_keys=True) #json_excepted_definitions = json.dumps(self.get_dict_from_file("alert_definition_expected.json"), indent=2, sort_keys=True) #print json_definitions #print json_excepted_definitions self.assertEquals( Utils.get_mutable_copy( self.initializer_module.alert_definitions_cache), self.get_dict_from_file("alert_definition_expected.json"))
def generate_command(self, command_header): cluster_id = str(command_header['clusterId']) if cluster_id != '-1' and cluster_id != 'null': service_name = command_header['serviceName'] component_name = command_header['role'] else: cluster_id = None service_name = None component_name = None required_config_timestamp = command_header['requiredConfigTimestamp'] if 'requiredConfigTimestamp' in command_header else None command_dict = self.configuration_builder.get_configuration(cluster_id, service_name, component_name, required_config_timestamp) command = Utils.update_nested(Utils.get_mutable_copy(command_dict), command_header) return command
def update_definitions(self, event_type): """ Updates the persisted alert definitions JSON. :return: """ # prune out things we don't want to store alert_definitions = [] for cluster_id, command in self.alert_definitions_cache.iteritems(): command_copy = Utils.get_mutable_copy(command) alert_definitions.append(command_copy) if event_type == "CREATE": # reschedule all jobs, creating new instances self.reschedule_all() else: # reschedule only the jobs that have changed self.reschedule()
def _get_mutable_copy(self): with self._cache_lock: return Utils.get_mutable_copy(self)