use of com.cloud.agent.transport.Response in project CloudStack-archive by CloudStack-extras.
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;
try {
if (s_logger.isDebugEnabled()) {
// this is a hack to make sure we do NOT log the ssh keys
if ((cmd instanceof ModifySshKeysCommand)) {
s_logger.debug("Received the request for command: ModifySshKeysCommand");
} else {
if (// ensures request is logged only once per method call
!requestLogged) {
s_logger.debug("Request:" + request.toString());
requestLogged = true;
}
}
s_logger.debug("Processing command: " + cmd.toString());
}
if (cmd instanceof CronCommand) {
final CronCommand watch = (CronCommand) cmd;
scheduleWatch(link, request, watch.getInterval() * 1000, watch.getInterval() * 1000);
answer = new Answer(cmd, true, null);
} else if (cmd instanceof UpgradeCommand) {
final UpgradeCommand upgrade = (UpgradeCommand) cmd;
answer = upgradeAgent(upgrade.getUpgradeUrl(), upgrade);
} else if (cmd instanceof ShutdownCommand) {
ShutdownCommand shutdown = (ShutdownCommand) cmd;
s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
cancelTasks();
_reconnectAllowed = false;
answer = new Answer(cmd, true, null);
} else if (cmd instanceof MaintainCommand) {
s_logger.debug("Received maintainCommand");
cancelTasks();
_reconnectAllowed = false;
answer = new MaintainAnswer((MaintainCommand) cmd);
} else if (cmd instanceof AgentControlCommand) {
answer = null;
synchronized (_controlListeners) {
for (IAgentControlListener listener : _controlListeners) {
answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
if (answer != null) {
break;
}
}
}
if (answer == null) {
s_logger.warn("No handler found to process cmd: " + cmd.toString());
answer = new AgentControlAnswer(cmd);
}
} else {
_inProgress.incrementAndGet();
try {
answer = _resource.executeRequest(cmd);
} finally {
_inProgress.decrementAndGet();
}
if (answer == null) {
s_logger.debug("Response: unsupported command" + cmd.toString());
answer = Answer.createUnsupportedCommandAnswer(cmd);
}
}
} catch (final Throwable th) {
s_logger.warn("Caught: ", th);
final StringWriter writer = new StringWriter();
th.printStackTrace(new PrintWriter(writer));
answer = new Answer(cmd, false, writer.toString());
}
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);
} finally {
if (s_logger.isDebugEnabled()) {
s_logger.debug(response != null ? response.toString() : "response is null");
}
if (response != null) {
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send response: " + response.toString());
}
}
}
}
use of com.cloud.agent.transport.Response in project cloudstack by apache.
the class AgentAttache method send.
public Answer[] send(final Request req, final int wait) throws AgentUnavailableException, OperationTimedoutException {
SynchronousListener sl = new SynchronousListener(null);
long seq = req.getSequence();
send(req, sl);
try {
for (int i = 0; i < 2; i++) {
Answer[] answers = null;
try {
answers = sl.waitFor(wait);
} catch (final InterruptedException e) {
s_logger.debug(log(seq, "Interrupted"));
}
if (answers != null) {
if (s_logger.isDebugEnabled()) {
new Response(req, answers).logD("Received: ", false);
}
return answers;
}
// Try it again.
answers = sl.getAnswers();
if (answers != null) {
if (s_logger.isDebugEnabled()) {
new Response(req, answers).logD("Received after timeout: ", true);
}
_agentMgr.notifyAnswersToMonitors(_id, seq, answers);
return answers;
}
final Long current = _currentSequence;
if (current != null && seq != current) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Waited too long."));
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Waiting some more time because this is the current command"));
}
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait * 2, true);
} catch (OperationTimedoutException e) {
s_logger.warn(log(seq, "Timed out on " + req.toString()));
cancel(seq);
final Long current = _currentSequence;
if (req.executeInSequence() && (current != null && current == seq)) {
sendNext(seq);
}
throw e;
} catch (Exception e) {
s_logger.warn(log(seq, "Exception while waiting for answer"), e);
cancel(seq);
final Long current = _currentSequence;
if (req.executeInSequence() && (current != null && current == seq)) {
sendNext(seq);
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
} finally {
unregisterListener(seq);
}
}
use of com.cloud.agent.transport.Response in project cloudstack by apache.
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, mgmtServiceConf.getPingInterval());
break;
}
}
Response response = null;
response = new Response(request, answers[0], _nodeId, -1);
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.debug("Failed to send startupanswer: " + e.toString());
}
_connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
use of com.cloud.agent.transport.Response 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();
_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();
_reconnectAllowed = false;
answer = new MaintainAnswer((MaintainCommand) cmd);
} else if (cmd instanceof AgentControlCommand) {
answer = null;
synchronized (_controlListeners) {
for (final IAgentControlListener listener : _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);
}
_inProgress.incrementAndGet();
try {
answer = resource.executeRequest(cmd);
} finally {
_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);
} 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.agent.transport.Response in project cosmic by MissionCriticalCloud.
the class DirectAgentAttache method send.
@Override
public void send(final Request req) throws AgentUnavailableException {
req.logD("Executing: ", true);
if (req instanceof Response) {
final Response resp = (Response) req;
final Answer[] answers = resp.getAnswers();
if (answers != null && answers[0] instanceof StartupAnswer) {
final StartupAnswer startup = (StartupAnswer) answers[0];
final int interval = startup.getPingInterval();
_futures.add(_agentMgr.getCronJobPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
}
} else {
final Command[] cmds = req.getCommands();
if (cmds.length > 0 && !(cmds[0] instanceof CronCommand)) {
queueTask(new Task(req));
scheduleFromQueue();
} else {
final CronCommand cmd = (CronCommand) cmds[0];
_futures.add(_agentMgr.getCronJobPool().scheduleAtFixedRate(new CronTask(req), cmd.getInterval(), cmd.getInterval(), TimeUnit.SECONDS));
}
}
}
Aggregations