Set<String> maintenanceClusters = new HashSet<String>();
for (ServiceComponentHostRequest request : requests) {
validateServiceComponentHostRequest(request);
Cluster cluster = clusters.getCluster(request.getClusterName());
if (StringUtils.isEmpty(request.getServiceName())) {
request.setServiceName(findServiceName(cluster, request.getComponentName()));
}
LOG.info("Received a updateHostComponent request"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + request.getServiceName()
+ ", componentName=" + request.getComponentName()
+ ", hostname=" + request.getHostname()
+ ", request=" + request);
clusterNames.add(request.getClusterName());
if (clusterNames.size() > 1) {
throw new IllegalArgumentException("Updates to multiple clusters is not"
+ " supported");
}
if (!hostComponentNames.containsKey(request.getClusterName())) {
hostComponentNames.put(request.getClusterName(),
new HashMap<String, Map<String, Set<String>>>());
}
if (!hostComponentNames.get(request.getClusterName())
.containsKey(request.getServiceName())) {
hostComponentNames.get(request.getClusterName()).put(
request.getServiceName(), new HashMap<String, Set<String>>());
}
if (!hostComponentNames.get(request.getClusterName())
.get(request.getServiceName())
.containsKey(request.getComponentName())) {
hostComponentNames.get(request.getClusterName())
.get(request.getServiceName()).put(request.getComponentName(),
new HashSet<String>());
}
if (hostComponentNames.get(request.getClusterName())
.get(request.getServiceName()).get(request.getComponentName())
.contains(request.getHostname())) {
throw new IllegalArgumentException("Invalid request contains duplicate"
+ " hostcomponents");
}
hostComponentNames.get(request.getClusterName())
.get(request.getServiceName()).get(request.getComponentName())
.add(request.getHostname());
Service s = cluster.getService(request.getServiceName());
ServiceComponent sc = s.getServiceComponent(
request.getComponentName());
ServiceComponentHost sch = sc.getServiceComponentHost(
request.getHostname());
State oldState = sch.getState();
State newState = null;
if (request.getDesiredState() != null) {
newState = State.valueOf(request.getDesiredState());
if (!newState.isValidDesiredState()) {
throw new IllegalArgumentException("Invalid arguments, invalid"
+ " desired state, desiredState=" + newState.toString());
}
}
if (null != request.getMaintenanceState()) {
MaintenanceStateHelper psh = injector.getInstance(MaintenanceStateHelper.class);
MaintenanceState newMaint = MaintenanceState.valueOf(request.getMaintenanceState());
MaintenanceState oldMaint = psh.getEffectiveState(sch);
if (newMaint != oldMaint) {
if (sc.isClientComponent()) {
throw new IllegalArgumentException("Invalid arguments, cannot set " +
"maintenance state on a client component");
} else if (newMaint.equals(MaintenanceState.IMPLIED_FROM_HOST)
|| newMaint.equals(MaintenanceState.IMPLIED_FROM_SERVICE)) {
throw new IllegalArgumentException("Invalid arguments, can only set " +
"maintenance state to one of " + EnumSet.of(MaintenanceState.OFF, MaintenanceState.ON));
} else {
sch.setMaintenanceState(newMaint);
maintenanceClusters.add(sch.getClusterName());
}
}
}
if (newState == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Nothing to do for new updateServiceComponentHost request"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + request.getServiceName()
+ ", componentName=" + request.getComponentName()
+ ", hostname=" + request.getHostname()
+ ", oldState=" + oldState
+ ", newDesiredState=null");
}
continue;
}
if (sc.isClientComponent() &&
!newState.isValidClientComponentState()) {
throw new IllegalArgumentException("Invalid desired state for a client"
+ " component");
}
seenNewStates.add(newState);
State oldSchState = sch.getState();
// Client component reinstall allowed
if (newState == oldSchState && !sc.isClientComponent()) {
ignoredScHosts.add(sch);
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring ServiceComponentHost"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", hostname=" + sch.getHostName()
+ ", currentState=" + oldSchState
+ ", newDesiredState=" + newState);
}
continue;
}
if (!State.isValidStateTransition(oldSchState, newState)) {
throw new AmbariException("Invalid transition for"
+ " servicecomponenthost"
+ ", clusterName=" + cluster.getClusterName()
+ ", clusterId=" + cluster.getClusterId()
+ ", serviceName=" + sch.getServiceName()
+ ", componentName=" + sch.getServiceComponentName()
+ ", hostname=" + sch.getHostName()
+ ", currentState=" + oldSchState
+ ", newDesiredState=" + newState);
}
if (isDirectTransition(oldSchState, newState)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Handling direct transition update to ServiceComponentHost"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", hostname=" + sch.getHostName()
+ ", currentState=" + oldSchState
+ ", newDesiredState=" + newState);
}
directTransitionScHosts.put(sch, newState);
} else {
if (!changedScHosts.containsKey(sc.getName())) {
changedScHosts.put(sc.getName(),
new HashMap<State, List<ServiceComponentHost>>());
}
if (!changedScHosts.get(sc.getName()).containsKey(newState)) {
changedScHosts.get(sc.getName()).put(newState,
new ArrayList<ServiceComponentHost>());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Handling update to ServiceComponentHost"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", hostname=" + sch.getHostName()
+ ", currentState=" + oldSchState
+ ", newDesiredState=" + newState);
}
changedScHosts.get(sc.getName()).get(newState).add(sch);
}
}
if (seenNewStates.size() > 1) {
// FIXME should we handle this scenario
throw new IllegalArgumentException("Cannot handle different desired"
+ " state changes for a set of service components at the same time");
}
// Perform direct transitions (without task generation)
for (Entry<ServiceComponentHost, State> entry : directTransitionScHosts.entrySet()) {
ServiceComponentHost componentHost = entry.getKey();
State newState = entry.getValue();
long timestamp = System.currentTimeMillis();
ServiceComponentHostEvent event;
componentHost.setDesiredState(newState);
switch (newState) {
case DISABLED:
event = new ServiceComponentHostDisableEvent(
componentHost.getServiceComponentName(),
componentHost.getHostName(),
timestamp);
break;
case INSTALLED:
event = new ServiceComponentHostRestoreEvent(
componentHost.getServiceComponentName(),
componentHost.getHostName(),
timestamp);
break;
default:
throw new AmbariException("Direct transition from " + componentHost.getState() + " to " + newState + " not supported");
}
try {
componentHost.handleEvent(event);
} catch (InvalidStateTransitionException e) {
//Should not occur, must be covered by previous checks
throw new AmbariException("Internal error - not supported transition", e);
}
}
if (maintenanceClusters.size() > 0) {
try {
maintenanceStateHelper.createRequests(this, requestProperties, maintenanceClusters);
} catch (Exception e) {
LOG.warn("Could not send maintenance status to Nagios (" + e.getMessage() + ")");
}
}
Cluster cluster = clusters.getCluster(clusterNames.iterator().next());
return createAndPersistStages(cluster, requestProperties, null, null, null,
changedScHosts, ignoredScHosts, runSmokeTest, false);
}