Bug 13038 - Host allocation state changes after the Management server restart

status 13038: resolved fixed
This commit is contained in:
frank 2012-01-16 16:40:45 -08:00
parent b20147a4b4
commit c6c711affe
4 changed files with 39 additions and 46 deletions

15
api/src/com/cloud/api/commands/UpdateHostCmd.java Normal file → Executable file
View File

@ -32,6 +32,7 @@ import com.cloud.api.BaseCmd.CommandType;
import com.cloud.api.response.HostResponse;
import com.cloud.host.Host;
import com.cloud.user.Account;
import com.cloud.utils.fsm.NoTransitionException;
@Implementation(description="Updates a host.", responseObject=HostResponse.class)
public class UpdateHostCmd extends BaseCmd {
@ -50,7 +51,7 @@ public class UpdateHostCmd extends BaseCmd {
@Parameter(name=ApiConstants.OS_CATEGORY_ID, type=CommandType.LONG, description="the id of Os category to update the host with")
private Long osCategoryId;
@Parameter(name=ApiConstants.ALLOCATION_STATE, type=CommandType.STRING, description="Allocation state of this Host for allocation of new resources")
@Parameter(name=ApiConstants.ALLOCATION_STATE, type=CommandType.STRING, description="Change resource state of host, valid values are [Enable, Disable]. Operation may failed if host in states not allowing Enable/Disable")
private String allocationState;
@Parameter(name=ApiConstants.HOST_TAGS, type=CommandType.LIST, collectionType=CommandType.STRING, description="list of tags to be added to the host")
@ -103,13 +104,15 @@ public class UpdateHostCmd extends BaseCmd {
@Override
public void execute(){
Host result = _resourceService.updateHost(this);
if (result != null) {
Host result;
try {
result = _resourceService.updateHost(this);
HostResponse hostResponse = _responseGenerator.createHostResponse(result);
hostResponse.setResponseName(getCommandName());
this.setResponseObject(hostResponse);
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update host");
}
} catch (Exception e) {
s_logger.debug("Failed to update host:" + getId(), e);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update host:" + getId() + "," + e.getMessage());
}
}
}

View File

@ -36,6 +36,7 @@ import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.org.Cluster;
import com.cloud.storage.Swift;
import com.cloud.utils.fsm.NoTransitionException;
public interface ResourceService {
/**
@ -44,8 +45,9 @@ public interface ResourceService {
* @param cmd
* - the command specifying hostId
* @return hostObject
* @throws NoTransitionException
*/
Host updateHost(UpdateHostCmd cmd);
Host updateHost(UpdateHostCmd cmd) throws NoTransitionException;
Host cancelMaintenance(CancelMaintenanceCmd cmd);

View File

@ -3,6 +3,7 @@ package com.cloud.resource;
import java.util.List;
import java.util.Set;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.fsm.StateMachine;
public enum ResourceState {
@ -40,6 +41,16 @@ public enum ResourceState {
public String getDescription() {
return this.comment;
}
public static Event toEvent(String e) {
if (Enable.toString().equals(e)) {
return Enable;
} else if (Disable.toString().equals(e)) {
return Disable;
}
return null;
}
}
public ResourceState getNextState(Event a) {

View File

@ -1119,18 +1119,26 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma
}
@Override
public Host updateHost(UpdateHostCmd cmd) {
public Host updateHost(UpdateHostCmd cmd) throws NoTransitionException {
Long hostId = cmd.getId();
Long guestOSCategoryId = cmd.getOsCategoryId();
// Verify that the host exists
HostVO host = _hostDao.findById(hostId);
if (host == null) {
throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist");
}
if (cmd.getAllocationState() != null) {
ResourceState.Event resourceEvent = ResourceState.Event.toEvent(cmd.getAllocationState());
if (resourceEvent != ResourceState.Event.Enable && resourceEvent != ResourceState.Event.Disable) {
throw new CloudRuntimeException("Invalid allocation state:" + cmd.getAllocationState() + ", only Enable/Disable are allowed");
}
resourceStateTransitTo(host, resourceEvent, _nodeId);
}
if (guestOSCategoryId != null) {
// Verify that the host exists
HostVO host = _hostDao.findById(hostId);
if (host == null) {
throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist");
}
// Verify that the guest OS Category exists
if (guestOSCategoryId > 0) {
if (_guestOSCategoryDao.findById(guestOSCategoryId) == null) {
@ -1150,40 +1158,9 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma
}
_hostDetailsDao.persist(hostId, hostDetails);
}
/*
String allocationState = cmd.getAllocationState();
if (allocationState != null) {
// Verify that the host exists
HostVO host = _hostDao.findById(hostId);
if (host == null) {
throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist");
}
try {
HostAllocationState newAllocationState = Host.HostAllocationState.valueOf(allocationState);
if (newAllocationState == null) {
s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State");
throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state");
} else {
host.setHostAllocationState(newAllocationState);
}
} catch (IllegalArgumentException ex) {
s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State");
throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state");
}
_hostDao.update(hostId, host);
}
*/
List<String> hostTags = cmd.getHostTags();
if (hostTags != null) {
// Verify that the host exists
HostVO host = _hostDao.findById(hostId);
if (host == null) {
throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist");
}
if(s_logger.isDebugEnabled()){
s_logger.debug("Updating Host Tags to :"+hostTags);
}