use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class Agent method processRequest.
protected void processRequest(final Request request, final Link link) {
boolean requestLogged = false;
Response response = null;
try {
final Command[] cmds = request.getCommands();
final Answer[] answers = new Answer[cmds.length];
for (int i = 0; i < cmds.length; i++) {
final Command cmd = cmds[i];
Answer answer;
if (cmd.getContextParam("logid") != null) {
MDC.put("logcontextid", cmd.getContextParam("logid"));
}
if (// ensures request is logged only once per method call
!requestLogged) {
final String requestMsg = request.toString();
if (requestMsg != null) {
logger.debug("Request:" + requestMsg);
}
requestLogged = true;
}
logger.debug("Processing command: " + cmd.toString());
if (cmd instanceof CronCommand) {
final CronCommand watch = (CronCommand) cmd;
scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
answer = new Answer(cmd, true, null);
} else if (cmd instanceof ShutdownCommand) {
final ShutdownCommand shutdown = (ShutdownCommand) cmd;
logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
cancelTasks();
this._reconnectAllowed = false;
answer = new Answer(cmd, true, null);
} else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
System.exit(1);
return;
} else if (cmd instanceof MaintainCommand) {
logger.debug("Received maintainCommand");
cancelTasks();
this._reconnectAllowed = false;
answer = new MaintainAnswer((MaintainCommand) cmd);
} else if (cmd instanceof AgentControlCommand) {
answer = null;
synchronized (this._controlListeners) {
for (final IAgentControlListener listener : this._controlListeners) {
answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
if (answer != null) {
break;
}
}
}
if (answer == null) {
logger.warn("No handler found to process cmd: " + cmd.toString());
answer = new AgentControlAnswer(cmd);
}
} else {
if (cmd instanceof ReadyCommand) {
processReadyCommand(cmd);
}
this._inProgress.incrementAndGet();
try {
answer = this.resource.executeRequest(cmd);
} finally {
this._inProgress.decrementAndGet();
}
if (answer == null) {
logger.debug("Response: unsupported command" + cmd.toString());
answer = Answer.createUnsupportedCommandAnswer(cmd);
}
}
answers[i] = answer;
if (!answer.getResult() && request.stopOnError()) {
for (i++; i < cmds.length; i++) {
answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
}
break;
}
}
response = new Response(request, answers);
} catch (final RuntimeException e) {
logger.error("Error while handling request: " + e.getMessage());
logger.error(ExceptionUtils.getRootCauseMessage(e));
} finally {
if (logger.isDebugEnabled()) {
final String responseMsg = response.toString();
if (responseMsg != null) {
logger.debug(response.toString());
}
}
if (response != null) {
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send response: " + response.toString());
}
}
}
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImpl method advanceExpunge.
protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException {
if (vm == null || vm.getRemoved() != null) {
s_logger.debug("Unable to find vm or vm is destroyed: " + vm);
return;
}
advanceStop(vm.getUuid(), false);
vm = _vmDao.findByUuid(vm.getUuid());
try {
if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) {
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
throw new CloudRuntimeException("Unable to destroy " + vm);
}
} catch (final NoTransitionException e) {
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
throw new CloudRuntimeException("Unable to destroy " + vm, e);
}
s_logger.debug("Destroying vm " + vm);
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
s_logger.debug("Cleaning up NICS");
final List<Command> nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, profile.getNics());
_networkMgr.cleanupNics(profile);
s_logger.debug("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage");
final List<Command> volumeExpungeCommands = hvGuru.finalizeExpungeVolumes(vm);
final Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
if (volumeExpungeCommands != null && volumeExpungeCommands.size() > 0 && hostId != null) {
final Commands cmds = new Commands(Command.OnError.Stop);
for (final Command volumeExpungeCommand : volumeExpungeCommands) {
cmds.addCommand(volumeExpungeCommand);
}
_agentMgr.send(hostId, cmds);
if (!cmds.isSuccessful()) {
for (final Answer answer : cmds.getAnswers()) {
if (!answer.getResult()) {
s_logger.warn("Failed to expunge vm due to: " + answer.getDetails());
throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails());
}
}
}
}
if (hostId != null) {
volumeMgr.revokeAccess(vm.getId(), hostId);
}
// Clean up volumes based on the vm's instance id
volumeMgr.cleanupVolumes(vm.getId());
final VirtualMachineGuru guru = getVmGuru(vm);
guru.finalizeExpunge(vm);
// remove the overcommit detials from the uservm details
_uservmDetailsDao.removeDetails(vm.getId());
// send hypervisor-dependent commands before removing
final List<Command> finalizeExpungeCommands = hvGuru.finalizeExpunge(vm);
if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) {
if (hostId != null) {
final Commands cmds = new Commands(Command.OnError.Stop);
for (final Command command : finalizeExpungeCommands) {
cmds.addCommand(command);
}
if (nicExpungeCommands != null) {
for (final Command command : nicExpungeCommands) {
cmds.addCommand(command);
}
}
_agentMgr.send(hostId, cmds);
if (!cmds.isSuccessful()) {
for (final Answer answer : cmds.getAnswers()) {
if (!answer.getResult()) {
s_logger.warn("Failed to expunge vm due to: " + answer.getDetails());
throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails());
}
}
}
}
}
s_logger.debug("Expunged " + vm);
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImpl method processCommands.
@Override
public boolean processCommands(final long agentId, final long seq, final Command[] cmds) {
boolean processed = false;
for (final Command cmd : cmds) {
if (cmd instanceof PingRoutingCommand) {
final PingRoutingCommand ping = (PingRoutingCommand) cmd;
if (ping.getHostVmStateReport() != null) {
_syncMgr.processHostVmStatePingReport(agentId, ping.getHostVmStateReport());
}
// take the chance to scan VMs that are stuck in transitional states
// and are missing from the report
scanStalledVMInTransitionStateOnUpHost(agentId);
processed = true;
}
}
return processed;
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class AgentManagerImpl method checkForCommandsAndTag.
/**
* @param commands
* @return
*/
private Command[] checkForCommandsAndTag(final Commands commands) {
final Command[] cmds = commands.toCommands();
assert cmds.length > 0 : "Ask yourself this about a hundred times. Why am I sending zero length commands?";
setEmptyAnswers(commands, cmds);
for (final Command cmd : cmds) {
tagCommand(cmd);
}
return cmds;
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class AgentManagerImpl method connectAgent.
protected void connectAgent(final Link link, final Command[] cmds, final Request request) {
// send startupanswer to agent in the very beginning, so agent can move on without waiting for the answer for an undetermined time, if we put this logic into another
// thread pool.
final StartupAnswer[] answers = new StartupAnswer[cmds.length];
Command cmd;
for (int i = 0; i < cmds.length; i++) {
cmd = cmds[i];
if (cmd instanceof StartupRoutingCommand || cmd instanceof StartupProxyCommand || cmd instanceof StartupSecondaryStorageCommand || cmd instanceof StartupStorageCommand) {
answers[i] = new StartupAnswer((StartupCommand) cmds[i], 0, getPingInterval());
break;
}
}
Response response = null;
response = new Response(request, answers[0], this._nodeId, -1);
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.debug("Failed to send startupanswer: " + e.toString());
}
this._connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
Aggregations