use of com.cloud.legacymodel.communication.command.ReadyCommand 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.ReadyCommand in project cosmic by MissionCriticalCloud.
the class Agent method processReadyCommand.
public void processReadyCommand(final Command cmd) {
final ReadyCommand ready = (ReadyCommand) cmd;
logger.info("Process agent ready command, agent id = " + ready.getHostId());
if (ready.getHostId() != null) {
setId(ready.getHostId());
}
logger.info("Ready command is processed: agent id = " + getId());
}
use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testReadyCommand.
@Test
public void testReadyCommand() {
final ReadyCommand command = new ReadyCommand(1l);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
}
use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.
the class AgentManagerImpl method notifyMonitorsOfConnection.
protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, final StartupCommand[] cmd, final boolean forRebalance) throws ConnectionException {
final long hostId = attache.getId();
final HostVO host = this._hostDao.findById(hostId);
for (final Pair<Integer, Listener> monitor : this._hostMonitors) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Sending Connect to listener: " + monitor.second().getClass().getSimpleName());
}
try {
monitor.second().processConnect(host, cmd, forRebalance);
} catch (final Exception e) {
if (e instanceof ConnectionException) {
final ConnectionException ce = (ConnectionException) e;
if (ce.isSetupError()) {
s_logger.warn("Monitor " + monitor.second().getClass().getSimpleName() + " says there is an error in the connect process for " + hostId + " due to " + e.getMessage());
handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
throw ce;
} else {
s_logger.info("Monitor " + monitor.second().getClass().getSimpleName() + " says not to continue the connect process for " + hostId + " due to " + e.getMessage());
handleDisconnectWithoutInvestigation(attache, Event.ShutdownRequested, true, true);
return attache;
}
} else if (e instanceof HypervisorVersionChangedException) {
handleDisconnectWithoutInvestigation(attache, Event.ShutdownRequested, true, true);
throw new CloudRuntimeException("Unable to connect " + attache.getId(), e);
} else {
s_logger.error("Monitor " + monitor.second().getClass().getSimpleName() + " says there is an error in the connect process for " + hostId + " due to " + e.getMessage(), e);
handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
throw new CloudRuntimeException("Unable to connect " + attache.getId(), e);
}
}
}
final Long dcId = host.getDataCenterId();
final ReadyCommand ready = new ReadyCommand(dcId, host.getId());
final Answer answer = easySend(hostId, ready);
if (answer == null || !answer.getResult()) {
// this is tricky part for secondary storage
// make it as disconnected, wait for secondary storage VM to be up
// return the attache instead of null, even it is disconnectede
handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
}
agentStatusTransitTo(host, Event.Ready, this._nodeId);
attache.ready();
return attache;
}
use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.
the class AgentManagerImpl method handleConnectedAgent.
private AgentAttache handleConnectedAgent(final Link link, final StartupCommand[] startup, final Request request) {
AgentAttache attache = null;
ReadyCommand ready = null;
try {
final HostVO host = this._resourceMgr.createHostVOForConnectedAgent(startup);
if (host != null) {
ready = new ReadyCommand(host.getDataCenterId(), host.getId());
attache = createAttacheForConnect(host, link);
attache = notifyMonitorsOfConnection(attache, startup, false);
}
} catch (final Exception e) {
s_logger.debug("Failed to handle host connection: " + e.toString());
ready = new ReadyCommand(null);
ready.setDetails(e.toString());
} finally {
if (ready == null) {
ready = new ReadyCommand(null);
}
}
try {
if (attache == null) {
final Request readyRequest = new Request(-1, -1, ready, false);
link.send(readyRequest.getBytes());
} else {
easySend(attache.getId(), ready);
}
} catch (final Exception e) {
s_logger.debug("Failed to send ready command:" + e.toString());
}
return attache;
}
Aggregations