AmbariMetaInfo { public void reconcileAlertDefinitions(Clusters clusters) throws AmbariException { Map<String, Cluster> clusterMap = clusters.getClusters(); if (null == clusterMap || clusterMap.size() == 0) { return; } for (Cluster cluster : clusterMap.values()) { long clusterId = cluster.getClusterId(); StackId stackId = cluster.getDesiredStackVersion(); StackInfo stackInfo = getStack(stackId.getStackName(), stackId.getStackVersion()); Collection<ServiceInfo> stackServices = stackInfo.getServices(); Map<String, ServiceInfo> stackServiceMap = new HashMap<String, ServiceInfo>(); Map<String, ComponentInfo> stackComponentMap = new HashMap<String, ComponentInfo>(); for (ServiceInfo stackService : stackServices) { stackServiceMap.put(stackService.getName(), stackService); List<ComponentInfo> components = stackService.getComponents(); for (ComponentInfo component : components) { stackComponentMap.put(component.getName(), component); } } Map<String, Service> clusterServiceMap = cluster.getServices(); Set<String> clusterServiceNames = clusterServiceMap.keySet(); List<AlertDefinition> stackDefinitions = new ArrayList<AlertDefinition>(50); for (String clusterServiceName : clusterServiceNames) { ServiceInfo stackService = stackServiceMap.get(clusterServiceName); if (null == stackService) { continue; } Set<AlertDefinition> serviceDefinitions = getAlertDefinitions(stackService); stackDefinitions.addAll(serviceDefinitions); } List<AlertDefinitionEntity> persist = new ArrayList<AlertDefinitionEntity>(); List<AlertDefinitionEntity> entities = alertDefinitionDao.findAll(clusterId); Map<String, AlertDefinitionEntity> mappedEntities = new HashMap<String, AlertDefinitionEntity>(100); for (AlertDefinitionEntity entity : entities) { mappedEntities.put(entity.getDefinitionName(), entity); } for( AlertDefinition stackDefinition : stackDefinitions ){ AlertDefinitionEntity entity = mappedEntities.get(stackDefinition.getName()); if (null == entity) { entity = alertDefinitionFactory.coerce(clusterId, stackDefinition); persist.add(entity); continue; } AlertDefinition databaseDefinition = alertDefinitionFactory.coerce(entity); if (!stackDefinition.deeplyEquals(databaseDefinition)) { LOG.debug( "The alert named {} has been modified from the stack definition and will not be merged", stackDefinition.getName()); } } List<AlertDefinition> agentDefinitions = ambariServiceAlertDefinitions.getAgentDefinitions(); for (AlertDefinition agentDefinition : agentDefinitions) { AlertDefinitionEntity entity = mappedEntities.get(agentDefinition.getName()); if (null == entity) { entity = alertDefinitionFactory.coerce(clusterId, agentDefinition); persist.add(entity); } } List<AlertDefinition> serverDefinitions = ambariServiceAlertDefinitions.getServerDefinitions(); for (AlertDefinition serverDefinition : serverDefinitions) { AlertDefinitionEntity entity = mappedEntities.get(serverDefinition.getName()); if (null == entity) { entity = alertDefinitionFactory.coerce(clusterId, serverDefinition); persist.add(entity); } } for (AlertDefinitionEntity entity : persist) { if (LOG.isDebugEnabled()) { LOG.info("Merging Alert Definition {} into the database", entity.getDefinitionName()); } alertDefinitionDao.createOrUpdate(entity); } for (AlertDefinitionEntity def : alertDefinitionDao.findAll(cluster.getClusterId())) { AlertDefinition realDef = alertDefinitionFactory.coerce(def); AlertDefinitionRegistrationEvent event = new AlertDefinitionRegistrationEvent( cluster.getClusterId(), realDef); eventPublisher.publish(event); } List<AlertDefinitionEntity> definitions = alertDefinitionDao.findAllEnabled(clusterId); List<AlertDefinitionEntity> definitionsToDisable = new ArrayList<AlertDefinitionEntity>(); for (AlertDefinitionEntity definition : definitions) { String serviceName = definition.getServiceName(); String componentName = definition.getComponentName(); if (Services.AMBARI.name().equals(serviceName)) { continue; } if (!stackServiceMap.containsKey(serviceName)) { LOG.info( "The {} service has been marked as deleted for stack {}, disabling alert {}", serviceName, stackId, definition.getDefinitionName()); definitionsToDisable.add(definition); } else if (null != componentName && !stackComponentMap.containsKey(componentName)) { LOG.info( "The {} component {} has been marked as deleted for stack {}, disabling alert {}", serviceName, componentName, stackId, definition.getDefinitionName()); definitionsToDisable.add(definition); } } for (AlertDefinitionEntity definition : definitionsToDisable) { definition.setEnabled(false); alertDefinitionDao.merge(definition); AlertDefinitionDisabledEvent event = new AlertDefinitionDisabledEvent( clusterId, definition.getDefinitionId()); eventPublisher.publish(event); } } } @Inject  AmbariMetaInfo(Configuration conf); @Inject void init(); StackManager getStackManager(); List<ComponentInfo> getComponentsByService(String stackName, String version, String serviceName); ComponentInfo getComponent(String stackName, String version, String serviceName,
                                    String componentName); List<DependencyInfo> getComponentDependencies(String stackName, String version,
                                                       String service, String component); DependencyInfo getComponentDependency(String stackName, String version, String service,
                                               String component, String dependencyName); Map<String, List<RepositoryInfo>> getRepository(String stackName,
                                                         String version); List<RepositoryInfo> getRepositories(String stackName,
                                              String version, String osType); RepositoryInfo getRepository(String stackName,
                                      String version, String osType, String repoId); boolean isSupportedStack(String stackName, String version); boolean isValidService(String stackName, String version, String serviceName); boolean isValidServiceComponent(String stackName, String version,
                                         String serviceName, String componentName); String getComponentToService(String stackName, String version,
                                      String componentName); Map<String, ServiceInfo> getServices(String stackName, String version); ServiceInfo getService(String stackName, String version, String serviceName); boolean isServiceRemovedInStack(String stackName, String version, String serviceName); Collection<String> getMonitoringServiceNames(String stackName, String version); Set<String> getRestartRequiredServicesNames(String stackName, String version); Set<String> getRackSensitiveServicesNames(String stackName, String version); Collection<StackInfo> getStacks(); Collection<StackInfo> getStacks(String stackName); StackInfo getStack(String stackName, String version); List<String> getStackParentVersions(String stackName, String version); Collection<ExtensionInfo> getExtensions(); Collection<ExtensionInfo> getExtensions(String extensionName); ExtensionInfo getExtension(String extensionName, String version); Set<PropertyInfo> getServiceProperties(String stackName, String version, String serviceName); Set<PropertyInfo> getStackProperties(String stackName, String version); Set<PropertyInfo> getPropertiesByName(String stackName, String version, String serviceName, String propertyName); Set<PropertyInfo> getStackPropertiesByName(String stackName, String version, String propertyName); Set<OperatingSystemInfo> getOperatingSystems(String stackName, String version); OperatingSystemInfo getOperatingSystem(String stackName, String version, String osType); List<ActionDefinition> getAllActionDefinition(); ActionDefinition getActionDefinition(String name); void addActionDefinition(ActionDefinition ad); String getServerVersion(); boolean isOsSupported(String osType); static String generateRepoMetaKey(String stackName, String stackVersion,
      String osType, String repoId, String field); void updateRepo(String stackName,
                         String stackVersion, String osType, String repoId, String newBaseUrl, String newMirrorsList); File getStackRoot(); File getExtensionsRoot(); Map<String, Map<String, List<MetricDefinition>>> getServiceMetrics(String stackName,
            String stackVersion, String serviceName); List<MetricDefinition> getMetrics(String stackName, String stackVersion,
      String serviceName, String componentName, String metricType); Set<AlertDefinition> getAlertDefinitions(String stackName, String stackVersion,
                                                  String serviceName); Set<AlertDefinition> getAlertDefinitions(ServiceInfo service); void reconcileAlertDefinitions(Clusters clusters); Map<String, UpgradePack> getUpgradePacks(String stackName, String stackVersion); ConfigUpgradePack getConfigUpgradePack(String stackName, String stackVersion); KerberosDescriptor getKerberosDescriptor(String stackName, String stackVersion); KerberosServiceDescriptor[] getKerberosDescriptor(ServiceInfo serviceInfo); Map<String, String> getAmbariServerProperties(); VersionDefinitionXml getVersionDefinition(String versionDefinitionId); Map<String, VersionDefinitionXml> getVersionDefinitions(); static final String SERVICE_CONFIG_FOLDER_NAME; static final String SERVICE_PROPERTIES_FOLDER_NAME; static final String SERVICE_THEMES_FOLDER_NAME; static final String SERVICE_QUICKLINKS_CONFIGURATIONS_FOLDER_NAME; static final String SERVICE_CONFIG_FILE_NAME_POSTFIX; static final String RCO_FILE_NAME; static final String SERVICE_METRIC_FILE_NAME; static final String SERVICE_ALERT_FILE_NAME; static final String SERVICE_ADVISOR_FILE_NAME; static final String KERBEROS_DESCRIPTOR_FILE_NAME; static final String WIDGETS_DESCRIPTOR_FILE_NAME; static final String SERVICE_THEME_FILE_NAME; static final String ANY_OS; static final String SCHEMA_VERSION_2; static final FilenameFilter FILENAME_FILTER; static final String REPOSITORY_XML_PROPERTY_BASEURL; static final String REPOSITORY_XML_PROPERTY_MIRRORSLIST; }
@Test public void testAlertDefinitionMerging() throws Exception { Injector injector = Guice.createInjector(Modules.override( new InMemoryDefaultTestModule()).with(new MockModule())); EventBusSynchronizer.synchronizeAmbariEventPublisher(injector); injector.getInstance(GuiceJpaInitializer.class); injector.getInstance(EntityManager.class); long clusterId = injector.getInstance(OrmTestHelper.class).createCluster( "cluster" + System.currentTimeMillis()); Class<?> c = metaInfo.getClass().getSuperclass(); Field f = c.getDeclaredField("alertDefinitionDao"); f.setAccessible(true); f.set(metaInfo, injector.getInstance(AlertDefinitionDAO.class)); f = c.getDeclaredField("ambariServiceAlertDefinitions"); f.setAccessible(true); f.set(metaInfo, injector.getInstance(AmbariServiceAlertDefinitions.class)); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = clusters.getClusterById(clusterId); cluster.setDesiredStackVersion( new StackId(STACK_NAME_HDP, "2.0.6")); cluster.addService("HDFS"); metaInfo.reconcileAlertDefinitions(clusters); AlertDefinitionDAO dao = injector.getInstance(AlertDefinitionDAO.class); List<AlertDefinitionEntity> definitions = dao.findAll(clusterId); assertEquals(11, definitions.size()); int hostAlertCount = 0; for (AlertDefinitionEntity definition : definitions) { if (definition.getServiceName().equals("AMBARI") && definition.getComponentName().equals("AMBARI_AGENT")) { hostAlertCount++; } } assertEquals(2, hostAlertCount); assertEquals(9, definitions.size() - hostAlertCount); for (AlertDefinitionEntity definition : definitions) { definition.setScheduleInterval(28); dao.merge(definition); } metaInfo.reconcileAlertDefinitions(clusters); definitions = dao.findAll(); assertEquals(11, definitions.size()); for (AlertDefinitionEntity definition : definitions) { assertEquals(28, definition.getScheduleInterval().intValue()); } definitions = dao.findAllEnabled(cluster.getClusterId()); assertEquals(10, definitions.size()); AlertDefinitionEntity entity = new AlertDefinitionEntity(); entity.setClusterId(clusterId); entity.setDefinitionName("bad_hdfs_alert"); entity.setLabel("Bad HDFS Alert"); entity.setDescription("A way to fake a component being removed"); entity.setEnabled(true); entity.setHash(UUID.randomUUID().toString()); entity.setScheduleInterval(1); entity.setServiceName("HDFS"); entity.setComponentName("BAD_COMPONENT"); entity.setSourceType(SourceType.METRIC); entity.setSource("{\"type\" : \"METRIC\"}"); dao.create(entity); definitions = dao.findAllEnabled(cluster.getClusterId()); assertEquals(11, definitions.size()); metaInfo.reconcileAlertDefinitions(clusters); definitions = dao.findAllEnabled(cluster.getClusterId()); assertEquals(10, definitions.size()); definitions = dao.findAll(); assertEquals(12, definitions.size()); entity = dao.findById(entity.getDefinitionId()); assertFalse(entity.getEnabled()); }