AutoSkipFailedSummaryAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { HostRoleCommand hostRoleCommand = getHostRoleCommand(); long requestId = hostRoleCommand.getRequestId(); long stageId = hostRoleCommand.getStageId(); String clusterName = hostRoleCommand.getExecutionCommandWrapper().getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); StackId stackId = cluster.getDesiredStackVersion(); UpgradeItemEntity upgradeItem = m_upgradeDAO.findUpgradeItemByRequestAndStage(requestId,stageId); UpgradeGroupEntity upgradeGroup = upgradeItem.getGroupEntity(); long upgradeGroupId = upgradeGroup.getId(); UpgradeGroupEntity upgradeGroupEntity = m_upgradeDAO.findUpgradeGroup(upgradeGroupId); List<UpgradeItemEntity> groupUpgradeItems = upgradeGroupEntity.getItems(); TreeSet<Long> stageIds = new TreeSet<>(); for (UpgradeItemEntity groupUpgradeItem : groupUpgradeItems) { stageIds.add(groupUpgradeItem.getStageId()); } long minStageId = stageIds.first(); long maxStageId = stageIds.last(); List<HostRoleCommandEntity> skippedTasks = m_hostRoleCommandDAO.findByStatusBetweenStages( hostRoleCommand.getRequestId(), HostRoleStatus.SKIPPED_FAILED, minStageId, maxStageId); if (skippedTasks.isEmpty()) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", "There were no skipped failures", null); } StringBuilder buffer = new StringBuilder("The following steps failed but were automatically skipped:\n"); Set<String> skippedCategories = new HashSet<>(); Map<String, Object> skippedFailures = new HashMap<>(); Set<String> skippedServiceChecks = new HashSet<>(); Map<String, Object> hostComponents= new HashMap<>(); Map<String, Set<Role>> publishedHostComponents= new HashMap<>(); for (HostRoleCommandEntity skippedTask : skippedTasks) { try { String skippedCategory; if (skippedTask.getRoleCommand().equals(RoleCommand.SERVICE_CHECK)) { skippedCategory = SKIPPED_SERVICE_CHECK; String serviceCheckActionName = skippedTask.getRole().toString(); String service = actionMetadata.getServiceNameByServiceCheckAction(serviceCheckActionName); skippedServiceChecks.add(service); skippedFailures.put(SKIPPED_SERVICE_CHECK, skippedServiceChecks); m_structuredFailures.put(FAILURES, skippedFailures); } else { skippedCategory = SKIPPED_HOST_COMPONENT; String hostName = skippedTask.getHostName(); if (null != hostName) { List<Object> failures = (List<Object>) hostComponents.get(hostName); if (null == failures) { failures = new ArrayList<>(); hostComponents.put(hostName, failures); publishedHostComponents.put(hostName, new HashSet<Role>()); } Set<Role> publishedHostComponentsOnHost = publishedHostComponents.get(hostName); Role role = skippedTask.getRole(); if (! publishedHostComponentsOnHost.contains(role)) { HashMap<String, String> details = new HashMap<>(); String service = ambariMetaInfo.getComponentToService( stackId.getStackName(), stackId.getStackVersion(), role.toString()); details.put("service", service); details.put("component", role.toString()); failures.add(details); } } skippedFailures.put(SKIPPED_HOST_COMPONENT, hostComponents); m_structuredFailures.put(FAILURES, skippedFailures); } skippedCategories.add(skippedCategory); ServiceComponentHostEventWrapper eventWrapper = new ServiceComponentHostEventWrapper( skippedTask.getEvent()); ServiceComponentHostEvent event = eventWrapper.getEvent(); buffer.append(event.getServiceComponentName()); if (null != event.getHostName()) { buffer.append(" on "); buffer.append(event.getHostName()); } buffer.append(": "); buffer.append(skippedTask.getCommandDetail()); buffer.append("\n"); } catch (Exception exception) { LOG.warn("Unable to extract failure information for {}", skippedTask); buffer.append(": "); buffer.append(skippedTask); } } m_structuredFailures.put(SKIPPED, skippedCategories); String structuredOutput = m_gson.toJson(m_structuredFailures); String standardOutput = MessageFormat.format(FAILURE_STD_OUT_TEMPLATE, skippedTasks.size()); String standardError = buffer.toString(); return createCommandReport(0, HostRoleStatus.HOLDING, structuredOutput, standardOutput, standardError); }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testAutoSkipFailedSummaryAction__green() throws Exception { AutoSkipFailedSummaryAction action = new AutoSkipFailedSummaryAction(); m_injector.injectMembers(action); ServiceComponentHostEvent event = createNiceMock(ServiceComponentHostEvent.class); HostRoleCommand hostRoleCommand = new HostRoleCommand("host1", Role.AMBARI_SERVER_ACTION, event, RoleCommand.EXECUTE, hostDAO, executionCommandDAO, ecwFactory); hostRoleCommand.setRequestId(1l); hostRoleCommand.setStageId(1l); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setClusterName("cc"); executionCommand.setRoleCommand(RoleCommand.EXECUTE); executionCommand.setRole("AMBARI_SERVER_ACTION"); executionCommand.setServiceName(""); executionCommand.setTaskId(1l); ExecutionCommandWrapper wrapper = new ExecutionCommandWrapper(executionCommand); hostRoleCommand.setExecutionCommandWrapper(wrapper); Field f = AbstractServerAction.class.getDeclaredField("hostRoleCommand"); f.setAccessible(true); f.set(action, hostRoleCommand); final UpgradeItemEntity upgradeItem1 = new UpgradeItemEntity(); upgradeItem1.setStageId(5l); final UpgradeItemEntity upgradeItem2 = new UpgradeItemEntity(); upgradeItem2.setStageId(6l); UpgradeGroupEntity upgradeGroupEntity = new UpgradeGroupEntity(); upgradeGroupEntity.setId(11l); List<UpgradeItemEntity> groupUpgradeItems = new ArrayList<UpgradeItemEntity>(){{ add(upgradeItem1); add(upgradeItem2); }}; upgradeGroupEntity.setItems(groupUpgradeItems); UpgradeItemEntity upgradeItemEntity = new UpgradeItemEntity(); upgradeItemEntity.setGroupEntity(upgradeGroupEntity); expect(upgradeDAOMock.findUpgradeItemByRequestAndStage(anyLong(), anyLong())).andReturn(upgradeItemEntity).anyTimes(); expect(upgradeDAOMock.findUpgradeGroup(anyLong())).andReturn(upgradeGroupEntity).anyTimes(); replay(upgradeDAOMock); List<HostRoleCommandEntity> skippedTasks = new ArrayList<HostRoleCommandEntity>() {{ }}; expect(hostRoleCommandDAOMock.findByStatusBetweenStages(anyLong(), anyObject(HostRoleStatus.class), anyLong(), anyLong())).andReturn(skippedTasks).anyTimes(); replay(hostRoleCommandDAOMock); ConcurrentMap<String, Object> requestSharedDataContext = new ConcurrentHashMap<String, Object>(); CommandReport result = action.execute(requestSharedDataContext); assertNotNull(result.getStructuredOut()); assertEquals(0, result.getExitCode()); assertEquals(HostRoleStatus.COMPLETED.toString(), result.getStatus()); assertEquals("There were no skipped failures", result.getStdOut()); assertEquals("{}", result.getStructuredOut()); assertEquals("", result.getStdErr()); }
@Test public void testAutoSkipFailedSummaryAction__red() throws Exception { AutoSkipFailedSummaryAction action = new AutoSkipFailedSummaryAction(); m_injector.injectMembers(action); ServiceComponentHostEvent event = createNiceMock(ServiceComponentHostEvent.class); final HostRoleCommand hostRoleCommand = new HostRoleCommand("host1", Role.AMBARI_SERVER_ACTION, event, RoleCommand.EXECUTE, hostDAO, executionCommandDAO, ecwFactory); hostRoleCommand.setRequestId(1l); hostRoleCommand.setStageId(1l); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setClusterName("cc"); executionCommand.setRoleCommand(RoleCommand.EXECUTE); executionCommand.setRole("AMBARI_SERVER_ACTION"); executionCommand.setServiceName(""); executionCommand.setTaskId(1l); ExecutionCommandWrapper wrapper = new ExecutionCommandWrapper(executionCommand); hostRoleCommand.setExecutionCommandWrapper(wrapper); Field f = AbstractServerAction.class.getDeclaredField("hostRoleCommand"); f.setAccessible(true); f.set(action, hostRoleCommand); final UpgradeItemEntity upgradeItem1 = new UpgradeItemEntity(); upgradeItem1.setStageId(5l); final UpgradeItemEntity upgradeItem2 = new UpgradeItemEntity(); upgradeItem2.setStageId(6l); UpgradeGroupEntity upgradeGroupEntity = new UpgradeGroupEntity(); upgradeGroupEntity.setId(11l); List<UpgradeItemEntity> groupUpgradeItems = new ArrayList<UpgradeItemEntity>(){{ add(upgradeItem1); add(upgradeItem2); }}; upgradeGroupEntity.setItems(groupUpgradeItems); UpgradeItemEntity upgradeItemEntity = new UpgradeItemEntity(); upgradeItemEntity.setGroupEntity(upgradeGroupEntity); expect(upgradeDAOMock.findUpgradeItemByRequestAndStage(anyLong(), anyLong())).andReturn(upgradeItemEntity).anyTimes(); expect(upgradeDAOMock.findUpgradeGroup(anyLong())).andReturn(upgradeGroupEntity).anyTimes(); replay(upgradeDAOMock); List<HostRoleCommandEntity> skippedTasks = new ArrayList<HostRoleCommandEntity>() {{ add(createSkippedTask("DATANODE", "DATANODE", "host1.vm", "RESTART HDFS/DATANODE", RoleCommand.CUSTOM_COMMAND, "RESTART" )); add(createSkippedTask("DATANODE", "DATANODE", "host2.vm", "RESTART HDFS/DATANODE", RoleCommand.CUSTOM_COMMAND, "RESTART")); add(createSkippedTask("ZOOKEEPER_QUORUM_SERVICE_CHECK", "ZOOKEEPER_CLIENT", "host2.vm", "SERVICE_CHECK ZOOKEEPER", RoleCommand.SERVICE_CHECK, null)); }}; expect(hostRoleCommandDAOMock.findByStatusBetweenStages(anyLong(), anyObject(HostRoleStatus.class), anyLong(), anyLong())).andReturn(skippedTasks).anyTimes(); replay(hostRoleCommandDAOMock); ConcurrentMap<String, Object> requestSharedDataContext = new ConcurrentHashMap<String, Object>(); CommandReport result = action.execute(requestSharedDataContext); assertNotNull(result.getStructuredOut()); assertEquals(0, result.getExitCode()); assertEquals(HostRoleStatus.HOLDING.toString(), result.getStatus()); assertEquals("There were 3 skipped failure(s) that must be addressed " + "before you can proceed. Please resolve each failure before continuing with the upgrade.", result.getStdOut()); assertEquals("{\"failures\":" + "{\"service_check\":[\"ZOOKEEPER\"]," + "\"host_component\":{" + "\"host1.vm\":[{\"component\":\"DATANODE\",\"service\":\"HDFS\"}]," + "\"host2.vm\":[{\"component\":\"DATANODE\",\"service\":\"HDFS\"}]}}," + "\"skipped\":[\"service_check\",\"host_component\"]}", result.getStructuredOut()); assertEquals("The following steps failed but were automatically skipped:\n" + "DATANODE on host1.vm: RESTART HDFS/DATANODE\n" + "DATANODE on host2.vm: RESTART HDFS/DATANODE\n" + "ZOOKEEPER_CLIENT on host2.vm: SERVICE_CHECK ZOOKEEPER\n", result.getStdErr()); }
@Test public void testAutoSkipFailedSummaryAction__red__service_checks_only() throws Exception { AutoSkipFailedSummaryAction action = new AutoSkipFailedSummaryAction(); m_injector.injectMembers(action); ServiceComponentHostEvent event = createNiceMock(ServiceComponentHostEvent.class); final HostRoleCommand hostRoleCommand = new HostRoleCommand("host1", Role.AMBARI_SERVER_ACTION, event, RoleCommand.EXECUTE, hostDAO, executionCommandDAO, ecwFactory); hostRoleCommand.setRequestId(1l); hostRoleCommand.setStageId(1l); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setClusterName("cc"); executionCommand.setRoleCommand(RoleCommand.EXECUTE); executionCommand.setRole("AMBARI_SERVER_ACTION"); executionCommand.setServiceName(""); executionCommand.setTaskId(1l); ExecutionCommandWrapper wrapper = new ExecutionCommandWrapper(executionCommand); hostRoleCommand.setExecutionCommandWrapper(wrapper); Field f = AbstractServerAction.class.getDeclaredField("hostRoleCommand"); f.setAccessible(true); f.set(action, hostRoleCommand); final UpgradeItemEntity upgradeItem1 = new UpgradeItemEntity(); upgradeItem1.setStageId(5l); final UpgradeItemEntity upgradeItem2 = new UpgradeItemEntity(); upgradeItem2.setStageId(6l); UpgradeGroupEntity upgradeGroupEntity = new UpgradeGroupEntity(); upgradeGroupEntity.setId(11l); List<UpgradeItemEntity> groupUpgradeItems = new ArrayList<UpgradeItemEntity>(){{ add(upgradeItem1); add(upgradeItem2); }}; upgradeGroupEntity.setItems(groupUpgradeItems); UpgradeItemEntity upgradeItemEntity = new UpgradeItemEntity(); upgradeItemEntity.setGroupEntity(upgradeGroupEntity); expect(upgradeDAOMock.findUpgradeItemByRequestAndStage(anyLong(), anyLong())).andReturn(upgradeItemEntity).anyTimes(); expect(upgradeDAOMock.findUpgradeGroup(anyLong())).andReturn(upgradeGroupEntity).anyTimes(); replay(upgradeDAOMock); List<HostRoleCommandEntity> skippedTasks = new ArrayList<HostRoleCommandEntity>() {{ add(createSkippedTask("ZOOKEEPER_QUORUM_SERVICE_CHECK", "ZOOKEEPER_CLIENT", "host2.vm", "SERVICE_CHECK ZOOKEEPER", RoleCommand.SERVICE_CHECK, null)); }}; expect(hostRoleCommandDAOMock.findByStatusBetweenStages(anyLong(), anyObject(HostRoleStatus.class), anyLong(), anyLong())).andReturn(skippedTasks).anyTimes(); replay(hostRoleCommandDAOMock); ConcurrentMap<String, Object> requestSharedDataContext = new ConcurrentHashMap<String, Object>(); CommandReport result = action.execute(requestSharedDataContext); assertNotNull(result.getStructuredOut()); assertEquals(0, result.getExitCode()); assertEquals(HostRoleStatus.HOLDING.toString(), result.getStatus()); assertEquals("There were 1 skipped failure(s) that must be addressed " + "before you can proceed. Please resolve each failure before continuing with the upgrade.", result.getStdOut()); assertEquals("{\"failures\":{\"service_check\":[\"ZOOKEEPER\"]},\"skipped\":[\"service_check\"]}", result.getStructuredOut()); assertEquals("The following steps failed but were automatically skipped:\n" + "ZOOKEEPER_CLIENT on host2.vm: SERVICE_CHECK ZOOKEEPER\n", result.getStdErr()); }
@Test public void testAutoSkipFailedSummaryAction__red__host_components_only() throws Exception { AutoSkipFailedSummaryAction action = new AutoSkipFailedSummaryAction(); m_injector.injectMembers(action); ServiceComponentHostEvent event = createNiceMock(ServiceComponentHostEvent.class); final HostRoleCommand hostRoleCommand = new HostRoleCommand("host1", Role.AMBARI_SERVER_ACTION, event, RoleCommand.EXECUTE, hostDAO, executionCommandDAO, ecwFactory); hostRoleCommand.setRequestId(1l); hostRoleCommand.setStageId(1l); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setClusterName("cc"); executionCommand.setRoleCommand(RoleCommand.EXECUTE); executionCommand.setRole("AMBARI_SERVER_ACTION"); executionCommand.setServiceName(""); executionCommand.setTaskId(1l); ExecutionCommandWrapper wrapper = new ExecutionCommandWrapper(executionCommand); hostRoleCommand.setExecutionCommandWrapper(wrapper); Field f = AbstractServerAction.class.getDeclaredField("hostRoleCommand"); f.setAccessible(true); f.set(action, hostRoleCommand); final UpgradeItemEntity upgradeItem1 = new UpgradeItemEntity(); upgradeItem1.setStageId(5l); final UpgradeItemEntity upgradeItem2 = new UpgradeItemEntity(); upgradeItem2.setStageId(6l); UpgradeGroupEntity upgradeGroupEntity = new UpgradeGroupEntity(); upgradeGroupEntity.setId(11l); List<UpgradeItemEntity> groupUpgradeItems = new ArrayList<UpgradeItemEntity>(){{ add(upgradeItem1); add(upgradeItem2); }}; upgradeGroupEntity.setItems(groupUpgradeItems); UpgradeItemEntity upgradeItemEntity = new UpgradeItemEntity(); upgradeItemEntity.setGroupEntity(upgradeGroupEntity); expect(upgradeDAOMock.findUpgradeItemByRequestAndStage(anyLong(), anyLong())).andReturn(upgradeItemEntity).anyTimes(); expect(upgradeDAOMock.findUpgradeGroup(anyLong())).andReturn(upgradeGroupEntity).anyTimes(); replay(upgradeDAOMock); List<HostRoleCommandEntity> skippedTasks = new ArrayList<HostRoleCommandEntity>() {{ add(createSkippedTask("DATANODE", "DATANODE", "host1.vm", "RESTART HDFS/DATANODE", RoleCommand.CUSTOM_COMMAND, "RESTART" )); add(createSkippedTask("DATANODE", "DATANODE", "host2.vm", "RESTART HDFS/DATANODE", RoleCommand.CUSTOM_COMMAND, "RESTART")); }}; expect(hostRoleCommandDAOMock.findByStatusBetweenStages(anyLong(), anyObject(HostRoleStatus.class), anyLong(), anyLong())).andReturn(skippedTasks).anyTimes(); replay(hostRoleCommandDAOMock); ConcurrentMap<String, Object> requestSharedDataContext = new ConcurrentHashMap<String, Object>(); CommandReport result = action.execute(requestSharedDataContext); assertNotNull(result.getStructuredOut()); assertEquals(0, result.getExitCode()); assertEquals(HostRoleStatus.HOLDING.toString(), result.getStatus()); assertEquals("There were 2 skipped failure(s) that must be addressed " + "before you can proceed. Please resolve each failure before continuing with the upgrade.", result.getStdOut()); assertEquals("{\"failures\":" + "{\"host_component\":" + "{\"host1.vm\":[{\"component\":\"DATANODE\",\"service\":\"HDFS\"}]," + "\"host2.vm\":[{\"component\":\"DATANODE\",\"service\":\"HDFS\"}]}}," + "\"skipped\":[\"host_component\"]}", result.getStructuredOut()); assertEquals("The following steps failed but were automatically skipped:\n" + "DATANODE on host1.vm: RESTART HDFS/DATANODE\n" + "DATANODE on host2.vm: RESTART HDFS/DATANODE\n", result.getStdErr()); }