String clusterName = actionContext.getClusterName();
Cluster cluster = clusters.getCluster(clusterName);
List<RequestResourceFilter> resourceFilters = actionContext.getResourceFilters();
RequestResourceFilter resourceFilter = new RequestResourceFilter();
if (resourceFilters != null && !resourceFilters.isEmpty()) {
resourceFilter = resourceFilters.get(0);
}
// List of host to select from
Set<String> candidateHosts = new HashSet<String>();
String serviceName = actionContext.getExpectedServiceName();
String componentName = actionContext.getExpectedComponentName();
StackId stackId = cluster.getCurrentStackVersion();
ComponentInfo componentInfo = null;
if (serviceName != null && !serviceName.isEmpty()) {
if (componentName != null && !componentName.isEmpty()) {
Map<String, ServiceComponentHost> componentHosts =
cluster.getService(serviceName)
.getServiceComponent(componentName).getServiceComponentHosts();
candidateHosts.addAll(componentHosts.keySet());
componentInfo = ambariMetaInfo.getComponentCategory(stackId.getStackName(),
stackId.getStackVersion(), serviceName, componentName);
} else {
for (String component : cluster.getService(serviceName).getServiceComponents().keySet()) {
Map<String, ServiceComponentHost> componentHosts =
cluster.getService(serviceName)
.getServiceComponent(component).getServiceComponentHosts();
candidateHosts.addAll(componentHosts.keySet());
}
}
} else {
// All hosts are valid target host
candidateHosts.addAll(clusters.getHostsForCluster(cluster.getClusterName()).keySet());
}
// If request did not specify hosts and there exists no host
if (resourceFilter.getHostNames().isEmpty() && candidateHosts.isEmpty()) {
throw new AmbariException("Suitable hosts not found, component="
+ componentName + ", service=" + serviceName
+ ", cluster=" + cluster.getClusterName() + ", " +
"actionName=" + actionContext.getActionName());
}
// Compare specified hosts to available hosts
if (!resourceFilter.getHostNames().isEmpty() && !candidateHosts.isEmpty()) {
for (String hostname : resourceFilter.getHostNames()) {
if (!candidateHosts.contains(hostname)) {
throw new AmbariException("Request specifies host " + hostname +
" but its not a valid host based on the " +
"target service=" + serviceName + " and component=" + componentName);
}
}
}
List<String> targetHosts = resourceFilter.getHostNames();
//Find target hosts to execute
if (targetHosts.isEmpty()) {
TargetHostType hostType = actionContext.getTargetType();
switch (hostType) {
case ALL:
targetHosts.addAll(candidateHosts);
break;
case ANY:
targetHosts.add(managementController.getHealthyHost(candidateHosts));
break;
case MAJORITY:
for (int i = 0; i < (candidateHosts.size() / 2) + 1; i++) {
String hostname = managementController.getHealthyHost(candidateHosts);
targetHosts.add(hostname);
candidateHosts.remove(hostname);
}
break;
default:
throw new AmbariException("Unsupported target type = " + hostType);
}
}
Set<Map<String, String>> maintenanceSCHs =
maintenanceStateHelper.getMaintenanceHostComponents(clusters, cluster);
//create tasks for each host
for (String hostName : targetHosts) {
stage.addHostRoleExecutionCommand(hostName,
Role.valueOf(actionContext.getActionName()), RoleCommand.ACTIONEXECUTE,
new ServiceComponentHostOpInProgressEvent(actionContext.getActionName(),
hostName, System.currentTimeMillis()), clusterName,
serviceName);
Map<String, Map<String, String>> configurations = new TreeMap<String, Map<String, String>>();
Map<String, Map<String, String>> configTags = null;
if (!serviceName.isEmpty()) {
configTags = managementController.findConfigurationTagsWithOverrides(cluster, hostName);
}
Map<String, String> commandParams = actionContext.getParameters();
commandParams.put(COMMAND_TIMEOUT, actionContext.getTimeout().toString());
commandParams.put(SCRIPT, actionName + ".py");
commandParams.put(SCRIPT_TYPE, TYPE_PYTHON);
commandParams.put(SCHEMA_VERSION, AmbariMetaInfo.SCHEMA_VERSION_2);
ExecutionCommand execCmd = stage.getExecutionCommandWrapper(hostName,
actionContext.getActionName()).getExecutionCommand();
/*
* TODO Execution command field population should be (partially?)
* combined with the same code at createHostAction()
*/
execCmd.setConfigurations(configurations);
execCmd.setConfigurationTags(configTags);
execCmd.setHostLevelParams(hostLevelParams);
execCmd.setCommandParams(commandParams);
execCmd.setServiceName(serviceName == null || serviceName.isEmpty() ?
resourceFilter.getServiceName() : serviceName);
execCmd.setComponentName(componentName == null || componentName.isEmpty() ?
resourceFilter.getComponentName() : componentName);
Map<String, String> roleParams = execCmd.getRoleParams();
if (roleParams == null) {
roleParams = new TreeMap<String, String>();
}