diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index f061e7598af..769141abde8 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -75,7 +75,6 @@ import com.cloud.exception.StorageUnavailableException; import com.cloud.host.Host.Type; import com.cloud.host.HostVO; import com.cloud.host.dao.HostDao; -import com.cloud.hypervisor.Hypervisor; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.info.ConsoleProxyConnectionInfo; import com.cloud.info.ConsoleProxyInfo; @@ -146,14 +145,14 @@ import com.google.gson.GsonBuilder; public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProxyService, Manager, AgentHook, VirtualMachineGuru { private static final Logger s_logger = Logger.getLogger(ConsoleProxyManagerImpl.class); - private static final int DEFAULT_CAPACITY_SCAN_INTERVAL = 30000; // 30 seconds - private static final int EXECUTOR_SHUTDOWN_TIMEOUT = 1000; // 1 second + private static final int DEFAULT_CAPACITY_SCAN_INTERVAL = 30000; // 30 seconds + private static final int EXECUTOR_SHUTDOWN_TIMEOUT = 1000; // 1 second - private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 3; // 3 seconds - private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC = 180; // 3 minutes + private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 3; // 3 seconds + private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC = 180; // 3 minutes - private static final int API_WAIT_TIMEOUT = 5000; // 5 seconds (in milliseconds) - private static final int STARTUP_DELAY = 60000; // 60 seconds + private static final int API_WAIT_TIMEOUT = 5000; // 5 seconds (in milliseconds) + private static final int STARTUP_DELAY = 60000; // 60 seconds private int _consoleProxyPort = ConsoleProxyManager.DEFAULT_PROXY_VNC_PORT; @@ -163,32 +162,48 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx private String _name; private Adapters _consoleProxyAllocators; - @Inject private ConsoleProxyDao _consoleProxyDao; - @Inject private DataCenterDao _dcDao; - @Inject private VMTemplateDao _templateDao; - @Inject private HostPodDao _podDao; - @Inject private HostDao _hostDao; - @Inject private ConfigurationDao _configDao; - @Inject private CertificateDao _certDao; - @Inject private VMInstanceDao _instanceDao; - @Inject private VMTemplateHostDao _vmTemplateHostDao; - @Inject private AgentManager _agentMgr; - @Inject private StorageManager _storageMgr; - @Inject NetworkManager _networkMgr; - @Inject AccountManager _accountMgr; - @Inject ServiceOfferingDao _offeringDao; - @Inject NetworkOfferingDao _networkOfferingDao; - + @Inject + private ConsoleProxyDao _consoleProxyDao; + @Inject + private DataCenterDao _dcDao; + @Inject + private VMTemplateDao _templateDao; + @Inject + private HostPodDao _podDao; + @Inject + private HostDao _hostDao; + @Inject + private ConfigurationDao _configDao; + @Inject + private CertificateDao _certDao; + @Inject + private VMInstanceDao _instanceDao; + @Inject + private VMTemplateHostDao _vmTemplateHostDao; + @Inject + private AgentManager _agentMgr; + @Inject + private StorageManager _storageMgr; + @Inject + NetworkManager _networkMgr; + @Inject + AccountManager _accountMgr; + @Inject + ServiceOfferingDao _offeringDao; + @Inject + NetworkOfferingDao _networkOfferingDao; + private ConsoleProxyListener _listener; private ServiceOfferingVO _serviceOffering; - + NetworkOfferingVO _publicNetworkOffering; NetworkOfferingVO _managementNetworkOffering; NetworkOfferingVO _linkLocalNetworkOffering; - @Inject private VirtualMachineManager _itMgr; - + @Inject + private VirtualMachineManager _itMgr; + private final ScheduledExecutorService _capacityScanScheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory("CP-Scan")); private final ExecutorService _requestHandlerScheduler = Executors.newCachedThreadPool(new NamedThreadFactory("Request-handler")); @@ -202,7 +217,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx private boolean _use_storage_vm; private boolean _disable_rp_filter = false; private String _instance; - + private int _proxySessionTimeoutValue = DEFAULT_PROXY_SESSION_TIMEOUT; private boolean _sslEnabled = false; @@ -240,17 +255,17 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx s_logger.info("Waiting for console proxy assignment is interrupted"); } } - + ConsoleProxyVO proxy = result.second(); if (proxy == null) { return null; } - - if(proxy.getPublicIpAddress() == null) { - s_logger.warn("Assigned console proxy does not have a valid public IP address"); - return null; + + if (proxy.getPublicIpAddress() == null) { + s_logger.warn("Assigned console proxy does not have a valid public IP address"); + return null; } - + return new ConsoleProxyInfo(proxy.isSslEnabled(), proxy.getPublicIpAddress(), _consoleProxyPort, proxy.getPort(), _configDao.getValue("consoleproxy.url.domain")); } @@ -270,8 +285,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx _allocProxyLock.unlock(); } } else { - s_logger.error("Unable to acquire synchronization lock to get/allocate proxy resource for vm :" + vmId - + ". Previous console proxy allocation is taking too long"); + s_logger.error("Unable to acquire synchronization lock to get/allocate proxy resource for vm :" + vmId + ". Previous console proxy allocation is taking too long"); } if (proxy == null) { @@ -301,7 +315,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } else { proxy.setPort(80); } - + return proxy; } } @@ -430,7 +444,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx return false; } } - + @Override public ConsoleProxyVO startProxy(long proxyVmId) { try { @@ -440,18 +454,19 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (proxy.getState() == VirtualMachine.State.Running) { return proxy; } - + String restart = _configDao.getValue(Config.ConsoleProxyRestart.key()); - if(restart != null && restart.equalsIgnoreCase("false")) { + if (restart != null && restart.equalsIgnoreCase("false")) { return null; } - - if(proxy.getState() == VirtualMachine.State.Stopped) { + + if (proxy.getState() == VirtualMachine.State.Stopped) { return _itMgr.start(proxy, null, systemUser, systemAcct); } - + // For VMs that are in Stopping, Starting, Migrating state, let client to wait by returning null - // as sooner or later, Starting/Migrating state will be transited to Running and Stopping will be transited to Stopped to allow + // as sooner or later, Starting/Migrating state will be transited to Running and Stopping will be transited to + // Stopped to allow // Starting of it return null; } catch (StorageUnavailableException e) { @@ -463,7 +478,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } catch (ResourceUnavailableException e) { s_logger.warn("Exception while trying to start console proxy", e); return null; - } catch(CloudRuntimeException e) { + } catch (CloudRuntimeException e) { s_logger.warn("Runtime Exception while trying to start console proxy", e); return null; } @@ -507,8 +522,9 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } public ConsoleProxyVO assignProxyFromStoppedPool(long dataCenterId) { - - // practically treat all console proxy VM that is not in Running state but can be entering into Running state as candidates + + // practically treat all console proxy VM that is not in Running state but can be entering into Running state as + // candidates // this is to prevent launching unneccessary console proxy VMs because of temporarily unavailable state List l = _consoleProxyDao.getProxyListInStates(dataCenterId, State.Starting, State.Stopped, State.Migrating, State.Stopping); if (l != null && l.size() > 0) { @@ -523,13 +539,13 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (s_logger.isDebugEnabled()) { s_logger.debug("Assign console proxy from a newly started instance for request from data center : " + dataCenterId); } - - if(!allowToLaunchNew(dataCenterId)) { + + if (!allowToLaunchNew(dataCenterId)) { s_logger.warn("The number of launched console proxy on zone " + dataCenterId + " has reached to limit"); return null; } HypervisorType currentHyp = currentHypervisorType(dataCenterId); - + Map context = createProxyInstance(dataCenterId, currentHyp); long proxyVmId = (Long) context.get("proxyVmId"); @@ -540,7 +556,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx return null; } - ConsoleProxyVO proxy = _consoleProxyDao.findById(proxyVmId); + ConsoleProxyVO proxy = _consoleProxyDao.findById(proxyVmId); if (proxy != null) { SubscriptionMgr.getInstance().notifySubscribers(ConsoleProxyManager.ALERT_SUBJECT, this, new ConsoleProxyAlertEventArgs(ConsoleProxyAlertEventArgs.PROXY_CREATED, dataCenterId, proxy.getId(), proxy, null)); @@ -550,11 +566,8 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx s_logger.debug("Unable to allocate console proxy storage, remove the console proxy record from DB, proxy id: " + proxyVmId); } - SubscriptionMgr.getInstance().notifySubscribers( - ConsoleProxyManager.ALERT_SUBJECT, - this, - new ConsoleProxyAlertEventArgs(ConsoleProxyAlertEventArgs.PROXY_CREATE_FAILURE, dataCenterId, proxyVmId, null, - "Unable to allocate storage")); + SubscriptionMgr.getInstance().notifySubscribers(ConsoleProxyManager.ALERT_SUBJECT, this, + new ConsoleProxyAlertEventArgs(ConsoleProxyAlertEventArgs.PROXY_CREATE_FAILURE, dataCenterId, proxyVmId, null, "Unable to allocate storage")); } return null; } @@ -565,14 +578,14 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx String name = VirtualMachineName.getConsoleProxyName(id, _instance); DataCenterVO dc = _dcDao.findById(dataCenterId); Account systemAcct = _accountMgr.getSystemAccount(); - + DataCenterDeployment plan = new DataCenterDeployment(dataCenterId); List defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork); if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) { defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemGuestNetwork); } - + List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork); List> networks = new ArrayList>(offerings.size() + 1); NicProfile defaultNic = new NicProfile(); @@ -582,21 +595,22 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx for (NetworkOfferingVO offering : offerings) { networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false, false).get(0), null)); } - + VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId, desiredHyp); if (template == null) { s_logger.debug("Can't find a template to start"); throw new CloudRuntimeException("Insufficient capacity exception"); } - - ConsoleProxyVO proxy = new ConsoleProxyVO(id, _serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId(), 0); + + ConsoleProxyVO proxy = new ConsoleProxyVO(id, _serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), + systemAcct.getId(), 0); try { proxy = _itMgr.allocate(proxy, template, _serviceOffering, networks, plan, null, systemAcct); } catch (InsufficientCapacityException e) { s_logger.warn("InsufficientCapacity", e); throw new CloudRuntimeException("Insufficient capacity exception", e); } - + Map context = new HashMap(); context.put("dc", dc); HostPodVO pod = _podDao.findById(proxy.getPodId()); @@ -605,7 +619,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx return context; } - + private ConsoleProxyAllocator getCurrentAllocator() { // for now, only one adapter is supported Enumeration it = _consoleProxyAllocators.enumeration(); @@ -691,37 +705,37 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public AgentControlAnswer onConsoleAccessAuthentication(ConsoleAccessAuthenticationCommand cmd) { long vmId = 0; - - String ticketInUrl = cmd.getTicket(); - if(ticketInUrl == null) { - s_logger.error("Access ticket could not be found, you could be running an old version of console proxy. vmId: " + cmd.getVmId()); - return new ConsoleAccessAuthenticationAnswer(cmd, false); - } - - if(s_logger.isDebugEnabled()) { + + String ticketInUrl = cmd.getTicket(); + if (ticketInUrl == null) { + s_logger.error("Access ticket could not be found, you could be running an old version of console proxy. vmId: " + cmd.getVmId()); + return new ConsoleAccessAuthenticationAnswer(cmd, false); + } + + if (s_logger.isDebugEnabled()) { s_logger.debug("Console authentication. Ticket in url for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticketInUrl); } String ticket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId()); - if(s_logger.isDebugEnabled()) { + if (s_logger.isDebugEnabled()) { s_logger.debug("Console authentication. Ticket in 1 minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticket); } - - if(!ticket.equals(ticketInUrl)) { - Date now = new Date(); - // considering of minute round-up - String minuteEarlyTicket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId(), - new Date(now.getTime() - 60*1000)); - if(s_logger.isDebugEnabled()) { + if (!ticket.equals(ticketInUrl)) { + Date now = new Date(); + // considering of minute round-up + String minuteEarlyTicket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId(), new Date(now.getTime() - 60 * 1000)); + + if (s_logger.isDebugEnabled()) { s_logger.debug("Console authentication. Ticket in 2-minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + minuteEarlyTicket); } - - if(!minuteEarlyTicket.equals(ticketInUrl)) { - s_logger.error("Access ticket expired or has been modified. vmId: " + cmd.getVmId() + "ticket in URL: " + ticketInUrl + ", tickets to check against: " + ticket + "," + minuteEarlyTicket); - return new ConsoleAccessAuthenticationAnswer(cmd, false); - } - } + + if (!minuteEarlyTicket.equals(ticketInUrl)) { + s_logger.error("Access ticket expired or has been modified. vmId: " + cmd.getVmId() + "ticket in URL: " + ticketInUrl + ", tickets to check against: " + ticket + "," + + minuteEarlyTicket); + return new ConsoleAccessAuthenticationAnswer(cmd, false); + } + } if (cmd.getVmId() != null && cmd.getVmId().isEmpty()) { if (s_logger.isDebugEnabled()) { @@ -768,32 +782,32 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public void onAgentConnect(HostVO host, StartupCommand cmd) { -// if (host.getType() == Type.ConsoleProxy) { -// // TODO we can use this event to mark the proxy is up and -// // functioning instead of -// // pinging the console proxy VM command port -// // -// // for now, just log a message -// if (s_logger.isInfoEnabled()) { -// s_logger.info("Console proxy agent is connected. proxy: " + host.getName()); -// } -// -// /* update public/private ip address */ -// if (_IpAllocator != null && _IpAllocator.exteralIpAddressAllocatorEnabled()) { -// try { -// ConsoleProxyVO console = findConsoleProxyByHost(host); -// if (console == null) { -// s_logger.debug("Can't find console proxy "); -// return; -// } -// console.setPrivateIpAddress(cmd.getPrivateIpAddress()); -// console.setPublicIpAddress(cmd.getPublicIpAddress()); -// console.setPublicNetmask(cmd.getPublicNetmask()); -// _consoleProxyDao.persist(console); -// } catch (NumberFormatException e) { -// } -// } -// } + // if (host.getType() == Type.ConsoleProxy) { + // // TODO we can use this event to mark the proxy is up and + // // functioning instead of + // // pinging the console proxy VM command port + // // + // // for now, just log a message + // if (s_logger.isInfoEnabled()) { + // s_logger.info("Console proxy agent is connected. proxy: " + host.getName()); + // } + // + // /* update public/private ip address */ + // if (_IpAllocator != null && _IpAllocator.exteralIpAddressAllocatorEnabled()) { + // try { + // ConsoleProxyVO console = findConsoleProxyByHost(host); + // if (console == null) { + // s_logger.debug("Can't find console proxy "); + // return; + // } + // console.setPrivateIpAddress(cmd.getPrivateIpAddress()); + // console.setPublicIpAddress(cmd.getPublicIpAddress()); + // console.setPublicNetmask(cmd.getPublicNetmask()); + // _consoleProxyDao.persist(console); + // } catch (NumberFormatException e) { + // } + // } + // } } @Override @@ -831,17 +845,13 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx // continue on forever /* - * _capacityScanScheduler.execute(new Runnable() { - * public void run() { if(s_logger.isInfoEnabled()) - * s_logger.info("Stop console proxy " + proxy.getName() - * + - * " VM because of that the agent running inside it has disconnected" - * ); stopProxy(proxy.getId()); } }); + * _capacityScanScheduler.execute(new Runnable() { public void run() { if(s_logger.isInfoEnabled()) + * s_logger.info("Stop console proxy " + proxy.getName() + + * " VM because of that the agent running inside it has disconnected" ); stopProxy(proxy.getId()); } }); */ } else { if (s_logger.isInfoEnabled()) { - s_logger.info("Console proxy agent disconnected but corresponding console proxy VM no longer exists in DB, proxy: " - + name); + s_logger.info("Console proxy agent disconnected but corresponding console proxy VM no longer exists in DB, proxy: " + name); } } } else { @@ -850,32 +860,30 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } } } - + private boolean reserveStandbyCapacity() { String value = _configDao.getValue(Config.SystemVMAutoReserveCapacity.key()); - if(value != null && value.equalsIgnoreCase("true")) { + if (value != null && value.equalsIgnoreCase("true")) { return true; } - + return false; } - + private boolean allowToLaunchNew(long dcId) { - List l = _consoleProxyDao.getProxyListInStates(dcId, VirtualMachine.State.Starting, - VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Stopped, - VirtualMachine.State.Migrating, VirtualMachine.State.Shutdowned, VirtualMachine.State.Unknown); - + List l = _consoleProxyDao.getProxyListInStates(dcId, VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Stopped, + VirtualMachine.State.Migrating, VirtualMachine.State.Shutdowned, VirtualMachine.State.Unknown); + String value = _configDao.getValue(Config.ConsoleProxyLaunchMax.key()); int launchLimit = NumbersUtil.parseInt(value, 10); return l.size() < launchLimit; } - + private HypervisorType currentHypervisorType(long dcId) { - List l = _consoleProxyDao.getProxyListInStates(dcId, VirtualMachine.State.Starting, - VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Stopped, - VirtualMachine.State.Migrating, VirtualMachine.State.Shutdowned, VirtualMachine.State.Unknown); - - return l.size() > 0? l.get(0).getHypervisorType():HypervisorType.Any; + List l = _consoleProxyDao.getProxyListInStates(dcId, VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Stopped, + VirtualMachine.State.Migrating, VirtualMachine.State.Shutdowned, VirtualMachine.State.Unknown); + + return l.size() > 0 ? l.get(0).getHypervisorType() : HypervisorType.Any; } private Runnable getCapacityScanTask() { @@ -898,9 +906,9 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (s_logger.isTraceEnabled()) { s_logger.trace("Begin console proxy capacity scan"); } - - if(!reserveStandbyCapacity()) { - if(s_logger.isDebugEnabled()) { + + if (!reserveStandbyCapacity()) { + if (s_logger.isDebugEnabled()) { s_logger.debug("Reserving standby capacity is disable, skip capacity scan"); } return; @@ -913,7 +921,6 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx return; } - Map zoneHostInfoMap = getZoneHostInfo(); if (isServiceReady(zoneHostInfoMap)) { if (s_logger.isTraceEnabled()) { @@ -1029,10 +1036,10 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (s_logger.isInfoEnabled()) { s_logger.info("No stopped console proxy is available, need to allocate a new console proxy"); } - + try { proxy = startNew(dataCenterId); - } catch (ConcurrentOperationException e) { + } catch (ConcurrentOperationException e) { s_logger.info("Concurrent Operation caught " + e); } } else { @@ -1044,7 +1051,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx _allocProxyLock.unlock(); } } else { - if(s_logger.isInfoEnabled()) { + if (s_logger.isInfoEnabled()) { s_logger.info("Unable to acquire proxy allocation lock, skip for next time"); } } @@ -1171,15 +1178,15 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public boolean stopProxy(long proxyVmId) { -// AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); -// if (asyncExecutor != null) { -// AsyncJobVO job = asyncExecutor.getJob(); -// -// if (s_logger.isInfoEnabled()) { -// s_logger.info("Stop console proxy " + proxyVmId + ", update async job-" + job.getId()); -// } -// _asyncMgr.updateAsyncJobAttachment(job.getId(), "console_proxy", proxyVmId); -// } + // AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); + // if (asyncExecutor != null) { + // AsyncJobVO job = asyncExecutor.getJob(); + // + // if (s_logger.isInfoEnabled()) { + // s_logger.info("Stop console proxy " + proxyVmId + ", update async job-" + job.getId()); + // } + // _asyncMgr.updateAsyncJobAttachment(job.getId(), "console_proxy", proxyVmId); + // } ConsoleProxyVO proxy = _consoleProxyDao.findById(proxyVmId); if (proxy == null) { @@ -1189,9 +1196,8 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx return false; } /* - * saveStartedEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, - * EventTypes.EVENT_PROXY_STOP, "Stopping console proxy with Id: " + - * proxyVmId, startEventId); + * saveStartedEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_STOP, + * "Stopping console proxy with Id: " + proxyVmId, startEventId); */ try { return _itMgr.stop(proxy, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); @@ -1203,15 +1209,15 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public boolean rebootProxy(long proxyVmId) { -// AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); -// if (asyncExecutor != null) { -// AsyncJobVO job = asyncExecutor.getJob(); -// -// if (s_logger.isInfoEnabled()) { -// s_logger.info("Reboot console proxy " + proxyVmId + ", update async job-" + job.getId()); -// } -// _asyncMgr.updateAsyncJobAttachment(job.getId(), "console_proxy", proxyVmId); -// } + // AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); + // if (asyncExecutor != null) { + // AsyncJobVO job = asyncExecutor.getJob(); + // + // if (s_logger.isInfoEnabled()) { + // s_logger.info("Reboot console proxy " + proxyVmId + ", update async job-" + job.getId()); + // } + // _asyncMgr.updateAsyncJobAttachment(job.getId(), "console_proxy", proxyVmId); + // } final ConsoleProxyVO proxy = _consoleProxyDao.findById(proxyVmId); @@ -1228,11 +1234,8 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx s_logger.debug("Successfully reboot console proxy " + proxy.getName()); } - SubscriptionMgr.getInstance().notifySubscribers( - ConsoleProxyManager.ALERT_SUBJECT, - this, - new ConsoleProxyAlertEventArgs(ConsoleProxyAlertEventArgs.PROXY_REBOOTED, proxy.getDataCenterId(), proxy.getId(), - proxy, null)); + SubscriptionMgr.getInstance().notifySubscribers(ConsoleProxyManager.ALERT_SUBJECT, this, + new ConsoleProxyAlertEventArgs(ConsoleProxyAlertEventArgs.PROXY_REBOOTED, proxy.getDataCenterId(), proxy.getId(), proxy, null)); return true; } else { @@ -1265,7 +1268,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx private String getAllocProxyLockName() { return "consoleproxy.alloc"; } - + @Override public boolean configure(String name, Map params) throws ConfigurationException { if (s_logger.isInfoEnabled()) { @@ -1284,7 +1287,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx _proxyRamSize = NumbersUtil.parseInt(configs.get(Config.ConsoleProxyRamSize.key()), DEFAULT_PROXY_VM_RAMSIZE); _proxyCpuMHz = NumbersUtil.parseInt(configs.get(Config.ConsoleProxyCpuMHz.key()), DEFAULT_PROXY_VM_CPUMHZ); - + String value = configs.get(Config.ConsoleProxyCmdPort.key()); value = configs.get("consoleproxy.sslEnabled"); if (value != null && value.equalsIgnoreCase("true")) { @@ -1302,9 +1305,9 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (value != null) { _consoleProxyPort = NumbersUtil.parseInt(value, ConsoleProxyManager.DEFAULT_PROXY_VNC_PORT); } - + value = configs.get(Config.ConsoleProxyDisableRpFilter.key()); - if(value != null && value.equalsIgnoreCase("true")) { + if (value != null && value.equalsIgnoreCase("true")) { _disable_rp_filter = true; } @@ -1317,7 +1320,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx if (value != null && value.equalsIgnoreCase("true")) { _use_storage_vm = true; } - + if (s_logger.isInfoEnabled()) { s_logger.info("Console proxy max session soft limit : " + _capacityPerProxy); s_logger.info("Console proxy standby capacity : " + _standbyCapacity); @@ -1353,7 +1356,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx _serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering); _capacityScanScheduler.scheduleAtFixedRate(getCapacityScanTask(), STARTUP_DELAY, _capacityScanInterval, TimeUnit.MILLISECONDS); - + if (s_logger.isInfoEnabled()) { s_logger.info("Console Proxy Manager is configured."); } @@ -1361,15 +1364,15 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } @Override - public boolean destroyConsoleProxy(DestroyConsoleProxyCmd cmd) throws ServerApiException{ + public boolean destroyConsoleProxy(DestroyConsoleProxyCmd cmd) throws ServerApiException { Long proxyId = cmd.getId(); - + // verify parameters ConsoleProxyVO proxy = _consoleProxyDao.findById(proxyId); if (proxy == null) { throw new InvalidParameterValueException("unable to find a console proxy with id " + proxyId); } - + return destroyProxy(proxyId); } @@ -1378,7 +1381,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { - StringBuilder buf = profile.getBootArgsBuilder(); + StringBuilder buf = profile.getBootArgsBuilder(); buf.append(" template=domP type=consoleproxy"); buf.append(" host=").append(_mgmt_host); buf.append(" port=").append(_mgmt_port); @@ -1390,26 +1393,26 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx buf.append(" pod=").append(dest.getPod().getId()); buf.append(" guid=Proxy.").append(profile.getId()); buf.append(" proxy_vm=").append(profile.getId()); - if(_disable_rp_filter) { + if (_disable_rp_filter) { buf.append(" disable_rp_filter=true"); } boolean externalDhcp = false; String externalDhcpStr = _configDao.getValue("direct.attach.network.externalIpAllocator.enabled"); - if(externalDhcpStr != null && externalDhcpStr.equalsIgnoreCase("true")) { + if (externalDhcpStr != null && externalDhcpStr.equalsIgnoreCase("true")) { externalDhcp = true; } - + for (NicProfile nic : profile.getNics()) { int deviceId = nic.getDeviceId(); - if(nic.getIp4Address() == null) { - buf.append(" eth").append(deviceId).append("ip=").append("0.0.0.0"); - buf.append(" eth").append(deviceId).append("mask=").append("0.0.0.0"); + if (nic.getIp4Address() == null) { + buf.append(" eth").append(deviceId).append("ip=").append("0.0.0.0"); + buf.append(" eth").append(deviceId).append("mask=").append("0.0.0.0"); } else { - buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address()); - buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask()); + buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address()); + buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask()); } - + if (nic.isDefaultNic()) { buf.append(" gateway=").append(nic.getGateway()); buf.append(" dns1=").append(nic.getDns1()); @@ -1417,91 +1420,95 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx buf.append(" dns2=").append(nic.getDns2()); } } - + if (nic.getTrafficType() == TrafficType.Management) { - String mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); - if (NetUtils.isValidCIDR(mgmt_cidr)) { - buf.append(" mgmtcidr=").append(mgmt_cidr); - } + String mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); + if (NetUtils.isValidCIDR(mgmt_cidr)) { + buf.append(" mgmtcidr=").append(mgmt_cidr); + } buf.append(" localgw=").append(dest.getPod().getGateway()); - } + } } - /*External DHCP mode*/ - if(externalDhcp) { + /* External DHCP mode */ + if (externalDhcp) { buf.append(" bootproto=dhcp"); } - + String bootArgs = buf.toString(); if (s_logger.isDebugEnabled()) { s_logger.debug("Boot Args for " + profile + ": " + bootArgs); } - + return true; } @Override public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { - + finalizeCommandsOnStart(cmds, profile); - + ConsoleProxyVO proxy = profile.getVirtualMachine(); DataCenter dc = dest.getDataCenter(); List nics = profile.getNics(); for (NicProfile nic : nics) { - if ((nic.getTrafficType() == TrafficType.Public && dc.getNetworkType() == NetworkType.Advanced) || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) { - proxy.setPublicIpAddress(nic.getIp4Address()); - proxy.setPublicNetmask(nic.getNetmask()); - proxy.setPublicMacAddress(nic.getMacAddress()); - } else if (nic.getTrafficType() == TrafficType.Management) { - proxy.setPrivateIpAddress(nic.getIp4Address()); - proxy.setPrivateMacAddress(nic.getMacAddress()); - } + if ((nic.getTrafficType() == TrafficType.Public && dc.getNetworkType() == NetworkType.Advanced) + || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) { + proxy.setPublicIpAddress(nic.getIp4Address()); + proxy.setPublicNetmask(nic.getNetmask()); + proxy.setPublicMacAddress(nic.getMacAddress()); + } else if (nic.getTrafficType() == TrafficType.Management) { + proxy.setPrivateIpAddress(nic.getIp4Address()); + proxy.setPrivateMacAddress(nic.getMacAddress()); + } } _consoleProxyDao.update(proxy.getId(), proxy); return true; } - + @Override public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile) { - + NicProfile managementNic = null; NicProfile controlNic = null; for (NicProfile nic : profile.getNics()) { - if (nic.getTrafficType() == TrafficType.Management) { - managementNic = nic; - } else if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { - controlNic = nic; - } + if (nic.getTrafficType() == TrafficType.Management) { + managementNic = nic; + } else if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { + controlNic = nic; + } } if (controlNic == null) { - assert (managementNic != null); - controlNic = managementNic; + if (managementNic == null) { + s_logger.error("Management network doesn't exist for the consoleProxy " + profile.getVirtualMachine()); + return false; + } + controlNic = managementNic; } CheckSshCommand check = new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20); cmds.addCommand("checkSsh", check); - + return true; } - + @Override public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) { - CheckSshAnswer answer = (CheckSshAnswer)cmds.getAnswer("checkSsh"); + CheckSshAnswer answer = (CheckSshAnswer) cmds.getAnswer("checkSsh"); if (answer == null || !answer.getResult()) { - if(answer != null) { + if (answer != null) { s_logger.warn("Unable to ssh to the VM: " + answer.getDetails()); } else { s_logger.warn("Unable to ssh to the VM: null answer"); } return false; } - + return true; } - - @Override + + @Override public void finalizeExpunge(ConsoleProxyVO proxy) { proxy.setPublicIpAddress(null); proxy.setPublicMacAddress(null); @@ -1510,56 +1517,55 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx proxy.setPrivateIpAddress(null); _consoleProxyDao.update(proxy.getId(), proxy); } - + @Override - public boolean applyCustomCertToNewProxy(StartupProxyCommand cmd){ - //this is the case for updating cust cert on each new starting proxy, if such cert exists - //get cert from db - CertificateVO cert = _certDao.listAll().get(0); - - if(cert.getUpdated().equalsIgnoreCase("Y")){ - String certStr = cert.getCertificate(); - long proxyVmId = (cmd).getProxyVmId(); - ConsoleProxyVO consoleProxy = _consoleProxyDao.findById(proxyVmId); - //find corresponding host - if(consoleProxy!=null){ - HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy); - //now send a command to console proxy host - UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr, true); - try { - Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd); - if(updateCertAns.getResult() == true) - { - //we have the cert copied over on cpvm - rebootProxy(consoleProxy.getId()); - //when cp reboots, the context will be reinit with the new cert - s_logger.info("Successfully rebooted console proxy resource after custom certificate application for proxy:"+cmd.getProxyVmId()); - return true; - } - } catch (AgentUnavailableException e) { - s_logger.warn("Unable to send update certificate command to the console proxy resource for proxy:"+cmd.getProxyVmId(), e); - return false; - } catch (OperationTimedoutException e) { - s_logger.warn("Unable to send update certificate command to the console proxy resource for proxy:"+cmd.getProxyVmId(), e); - return false; - } - } - } else { - return false;//no cert entry in the db record - } - return false;//cert already applied in previous cycles + public boolean applyCustomCertToNewProxy(StartupProxyCommand cmd) { + // this is the case for updating cust cert on each new starting proxy, if such cert exists + // get cert from db + CertificateVO cert = _certDao.listAll().get(0); + + if (cert.getUpdated().equalsIgnoreCase("Y")) { + String certStr = cert.getCertificate(); + long proxyVmId = (cmd).getProxyVmId(); + ConsoleProxyVO consoleProxy = _consoleProxyDao.findById(proxyVmId); + // find corresponding host + if (consoleProxy != null) { + HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy); + // now send a command to console proxy host + UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr, true); + try { + Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd); + if (updateCertAns.getResult() == true) { + // we have the cert copied over on cpvm + rebootProxy(consoleProxy.getId()); + // when cp reboots, the context will be reinit with the new cert + s_logger.info("Successfully rebooted console proxy resource after custom certificate application for proxy:" + cmd.getProxyVmId()); + return true; + } + } catch (AgentUnavailableException e) { + s_logger.warn("Unable to send update certificate command to the console proxy resource for proxy:" + cmd.getProxyVmId(), e); + return false; + } catch (OperationTimedoutException e) { + s_logger.warn("Unable to send update certificate command to the console proxy resource for proxy:" + cmd.getProxyVmId(), e); + return false; + } + } + } else { + return false;// no cert entry in the db record + } + return false;// cert already applied in previous cycles } - + @Override public ConsoleProxyVO persist(ConsoleProxyVO proxy) { return _consoleProxyDao.persist(proxy); } - + @Override public ConsoleProxyVO findById(long id) { return _consoleProxyDao.findById(id); } - + @Override public ConsoleProxyVO findByName(String name) { if (!VirtualMachineName.isValidConsoleProxyName(name)) { @@ -1567,7 +1573,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx } return findById(VirtualMachineName.getConsoleProxyId(name)); } - + @Override public void finalizeStop(VirtualMachineProfile profile, StopAnswer answer) { } diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java index bef728d3e90..b0ada72dff6 100644 --- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java +++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java @@ -258,7 +258,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V String privateCidr = NetUtils.ipAndNetMaskToCidr(privateNic.getIp4Address(), privateNic.getNetmask()); String publicCidr = NetUtils.ipAndNetMaskToCidr(secStorageVm.getPublicIpAddress(), secStorageVm.getPublicNetmask()); if (NetUtils.isNetworkAWithinNetworkB(privateCidr, publicCidr) || NetUtils.isNetworkAWithinNetworkB(publicCidr, privateCidr)) { - s_logger.info("private and public interface overlaps, add a default route through private interface. privateCidr: " + privateCidr + ", publicCidr: " + publicCidr); + s_logger.info("private and public interface overlaps, add a default route through private interface. privateCidr: " + privateCidr + ", publicCidr: " + publicCidr); allowedCidrs.add("0.0.0.0/0"); } setupCmd.setAllowedInternalSites(allowedCidrs.toArray(new String[allowedCidrs.size()])); @@ -352,15 +352,11 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V return secStorageVm; } else { if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to allocate secondary storage vm storage, remove the secondary storage vm record from DB, secondary storage vm id: " - + secStorageVmId); + s_logger.debug("Unable to allocate secondary storage vm storage, remove the secondary storage vm record from DB, secondary storage vm id: " + secStorageVmId); } - SubscriptionMgr.getInstance().notifySubscribers( - ALERT_SUBJECT, - this, - new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATE_FAILURE, dataCenterId, secStorageVmId, null, - "Unable to allocate storage")); + SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, + new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATE_FAILURE, dataCenterId, secStorageVmId, null, "Unable to allocate storage")); } return null; } @@ -381,37 +377,34 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V DataCenter dc = _dcDao.findById(plan.getDataCenterId()); List defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork); - + if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) { defaultOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemGuestNetwork); } - List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, - NetworkOfferingVO.SystemManagementNetwork); + List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork); List> networks = new ArrayList>(offerings.size() + 1); NicProfile defaultNic = new NicProfile(); defaultNic.setDefaultNic(true); defaultNic.setDeviceId(2); try { - networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering.get(0), plan, null, null, false, false) - .get(0), defaultNic)); + networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, defaultOffering.get(0), plan, null, null, false, false).get(0), defaultNic)); for (NetworkOfferingVO offering : offerings) { - networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false, false).get(0), - null)); + networks.add(new Pair(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false, false).get(0), null)); } } catch (ConcurrentOperationException e) { s_logger.info("Unable to setup due to concurrent operation. " + e); return new HashMap(); } - + VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId); if (template == null) { s_logger.debug("Can't find a template to start"); throw new CloudRuntimeException("Insufficient capacity exception"); } - - SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, _serviceOffering.getId(), name, template.getId(), - template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId()); + + SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, _serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, + systemAcct.getDomainId(), systemAcct.getId()); try { secStorageVm = _itMgr.allocate(secStorageVm, template, _serviceOffering, networks, plan, null, systemAcct); } catch (InsufficientCapacityException e) { @@ -483,17 +476,14 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V for (DataCenterVO dc : datacenters) { if (isZoneReady(zoneHostInfoMap, dc.getId())) { - List alreadyRunning = _secStorageVmDao.getSecStorageVmListInStates(dc.getId(), State.Running, - State.Migrating, State.Starting); - List stopped = _secStorageVmDao.getSecStorageVmListInStates(dc.getId(), State.Stopped, - State.Stopping); + List alreadyRunning = _secStorageVmDao.getSecStorageVmListInStates(dc.getId(), State.Running, State.Migrating, State.Starting); + List stopped = _secStorageVmDao.getSecStorageVmListInStates(dc.getId(), State.Stopped, State.Stopping); if (alreadyRunning.size() == 0) { if (stopped.size() == 0) { s_logger.info("No secondary storage vms found in datacenter id=" + dc.getId() + ", starting a new one"); allocCapacity(dc.getId()); } else { - s_logger.warn("Stopped secondary storage vms found in datacenter id=" + dc.getId() - + ", not restarting them automatically"); + s_logger.warn("Stopped secondary storage vms found in datacenter id=" + dc.getId() + ", not restarting them automatically"); } } @@ -604,8 +594,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V } } else { if (s_logger.isInfoEnabled()) { - s_logger.info("Unable to acquire synchronization lock to start secStorageVm for standby capacity, secStorageVm vm id : " - + secStorageVm.getId()); + s_logger.info("Unable to acquire synchronization lock to start secStorageVm for standby capacity, secStorageVm vm id : " + secStorageVm.getId()); } return; } @@ -615,8 +604,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V if (secStorageVm == null) { if (s_logger.isInfoEnabled()) { - s_logger.info("Unable to start secondary storage vm for standby capacity, secStorageVm vm Id : " + secStorageVmId - + ", will recycle it and start a new one"); + s_logger.info("Unable to start secondary storage vm for standby capacity, secStorageVm vm Id : " + secStorageVmId + ", will recycle it and start a new one"); } if (secStorageVmFromStoppedPool) { @@ -650,8 +638,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V HostVO secHost = _hostDao.findSecondaryStorageHost(dataCenterId); if (secHost == null) { if (s_logger.isDebugEnabled()) { - s_logger.debug("No secondary storage available in zone " + dataCenterId - + ", wait until it is ready to launch secondary storage vm"); + s_logger.debug("No secondary storage available in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm"); } return false; } @@ -808,15 +795,15 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V @Override public boolean stopSecStorageVm(long secStorageVmId) { -// AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); -// if (asyncExecutor != null) { -// AsyncJobVO job = asyncExecutor.getJob(); -// -// if (s_logger.isInfoEnabled()) { -// s_logger.info("Stop secondary storage vm " + secStorageVmId + ", update async job-" + job.getId()); -// } -// _asyncMgr.updateAsyncJobAttachment(job.getId(), "secStorageVm", secStorageVmId); -// } + // AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); + // if (asyncExecutor != null) { + // AsyncJobVO job = asyncExecutor.getJob(); + // + // if (s_logger.isInfoEnabled()) { + // s_logger.info("Stop secondary storage vm " + secStorageVmId + ", update async job-" + job.getId()); + // } + // _asyncMgr.updateAsyncJobAttachment(job.getId(), "secStorageVm", secStorageVmId); + // } SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId); if (secStorageVm == null) { @@ -862,15 +849,15 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V @Override public boolean rebootSecStorageVm(long secStorageVmId) { -// AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); -// if (asyncExecutor != null) { -// AsyncJobVO job = asyncExecutor.getJob(); -// -// if (s_logger.isInfoEnabled()) { -// s_logger.info("Reboot secondary storage vm " + secStorageVmId + ", update async job-" + job.getId()); -// } -// _asyncMgr.updateAsyncJobAttachment(job.getId(), "secstorage_vm", secStorageVmId); -// } + // AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); + // if (asyncExecutor != null) { + // AsyncJobVO job = asyncExecutor.getJob(); + // + // if (s_logger.isInfoEnabled()) { + // s_logger.info("Reboot secondary storage vm " + secStorageVmId + ", update async job-" + job.getId()); + // } + // _asyncMgr.updateAsyncJobAttachment(job.getId(), "secstorage_vm", secStorageVmId); + // } final SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId); @@ -887,11 +874,8 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V s_logger.debug("Successfully reboot secondary storage vm " + secStorageVm.getName()); } - SubscriptionMgr.getInstance().notifySubscribers( - ALERT_SUBJECT, - this, - new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_REBOOTED, secStorageVm.getDataCenterId(), - secStorageVm.getId(), secStorageVm, null)); + SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, + new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_REBOOTED, secStorageVm.getDataCenterId(), secStorageVm.getId(), secStorageVm, null)); return true; } else { @@ -957,8 +941,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V } @Override - public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, - ReservationContext context) { + public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { HostVO secHost = _hostDao.findSecondaryStorageHost(dest.getDataCenter().getId()); assert (secHost != null); @@ -979,10 +962,10 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V } buf.append(" mount.path=").append(nfsMountPoint); - if(_configDao.isPremium()) + if (_configDao.isPremium()) buf.append(" resource=com.cloud.storage.resource.PremiumSecondaryStorageResource"); - else - buf.append(" resource=com.cloud.storage.resource.NfsSecondaryStorageResource"); + else + buf.append(" resource=com.cloud.storage.resource.NfsSecondaryStorageResource"); buf.append(" instance=SecStorage"); buf.append(" sslcopy=").append(Boolean.toString(_useSSlCopy)); @@ -1007,11 +990,11 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V buf.append(" gateway=").append(nic.getGateway()); } if (nic.getTrafficType() == TrafficType.Management) { - String mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); - if (NetUtils.isValidCIDR(mgmt_cidr)) { - buf.append(" mgmtcidr=").append(mgmt_cidr); - } - + String mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); + if (NetUtils.isValidCIDR(mgmt_cidr)) { + buf.append(" mgmtcidr=").append(mgmt_cidr); + } + buf.append(" localgw=").append(dest.getPod().getGateway()); buf.append(" private.network.device=").append("eth").append(deviceId); } else if (nic.getTrafficType() == TrafficType.Public) { @@ -1039,9 +1022,8 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V } @Override - public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, - ReservationContext context) { - + public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { + finalizeCommandsOnStart(cmds, profile); SecondaryStorageVmVO secVm = profile.getVirtualMachine(); @@ -1049,7 +1031,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V List nics = profile.getNics(); for (NicProfile nic : nics) { if ((nic.getTrafficType() == TrafficType.Public && dc.getNetworkType() == NetworkType.Advanced) - || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) { + || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) { secVm.setPublicIpAddress(nic.getIp4Address()); secVm.setPublicNetmask(nic.getNetmask()); secVm.setPublicMacAddress(nic.getMacAddress()); @@ -1061,31 +1043,33 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V _secStorageVmDao.update(secVm.getId(), secVm); return true; } - + @Override public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile) { - + NicProfile managementNic = null; NicProfile controlNic = null; for (NicProfile nic : profile.getNics()) { - if (nic.getTrafficType() == TrafficType.Management) { - managementNic = nic; - } else if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { - controlNic = nic; - } + if (nic.getTrafficType() == TrafficType.Management) { + managementNic = nic; + } else if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { + controlNic = nic; + } } if (controlNic == null) { - assert (managementNic != null); - controlNic = managementNic; + if (managementNic == null) { + s_logger.error("Management network doesn't exist for the secondaryStorageVm " + profile.getVirtualMachine()); + return false; + } + controlNic = managementNic; } CheckSshCommand check = new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20); cmds.addCommand("checkSsh", check); - + return true; } - @Override public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) { @@ -1101,7 +1085,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V @Override public void finalizeStop(VirtualMachineProfile profile, StopAnswer answer) { } - + @Override public void finalizeExpunge(SecondaryStorageVmVO vm) { } diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index 916311110e0..7463bcc6aa8 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -96,6 +96,7 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.HypervisorGuru; import com.cloud.hypervisor.HypervisorGuruManager; import com.cloud.maid.StackMaid; +import com.cloud.network.Network; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; import com.cloud.org.Cluster; @@ -142,52 +143,83 @@ import com.cloud.vm.dao.SecondaryStorageVmDao; import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDao; -@Local(value=VirtualMachineManager.class) +@Local(value = VirtualMachineManager.class) public class VirtualMachineManagerImpl implements VirtualMachineManager, Listener { private static final Logger s_logger = Logger.getLogger(VirtualMachineManagerImpl.class); - + String _name; - @Inject protected StorageManager _storageMgr; - @Inject protected NetworkManager _networkMgr; - @Inject protected AgentManager _agentMgr; - @Inject protected VMInstanceDao _vmDao; - @Inject protected ServiceOfferingDao _offeringDao; - @Inject protected VMTemplateDao _templateDao; - @Inject protected UserDao _userDao; - @Inject protected AccountDao _accountDao; - @Inject protected DomainDao _domainDao; - @Inject protected ClusterManager _clusterMgr; - @Inject protected ItWorkDao _workDao; - @Inject protected UserVmDao _userVmDao; - @Inject protected DomainRouterDao _routerDao; - @Inject protected ConsoleProxyDao _consoleDao; - @Inject protected SecondaryStorageVmDao _secondaryDao; - @Inject protected UsageEventDao _usageEventDao; - @Inject protected NicDao _nicsDao; - @Inject protected AccountManager _accountMgr; - @Inject protected HostDao _hostDao; - @Inject protected AlertManager _alertMgr; - @Inject protected GuestOSCategoryDao _guestOsCategoryDao; - @Inject protected GuestOSDao _guestOsDao; - @Inject protected VolumeDao _volsDao; - @Inject protected ConsoleProxyManager _consoleProxyMgr; - @Inject protected ConfigurationManager _configMgr; - @Inject protected CapacityManager _capacityMgr; - @Inject protected HighAvailabilityManager _haMgr; - @Inject protected HostPodDao _podDao; - @Inject protected DataCenterDao _dcDao; - @Inject protected StoragePoolDao _storagePoolDao; - @Inject protected HypervisorGuruManager _hvGuruMgr; - - @Inject(adapter=DeploymentPlanner.class) + @Inject + protected StorageManager _storageMgr; + @Inject + protected NetworkManager _networkMgr; + @Inject + protected AgentManager _agentMgr; + @Inject + protected VMInstanceDao _vmDao; + @Inject + protected ServiceOfferingDao _offeringDao; + @Inject + protected VMTemplateDao _templateDao; + @Inject + protected UserDao _userDao; + @Inject + protected AccountDao _accountDao; + @Inject + protected DomainDao _domainDao; + @Inject + protected ClusterManager _clusterMgr; + @Inject + protected ItWorkDao _workDao; + @Inject + protected UserVmDao _userVmDao; + @Inject + protected DomainRouterDao _routerDao; + @Inject + protected ConsoleProxyDao _consoleDao; + @Inject + protected SecondaryStorageVmDao _secondaryDao; + @Inject + protected UsageEventDao _usageEventDao; + @Inject + protected NicDao _nicsDao; + @Inject + protected AccountManager _accountMgr; + @Inject + protected HostDao _hostDao; + @Inject + protected AlertManager _alertMgr; + @Inject + protected GuestOSCategoryDao _guestOsCategoryDao; + @Inject + protected GuestOSDao _guestOsDao; + @Inject + protected VolumeDao _volsDao; + @Inject + protected ConsoleProxyManager _consoleProxyMgr; + @Inject + protected ConfigurationManager _configMgr; + @Inject + protected CapacityManager _capacityMgr; + @Inject + protected HighAvailabilityManager _haMgr; + @Inject + protected HostPodDao _podDao; + @Inject + protected DataCenterDao _dcDao; + @Inject + protected StoragePoolDao _storagePoolDao; + @Inject + protected HypervisorGuruManager _hvGuruMgr; + + @Inject(adapter = DeploymentPlanner.class) protected Adapters _planners; - + Map> _vmGurus = new HashMap>(); protected StateMachine2 _stateMachine; - + ScheduledExecutorService _executor = null; protected int _operationTimeout; - + protected int _retry; protected long _nodeId; protected long _cleanupWait; @@ -198,28 +230,22 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public void registerGuru(VirtualMachine.Type type, VirtualMachineGuru guru) { - synchronized(_vmGurus) { + synchronized (_vmGurus) { _vmGurus.put(type, guru); } } - - @Override @DB - public T allocate(T vm, - VMTemplateVO template, - ServiceOfferingVO serviceOffering, - Pair rootDiskOffering, - List> dataDiskOfferings, - List> networks, - Map params, - DeploymentPlan plan, - HypervisorType hyperType, - Account owner) throws InsufficientCapacityException { + + @Override + @DB + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Pair rootDiskOffering, + List> dataDiskOfferings, List> networks, Map params, DeploymentPlan plan, + HypervisorType hyperType, Account owner) throws InsufficientCapacityException { if (s_logger.isDebugEnabled()) { s_logger.debug("Allocating entries for VM: " + vm); } - + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, serviceOffering, owner, params); - + vm.setDataCenterId(plan.getDataCenterId()); if (plan.getPodId() != null) { vm.setPodId(plan.getPodId()); @@ -228,10 +254,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene vm.setHostId(plan.getHostId()); } assert (plan.getPoolId() == null) : "We currently don't support pool preset yet"; - + @SuppressWarnings("unchecked") - VirtualMachineGuru guru = (VirtualMachineGuru)_vmGurus.get(vm.getType()); - + VirtualMachineGuru guru = (VirtualMachineGuru) _vmGurus.get(vm.getType()); + Transaction txn = Transaction.currentTxn(); txn.start(); vm = guru.persist(vm); @@ -248,18 +274,18 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (dataDiskOfferings == null) { dataDiskOfferings = new ArrayList>(0); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Allocaing disks for " + vm); } - + if (template.getFormat() == ImageFormat.ISO) { _storageMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vm, owner); } else if (template.getFormat() == ImageFormat.BAREMETAL) { - }else { + } else { _storageMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), template, vm, owner); } - + for (Pair offering : dataDiskOfferings) { _storageMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vm.getId(), offering.first(), offering.second(), vm, owner); } @@ -268,53 +294,41 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (s_logger.isDebugEnabled()) { s_logger.debug("Allocation completed for VM: " + vm); } - + return vm; } - + @Override - public T allocate(T vm, - VMTemplateVO template, - ServiceOfferingVO serviceOffering, - Long rootSize, - Pair dataDiskOffering, - List> networks, - DeploymentPlan plan, - HypervisorType hyperType, - Account owner) throws InsufficientCapacityException { + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Long rootSize, Pair dataDiskOffering, + List> networks, DeploymentPlan plan, HypervisorType hyperType, Account owner) throws InsufficientCapacityException { List> diskOfferings = new ArrayList>(1); if (dataDiskOffering != null) { diskOfferings.add(dataDiskOffering); } return allocate(vm, template, serviceOffering, new Pair(serviceOffering, rootSize), diskOfferings, networks, null, plan, hyperType, owner); } - + @Override - public T allocate(T vm, - VMTemplateVO template, - ServiceOfferingVO serviceOffering, - List> networks, - DeploymentPlan plan, - HypervisorType hyperType, - Account owner) throws InsufficientCapacityException { + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, List> networks, DeploymentPlan plan, + HypervisorType hyperType, Account owner) throws InsufficientCapacityException { return allocate(vm, template, serviceOffering, new Pair(serviceOffering, null), null, networks, null, plan, hyperType, owner); } - + @SuppressWarnings("unchecked") private VirtualMachineGuru getVmGuru(T vm) { - return (VirtualMachineGuru)_vmGurus.get(vm.getType()); + return (VirtualMachineGuru) _vmGurus.get(vm.getType()); } - + @SuppressWarnings("unchecked") private VirtualMachineGuru getBareMetalVmGuru(T vm) { - return (VirtualMachineGuru)_vmGurus.get(VirtualMachine.Type.UserBareMetal); + return (VirtualMachineGuru) _vmGurus.get(VirtualMachine.Type.UserBareMetal); } - + @Override public boolean expunge(T vm, User caller, Account account) throws ResourceUnavailableException { try { if (advanceExpunge(vm, caller, account)) { - //Mark vms as removed + // Mark vms as removed remove(vm, caller, account); return true; } else { @@ -327,7 +341,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene throw new CloudRuntimeException("Concurrent operation ", e); } } - + @Override public boolean advanceExpunge(T vm, User caller, Account account) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException { if (vm == null || vm.getRemoved() != null) { @@ -336,7 +350,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return true; } - + if (!this.advanceStop(vm, false, caller, account)) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to stop the VM so we can't expunge it."); @@ -347,20 +361,20 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm.toString()); return false; } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Destroying vm " + vm); } - + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); _networkMgr.cleanupNics(profile); - //Clean up volumes based on the vm's instance id - _storageMgr.cleanupVolumes(vm.getId()); - + // Clean up volumes based on the vm's instance id + _storageMgr.cleanupVolumes(vm.getId()); + VirtualMachineGuru guru = getVmGuru(vm); guru.finalizeExpunge(vm); - + if (s_logger.isDebugEnabled()) { s_logger.debug("Expunged " + vm); } @@ -379,17 +393,17 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene public boolean stop() { return true; } - + @Override public boolean configure(String name, Map xmlParams) throws ConfigurationException { _name = name; - + ComponentLocator locator = ComponentLocator.getCurrentLocator(); ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); Map params = configDao.getConfiguration(xmlParams); - + _retry = NumbersUtil.parseInt(params.get(Config.StartRetry.key()), 10); - + ReservationContextImpl.setComponents(_userDao, _domainDao, _accountDao); VirtualMachineProfileImpl.setComponents(_offeringDao, _templateDao, _accountDao); @@ -399,24 +413,24 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene _opWaitInterval = NumbersUtil.parseLong(params.get(Config.VmOpWaitInterval.key()), 120) * 1000; _lockStateRetry = NumbersUtil.parseInt(params.get(Config.VmOpLockStateRetry.key()), 5); _operationTimeout = NumbersUtil.parseInt(params.get(Config.Wait.key()), 1800) * 2; - + _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vm-Operations-Cleanup")); _nodeId = _clusterMgr.getManagementNodeId(); - + _agentMgr.registerForHostEvents(this, true, true, true); - + return true; } - + @Override public String getName() { return _name; } - + protected VirtualMachineManagerImpl() { setStateMachine(); } - + @Override public T start(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { try { @@ -435,19 +449,19 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return true; } - + if (vo.getStep() == Step.Done) { if (s_logger.isDebugEnabled()) { s_logger.debug("Work for " + vm + " is " + vo.getStep()); } return true; } - + if (vo.getSecondsTaskIsInactive() > _cancelWait) { s_logger.warn("The task item for vm " + vm + " has been inactive for " + vo.getSecondsTaskIsInactive()); return false; } - + try { Thread.sleep(_opWaitInterval); } catch (InterruptedException e) { @@ -456,14 +470,14 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } s_logger.debug("Waiting some more to make sure there's no activity on " + vm); } - - + } - + @DB - protected Ternary changeToStartState(VirtualMachineGuru vmGuru, T vm, User caller, Account account) throws ConcurrentOperationException { + protected Ternary changeToStartState(VirtualMachineGuru vmGuru, T vm, User caller, Account account) + throws ConcurrentOperationException { long vmId = vm.getId(); - + ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Starting, vm.getType(), vm.getId()); int retry = _lockStateRetry; while (retry-- != 0) { @@ -471,26 +485,26 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene txn.start(); try { if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) { - + Journal journal = new Journal.LogJournal("Creating " + vm, s_logger); work = _workDao.persist(work); ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account); - + if (s_logger.isDebugEnabled()) { s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); } return new Ternary(vmGuru.findById(vmId), context, work); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Determining why we're unable to update the state to Starting for " + vm); - } - + } + VMInstanceVO instance = _vmDao.findById(vmId); if (instance == null) { throw new ConcurrentOperationException("Unable to acquire lock on " + vm); } - + State state = instance.getState(); if (state == State.Running) { if (s_logger.isDebugEnabled()) { @@ -498,7 +512,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return null; } - + if (state.isTransitional()) { if (!checkWorkItems(vm, state)) { throw new ConcurrentOperationException("There are concurrent operations on the VM " + vm); @@ -506,7 +520,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene continue; } } - + if (state != State.Stopped) { s_logger.debug("VM " + vm + " is not in a state to be started: " + state); return null; @@ -515,10 +529,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene txn.commit(); } } - + throw new ConcurrentOperationException("Unable to change the state of " + vm); } - + @DB protected boolean changeState(T vm, Event event, Long hostId, ItWorkVO work, Step step) { Transaction txn = Transaction.currentTxn(); @@ -530,66 +544,68 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene txn.commit(); return true; } - + @Override - public T advanceStart(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { + public T advanceStart(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, + ConcurrentOperationException, ResourceUnavailableException { long vmId = vm.getId(); - Long hostIdSpecified = vm.getHostId(); + Long hostIdSpecified = vm.getHostId(); VirtualMachineGuru vmGuru; if (vm.getHypervisorType() == HypervisorType.BareMetal) { - vmGuru = getBareMetalVmGuru(vm); + vmGuru = getBareMetalVmGuru(vm); } else { - vmGuru = getVmGuru(vm); + vmGuru = getVmGuru(vm); } - - vm = vmGuru.findById(vm.getId()); + + vm = vmGuru.findById(vm.getId()); Ternary start = changeToStartState(vmGuru, vm, caller, account); if (start == null) { return vmGuru.findById(vmId); } - + vm = start.first(); ReservationContext ctx = start.second(); ItWorkVO work = start.third(); - + T startedVm = null; ServiceOfferingVO offering = _offeringDao.findById(vm.getServiceOfferingId()); VMTemplateVO template = _templateDao.findById(vm.getTemplateId()); - + Long clusterSpecified = null; - if(hostIdSpecified != null){ - Host destinationHost = _hostDao.findById(hostIdSpecified); - clusterSpecified = destinationHost.getClusterId(); + if (hostIdSpecified != null) { + Host destinationHost = _hostDao.findById(hostIdSpecified); + clusterSpecified = destinationHost.getClusterId(); } DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterId(), vm.getPodId(), clusterSpecified, hostIdSpecified, null); HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - + try { Journal journal = start.second().getJournal(); - //edit plan if this vm's ROOT volume is in READY state already + // edit plan if this vm's ROOT volume is in READY state already VolumeVO readyRootVolume = null; List vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); - + for (VolumeVO vol : vols) { Volume.State state = vol.getState(); if (state == Volume.State.Ready) { - //make sure if this is a System VM, templateId is unchanged. If it is changed, let planner - //reassign pool for the volume - if(VirtualMachine.Type.isSystemVM(vm.getType())){ - Long volTemplateId = vol.getTemplateId(); - if(volTemplateId != null && template != null){ - if(volTemplateId.longValue() != template.getId()){ + // make sure if this is a System VM, templateId is unchanged. If it is changed, let planner + // reassign pool for the volume + if (VirtualMachine.Type.isSystemVM(vm.getType())) { + Long volTemplateId = vol.getTemplateId(); + if (volTemplateId != null && template != null) { + if (volTemplateId.longValue() != template.getId()) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Root Volume " + vol + " of "+vm.getType().toString() +" System VM is ready, but volume's templateId does not match the System VM Template, updating templateId and reassigning a new pool"); + s_logger.debug("Root Volume " + vol + " of " + vm.getType().toString() + + " System VM is ready, but volume's templateId does not match the System VM Template, updating templateId and reassigning a new pool"); } - vol.setTemplateId(template.getId()); - _volsDao.update(vol.getId(), vol); - continue; - } - } + vol.setTemplateId(template.getId()); + _volsDao.update(vol.getId(), vol); + continue; + } + } - } + } StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId()); if (!pool.isInMaintenance()) { long rootVolDcId = pool.getDataCenterId(); @@ -598,12 +614,13 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId()); readyRootVolume = vol; if (s_logger.isDebugEnabled()) { - s_logger.debug("Root Volume " + vol + " is ready, changing deployment plan to use this pool's datacenterId: "+rootVolDcId +" , podId: "+rootVolPodId +" , and clusterId: "+rootVolClusterId); + s_logger.debug("Root Volume " + vol + " is ready, changing deployment plan to use this pool's datacenterId: " + rootVolDcId + " , podId: " + rootVolPodId + + " , and clusterId: " + rootVolClusterId); } } - } + } } - + ExcludeList avoids = new ExcludeList(); int retry = _retry; while (retry-- != 0) { // It's != so that it can match -1. @@ -611,59 +628,59 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, account, params); DeployDestination dest = null; for (DeploymentPlanner planner : _planners) { - if (planner.canHandle(vmProfile, plan, avoids)) { - dest = planner.plan(vmProfile, plan, avoids); - } else { - continue; - } + if (planner.canHandle(vmProfile, plan, avoids)) { + dest = planner.plan(vmProfile, plan, avoids); + } else { + continue; + } if (dest != null) { avoids.addHost(dest.getHost().getId()); journal.record("Deployment found ", vmProfile, dest); break; } } - + if (dest == null) { - throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId()); + throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId()); } - + long destHostId = dest.getHost().getId(); - + if (!changeState(vm, Event.OperationRetry, destHostId, work, Step.Prepare)) { throw new ConcurrentOperationException("Unable to update the state of the Virtual Machine"); } - + try { - if (vm.getHypervisorType() != HypervisorType.BareMetal) { - if(readyRootVolume != null){ - //remove the vol<->pool from destination, since we don't have to prepare this volume. - if(dest.getStorageForDisks() != null){ + if (vm.getHypervisorType() != HypervisorType.BareMetal) { + if (readyRootVolume != null) { + // remove the vol<->pool from destination, since we don't have to prepare this volume. + if (dest.getStorageForDisks() != null) { if (s_logger.isDebugEnabled()) { s_logger.debug("No need to prepare the READY Root Volume " + readyRootVolume + ", removing it from deploydestination"); } - dest.getStorageForDisks().remove(readyRootVolume); - } - } - _storageMgr.prepare(vmProfile, dest); - } + dest.getStorageForDisks().remove(readyRootVolume); + } + } + _storageMgr.prepare(vmProfile, dest); + } _networkMgr.prepare(vmProfile, dest, ctx); - + vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx); - + VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - + Commands cmds = new Commands(OnError.Revert); cmds.addCommand(new StartCommand(vmTO)); - + vmGuru.finalizeDeployment(cmds, vmProfile, dest, ctx); vm.setPodId(dest.getPod().getId()); - + work = _workDao.findById(work.getId()); if (work == null || work.getStep() != Step.Prepare) { throw new ConcurrentOperationException("Work steps have been changed: " + work); } _workDao.updateStep(work, Step.Starting); - + _agentMgr.send(destHostId, cmds); _workDao.updateStep(work, Step.Started); Answer startAnswer = cmds.getAnswer(StartAnswer.class); @@ -683,7 +700,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } catch (OperationTimedoutException e) { s_logger.debug("Unable to send the start command to host " + dest.getHost()); if (e.isActive()) { - //TODO: This one is different as we're not sure if the VM is actually started. + // TODO: This one is different as we're not sure if the VM is actually started. } avoids.addHost(destHostId); continue; @@ -717,19 +734,19 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene cleanup(vmGuru, vmProfile, work, Event.OperationFailed, false, caller, account); } } - } + } } finally { if (startedVm == null) { - if(vm.getType().equals(VirtualMachine.Type.User)) { + if (vm.getType().equals(VirtualMachine.Type.User)) { _accountMgr.decrementResourceCount(vm.getAccountId(), ResourceType.user_vm); } changeState(vm, Event.OperationFailed, null, work, Step.Done); } } - + return startedVm; } - + @Override public boolean stop(T vm, User user, Account account) throws ResourceUnavailableException { try { @@ -740,17 +757,17 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene throw new CloudRuntimeException("Unable to stop vm because of a concurrent operation", e); } } - + protected boolean sendStop(VirtualMachineGuru guru, VirtualMachineProfile profile, boolean force) { VMInstanceVO vm = profile.getVirtualMachine(); StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); try { - StopAnswer answer = (StopAnswer)_agentMgr.send(vm.getHostId(), stop); + StopAnswer answer = (StopAnswer) _agentMgr.send(vm.getHostId(), stop); if (!answer.getResult()) { s_logger.debug("Unable to stop VM due to " + answer.getDetails()); return false; } - + guru.finalizeStop(profile, answer); } catch (AgentUnavailableException e) { if (!force) { @@ -761,10 +778,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return false; } } - + return true; } - + protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile, ItWorkVO work, Event event, boolean force, User user, Account account) { T vm = profile.getVirtualMachine(); State state = vm.getState(); @@ -775,7 +792,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step); return false; } - + if (step == Step.Started || step == Step.Starting) { if (vm.getHostId() != null) { if (!sendStop(guru, profile, force)) { @@ -784,7 +801,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) { s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step); return true; @@ -802,7 +819,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); return false; } - } + } if (vm.getLastHostId() != null) { if (!sendStop(guru, profile, force)) { s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); @@ -815,7 +832,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return false; } } - + _networkMgr.release(profile, force); _storageMgr.release(profile); s_logger.debug("Successfully cleanued up resources for the vm " + vm + " in " + state + " state"); @@ -832,21 +849,21 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return true; } - + if (state == State.Destroyed || state == State.Expunging || state == State.Error) { if (s_logger.isDebugEnabled()) { s_logger.debug("Stopped called on " + vm + " but the state is " + state); } return true; } - + VirtualMachineGuru vmGuru = getVmGuru(vm); - + if (!stateTransitTo(vm, Event.StopRequested, vm.getHostId())) { if (!forced) { throw new ConcurrentOperationException("VM is being operated on by someone else."); } - + vm = vmGuru.findById(vmId); if (vm == null) { if (s_logger.isDebugEnabled()) { @@ -855,7 +872,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return true; } } - + if ((vm.getState() == State.Starting || vm.getState() == State.Stopping || vm.getState() == State.Migrating) && forced) { ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); if (work != null) { @@ -864,24 +881,24 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); if (vm.getHostId() != null) { String routerPrivateIp = null; - if(vm.getType() == VirtualMachine.Type.DomainRouter){ + if (vm.getType() == VirtualMachine.Type.DomainRouter) { routerPrivateIp = vm.getPrivateIpAddress(); } StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null, routerPrivateIp); boolean stopped = false; StopAnswer answer = null; try { - answer = (StopAnswer)_agentMgr.send(vm.getHostId(), stop); + answer = (StopAnswer) _agentMgr.send(vm.getHostId(), stop); stopped = answer.getResult(); if (!stopped) { throw new CloudRuntimeException("Unable to stop the virtual machine due to " + answer.getDetails()); } vmGuru.finalizeStop(profile, answer); - + } catch (AgentUnavailableException e) { } catch (OperationTimedoutException e) { } finally { @@ -896,65 +913,65 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + if (s_logger.isDebugEnabled()) { s_logger.debug(vm + " is stopped on the host. Proceeding to release resource held."); } - + try { _networkMgr.release(profile, forced); s_logger.debug("Successfully released network resources for the vm " + vm); } catch (Exception e) { s_logger.warn("Unable to release some network resources.", e); } - + try { - if (vm.getHypervisorType() != HypervisorType.BareMetal) { - _storageMgr.release(profile); - s_logger.debug("Successfully released storage resources for the vm " + vm); - } + if (vm.getHypervisorType() != HypervisorType.BareMetal) { + _storageMgr.release(profile); + s_logger.debug("Successfully released storage resources for the vm " + vm); + } } catch (Exception e) { s_logger.warn("Unable to release storage resources.", e); } - + vm.setReservationId(null); - + return stateTransitTo(vm, Event.OperationSucceeded, null); } - + private void setStateMachine() { - _stateMachine = VirtualMachine.State.getStateMachine(); + _stateMachine = VirtualMachine.State.getStateMachine(); } - + protected boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId, String reservationId) { vm.setReservationId(reservationId); return _stateMachine.transitTo(vm, e, hostId, _vmDao); } - + @Override public boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) { State oldState = vm.getState(); - if (oldState == State.Starting ) { + if (oldState == State.Starting) { if (e == Event.OperationSucceeded) { vm.setLastHostId(hostId); } - }else if (oldState == State.Stopping ) { + } else if (oldState == State.Stopping) { if (e == Event.OperationSucceeded) { vm.setLastHostId(vm.getHostId()); } } return _stateMachine.transitTo(vm, e, hostId, _vmDao); } - + @Override public boolean remove(T vm, User user, Account caller) { - //expunge the corresponding nics + // expunge the corresponding nics VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); _networkMgr.expungeNics(profile); s_logger.trace("Nics of the vm " + vm + " are expunged successfully"); return _vmDao.remove(vm.getId()); } - + @Override public boolean destroy(T vm, User user, Account caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException { if (s_logger.isDebugEnabled()) { @@ -966,12 +983,12 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return true; } - + if (!advanceStop(vm, false, user, caller)) { s_logger.debug("Unable to stop " + vm); return false; } - + if (!stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, vm.getHostId())) { s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm.toString()); return false; @@ -979,34 +996,35 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return true; } - + protected boolean checkVmOnHost(VirtualMachine vm, long hostId) throws AgentUnavailableException, OperationTimedoutException { - CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer)_agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName())); + CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName())); if (!answer.getResult() || answer.getState() == State.Stopped) { return false; } - + return true; } - + @Override - public T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException { + public T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, + VirtualMachineMigrationException { s_logger.info("Migrating " + vm + " to " + dest); - + long dstHostId = dest.getHost().getId(); Host fromHost = _hostDao.findById(srcHostId); if (fromHost == null) { s_logger.info("Unable to find the host to migrate from: " + srcHostId); throw new CloudRuntimeException("Unable to find the host to migrate from: " + srcHostId); - } - - if(fromHost.getClusterId().longValue() != dest.getCluster().getId()){ + } + + if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) { s_logger.info("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); throw new CloudRuntimeException("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); } - + VirtualMachineGuru vmGuru = getVmGuru(vm); - + long vmId = vm.getId(); vm = vmGuru.findById(vmId); if (vm == null) { @@ -1015,38 +1033,38 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } throw new ManagementServerException("Unable to find a virtual machine with id " + vmId); } - - if(vm.getState() != State.Running){ + + if (vm.getState() != State.Running) { if (s_logger.isDebugEnabled()) { s_logger.debug("VM is not Running, unable to migrate the vm " + vm); } throw new VirtualMachineMigrationException("VM is not Running, unable to migrate the vm currently " + vm); } - + short alertType = AlertManager.ALERT_TYPE_USERVM_MIGRATE; if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE; } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY_MIGRATE; } - + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); _networkMgr.prepareNicForMigration(profile, dest); _storageMgr.prepareForMigration(profile, dest); HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - + VirtualMachineTO to = hvGuru.implement(profile); PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand(to); - + ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Migrating, vm.getType(), vm.getId()); work.setStep(Step.Prepare); work.setResourceType(ItWorkVO.ResourceType.Host); work.setResourceId(dstHostId); work = _workDao.persist(work); - + PrepareForMigrationAnswer pfma = null; try { - pfma = (PrepareForMigrationAnswer)_agentMgr.send(dstHostId, pfmc); + pfma = (PrepareForMigrationAnswer) _agentMgr.send(dstHostId, pfmc); if (!pfma.getResult()) { String msg = "Unable to prepare for migration due to " + pfma.getDetails(); pfma = null; @@ -1060,19 +1078,19 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene _workDao.update(work.getId(), work); } } - + vm.setLastHostId(srcHostId); if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { s_logger.info("Migration cancelled because state has changed: " + vm); throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm); - } - + } + boolean migrated = false; try { boolean isWindows = _guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows"); MigrateCommand mc = new MigrateCommand(vm.getInstanceName(), dest.getHost().getPrivateIpAddress(), isWindows); try { - MigrateAnswer ma = (MigrateAnswer)_agentMgr.send(vm.getLastHostId(), mc); + MigrateAnswer ma = (MigrateAnswer) _agentMgr.send(vm.getLastHostId(), mc); if (!ma.getResult()) { s_logger.error("Unable to migrate due to " + ma.getDetails()); return null; @@ -1084,45 +1102,45 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId); } - + changeState(vm, VirtualMachine.Event.OperationSucceeded, dstHostId, work, Step.Started); - + try { if (!checkVmOnHost(vm, dstHostId)) { s_logger.error("Unable to complete migration for " + vm); - try{ - _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); - }catch (AgentUnavailableException e) { + try { + _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); + } catch (AgentUnavailableException e) { s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId); } cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); return null; } } catch (OperationTimedoutException e) { - } - + } + migrated = true; return vm; } finally { if (!migrated) { s_logger.info("Migration was unsuccessful. Cleaning up: " + vm); - _alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHost.getName() + " in zone " + dest.getDataCenter().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed. Please check logs."); - try{ - _agentMgr.send(dstHostId, new Commands(cleanup(vm.getInstanceName())), null); - }catch(AgentUnavailableException ae){ - s_logger.info("Looks like the destination Host is unavailable for cleanup"); + _alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHost.getName() + " in zone " + + dest.getDataCenter().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed. Please check logs."); + try { + _agentMgr.send(dstHostId, new Commands(cleanup(vm.getInstanceName())), null); + } catch (AgentUnavailableException ae) { + s_logger.info("Looks like the destination Host is unavailable for cleanup"); } - + stateTransitTo(vm, Event.OperationFailed, srcHostId); } - + work.setStep(Step.Done); _workDao.update(work.getId(), work); } } - protected void cancelWorkItems(long nodeId) { GlobalLock scanLock = GlobalLock.getInternLock("vmmgr.cancel.workitem"); @@ -1142,7 +1160,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } else if (work.getType() == State.Migrating) { _haMgr.scheduleMigration(vm); } - } + } work.setStep(Step.Done); _workDao.update(work.getId(), work); } catch (Exception e) { @@ -1157,7 +1175,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene scanLock.releaseRef(); } } - + @Override public boolean migrateAway(VirtualMachine.Type vmType, long vmId, long srcHostId) throws InsufficientServerCapacityException, VirtualMachineMigrationException { VirtualMachineGuru vmGuru = _vmGurus.get(vmType); @@ -1166,21 +1184,21 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Unable to find a VM for " + vmId); return true; } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + Long hostId = vm.getHostId(); if (hostId == null) { s_logger.debug("Unable to migrate because the VM doesn't have a host id: " + vm); return true; } - + Host host = _hostDao.findById(hostId); - + DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), null, null); ExcludeList excludes = new ExcludeList(); excludes.addHost(hostId); - + DeployDestination dest = null; while (true) { for (DeploymentPlanner planner : _planners) { @@ -1195,11 +1213,11 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Planner " + planner + " was unable to find anything."); } } - + if (dest == null) { throw new InsufficientServerCapacityException("Unable to find a server to migrate to.", host.getClusterId()); } - + excludes.addHost(dest.getHost().getId()); VMInstanceVO vmInstance = null; try { @@ -1207,24 +1225,24 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } catch (ResourceUnavailableException e) { s_logger.debug("Unable to migrate to unavailable " + dest); } catch (ConcurrentOperationException e) { - s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); - } catch (ManagementServerException e) { - s_logger.debug("Unable to migrate VM: " + e.getMessage()); - } catch (VirtualMachineMigrationException e) { - s_logger.debug("Got VirtualMachineMigrationException, Unable to migrate: " + e.getMessage()); - if(vm.getState() == State.Starting){ - s_logger.debug("VM seems to be still Starting, we should retry migration later"); - throw e; - }else{ - s_logger.debug("Unable to migrate VM, VM is not in Running or even Starting state, current state: "+vm.getState().toString()); - } - } + s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); + } catch (ManagementServerException e) { + s_logger.debug("Unable to migrate VM: " + e.getMessage()); + } catch (VirtualMachineMigrationException e) { + s_logger.debug("Got VirtualMachineMigrationException, Unable to migrate: " + e.getMessage()); + if (vm.getState() == State.Starting) { + s_logger.debug("VM seems to be still Starting, we should retry migration later"); + throw e; + } else { + s_logger.debug("Unable to migrate VM, VM is not in Running or even Starting state, current state: " + vm.getState().toString()); + } + } if (vmInstance != null) { return true; } - try { - boolean result = advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); - return result; + try { + boolean result = advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); + return result; } catch (ResourceUnavailableException e) { s_logger.debug("Unable to stop VM due to " + e.getMessage()); } catch (ConcurrentOperationException e) { @@ -1232,10 +1250,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } catch (OperationTimedoutException e) { s_logger.debug("Unable to stop VM due to " + e.getMessage()); } - return false; - } + return false; + } } - + protected class CleanupTask implements Runnable { @Override public void run() { @@ -1247,7 +1265,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + @Override public T reboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { try { @@ -1256,11 +1274,12 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene throw new CloudRuntimeException("Unable to reboot a VM due to concurrent operation", e); } } - + @Override - public T advanceReboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { + public T advanceReboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, + ConcurrentOperationException, ResourceUnavailableException { T rebootedVm = null; - + DataCenter dc = _configMgr.getZone(vm.getDataCenterId()); HostPodVO pod = _configMgr.getPod(vm.getPodId()); Host host = _hostDao.findById(vm.getHostId()); @@ -1269,13 +1288,13 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene cluster = _configMgr.getCluster(host.getClusterId()); } DeployDestination dest = new DeployDestination(dc, pod, cluster, host); - + try { - + Commands cmds = new Commands(OnError.Revert); cmds.addCommand(new RebootCommand(vm.getName())); _agentMgr.send(host.getId(), cmds); - + Answer rebootAnswer = cmds.getAnswer(RebootAnswer.class); if (rebootAnswer != null && rebootAnswer.getResult()) { rebootedVm = vm; @@ -1286,26 +1305,26 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.warn("Unable to send the reboot command to host " + dest.getHost() + " for the vm " + vm + " due to operation timeout", e); throw new CloudRuntimeException("Failed to reboot the vm on host " + dest.getHost()); } - + return rebootedVm; } - + @Override public VMInstanceVO findById(VirtualMachine.Type type, long vmId) { VirtualMachineGuru guru = _vmGurus.get(type); return guru.findById(vmId); } - + public Command cleanup(String vmName) { return new StopCommand(vmName); } - + public Commands deltaSync(long hostId, Map newStates) { Map states = convertToInfos(newStates); Commands commands = new Commands(OnError.Continue); - + boolean nativeHA = _agentMgr.isHostNativeHAEnabled(hostId); - + for (Map.Entry entry : states.entrySet()) { AgentVmInfo info = entry.getValue(); @@ -1314,7 +1333,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene Command command = null; if (vm != null) { command = compareState(vm, info, false, nativeHA); - } else { + } else { if (s_logger.isDebugEnabled()) { s_logger.debug("Cleaning up a VM that is no longer found: " + info.name); } @@ -1328,7 +1347,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return commands; } - + protected Map convertToInfos(final Map states) { final HashMap map = new HashMap(); @@ -1348,7 +1367,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue())); break; } - + Long id = vmGuru.convertToId(name); if (id != null) { map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null, entry.getValue())); @@ -1360,28 +1379,27 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } /** - * compareState does as its name suggests and compares the states between - * management server and agent. It returns whether something should be - * cleaned up - * + * compareState does as its name suggests and compares the states between management server and agent. It returns whether + * something should be cleaned up + * */ protected Command compareState(VMInstanceVO vm, final AgentVmInfo info, final boolean fullSync, boolean nativeHA) { State agentState = info.state; final String agentName = info.name; final State serverState = vm.getState(); final String serverName = vm.getName(); - + VirtualMachineGuru vmGuru = getVmGuru(vm); - + Command command = null; if (s_logger.isDebugEnabled()) { s_logger.debug("VM " + serverName + ": server state = " + serverState.toString() + " and agent state = " + agentState.toString()); } - + if (agentState == State.Error) { agentState = State.Stopped; - + short alertType = AlertManager.ALERT_TYPE_USERVM; if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER; @@ -1392,15 +1410,16 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene HostPodVO podVO = _podDao.findById(vm.getPodId()); DataCenterVO dcVO = _dcDao.findById(vm.getDataCenterId()); HostVO hostVO = _hostDao.findById(vm.getHostId()); - + String hostDesc = "name: " + hostVO.getName() + " (id:" + hostVO.getId() + "), availability zone: " + dcVO.getName() + ", pod: " + podVO.getName(); - _alertMgr.sendAlert(alertType, vm.getDataCenterId(), vm.getPodId(), "VM (name: " + vm.getName() + ", id: " + vm.getId() + ") stopped on host " + hostDesc + " due to storage failure", "Virtual Machine " + vm.getName() + " (id: " + vm.getId() + ") running on host [" + vm.getHostId() + "] stopped due to storage failure."); + _alertMgr.sendAlert(alertType, vm.getDataCenterId(), vm.getPodId(), "VM (name: " + vm.getName() + ", id: " + vm.getId() + ") stopped on host " + hostDesc + " due to storage failure", + "Virtual Machine " + vm.getName() + " (id: " + vm.getId() + ") running on host [" + vm.getHostId() + "] stopped due to storage failure."); } - -// if (serverState == State.Migrating) { -// s_logger.debug("Skipping vm in migrating state: " + vm.toString()); -// return null; -// } + + // if (serverState == State.Migrating) { + // s_logger.debug("Skipping vm in migrating state: " + vm.toString()); + // return null; + // } if (agentState == serverState) { if (s_logger.isDebugEnabled()) { @@ -1409,34 +1428,34 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene assert (agentState == State.Stopped || agentState == State.Running) : "If the states we send up is changed, this must be changed."; if (agentState == State.Running) { stateTransitTo(vm, VirtualMachine.Event.AgentReportRunning, vm.getHostId()); - // FIXME: What if someone comes in and sets it to stopping? Then what? + // FIXME: What if someone comes in and sets it to stopping? Then what? return null; } s_logger.debug("State matches but the agent said stopped so let's send a cleanup command anyways."); return cleanup(agentName); - } - + } + if (agentState == State.Shutdowned) { if (serverState == State.Running || serverState == State.Starting || serverState == State.Stopping) { try { advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); } catch (AgentUnavailableException e) { - assert(false) : "How do we hit this with forced on?"; + assert (false) : "How do we hit this with forced on?"; return null; } catch (OperationTimedoutException e) { - assert(false) : "How do we hit this with forced on?"; + assert (false) : "How do we hit this with forced on?"; return null; } catch (ConcurrentOperationException e) { - assert(false) : "How do we hit this with forced on?"; + assert (false) : "How do we hit this with forced on?"; return null; } } else { - s_logger.debug("Sending cleanup to a shutdowned vm: " + agentName); + s_logger.debug("Sending cleanup to a shutdowned vm: " + agentName); command = cleanup(agentName); } } else if (agentState == State.Stopped) { // This state means the VM on the agent was detected previously - // and now is gone. This is slightly different than if the VM + // and now is gone. This is slightly different than if the VM // was never completed but we still send down a Stop Command // to ensure there's cleanup. if (serverState == State.Running) { @@ -1448,7 +1467,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } else if (serverState == State.Starting) { s_logger.debug("Ignoring VM in starting mode: " + vm.getName()); _haMgr.scheduleRestart(vm, false); - } + } command = cleanup(agentName); } else if (agentState == State.Running) { if (serverState == State.Starting) { @@ -1458,38 +1477,42 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene stateTransitTo(vm, Event.AgentReportRunning, vm.getHostId()); s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running"); vm = vmGuru.findById(vm.getId()); - + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - + List nics = _nicsDao.listByVmId(profile.getId()); + for (NicVO nic : nics) { + Network network = _networkMgr.getNetwork(nic.getNetworkId()); + NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null); + profile.addNic(nicProfile); + } + Commands cmds = new Commands(OnError.Revert); s_logger.debug("Finalizing commands that need to be send to complete Start process for the vm " + vm); - - + if (vmGuru.finalizeCommandsOnStart(cmds, profile)) { if (cmds.size() != 0) { try { _agentMgr.send(vm.getHostId(), cmds); - } catch (OperationTimedoutException e){ - s_logger.error("Exception during update for running vm: " + vm, e); + } catch (OperationTimedoutException e) { + s_logger.error("Exception during update for running vm: " + vm, e); return null; } catch (ResourceUnavailableException e) { - s_logger.error("Exception during update for running vm: " + vm, e); + s_logger.error("Exception during update for running vm: " + vm, e); return null; } } - + if (vmGuru.finalizeStart(profile, vm.getHostId(), cmds, null)) { stateTransitTo(vm, Event.AgentReportRunning, vm.getHostId()); } else { - s_logger.error("Exception during update for running vm: " + vm); + s_logger.error("Exception during update for running vm: " + vm); return null; } } else { s_logger.error("Unable to finalize commands on start for vm: " + vm); return null; } - - + } } else if (serverState == State.Stopping) { s_logger.debug("Scheduling a stop command for " + vm); @@ -1497,7 +1520,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } else { s_logger.debug("VM state is in stopped so stopping it on the agent"); command = cleanup(agentName); - } + } } return command; } @@ -1518,10 +1541,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (info == null) { info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); castedVm = info.guru.findById(vm.getId()); - } else { + } else { castedVm = info.vm; } - + Command command = compareState(castedVm, info, true, nativeHA); if (command != null) { commands.addCommand(command); @@ -1551,7 +1574,6 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return commands; } - @Override public boolean isRecurring() { return false; @@ -1570,12 +1592,12 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } return true; } - + @Override public boolean processTimeout(long agentId, long seq) { return true; } - + @Override public int getTimeout() { return -1; @@ -1586,7 +1608,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene boolean processed = false; for (Command cmd : cmds) { if (cmd instanceof PingRoutingCommand) { - PingRoutingCommand ping = (PingRoutingCommand)cmd; + PingRoutingCommand ping = (PingRoutingCommand) cmd; if (ping.getNewStates().size() > 0) { Commands commands = deltaSync(agentId, ping.getNewStates()); if (commands.size() > 0) { @@ -1607,24 +1629,24 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene public AgentControlAnswer processControlCommand(long agentId, AgentControlCommand cmd) { return null; } - + @Override public boolean processDisconnect(long agentId, Status state) { return true; } - + @Override public void processConnect(HostVO agent, StartupCommand cmd) throws ConnectionException { if (!(cmd instanceof StartupRoutingCommand)) { return; } - + long agentId = agent.getId(); - - StartupRoutingCommand startup = (StartupRoutingCommand)cmd; - + + StartupRoutingCommand startup = (StartupRoutingCommand) cmd; + Commands commands = fullSync(agentId, startup.getVmStates()); - + if (commands.size() > 0) { s_logger.debug("Sending clean commands to the agent"); @@ -1649,7 +1671,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + protected class TransitionTask implements Runnable { @Override public void run() { @@ -1658,7 +1680,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Couldn't get the global lock"); return; } - + if (!lock.lock(30)) { s_logger.debug("Couldn't lock the db"); return; @@ -1682,7 +1704,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + protected class AgentVmInfo { public String name; public State state; @@ -1694,7 +1716,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene this.name = name; this.state = state; this.vm = vm; - this.guru = (VirtualMachineGuru)guru; + this.guru = (VirtualMachineGuru) guru; } } }