use of com.cloud.agent.transport.Request in project CloudStack-archive by CloudStack-extras.
the class Agent method sendRequest.
@Override
public AgentControlAnswer sendRequest(AgentControlCommand cmd, int timeoutInMilliseconds) throws AgentControlChannelException {
Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
request.setSequence(getNextSequence());
AgentControlListener listener = new AgentControlListener(request);
registerControlListener(listener);
try {
postRequest(request);
synchronized (listener) {
try {
listener.wait(timeoutInMilliseconds);
} catch (InterruptedException e) {
s_logger.warn("sendRequest is interrupted, exit waiting");
}
}
return listener.getAnswer();
} finally {
unregisterControlListener(listener);
}
}
use of com.cloud.agent.transport.Request in project CloudStack-archive by CloudStack-extras.
the class Agent method postRequest.
@Override
public void postRequest(AgentControlCommand cmd) throws AgentControlChannelException {
Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
request.setSequence(getNextSequence());
postRequest(request);
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class ResourceManagerImpl method createHostAndAgent.
private Host createHostAndAgent(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) {
HostVO host = null;
StartupCommand[] cmds = null;
boolean hostExists = false;
boolean created = false;
try {
cmds = resource.initialize();
if (cmds == null) {
s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
return null;
}
/* Generate a random version in a dev setup situation */
if (this.getClass().getPackage().getImplementationVersion() == null) {
for (final StartupCommand cmd : cmds) {
if (cmd.getVersion() == null) {
cmd.setVersion(Long.toString(System.currentTimeMillis()));
}
}
}
if (s_logger.isDebugEnabled()) {
new Request(-1l, -1l, cmds, true, false).logD("Startup request from directly connected host: ", true);
}
if (old) {
final StartupCommand firstCmd = cmds[0];
host = findHostByGuid(firstCmd.getGuid());
if (host == null) {
host = findHostByGuid(firstCmd.getGuidWithoutResource());
}
if (host != null && host.getRemoved() == null) {
// host already added, no need to add again
s_logger.debug("Found the host " + host.getId() + " by guid: " + firstCmd.getGuid() + ", old host reconnected as new");
// ensures that host status is left unchanged in case of adding same one again
hostExists = true;
return null;
}
}
// find out if the host we want to connect to is new (so we can send an event)
boolean newHost = getNewHost(cmds) == null;
host = createHostVO(cmds, resource, details, hostTags, ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_DIRECT_CONNECT);
if (host != null) {
created = _agentMgr.handleDirectConnectAgent(host, cmds, resource, forRebalance, newHost);
/* reload myself from database */
host = _hostDao.findById(host.getId());
}
} catch (final Exception e) {
s_logger.warn("Unable to connect due to ", e);
} finally {
if (hostExists) {
if (cmds != null) {
resource.disconnected();
}
} else {
if (!created) {
if (cmds != null) {
resource.disconnected();
}
markHostAsDisconnected(host, cmds);
}
}
}
return host;
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class Agent method stop.
public void stop(final String reason, final String detail) {
s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
if (_connection != null) {
final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
try {
if (_link != null) {
final Request req = new Request(_id != null ? _id : -1, -1, cmd, false);
_link.send(req.toBytes());
}
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send: " + cmd.toString());
} catch (final Exception e) {
s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
}
s_logger.debug("Sending shutdown to management server");
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
s_logger.debug("Who the heck interrupted me here?");
}
_connection.stop();
_connection = null;
}
if (_resource != null) {
_resource.stop();
_resource = null;
}
_ugentTaskPool.shutdownNow();
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
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 = _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