Search in sources :

Example 1 with Command

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());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) Response(com.cloud.common.transport.Response) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) IAgentControlListener(com.cloud.common.agent.IAgentControlListener)

Example 2 with Command

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);
}
Also used : UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) MigrateCommand(com.cloud.legacymodel.communication.command.MigrateCommand) ScaleVmCommand(com.cloud.legacymodel.communication.command.ScaleVmCommand) ShutdownEventCommand(com.cloud.legacymodel.communication.command.agentcontrol.ShutdownEventCommand) StopCommand(com.cloud.legacymodel.communication.command.StopCommand) RestoreVMSnapshotCommand(com.cloud.legacymodel.communication.command.RestoreVMSnapshotCommand) AttachOrDettachConfigDriveCommand(com.cloud.legacymodel.communication.command.AttachOrDettachConfigDriveCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) CheckVirtualMachineCommand(com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) RebootCommand(com.cloud.legacymodel.communication.command.RebootCommand) PrepareForMigrationCommand(com.cloud.legacymodel.communication.command.PrepareForMigrationCommand) ClusterVMMetaDataSyncCommand(com.cloud.legacymodel.communication.command.ClusterVMMetaDataSyncCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) Commands(com.cloud.agent.manager.Commands)

Example 3 with Command

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;
}
Also used : PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) MigrateCommand(com.cloud.legacymodel.communication.command.MigrateCommand) ScaleVmCommand(com.cloud.legacymodel.communication.command.ScaleVmCommand) ShutdownEventCommand(com.cloud.legacymodel.communication.command.agentcontrol.ShutdownEventCommand) StopCommand(com.cloud.legacymodel.communication.command.StopCommand) RestoreVMSnapshotCommand(com.cloud.legacymodel.communication.command.RestoreVMSnapshotCommand) AttachOrDettachConfigDriveCommand(com.cloud.legacymodel.communication.command.AttachOrDettachConfigDriveCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) CheckVirtualMachineCommand(com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) RebootCommand(com.cloud.legacymodel.communication.command.RebootCommand) PrepareForMigrationCommand(com.cloud.legacymodel.communication.command.PrepareForMigrationCommand) ClusterVMMetaDataSyncCommand(com.cloud.legacymodel.communication.command.ClusterVMMetaDataSyncCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand)

Example 4 with Command

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;
}
Also used : ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand) StartupSecondaryStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) CheckHealthCommand(com.cloud.legacymodel.communication.command.CheckHealthCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) StartupProxyCommand(com.cloud.legacymodel.communication.command.startup.StartupProxyCommand)

Example 5 with Command

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));
}
Also used : StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) StartupProxyCommand(com.cloud.legacymodel.communication.command.startup.StartupProxyCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Response(com.cloud.common.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand) StartupSecondaryStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) CheckHealthCommand(com.cloud.legacymodel.communication.command.CheckHealthCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) StartupProxyCommand(com.cloud.legacymodel.communication.command.startup.StartupProxyCommand) StartupSecondaryStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)

Aggregations

Command (com.cloud.legacymodel.communication.command.Command)21 Answer (com.cloud.legacymodel.communication.answer.Answer)13 StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)10 AgentControlCommand (com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand)9 PingCommand (com.cloud.legacymodel.communication.command.PingCommand)8 ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)7 ShutdownCommand (com.cloud.legacymodel.communication.command.ShutdownCommand)7 StartupRoutingCommand (com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)7 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)7 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)7 PingRoutingCommand (com.cloud.legacymodel.communication.command.PingRoutingCommand)6 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)5 Response (com.cloud.common.transport.Response)4 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)4 CheckHealthCommand (com.cloud.legacymodel.communication.command.CheckHealthCommand)4 CronCommand (com.cloud.legacymodel.communication.command.CronCommand)4 MaintainCommand (com.cloud.legacymodel.communication.command.MaintainCommand)4 StartupProxyCommand (com.cloud.legacymodel.communication.command.startup.StartupProxyCommand)4 StartupSecondaryStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand)4 StartupStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupStorageCommand)4