for (Entry<String, String> entry :
request.getConfigVersions().entrySet()) {
Config config = cluster.getConfig(
entry.getKey(), entry.getValue());
if (null == config) {
throw new AmbariException("Trying to update servicecomponenthost"
+ " with invalid configs"
+ ", clusterName=" + cluster.getClusterName()
+ ", clusterId=" + cluster.getClusterId()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", hostname=" + sch.getHostName()
+ ", invalidConfigType=" + entry.getKey()
+ ", invalidConfigTag=" + entry.getValue());
}
}
}
// If upgrade request comes without state information then its an error
boolean upgradeRequest = checkIfUpgradeRequestAndValidate(request, cluster, s, sc, sch);
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);
if (!processingUpgradeRequest && upgradeRequest) {
processingUpgradeRequest = true;
// this needs to be the first request
if (numberOfRequestsProcessed > 1) {
throw new AmbariException("An upgrade request cannot be combined with " +
"other non-upgrade requests.");
}
fromStackVersion = sch.getStackVersion();
}
if (processingUpgradeRequest) {
if (!upgradeRequest) {
throw new AmbariException("An upgrade request cannot be combined with " +
"other non-upgrade requests.");
}
sch.setState(State.UPGRADING);
sch.setDesiredStackVersion(cluster.getCurrentStackVersion());
}
State oldSchState = sch.getState();
if (newState == oldSchState) {
sch.setDesiredState(newState);
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 (!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 (newState == State.DELETED) {
// if (!sch.canBeRemoved()) {
// throw new AmbariException("Servicecomponenthost cannot be removed"
// + ", clusterName=" + cluster.getClusterName()
// + ", clusterId=" + cluster.getClusterId()
// + ", serviceName=" + sch.getServiceName()
// + ", componentName=" + sch.getServiceComponentName()
// + ", hostname=" + sch.getHostName()
// + ", currentState=" + oldSchState
// + ", newDesiredState=" + 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");
}
// TODO additional validation?
for (ServiceComponentHostRequest request : requests) {
Cluster cluster = clusters.getCluster(request.getClusterName());
Service s = cluster.getService(request.getServiceName());
ServiceComponent sc = s.getServiceComponent(
request.getComponentName());
ServiceComponentHost sch = sc.getServiceComponentHost(
request.getHostname());
if (request.getConfigVersions() != null) {
Map<String, Config> updated = new HashMap<String, Config>();
for (Entry<String, String> entry : request.getConfigVersions().entrySet()) {
Config config = cluster.getConfig(
entry.getKey(), entry.getValue());
updated.put(config.getType(), config);
if (!updated.isEmpty()) {
sch.updateDesiredConfigs(updated);
}
}
}
}
// 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 MAINTENANCE:
event = new ServiceComponentHostMaintenanceEvent(
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);
}
}
Cluster cluster = clusters.getCluster(clusterNames.iterator().next());