ServiceCheckValidityCheck extends AbstractCheckDescriptor { @Override public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) throws AmbariException { ServiceConfigDAO serviceConfigDAO = serviceConfigDAOProvider.get(); HostRoleCommandDAO hostRoleCommandDAO = hostRoleCommandDAOProvider.get(); final String clusterName = request.getClusterName(); final Cluster cluster = clustersProvider.get().getCluster(clusterName); long clusterId = cluster.getClusterId(); Map<String, Long> lastServiceConfigUpdates = new HashMap<>(); for (Service service : cluster.getServices().values()) { if (service.getMaintenanceState() != MaintenanceState.OFF || !hasAtLeastOneComponentVersionAdvertised(service)) { continue; } ServiceConfigEntity lastServiceConfig = serviceConfigDAO.getLastServiceConfig(clusterId, service.getName()); lastServiceConfigUpdates.put(service.getName(), lastServiceConfig.getCreateTimestamp()); } List<HostRoleCommandEntity> commands = hostRoleCommandDAO.findAll(REQUEST, PREDICATE); Map<Role, HostRoleCommandEntity> latestTimestamps = new HashMap<>(); for (HostRoleCommandEntity command : commands) { Role role = command.getRole(); if (!latestTimestamps.containsKey(role)) { latestTimestamps.put(role, command); } } LinkedHashSet<String> failedServiceNames = new LinkedHashSet<>(); for (Map.Entry<String, Long> serviceEntry : lastServiceConfigUpdates.entrySet()) { String serviceName = serviceEntry.getKey(); Long configTimestamp = serviceEntry.getValue(); boolean serviceCheckWasExecuted = false; for (HostRoleCommandEntity command : latestTimestamps.values()) { if (command.getCommandDetail().contains(serviceName)) { serviceCheckWasExecuted = true; Long serviceCheckTimestamp = command.getStartTime(); if (serviceCheckTimestamp < configTimestamp) { failedServiceNames.add(serviceName); LOG.info("Service {} latest config change is {}, latest service check executed at {}", serviceName, DATE_FORMAT.format(new Date(configTimestamp)), DATE_FORMAT.format(new Date(serviceCheckTimestamp))); } } } if (!serviceCheckWasExecuted) { failedServiceNames.add(serviceName); LOG.info("Service {} service check has never been executed", serviceName); } } if (!failedServiceNames.isEmpty()) { prerequisiteCheck.setFailedOn(failedServiceNames); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); String failReason = getFailReason(prerequisiteCheck, request); prerequisiteCheck.setFailReason(String.format(failReason, StringUtils.join(failedServiceNames, ", "))); } } ServiceCheckValidityCheck(); @Override void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request);  }
@Test public void testFailWhenServiceWithOutdatedServiceCheckExists() throws AmbariException { ServiceComponent serviceComponent = mock(ServiceComponent.class); when(serviceComponent.isVersionAdvertised()).thenReturn(true); when(service.getMaintenanceState()).thenReturn(MaintenanceState.OFF); when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent)); ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity(); serviceConfigEntity.setServiceName(SERVICE_NAME); serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP); HostRoleCommandEntity hostRoleCommandEntity = new HostRoleCommandEntity(); hostRoleCommandEntity.setRoleCommand(RoleCommand.SERVICE_CHECK); hostRoleCommandEntity.setCommandDetail(COMMAND_DETAIL); hostRoleCommandEntity.setStartTime(SERVICE_CHECK_START_TIME); hostRoleCommandEntity.setRole(Role.HDFS_SERVICE_CHECK); when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity); when(hostRoleCommandDAO.findAll(any(Request.class), any(Predicate.class))).thenReturn(singletonList(hostRoleCommandEntity)); PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME); serviceCheckValidityCheck.perform(check, new PrereqCheckRequest(CLUSTER_NAME)); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); }
@Test public void testFailWhenServiceWithNoServiceCheckExists() throws AmbariException { ServiceComponent serviceComponent = mock(ServiceComponent.class); when(serviceComponent.isVersionAdvertised()).thenReturn(true); when(service.getMaintenanceState()).thenReturn(MaintenanceState.OFF); when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent)); ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity(); serviceConfigEntity.setServiceName(SERVICE_NAME); serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP); when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity); when(hostRoleCommandDAO.findAll(any(Request.class), any(Predicate.class))).thenReturn(Collections.<HostRoleCommandEntity>emptyList()); PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME); serviceCheckValidityCheck.perform(check, new PrereqCheckRequest(CLUSTER_NAME)); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); }
@Test public void testFailWhenServiceWithOutdatedServiceCheckExistsRepeated() throws AmbariException { ServiceComponent serviceComponent = mock(ServiceComponent.class); when(serviceComponent.isVersionAdvertised()).thenReturn(true); when(service.getMaintenanceState()).thenReturn(MaintenanceState.OFF); when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent)); ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity(); serviceConfigEntity.setServiceName(SERVICE_NAME); serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP); HostRoleCommandEntity hostRoleCommandEntity1 = new HostRoleCommandEntity(); hostRoleCommandEntity1.setRoleCommand(RoleCommand.SERVICE_CHECK); hostRoleCommandEntity1.setCommandDetail(COMMAND_DETAIL); hostRoleCommandEntity1.setStartTime(SERVICE_CHECK_START_TIME); hostRoleCommandEntity1.setRole(Role.HDFS_SERVICE_CHECK); HostRoleCommandEntity hostRoleCommandEntity2 = new HostRoleCommandEntity(); hostRoleCommandEntity2.setRoleCommand(RoleCommand.SERVICE_CHECK); hostRoleCommandEntity2.setCommandDetail(COMMAND_DETAIL); hostRoleCommandEntity2.setStartTime(CONFIG_CREATE_TIMESTAMP - 1L); hostRoleCommandEntity2.setRole(Role.HDFS_SERVICE_CHECK); when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity); when(hostRoleCommandDAO.findAll(any(Request.class), any(Predicate.class))).thenReturn(Arrays.asList(hostRoleCommandEntity1, hostRoleCommandEntity2)); PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME); serviceCheckValidityCheck.perform(check, new PrereqCheckRequest(CLUSTER_NAME)); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); }