From 898051ecff5c10540ea9a0933a0df3f752d87826 Mon Sep 17 00:00:00 2001 From: Sheng Yang Date: Tue, 16 Aug 2011 11:41:59 -0700 Subject: [PATCH] Fix NPE when a router is fail to start Also enforce the check for restartNetworkCommand --- .../cloud/network/element/VirtualRouterElement.java | 12 +++++++++--- .../router/VirtualNetworkApplianceManagerImpl.java | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/server/src/com/cloud/network/element/VirtualRouterElement.java b/server/src/com/cloud/network/element/VirtualRouterElement.java index c6862261354..4cfc3fccbaa 100644 --- a/server/src/com/cloud/network/element/VirtualRouterElement.java +++ b/server/src/com/cloud/network/element/VirtualRouterElement.java @@ -63,6 +63,7 @@ import com.cloud.vm.ReservationContext; import com.cloud.vm.UserVmManager; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineProfile; +import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.UserVmDao; @@ -153,17 +154,22 @@ public class VirtualRouterElement extends DhcpElement implements NetworkElement, } /* Get the host_id in order to find the cluster */ - long host_id = 0; + Long host_id = new Long(0); for (DomainRouterVO router : routers) { - host_id = router.getHostId(); + if (host_id == null || host_id == 0) { + host_id = (router.getHostId() != null ? router.getHostId() : router.getLastHostId()); + } /* FIXME it's not completely safe to ignore these failure, but we would try to push on now */ - if (_routerMgr.stopRouter(router.getId(), false) == null) { + if (router.getState() != State.Stopped || _routerMgr.stopRouter(router.getId(), false) == null) { s_logger.warn("Failed to stop virtual router element " + router + " as a part of network " + network + " restart"); } if (!_routerMgr.destroyRouter(router.getId())) { s_logger.warn("Failed to destroy virtual router element " + router + " as a part of network " + network + " restart"); } } + if (host_id == null || host_id == 0) { + throw new ResourceUnavailableException("Fail to locate virtual router element in network " + network.getId(), this.getClass(), 0); + } /* The cluster here is only used to determine hypervisor type, not the real deployment */ Cluster cluster = _configMgr.getCluster(_hostDao.findById(host_id).getClusterId()); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 39eee8c71a6..85e7236a4bb 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -1066,7 +1066,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (state != State.Running) { router = startVirtualRouter(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); } - runningRouters.add(router); + if (router != null) { + runningRouters.add(router); + } } } return runningRouters;