HeartbeatProcessor extends AbstractService { public void processHeartbeat(HeartBeat heartbeat) throws AmbariException { long now = System.currentTimeMillis(); processAlerts(heartbeat); processStatusReports(heartbeat); processCommandReports(heartbeat, now); processHostStatus(heartbeat); } @Inject  HeartbeatProcessor(Clusters clusterFsm, ActionManager am, HeartbeatMonitor heartbeatMonitor,
                            Injector injector); void addHeartbeat(HeartBeat heartBeat); void processHeartbeat(HeartBeat heartbeat);  }
@Test @SuppressWarnings("unchecked") public void testHeartbeatWithConfigs() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(SECONDARY_NAMENODE); hdfs.getServiceComponent(SECONDARY_NAMENODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.INSTALLED); serviceComponentHost2.setState(State.INSTALLED); HeartBeat hb = new HeartBeat(); hb.setResponseId(0); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setHostname(DummyHostname1); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setServiceName(HDFS); cr.setTaskId(1); cr.setRole(DATANODE); cr.setStatus("COMPLETED"); cr.setStdErr(""); cr.setStdOut(""); cr.setExitCode(215); cr.setRoleCommand("START"); cr.setClusterName(DummyCluster); cr.setConfigurationTags(new HashMap<String, Map<String, String>>() {{ put("global", new HashMap<String, String>() {{ put("tag", "version1"); }}); }}); reports.add(cr); hb.setReports(reports); HostEntity host1 = hostDAO.findByName(DummyHostname1); Assert.assertNotNull(host1); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); Assert.assertNotNull(serviceComponentHost1.getActualConfigs()); Assert.assertEquals(serviceComponentHost1.getActualConfigs().size(), 1); }
@Test @SuppressWarnings("unchecked") public void testRestartRequiredAfterInstallClient() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(HDFS_CLIENT); hdfs.getServiceComponent(HDFS_CLIENT).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(HDFS_CLIENT).getServiceComponentHost(DummyHostname1); serviceComponentHost.setState(State.INSTALLED); serviceComponentHost.setRestartRequired(true); HeartBeat hb = new HeartBeat(); hb.setResponseId(0); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setHostname(DummyHostname1); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setServiceName(HDFS); cr.setRoleCommand("INSTALL"); cr.setCustomCommand("EXECUTION_COMMAND"); cr.setTaskId(1); cr.setRole(HDFS_CLIENT); cr.setStatus("COMPLETED"); cr.setStdErr(""); cr.setStdOut(""); cr.setExitCode(215); cr.setClusterName(DummyCluster); cr.setConfigurationTags(new HashMap<String, Map<String, String>>() {{ put("global", new HashMap<String, String>() {{ put("tag", "version1"); }}); }}); reports.add(cr); hb.setReports(reports); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); Assert.assertNotNull(serviceComponentHost.getActualConfigs()); Assert.assertFalse(serviceComponentHost.isRestartRequired()); Assert.assertEquals(serviceComponentHost.getActualConfigs().size(), 1); }
@Test @SuppressWarnings("unchecked") public void testHeartbeatCustomCommandWithConfigs() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(SECONDARY_NAMENODE); hdfs.getServiceComponent(SECONDARY_NAMENODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.INSTALLED); serviceComponentHost2.setState(State.INSTALLED); HeartBeat hb = new HeartBeat(); hb.setResponseId(0); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setHostname(DummyHostname1); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setServiceName(HDFS); cr.setRoleCommand("CUSTOM_COMMAND"); cr.setCustomCommand("RESTART"); cr.setTaskId(1); cr.setRole(DATANODE); cr.setStatus("COMPLETED"); cr.setStdErr(""); cr.setStdOut(""); cr.setExitCode(215); cr.setClusterName(DummyCluster); cr.setConfigurationTags(new HashMap<String, Map<String,String>>() {{ put("global", new HashMap<String,String>() {{ put("tag", "version1"); }}); }}); CommandReport crn = new CommandReport(); crn.setActionId(StageUtils.getActionId(requestId, stageId)); crn.setServiceName(HDFS); crn.setRoleCommand("CUSTOM_COMMAND"); crn.setCustomCommand("START"); crn.setTaskId(1); crn.setRole(NAMENODE); crn.setStatus("COMPLETED"); crn.setStdErr(""); crn.setStdOut(""); crn.setExitCode(215); crn.setClusterName(DummyCluster); crn.setConfigurationTags(new HashMap<String, Map<String,String>>() {{ put("global", new HashMap<String,String>() {{ put("tag", "version1"); }}); }}); reports.add(cr); reports.add(crn); hb.setReports(reports); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); Assert.assertNotNull(serviceComponentHost1.getActualConfigs()); Assert.assertEquals(serviceComponentHost1.getActualConfigs().size(), 1); Assert.assertNotNull(serviceComponentHost2.getActualConfigs()); Assert.assertEquals(serviceComponentHost2.getActualConfigs().size(), 1); }
@Test @SuppressWarnings("unchecked") public void testHeartbeatCustomStartStop() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(SECONDARY_NAMENODE); hdfs.getServiceComponent(SECONDARY_NAMENODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.INSTALLED); serviceComponentHost2.setState(State.STARTED); serviceComponentHost1.setRestartRequired(true); serviceComponentHost2.setRestartRequired(true); HeartBeat hb = new HeartBeat(); hb.setResponseId(0); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setHostname(DummyHostname1); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setServiceName(HDFS); cr.setRoleCommand("CUSTOM_COMMAND"); cr.setCustomCommand("START"); cr.setTaskId(1); cr.setRole(DATANODE); cr.setStatus("COMPLETED"); cr.setStdErr(""); cr.setStdOut(""); cr.setExitCode(215); cr.setClusterName(DummyCluster); CommandReport crn = new CommandReport(); crn.setActionId(StageUtils.getActionId(requestId, stageId)); crn.setServiceName(HDFS); crn.setRoleCommand("CUSTOM_COMMAND"); crn.setCustomCommand("STOP"); crn.setTaskId(1); crn.setRole(NAMENODE); crn.setStatus("COMPLETED"); crn.setStdErr(""); crn.setStdOut(""); crn.setExitCode(215); crn.setClusterName(DummyCluster); reports.add(cr); reports.add(crn); hb.setReports(reports); assertTrue(serviceComponentHost1.isRestartRequired()); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); State componentState1 = serviceComponentHost1.getState(); assertEquals(State.STARTED, componentState1); assertFalse(serviceComponentHost1.isRestartRequired()); State componentState2 = serviceComponentHost2.getState(); assertEquals(State.INSTALLED, componentState2); assertTrue(serviceComponentHost2.isRestartRequired()); }
@Test @SuppressWarnings("unchecked") public void testStatusHeartbeat() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(SECONDARY_NAMENODE); hdfs.getServiceComponent(SECONDARY_NAMENODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost3 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(SECONDARY_NAMENODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.INSTALLED); serviceComponentHost1.setSecurityState(SecurityState.UNSECURED); serviceComponentHost2.setState(State.INSTALLED); serviceComponentHost2.setSecurityState(SecurityState.SECURING); serviceComponentHost3.setState(State.STARTING); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setReports(new ArrayList<CommandReport>()); ArrayList<ComponentStatus> componentStatuses = new ArrayList<ComponentStatus>(); ComponentStatus componentStatus1 = new ComponentStatus(); componentStatus1.setClusterName(DummyCluster); componentStatus1.setServiceName(HDFS); componentStatus1.setMessage(DummyHostStatus); componentStatus1.setStatus(State.STARTED.name()); componentStatus1.setSecurityState(SecurityState.SECURED_KERBEROS.name()); componentStatus1.setComponentName(DATANODE); componentStatuses.add(componentStatus1); ComponentStatus componentStatus2 = new ComponentStatus(); componentStatus2.setClusterName(DummyCluster); componentStatus2.setServiceName(HDFS); componentStatus2.setMessage(DummyHostStatus); componentStatus2.setStatus(State.STARTED.name()); componentStatus2.setSecurityState(SecurityState.UNSECURED.name()); componentStatus2.setComponentName(SECONDARY_NAMENODE); componentStatuses.add(componentStatus2); hb.setComponentStatus(componentStatuses); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); State componentState1 = serviceComponentHost1.getState(); State componentState2 = serviceComponentHost2.getState(); State componentState3 = serviceComponentHost3.getState(); assertEquals(State.STARTED, componentState1); assertEquals(SecurityState.SECURED_KERBEROS, serviceComponentHost1.getSecurityState()); assertEquals(State.INSTALLED, componentState2); assertEquals(SecurityState.SECURING, serviceComponentHost2.getSecurityState()); assertEquals(State.STARTING, componentState3); assertEquals(SecurityState.UNSECURED, serviceComponentHost3.getSecurityState()); }
@Test @SuppressWarnings("unchecked") public void testCommandReportOnHeartbeatUpdatedState() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.INSTALLED); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setTaskId(1); cr.setClusterName(DummyCluster); cr.setServiceName(HDFS); cr.setRole(DATANODE); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setStdErr("none"); cr.setStdOut("dummy output"); cr.setExitCode(777); cr.setRoleCommand("START"); reports.add(cr); hb.setReports(reports); hb.setComponentStatus(new ArrayList<ComponentStatus>()); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); }}).anyTimes(); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.INSTALLED, State.INSTALLED, serviceComponentHost1.getState()); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(1); cr.setStatus(HostRoleStatus.COMPLETED.toString()); cr.setExitCode(0); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.STARTED, State.STARTED, serviceComponentHost1.getState()); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(2); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setRoleCommand("STOP"); cr.setExitCode(777); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.STARTED, State.STARTED, serviceComponentHost1.getState()); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(3); cr.setStatus(HostRoleStatus.COMPLETED.toString()); cr.setExitCode(0); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.INSTALLED, State.INSTALLED, serviceComponentHost1.getState()); serviceComponentHost1.setState(State.STARTING); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setExitCode(777); cr.setRoleCommand("START"); hb.setResponseId(4); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.STARTING, State.STARTING, serviceComponentHost1.getState()); cr.setStatus(HostRoleStatus.COMPLETED.toString()); cr.setExitCode(0); hb.setResponseId(5); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.STARTED, State.STARTED, serviceComponentHost1.getState()); serviceComponentHost1.setState(State.STOPPING); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setExitCode(777); cr.setRoleCommand("STOP"); hb.setResponseId(6); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.STOPPING, State.STOPPING, serviceComponentHost1.getState()); cr.setStatus(HostRoleStatus.COMPLETED.toString()); cr.setExitCode(0); hb.setResponseId(7); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.INSTALLED, State.INSTALLED, serviceComponentHost1.getState()); }
@Test @SuppressWarnings("unchecked") public void testUpgradeSpecificHandling() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.UPGRADING); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); List<CommandReport> reports = new ArrayList<CommandReport>(); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setTaskId(1); cr.setClusterName(DummyCluster); cr.setServiceName(HDFS); cr.setRole(DATANODE); cr.setRoleCommand("INSTALL"); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setStdErr("none"); cr.setStdOut("dummy output"); cr.setExitCode(777); reports.add(cr); hb.setReports(reports); hb.setComponentStatus(new ArrayList<ComponentStatus>()); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); }}).anyTimes(); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.UPGRADING, State.UPGRADING, serviceComponentHost1.getState()); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(1); cr.setStatus(HostRoleStatus.COMPLETED.toString()); cr.setExitCode(0); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.INSTALLED, State.INSTALLED, serviceComponentHost1.getState()); serviceComponentHost1.setState(State.UPGRADING); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(2); cr.setStatus(HostRoleStatus.FAILED.toString()); cr.setExitCode(3); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.UPGRADING, State.UPGRADING, serviceComponentHost1.getState()); serviceComponentHost1.setState(State.UPGRADING); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(3); cr.setStatus(HostRoleStatus.PENDING.toString()); cr.setExitCode(55); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.UPGRADING, State.UPGRADING, serviceComponentHost1.getState()); serviceComponentHost1.setState(State.UPGRADING); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(4); cr.setStatus(HostRoleStatus.QUEUED.toString()); cr.setExitCode(55); heartbeatProcessor.processHeartbeat(hb); assertEquals("Host state should be " + State.UPGRADING, State.UPGRADING, serviceComponentHost1.getState()); }
@Test @SuppressWarnings("unchecked") public void testCommandStatusProcesses() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1).setState(State.STARTED); ActionQueue aq = new ActionQueue(); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setReports(new ArrayList<CommandReport>()); List<Map<String, String>> procs = new ArrayList<Map<String, String>>(); Map<String, String> proc1info = new HashMap<String, String>(); proc1info.put("name", "a"); proc1info.put("status", "RUNNING"); procs.add(proc1info); Map<String, String> proc2info = new HashMap<String, String>(); proc2info.put("name", "b"); proc2info.put("status", "NOT_RUNNING"); procs.add(proc2info); Map<String, Object> extra = new HashMap<String, Object>(); extra.put("processes", procs); ArrayList<ComponentStatus> componentStatuses = new ArrayList<ComponentStatus>(); ComponentStatus componentStatus1 = new ComponentStatus(); componentStatus1.setClusterName(DummyCluster); componentStatus1.setServiceName(HDFS); componentStatus1.setMessage(DummyHostStatus); componentStatus1.setStatus(State.STARTED.name()); componentStatus1.setSecurityState(SecurityState.UNSECURED.name()); componentStatus1.setComponentName(DATANODE); componentStatus1.setExtra(extra); componentStatuses.add(componentStatus1); hb.setComponentStatus(componentStatuses); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); }}).anyTimes(); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); ServiceComponentHost sch = hdfs.getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); Assert.assertEquals(Integer.valueOf(2), Integer.valueOf(sch.getProcesses().size())); hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(1); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setReports(new ArrayList<CommandReport>()); componentStatus1 = new ComponentStatus(); componentStatus1.setClusterName(DummyCluster); componentStatus1.setServiceName(HDFS); componentStatus1.setMessage(DummyHostStatus); componentStatus1.setStatus(State.STARTED.name()); componentStatus1.setSecurityState(SecurityState.UNSECURED.name()); componentStatus1.setComponentName(DATANODE); hb.setComponentStatus(Collections.singletonList(componentStatus1)); heartbeatProcessor.processHeartbeat(hb); }
@Test @SuppressWarnings("unchecked") public void testComponentUpgradeCompleteReport() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(HDFS_CLIENT); hdfs.getServiceComponent(HDFS_CLIENT).addServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); StackId stack130 = new StackId("HDP-1.3.0"); StackId stack120 = new StackId("HDP-1.2.0"); serviceComponentHost1.setState(State.UPGRADING); serviceComponentHost2.setState(State.INSTALLING); serviceComponentHost1.setStackVersion(stack120); serviceComponentHost1.setDesiredStackVersion(stack130); serviceComponentHost2.setStackVersion(stack120); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); CommandReport cr1 = new CommandReport(); cr1.setActionId(StageUtils.getActionId(requestId, stageId)); cr1.setTaskId(1); cr1.setClusterName(DummyCluster); cr1.setServiceName(HDFS); cr1.setRole(DATANODE); cr1.setStatus(HostRoleStatus.COMPLETED.toString()); cr1.setStdErr("none"); cr1.setStdOut("dummy output"); cr1.setExitCode(0); cr1.setRoleCommand(RoleCommand.UPGRADE.toString()); CommandReport cr2 = new CommandReport(); cr2.setActionId(StageUtils.getActionId(requestId, stageId)); cr2.setTaskId(2); cr2.setClusterName(DummyCluster); cr2.setServiceName(HDFS); cr2.setRole(NAMENODE); cr2.setStatus(HostRoleStatus.COMPLETED.toString()); cr2.setStdErr("none"); cr2.setStdOut("dummy output"); cr2.setExitCode(0); cr2.setRoleCommand(RoleCommand.UPGRADE.toString()); ArrayList<CommandReport> reports = new ArrayList<CommandReport>(); reports.add(cr1); reports.add(cr2); hb.setReports(reports); ActionQueue aq = new ActionQueue(); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); assertEquals("Stack version for SCH should be updated to " + serviceComponentHost1.getDesiredStackVersion(), stack130, serviceComponentHost1.getStackVersion()); assertEquals("Stack version for SCH should not change ", stack120, serviceComponentHost2.getStackVersion()); }
@Test @SuppressWarnings("unchecked") public void testComponentUpgradeFailReport() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE).addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(HDFS_CLIENT); hdfs.getServiceComponent(HDFS_CLIENT).addServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters.getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE).getServiceComponentHost(DummyHostname1); StackId stack130 = new StackId("HDP-1.3.0"); StackId stack120 = new StackId("HDP-1.2.0"); serviceComponentHost1.setState(State.UPGRADING); serviceComponentHost2.setState(State.INSTALLING); serviceComponentHost1.setStackVersion(stack120); serviceComponentHost1.setDesiredStackVersion(stack130); serviceComponentHost2.setStackVersion(stack120); Stage s = stageFactory.createNew(requestId, "/a/b", "cluster1", 1L, "action manager test", "clusterHostInfo", "commandParamsStage", "hostParamsStage"); s.setStageId(stageId); s.addHostRoleExecutionCommand(DummyHostname1, Role.DATANODE, RoleCommand.UPGRADE, new ServiceComponentHostUpgradeEvent(Role.DATANODE.toString(), DummyHostname1, System.currentTimeMillis(), "HDP-1.3.0"), DummyCluster, "HDFS", false, false); s.addHostRoleExecutionCommand(DummyHostname1, Role.NAMENODE, RoleCommand.INSTALL, new ServiceComponentHostInstallEvent(Role.NAMENODE.toString(), DummyHostname1, System.currentTimeMillis(), "HDP-1.3.0"), DummyCluster, "HDFS", false, false); List<Stage> stages = new ArrayList<Stage>(); stages.add(s); Request request = new Request(stages, clusters); actionDBAccessor.persistActions(request); CommandReport cr = new CommandReport(); cr.setActionId(StageUtils.getActionId(requestId, stageId)); cr.setTaskId(1); cr.setClusterName(DummyCluster); cr.setServiceName(HDFS); cr.setRole(DATANODE); cr.setStatus(HostRoleStatus.IN_PROGRESS.toString()); cr.setStdErr("none"); cr.setStdOut("dummy output"); actionDBAccessor.updateHostRoleState(DummyHostname1, requestId, stageId, Role.DATANODE.name(), cr); cr.setRole(NAMENODE); cr.setTaskId(2); actionDBAccessor.updateHostRoleState(DummyHostname1, requestId, stageId, Role.NAMENODE.name(), cr); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); CommandReport cr1 = new CommandReport(); cr1.setActionId(StageUtils.getActionId(requestId, stageId)); cr1.setTaskId(1); cr1.setClusterName(DummyCluster); cr1.setServiceName(HDFS); cr1.setRole(DATANODE); cr1.setRoleCommand("INSTALL"); cr1.setStatus(HostRoleStatus.FAILED.toString()); cr1.setStdErr("none"); cr1.setStdOut("dummy output"); cr1.setExitCode(0); CommandReport cr2 = new CommandReport(); cr2.setActionId(StageUtils.getActionId(requestId, stageId)); cr2.setTaskId(2); cr2.setClusterName(DummyCluster); cr2.setServiceName(HDFS); cr2.setRole(NAMENODE); cr2.setRoleCommand("INSTALL"); cr2.setStatus(HostRoleStatus.FAILED.toString()); cr2.setStdErr("none"); cr2.setStdOut("dummy output"); cr2.setExitCode(0); ArrayList<CommandReport> reports = new ArrayList<CommandReport>(); reports.add(cr1); reports.add(cr2); hb.setReports(reports); ActionQueue aq = new ActionQueue(); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); assertEquals("State of SCH should change after fail report", State.UPGRADING, serviceComponentHost1.getState()); assertEquals("State of SCH should change after fail report", State.INSTALL_FAILED, serviceComponentHost2.getState()); assertEquals("Stack version of SCH should not change after fail report", stack120, serviceComponentHost1.getStackVersion()); assertEquals("Stack version of SCH should not change after fail report", stack130, serviceComponentHost1.getDesiredStackVersion()); assertEquals("Stack version of SCH should not change after fail report", State.INSTALL_FAILED, serviceComponentHost2.getState()); }
@Test @SuppressWarnings("unchecked") public void testHeartBeatWithAlertAndInvalidCluster() throws Exception { ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>()); replay(am); Cluster cluster = heartbeatTestHelper.getDummyCluster(); Clusters fsm = clusters; Host hostObject = clusters.getHost(DummyHostname1); hostObject.setIPv4("ipv4"); hostObject.setIPv6("ipv6"); hostObject.setOsType(DummyOsType); ActionQueue aq = new ActionQueue(); HeartBeatHandler handler = new HeartBeatHandler(fsm, aq, am, injector); Register reg = new Register(); HostInfo hi = new HostInfo(); hi.setHostName(DummyHostname1); hi.setOS(DummyOs); hi.setOSRelease(DummyOSRelease); reg.setHostname(DummyHostname1); reg.setHardwareProfile(hi); reg.setAgentVersion(metaInfo.getServerVersion()); handler.handleRegistration(reg); hostObject.setState(HostState.UNHEALTHY); ExecutionCommand execCmd = new ExecutionCommand(); execCmd.setRequestAndStage(2, 34); execCmd.setHostname(DummyHostname1); aq.enqueue(DummyHostname1, new ExecutionCommand()); HeartBeat hb = new HeartBeat(); HostStatus hs = new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus); hb.setResponseId(0); hb.setNodeStatus(hs); hb.setHostname(DummyHostname1); Alert alert = new Alert("foo", "bar", "baz", "foobar", "foobarbaz", AlertState.OK); alert.setCluster("BADCLUSTER"); List<Alert> alerts = Collections.singletonList(alert); hb.setAlerts(alerts); handler.getHeartbeatProcessor().processHeartbeat(hb); }
@Test public void testInstallPackagesWithVersion() throws Exception { EventBusSynchronizer.synchronizeAmbariEventPublisher(injector); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( Collections.singletonList(command)).anyTimes(); replay(am); Cluster cluster = heartbeatTestHelper.getDummyCluster(); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, new ActionQueue()); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); HeartBeat hb = new HeartBeat(); JsonObject json = new JsonObject(); json.addProperty("actual_version", "2.2.1.0-2222"); json.addProperty("package_installation_result", "SUCCESS"); json.addProperty("installed_repository_version", "0.1"); json.addProperty("stack_id", cluster.getDesiredStackVersion().getStackId()); CommandReport cmdReport = new CommandReport(); cmdReport.setActionId(StageUtils.getActionId(requestId, stageId)); cmdReport.setTaskId(1); cmdReport.setCustomCommand("install_packages"); cmdReport.setStructuredOut(json.toString()); cmdReport.setRoleCommand(RoleCommand.ACTIONEXECUTE.name()); cmdReport.setStatus(HostRoleStatus.COMPLETED.name()); cmdReport.setRole("install_packages"); cmdReport.setClusterName(DummyCluster); List<CommandReport> reports = new ArrayList<>(); reports.add(cmdReport); hb.setReports(reports); hb.setTimestamp(0L); hb.setResponseId(0); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setHostname(DummyHostname1); hb.setComponentStatus(new ArrayList<ComponentStatus>()); StackId stackId = new StackId("HDP", "0.1"); RepositoryVersionDAO dao = injector.getInstance(RepositoryVersionDAO.class); RepositoryVersionEntity entity = dao.findByStackAndVersion(stackId, "0.1"); Assert.assertNotNull(entity); heartbeatProcessor.processHeartbeat(hb); entity = dao.findByStackAndVersion(stackId, "0.1"); Assert.assertNull(entity); entity = dao.findByStackAndVersion(stackId, "2.2.1.0-2222"); Assert.assertNotNull(entity); }
@Test @SuppressWarnings("unchecked") public void testComponentInProgressStatusSafeAfterStatusReport() throws Exception { Cluster cluster = heartbeatTestHelper.getDummyCluster(); Service hdfs = cluster.addService(HDFS); hdfs.addServiceComponent(DATANODE); hdfs.getServiceComponent(DATANODE). addServiceComponentHost(DummyHostname1); hdfs.addServiceComponent(NAMENODE); hdfs.getServiceComponent(NAMENODE). addServiceComponentHost(DummyHostname1); ActionQueue aq = new ActionQueue(); ServiceComponentHost serviceComponentHost1 = clusters. getCluster(DummyCluster).getService(HDFS). getServiceComponent(DATANODE). getServiceComponentHost(DummyHostname1); ServiceComponentHost serviceComponentHost2 = clusters. getCluster(DummyCluster).getService(HDFS). getServiceComponent(NAMENODE). getServiceComponentHost(DummyHostname1); serviceComponentHost1.setState(State.STARTING); serviceComponentHost2.setState(State.STOPPING); HeartBeat hb = new HeartBeat(); hb.setTimestamp(System.currentTimeMillis()); hb.setResponseId(0); hb.setHostname(DummyHostname1); hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, DummyHostStatus)); hb.setReports(new ArrayList<CommandReport>()); ArrayList<ComponentStatus> componentStatuses = new ArrayList<ComponentStatus>(); ComponentStatus componentStatus1 = new ComponentStatus(); componentStatus1.setClusterName(DummyCluster); componentStatus1.setServiceName(HDFS); componentStatus1.setMessage(DummyHostStatus); componentStatus1.setStatus(State.INSTALLED.name()); componentStatus1.setSecurityState(SecurityState.UNSECURED.name()); componentStatus1.setComponentName(DATANODE); componentStatuses.add(componentStatus1); ComponentStatus componentStatus2 = new ComponentStatus(); componentStatus2.setClusterName(DummyCluster); componentStatus2.setServiceName(HDFS); componentStatus2.setMessage(DummyHostStatus); componentStatus2.setStatus(State.INSTALLED.name()); componentStatus2.setSecurityState(SecurityState.UNSECURED.name()); componentStatus2.setComponentName(NAMENODE); componentStatuses.add(componentStatus2); hb.setComponentStatus(componentStatuses); final HostRoleCommand command = hostRoleCommandFactory.create(DummyHostname1, Role.DATANODE, null, null); ActionManager am = actionManagerTestHelper.getMockActionManager(); expect(am.getTasks(EasyMock.<List<Long>>anyObject())).andReturn( new ArrayList<HostRoleCommand>() {{ add(command); add(command); }}); replay(am); HeartBeatHandler handler = heartbeatTestHelper.getHeartBeatHandler(am, aq); HeartbeatProcessor heartbeatProcessor = handler.getHeartbeatProcessor(); heartbeatProcessor.processHeartbeat(hb); State componentState1 = serviceComponentHost1.getState(); State componentState2 = serviceComponentHost2.getState(); assertEquals(State.STARTING, componentState1); assertEquals(State.STOPPING, componentState2); }