AgentProviderService extends AbstractProviderService implements
    ProviderCore,
    AgentKeys,
    SliderKeys, AgentRestOperations { @VisibleForTesting protected void addStartCommand(String roleName, String roleGroup, String containerId, HeartBeatResponse response, String scriptPath, ComponentCommand startCommand, ComponentCommand stopCommand, long timeout, boolean isMarkedAutoRestart) throws SliderException { assert getAmState().isApplicationLive(); ConfTreeOperations appConf = getAmState().getAppConfSnapshot(); ConfTreeOperations internalsConf = getAmState().getInternalsSnapshot(); ExecutionCommand cmd = new ExecutionCommand(AgentCommandType.EXECUTION_COMMAND); prepareExecutionCommand(cmd); String clusterName = internalsConf.get(OptionKeys.APPLICATION_NAME); String hostName = getClusterInfoPropertyValue(StatusKeys.INFO_AM_HOSTNAME); cmd.setHostname(hostName); cmd.setClusterName(clusterName); cmd.setRoleCommand(Command.START.toString()); cmd.setServiceName(clusterName); cmd.setComponentName(roleName); cmd.setRole(roleName); Map<String, String> hostLevelParams = new TreeMap<>(); hostLevelParams.put(JAVA_HOME, appConf.getGlobalOptions().getOption(JAVA_HOME, getJDKDir())); hostLevelParams.put(CONTAINER_ID, containerId); cmd.setHostLevelParams(hostLevelParams); Map<String, String> roleParams = new TreeMap<>(); cmd.setRoleParams(roleParams); cmd.getRoleParams().put("auto_restart", Boolean.toString(isMarkedAutoRestart)); Map<String, Map<String, String>> configurations = buildCommandConfigurations(appConf, containerId, roleName, roleGroup); cmd.setConfigurations(configurations); Map<String, Map<String, String>> componentConfigurations = buildComponentConfigurations(appConf); cmd.setComponentConfigurations(componentConfigurations); if (SliderUtils.isSet(scriptPath)) { cmd.setCommandParams(commandParametersSet(scriptPath, timeout, true)); } else { if (startCommand == null) { throw new SliderException("Expected START command not found for component " + roleName); } cmd.setCommandParams(commandParametersSet(startCommand, timeout, true)); configurations.get("global").put("exec_cmd", startCommand.getExec()); } response.addExecutionCommand(cmd); log.debug("command looks like: {}", cmd); ExecutionCommand cmdStop = new ExecutionCommand( AgentCommandType.EXECUTION_COMMAND); cmdStop.setTaskId(taskId.get()); cmdStop.setCommandId(cmdStop.getTaskId() + "-1"); cmdStop.setHostname(hostName); cmdStop.setClusterName(clusterName); cmdStop.setRoleCommand(Command.STOP.toString()); cmdStop.setServiceName(clusterName); cmdStop.setComponentName(roleName); cmdStop.setRole(roleName); Map<String, String> hostLevelParamsStop = new TreeMap<String, String>(); hostLevelParamsStop.put(JAVA_HOME, appConf.getGlobalOptions() .getOption(JAVA_HOME, "")); hostLevelParamsStop.put(CONTAINER_ID, containerId); cmdStop.setHostLevelParams(hostLevelParamsStop); Map<String, String> roleParamsStop = new TreeMap<String, String>(); cmdStop.setRoleParams(roleParamsStop); cmdStop.getRoleParams().put("auto_restart", Boolean.toString(isMarkedAutoRestart)); if (SliderUtils.isSet(scriptPath)) { cmdStop.setCommandParams(commandParametersSet(scriptPath, timeout, true)); } else { if (stopCommand == null) { stopCommand = ComponentCommand.getDefaultComponentCommand("STOP"); } cmd.setCommandParams(commandParametersSet(stopCommand, timeout, true)); configurations.get("global").put("exec_cmd", startCommand.getExec()); } Map<String, Map<String, String>> configurationsStop = buildCommandConfigurations( appConf, containerId, roleName, roleGroup); cmdStop.setConfigurations(configurationsStop); response.addExecutionCommand(cmdStop); } AgentProviderService(); @Override String getHumanName(); @Override List<ProviderRole> getRoles(); @Override Configuration loadProviderConfigurationInformation(File confDir); @Override void validateInstanceDefinition(AggregateConf instanceDefinition); @Override void initializeApplicationConfiguration(
      AggregateConf instanceDefinition, SliderFileSystem fileSystem,
      String roleGroup); @Override void buildContainerLaunchContext(ContainerLauncher launcher,
                                          AggregateConf instanceDefinition,
                                          Container container,
                                          ProviderRole providerRole,
                                          SliderFileSystem fileSystem,
                                          Path generatedConfPath,
                                          MapOperations resourceComponent,
                                          MapOperations appComponent,
                                          Path containerTmpDirPath); @Override void rebuildContainerDetails(List<Container> liveContainers,
                                      String applicationId, Map<Integer, ProviderRole> providerRoleMap); @Override boolean isSupportedRole(String role); @Override RegistrationResponse handleRegistration(Register registration); @Override HeartBeatResponse handleHeartBeat(HeartBeat heartBeat); @Override Map<String, MonitorDetail> buildMonitorDetails(ClusterDescription clusterDesc); @Override void applyInitialRegistryDefinitions(URL amWebURI,
      URL agentOpsURI,
      URL agentStatusURI,
      ServiceRecord serviceRecord); @Override void notifyContainerCompleted(ContainerId containerId); void setInUpgradeMode(boolean inUpgradeMode); void addUpgradeContainers(Set<String> upgradeContainers); void setAppStopInitiated(boolean appStopInitiated); Map<String, String> buildProviderStatus();  }
@Test public void testAddStartCommand() throws Exception { AgentProviderService aps = createAgentProviderService(new Configuration()); HeartBeatResponse hbr = new HeartBeatResponse(); StateAccessForProviders access = createNiceMock(StateAccessForProviders.class); AgentProviderService mockAps = Mockito.spy(aps); doReturn(access).when(mockAps).getAmState(); AggregateConf aggConf = new AggregateConf(); ConfTreeOperations treeOps = aggConf.getAppConfOperations(); treeOps.getGlobalOptions().put(AgentKeys.JAVA_HOME, "java_home"); treeOps.set(OptionKeys.APPLICATION_NAME, "HBASE"); treeOps.set("site.fs.defaultFS", "hdfs: treeOps.set("internal.data.dir.path", "hdfs: treeOps.set(OptionKeys.ZOOKEEPER_HOSTS, "HOST1"); treeOps.getGlobalOptions().put("site.hbase-site.a.port", "${HBASE_MASTER.ALLOCATED_PORT}"); treeOps.getGlobalOptions().put("site.hbase-site.b.port", "${HBASE_MASTER.ALLOCATED_PORT}"); treeOps.getGlobalOptions().put("site.hbase-site.random.port", "${HBASE_MASTER.ALLOCATED_PORT}{PER_CONTAINER}"); treeOps.getGlobalOptions().put("site.hbase-site.random2.port", "${HBASE_MASTER.ALLOCATED_PORT}"); Map<String, DefaultConfig> defaultConfigMap = new HashMap<String, DefaultConfig>(); DefaultConfig defaultConfig = new DefaultConfig(); PropertyInfo propertyInfo1 = new PropertyInfo(); propertyInfo1.setName("defaultA"); propertyInfo1.setValue("Avalue"); defaultConfig.addPropertyInfo(propertyInfo1); propertyInfo1 = new PropertyInfo(); propertyInfo1.setName("defaultB"); propertyInfo1.setValue(""); defaultConfig.addPropertyInfo(propertyInfo1); defaultConfigMap.put("hbase-site", defaultConfig); expect(access.getAppConfSnapshot()).andReturn(treeOps).anyTimes(); expect(access.getInternalsSnapshot()).andReturn(treeOps).anyTimes(); expect(access.isApplicationLive()).andReturn(true).anyTimes(); doReturn("HOST1").when(mockAps).getClusterInfoPropertyValue(anyString()); doReturn(defaultConfigMap).when(mockAps).getDefaultConfigs(anyString()); List<String> configurations = new ArrayList<String>(); configurations.add("hbase-site"); configurations.add("global"); List<String> sysConfigurations = new ArrayList<String>(); configurations.add("core-site"); doReturn(configurations).when(mockAps).getApplicationConfigurationTypes(anyString()); doReturn(sysConfigurations).when(mockAps).getSystemConfigurationsRequested(any(ConfTreeOperations.class)); Map<String, Map<String, ClusterNode>> roleClusterNodeMap = new HashMap<String, Map<String, ClusterNode>>(); Map<String, ClusterNode> container = new HashMap<String, ClusterNode>(); ClusterNode cn1 = new ClusterNode(new MockContainerId(1)); cn1.host = "HOST1"; container.put("cid1", cn1); roleClusterNodeMap.put("HBASE_MASTER", container); doReturn(roleClusterNodeMap).when(mockAps).getRoleClusterNodeMapping(); Map<String, String> allocatedPorts = new HashMap<String, String>(); allocatedPorts.put("hbase-site.a.port", "10023"); allocatedPorts.put("hbase-site.b.port", "10024"); doReturn(allocatedPorts).when(mockAps).getAllocatedPorts(); Map<String, String> allocatedPorts2 = new HashMap<String, String>(); allocatedPorts2.put("hbase-site.random.port", "10025"); doReturn(allocatedPorts2).when(mockAps).getAllocatedPorts(anyString()); replay(access); ComponentCommand startCmd = ComponentCommand.getDefaultComponentCommand(); ComponentCommand stopCmd = ComponentCommand.getDefaultComponentCommand("STOP"); mockAps.addStartCommand("HBASE_MASTER", "HBASE_MASTER", "cid1", hbr, "", startCmd, stopCmd, 0, Boolean.FALSE); Assert.assertTrue(hbr.getExecutionCommands().get(0).getConfigurations().containsKey("hbase-site")); Assert.assertTrue(hbr.getExecutionCommands().get(0).getConfigurations().containsKey("core-site")); Map<String, String> hbaseSiteConf = hbr.getExecutionCommands().get(0).getConfigurations().get("hbase-site"); Assert.assertTrue(hbaseSiteConf.containsKey("a.port")); Assert.assertEquals("10023", hbaseSiteConf.get("a.port")); Assert.assertEquals("10024", hbaseSiteConf.get("b.port")); Assert.assertEquals("10025", hbaseSiteConf.get("random.port")); assertEquals("${HBASE_MASTER.ALLOCATED_PORT}", hbaseSiteConf.get("random2.port")); ExecutionCommand cmd = hbr.getExecutionCommands().get(0); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_log_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_pid_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_install_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_input_conf_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_container_id")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("pid_file")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_root")); Assert.assertTrue(cmd.getConfigurations().get("hbase-site").containsKey("defaultA")); Assert.assertFalse(cmd.getConfigurations().get("hbase-site").containsKey("defaultB")); }