}
componentNames.get(request.getClusterName())
.get(request.getServiceName()).add(request.getComponentName());
Service s = cluster.getService(request.getServiceName());
ServiceComponent sc = s.getServiceComponent(
request.getComponentName());
State oldState = sc.getDesiredState();
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 (request.getConfigVersions() != null) {
safeToUpdateConfigsForServiceComponent(sc, oldState, newState);
for (Entry<String,String> entry :
request.getConfigVersions().entrySet()) {
Config config = cluster.getConfig(
entry.getKey(), entry.getValue());
if (null == config) {
// throw error for invalid config
throw new AmbariException("Trying to update servicecomponent with"
+ " invalid configs"
+ ", clusterName=" + cluster.getClusterName()
+ ", clusterId=" + cluster.getClusterId()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", invalidConfigType=" + entry.getKey()
+ ", invalidConfigTag=" + entry.getValue());
}
}
}
if (newState == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Nothing to do for new updateServiceComponent request"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + request.getServiceName()
+ ", componentName=" + request.getComponentName()
+ ", newDesiredState=null");
}
continue;
}
if (sc.isClientComponent() &&
!newState.isValidClientComponentState()) {
throw new AmbariException("Invalid desired state for a client"
+ " component");
}
seenNewStates.add(newState);
State oldScState = sc.getDesiredState();
if (newState != oldScState) {
if (!isValidDesiredStateTransition(oldScState, newState)) {
// FIXME throw correct error
throw new AmbariException("Invalid transition for"
+ " servicecomponent"
+ ", clusterName=" + cluster.getClusterName()
+ ", clusterId=" + cluster.getClusterId()
+ ", serviceName=" + sc.getServiceName()
+ ", componentName=" + sc.getName()
+ ", currentDesiredState=" + oldScState
+ ", newDesiredState=" + newState);
}
if (!changedComps.containsKey(newState)) {
changedComps.put(newState, new ArrayList<ServiceComponent>());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Handling update to ServiceComponent"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + s.getName()
+ ", componentName=" + sc.getName()
+ ", currentDesiredState=" + oldScState
+ ", newDesiredState=" + newState);
}
changedComps.get(newState).add(sc);
}
for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
State oldSchState = sch.getState();
if (oldSchState == State.MAINTENANCE || oldSchState == State.UNKNOWN) {
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 (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)) {
// FIXME throw correct error
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 (!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?
// TODO if all components reach a common state, should service state be
// modified?
for (ServiceComponentRequest request : requests) {
Cluster cluster = clusters.getCluster(request.getClusterName());
Service s = cluster.getService(request.getServiceName());
ServiceComponent sc = s.getServiceComponent(
request.getComponentName());
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()) {
sc.updateDesiredConfigs(updated);
for (ServiceComponentHost sch :
sc.getServiceComponentHosts().values()) {
sch.deleteDesiredConfigs(updated.keySet());
sch.persist();
}
sc.persist();
}
}
}
Cluster cluster = clusters.getCluster(clusterNames.iterator().next());