Cleanup of worker VMs left over from previous session in a reliable way

This commit is contained in:
Kelven Yang 2011-01-31 17:40:07 -08:00
parent 930cf13894
commit ddda5fc431
5 changed files with 35 additions and 15 deletions

View File

@ -612,7 +612,8 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory,
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(cmd.getHypervisor());
if (hypervisorType == null) {
throw new InvalidParameterValueException("Please specify a valid hypervisor name");
s_logger.error("Unable to resolve " + cmd.getHypervisor() + " to a valid supported hypervisor type");
throw new InvalidParameterValueException("Unable to resolve " + cmd.getHypervisor() + " to a supported ");
}
Cluster.ClusterType clusterType = null;
@ -625,8 +626,8 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory,
Discoverer discoverer = getMatchingDiscover(hypervisorType);
if (discoverer == null) {
throw new InvalidParameterValueException(
"Please specify a valid hypervisor");
throw new InvalidParameterValueException("Could not find corresponding resource manager for " + cmd.getHypervisor());
}
List<ClusterVO> result = new ArrayList<ClusterVO>();
@ -664,8 +665,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory,
uri = new URI(UriUtils.encodeURIComponent(url));
if (uri.getScheme() == null) {
throw new InvalidParameterValueException(
"uri.scheme is null " + url
+ ", add http:// as a prefix");
"uri.scheme is null " + url + ", add http:// as a prefix");
} else if (uri.getScheme().equalsIgnoreCase("http")) {
if (uri.getHost() == null
|| uri.getHost().equalsIgnoreCase("")

View File

@ -8,7 +8,8 @@ import org.apache.log4j.Logger;
import com.cloud.maid.dao.StackMaidDao;
import com.cloud.maid.dao.StackMaidDaoImpl;
import com.cloud.serializer.SerializerHelper;
import com.cloud.utils.ActionDelegate;
import com.cloud.utils.CleanupDelegate;
import com.cloud.utils.db.Transaction;
public class StackMaid {
protected final static Logger s_logger = Logger.getLogger(StackMaid.class);
@ -43,6 +44,16 @@ public class StackMaid {
public Object getContext(String key) {
return context.get(key);
}
public void expungeMaidItem(long maidId) {
// this is a bit ugly, but when it is not loaded by component locator, this is just a workable way for now
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
maidDao.expunge(maidId);
} finally {
txn.close();
}
}
public int push(String delegateClzName, Object context) {
assert(msid_setby_manager != 0) : "Fatal, make sure StackMaidManager is loaded";
@ -98,13 +109,15 @@ public class StackMaid {
context.clear();
}
public static void doCleanup(StackMaidVO maid) {
public static boolean doCleanup(StackMaidVO maid) {
if(maid.getDelegate() != null) {
try {
Class<?> clz = Class.forName(maid.getDelegate());
Object delegate = clz.newInstance();
if(delegate instanceof ActionDelegate) {
((ActionDelegate)delegate).action(SerializerHelper.fromSerializedString(maid.getContext()));
if(delegate instanceof CleanupDelegate) {
return ((CleanupDelegate)delegate).cleanup(SerializerHelper.fromSerializedString(maid.getContext()), maid);
} else {
assert(false);
}
} catch (final ClassNotFoundException e) {
s_logger.error("Unable to load StackMaid delegate class: " + maid.getDelegate(), e);
@ -117,6 +130,9 @@ public class StackMaid {
} catch (final IllegalAccessException e) {
s_logger.error("Illegal access exception when loading resource: " + maid.getDelegate());
}
return false;
}
return true;
}
}

View File

@ -51,8 +51,8 @@ public class StackMaidManagerImpl implements StackMaidManager {
private void cleanupLeftovers(List<StackMaidVO> l) {
for(StackMaidVO maid : l) {
StackMaid.doCleanup(maid);
_maidDao.expunge(maid.getId());
if(StackMaid.doCleanup(maid))
_maidDao.expunge(maid.getId());
}
}

View File

@ -2,13 +2,12 @@ package com.cloud.async;
import org.apache.log4j.Logger;
import com.cloud.utils.ActionDelegate;
public class CleanupDelegate implements ActionDelegate<String> {
public class CleanupDelegate implements com.cloud.utils.CleanupDelegate<String, Object> {
private static final Logger s_logger = Logger.getLogger(CleanupDelegate.class);
@Override
public void action(String param) {
public boolean cleanup(String param, Object managerContext) {
s_logger.info("Action called with param: " + param);
return true;
}
}

View File

@ -0,0 +1,5 @@
package com.cloud.utils;
public interface CleanupDelegate<T, M> {
boolean cleanup(T itemContext, M managerContext);
}