use of com.cloud.legacymodel.communication.command.Command 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();
this._futures.add(this._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];
this._futures.add(this._agentMgr.getCronJobPool().scheduleAtFixedRate(new CronTask(req), cmd.getInterval(), cmd.getInterval(), TimeUnit.SECONDS));
}
}
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class StorageManagerImpl method sendToPool.
@Override
public Pair<Long, Answer[]> sendToPool(final StoragePool pool, final long[] hostIdsToTryFirst, final List<Long> hostIdsToAvoid, final Commands cmds) throws StorageUnavailableException {
final List<Long> hostIds = getUpHostsInPool(pool.getId());
Collections.shuffle(hostIds);
if (hostIdsToTryFirst != null) {
for (int i = hostIdsToTryFirst.length - 1; i >= 0; i--) {
if (hostIds.remove(hostIdsToTryFirst[i])) {
hostIds.add(0, hostIdsToTryFirst[i]);
}
}
}
if (hostIdsToAvoid != null) {
hostIds.removeAll(hostIdsToAvoid);
}
if (hostIds == null || hostIds.isEmpty()) {
throw new StorageUnavailableException("Unable to send command to the pool " + pool.getId() + " due to there is no enabled hosts up in this cluster", pool.getId());
}
for (final Long hostId : hostIds) {
try {
final List<Answer> answers = new ArrayList<>();
final Command[] cmdArray = cmds.toCommands();
for (final Command cmd : cmdArray) {
final long targetHostId = this._hvGuruMgr.getGuruProcessedCommandTargetHost(hostId, cmd);
answers.add(this._agentMgr.send(targetHostId, cmd));
}
return new Pair<>(hostId, answers.toArray(new Answer[answers.size()]));
} catch (final AgentUnavailableException e) {
s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
} catch (final OperationTimedoutException e) {
s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
}
}
throw new StorageUnavailableException("Unable to send command to the pool ", pool.getId());
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method attachISOToVM.
private boolean attachISOToVM(final long vmId, final long isoId, final boolean attach) {
final UserVmVO vm = this._userVmDao.findById(vmId);
if (vm == null) {
return false;
} else if (vm.getState() != State.Running) {
return true;
}
// prepare ISO ready to mount on hypervisor resource level
final TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId());
final String vmName = vm.getInstanceName();
final HostVO host = this._hostDao.findById(vm.getHostId());
if (host == null) {
s_logger.warn("Host: " + vm.getHostId() + " does not exist");
return false;
}
final DataTO isoTO = tmplt.getTO();
final DiskTO disk = new DiskTO(isoTO, null, null, VolumeType.ISO);
final Command cmd;
if (attach) {
cmd = new AttachCommand(disk, vmName);
} else {
cmd = new DettachCommand(disk, vmName);
}
final Answer a = this._agentMgr.easySend(vm.getHostId(), cmd);
return a != null && a.getResult();
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class Agent method sendStartup.
public void sendStartup(final Link link) {
final StartupCommand[] startup = this.resource.initialize();
if (startup != null) {
final Command[] commands = new Command[startup.length];
for (int i = 0; i < startup.length; i++) {
setupStartupCommand(startup[i]);
commands[i] = startup[i];
}
final Request request = new Request(this._id != null ? this._id : -1, -1, commands, false, false);
request.setSequence(getNextSequence());
if (logger.isDebugEnabled()) {
logger.debug("Sending Startup: " + request.toString());
}
lockStartupTask(link);
try {
link.send(request.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send request: " + request.toString());
}
}
}
use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.
the class Agent method processOtherTask.
public void processOtherTask(final Task task) {
final Object obj = task.get();
if (obj instanceof Response) {
if (System.currentTimeMillis() - this._lastPingResponseTime > this._pingInterval * this.agentConfiguration.getPingRetries()) {
logger.error("Ping Interval has gone past " + this._pingInterval * this.agentConfiguration.getPingRetries() + ". Won't reconnect to mgt server, as connection is still alive");
return;
}
final PingCommand ping = this.resource.getCurrentStatus(getId());
final Request request = new Request(this._id, -1, ping, false);
request.setSequence(getNextSequence());
if (logger.isDebugEnabled()) {
logger.debug("Sending ping: " + request.toString());
}
try {
task.getLink().send(request.toBytes());
// if i can send pingcommand out, means the link is ok
setLastPingResponseTime();
} catch (final ClosedChannelException e) {
logger.warn("Unable to send request: " + request.toString());
}
} else if (obj instanceof Request) {
final Request req = (Request) obj;
final Command command = req.getCommand();
if (command.getContextParam("logid") != null) {
MDC.put("logcontextid", command.getContextParam("logid"));
}
Answer answer = null;
this._inProgress.incrementAndGet();
try {
answer = this.resource.executeRequest(command);
} finally {
this._inProgress.decrementAndGet();
}
if (answer != null) {
final Response response = new Response(req, answer);
if (logger.isDebugEnabled()) {
logger.debug("Watch Sent: " + response.toString());
}
try {
task.getLink().send(response.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send response: " + response.toString());
}
}
} else {
logger.warn("Ignoring an unknown task");
}
}
Aggregations