@Test public void test() throws ApiSenderException, InterruptedException { final BackupStorageInventory sftp = deployer.backupStorages.get("sftp"); final ImageInventory img = new ImageInventory(); img.setName("image"); img.setPlatform(ImagePlatform.Linux.toString()); img.setMediaType(ImageMediaType.RootVolumeTemplate.toString()); img.setFormat("qcow2"); img.setUrl("http://test.img"); for (int i = 0; i < num; i++) { createVm(img, sftp.getUuid()); } latch.await(5, TimeUnit.MINUTES); Assert.assertEquals(num, vms.size()); fconfig.applyDhcpCmdList.clear(); HostInventory host = deployer.hosts.get("host1"); api.reconnectHost(host.getUuid()); Assert.assertFalse(fconfig.applyDhcpCmdList.isEmpty()); ApplyDhcpCmd cmd = fconfig.applyDhcpCmdList.get(0); Assert.assertEquals(num + 1, cmd.dhcp.size()); List<VmInstanceVO> vms = dbf.listAll(VmInstanceVO.class); for (VmInstanceVO vm : vms) { checkNic(vm, cmd.dhcp); } }
@Test public void test() throws ApiSenderException, InterruptedException { long requiredSize = SizeUnit.GIGABYTE.toByte(10); SimulatorPrimaryStorageDetails sp = new SimulatorPrimaryStorageDetails(); sp.setTotalCapacity(SizeUnit.TERABYTE.toByte(10)); sp.setAvailableCapacity(sp.getTotalCapacity()); sp.setUrl("nfs://simulator/primary/"); PrimaryStorageInventory pinv = api.createSimulatoPrimaryStorage(1, sp).get(0); ZoneInventory zone = api.createZones(1).get(0); ClusterInventory cluster = api.createClusters(1, zone.getUuid()).get(0); HostInventory host = api.createHost(1, cluster.getUuid()).get(0); api.attachPrimaryStorage(cluster.getUuid(), pinv.getUuid()); AllocatePrimaryStorageMsg msg = new AllocatePrimaryStorageMsg(); msg.setRequiredHostUuid(host.getUuid()); msg.setSize(requiredSize); msg.setServiceId(bus.makeLocalServiceId(PrimaryStorageConstant.SERVICE_ID)); MessageReply reply = bus.call(msg); Assert.assertEquals(AllocatePrimaryStorageReply.class, reply.getClass()); AllocatePrimaryStorageReply ar = (AllocatePrimaryStorageReply) reply; Assert.assertEquals(pinv.getUuid(), ar.getPrimaryStorageInventory().getUuid()); ReturnPrimaryStorageCapacityMsg rmsg = new ReturnPrimaryStorageCapacityMsg(); rmsg.setDiskSize(requiredSize); rmsg.setPrimaryStorageUuid(pinv.getUuid()); rmsg.setServiceId(bus.makeLocalServiceId(PrimaryStorageConstant.SERVICE_ID)); bus.send(rmsg); Thread.sleep(2000); PrimaryStorageVO pvo = dbf.findByUuid(pinv.getUuid(), PrimaryStorageVO.class); Assert.assertEquals( pvo.getCapacity().getTotalCapacity(), pvo.getCapacity().getAvailableCapacity()); }
@Test public void test() throws ApiSenderException, InterruptedException { VmInstanceInventory vm1 = deployer.vms.get("TestVm1"); VmInstanceInventory vm2 = deployer.vms.get("TestVm2"); api.setVmHaLevel(vm1.getUuid(), VmHaLevel.NeverStop, null); api.setVmHaLevel(vm2.getUuid(), VmHaLevel.OnHostFailure, null); api.maintainHost(vm1.getHostUuid()); HostInventory host2 = deployer.hosts.get("host2"); VmInstanceVO vmvo1 = dbf.findByUuid(vm1.getUuid(), VmInstanceVO.class); VmInstanceVO vmvo2 = dbf.findByUuid(vm2.getUuid(), VmInstanceVO.class); Assert.assertEquals(VmInstanceState.Running, vmvo1.getState()); Assert.assertEquals(host2.getUuid(), vmvo1.getHostUuid()); Assert.assertEquals(VmInstanceState.Stopped, vmvo2.getState()); }
@Override public void afterHostConnected(HostInventory host) { SimpleQuery<LocalStorageHostRefVO> q = dbf.createQuery(LocalStorageHostRefVO.class); q.add(LocalStorageHostRefVO_.hostUuid, Op.EQ, host.getUuid()); LocalStorageHostRefVO ref = q.find(); if (ref != null) { RecalculatePrimaryStorageCapacityMsg msg = new RecalculatePrimaryStorageCapacityMsg(); msg.setPrimaryStorageUuid(ref.getPrimaryStorageUuid()); bus.makeTargetServiceIdByResourceUuid( msg, PrimaryStorageConstant.SERVICE_ID, ref.getPrimaryStorageUuid()); bus.send(msg); } }
@Override @Transactional(readOnly = true) public HostMaintenancePolicy getHostMaintenancePolicy(HostInventory host) { String sql = "select count(ps) from PrimaryStorageVO ps, PrimaryStorageClusterRefVO ref where ps.uuid = ref.primaryStorageUuid" + " and ps.type = :type and ref.clusterUuid = :cuuid"; TypedQuery<Long> q = dbf.getEntityManager().createQuery(sql, Long.class); q.setParameter("type", LocalStorageConstants.LOCAL_STORAGE_TYPE); q.setParameter("cuuid", host.getClusterUuid()); q.setMaxResults(1); Long count = q.getSingleResult(); return count > 0 ? HostMaintenancePolicy.StopVm : null; }
@Override public void afterDeleteHost(final HostInventory inventory) { final String priUuid = getLocalStorageInCluster(inventory.getClusterUuid()); if (priUuid != null) { RemoveHostFromLocalStorageMsg msg = new RemoveHostFromLocalStorageMsg(); msg.setPrimaryStorageUuid(priUuid); msg.setHostUuid(inventory.getUuid()); bus.makeTargetServiceIdByResourceUuid(msg, PrimaryStorageConstant.SERVICE_ID, priUuid); MessageReply reply = bus.call(msg); if (!reply.isSuccess()) { // TODO logger.warn( String.format( "failed to remove host[uuid:%s] from local primary storage[uuid:%s], %s", inventory.getUuid(), priUuid, reply.getError())); } else { logger.debug( String.format( "removed host[uuid:%s] from local primary storage[uuid:%s]", inventory.getUuid(), priUuid)); } } }
@Test public void test() throws ApiSenderException, InterruptedException { HostInventory host2 = deployer.hosts.get("host2"); HostInventory host1 = deployer.hosts.get("host1"); VmInstanceInventory vm = deployer.vms.get("TestVm"); PrimaryStorageInventory local = deployer.primaryStorages.get("local"); List<VolumeVO> vols = dbf.listAll(VolumeVO.class); List<VolumeVO> volumesOnLocal = new ArrayList<VolumeVO>(); ImageCacheVO cacheVO = dbf.listAll(ImageCacheVO.class).get(0); long imageSize = cacheVO.getSize(); long usedVolumeSize = 0; for (VolumeVO vol : vols) { if (vol.getPrimaryStorageUuid().equals(local.getUuid())) { volumesOnLocal.add(vol); usedVolumeSize += ratioMgr.calculateByRatio(vol.getPrimaryStorageUuid(), vol.getSize()); } } config.createEmptyVolumeCmds.clear(); config.deleteBitsCmds.clear(); vm = api.migrateVmInstance(vm.getUuid(), host2.getUuid()); TimeUnit.SECONDS.sleep(5); LocalStorageHostRefVO ref1 = dbf.findByUuid(host1.getUuid(), LocalStorageHostRefVO.class); Assert.assertEquals(ref1.getTotalCapacity() - imageSize, ref1.getAvailableCapacity()); LocalStorageHostRefVO ref2 = dbf.findByUuid(host2.getUuid(), LocalStorageHostRefVO.class); Assert.assertEquals( ref2.getTotalCapacity() - imageSize - usedVolumeSize, ref2.getAvailableCapacity()); Assert.assertEquals(volumesOnLocal.size(), config.createEmptyVolumeCmds.size()); for (final VolumeVO vol : volumesOnLocal) { // volumes are created on dst host CreateEmptyVolumeCmd cmd = CollectionUtils.find( config.createEmptyVolumeCmds, new Function<CreateEmptyVolumeCmd, CreateEmptyVolumeCmd>() { @Override public CreateEmptyVolumeCmd call(CreateEmptyVolumeCmd arg) { return arg.getVolumeUuid().equals(vol.getUuid()) ? arg : null; } }); Assert.assertNotNull(cmd); Assert.assertEquals(vol.getInstallPath(), cmd.getInstallUrl()); LocalStorageResourceRefVO r = dbf.findByUuid(vol.getUuid(), LocalStorageResourceRefVO.class); Assert.assertEquals(host2.getUuid(), r.getHostUuid()); // volumes are deleted on src host DeleteBitsCmd dcmd = CollectionUtils.find( config.deleteBitsCmds, new Function<DeleteBitsCmd, DeleteBitsCmd>() { @Override public DeleteBitsCmd call(DeleteBitsCmd arg) { return arg.getPath().equals(vol.getInstallPath()) ? arg : null; } }); Assert.assertNotNull( String.format( "no delete command for volume[uuid:%s, path:%s]", vol.getUuid(), vol.getInstallPath()), dcmd); } Assert.assertFalse(kconfig.migrateVmCmds.isEmpty()); MigrateVmCmd mcmd = kconfig.migrateVmCmds.get(0); Assert.assertEquals(host2.getManagementIp(), mcmd.getDestHostIp()); Assert.assertEquals(vm.getUuid(), mcmd.getVmUuid()); Assert.assertEquals( StorageMigrationPolicy.IncCopy.toString(), mcmd.getStorageMigrationPolicy()); }
@Override public void beforeDeleteHost(final HostInventory inventory) { SimpleQuery<LocalStorageHostRefVO> q = dbf.createQuery(LocalStorageHostRefVO.class); q.select(LocalStorageHostRefVO_.primaryStorageUuid); q.add(LocalStorageHostRefVO_.hostUuid, Op.EQ, inventory.getUuid()); final String psUuid = q.findValue(); if (psUuid == null) { return; } logger.debug( String.format( "the host[uuid:%s] belongs to the local storage[uuid:%s], starts to delete vms and" + " volumes on the host", inventory.getUuid(), psUuid)); final List<String> vmUuids = new Callable<List<String>>() { @Override @Transactional(readOnly = true) public List<String> call() { String sql = "select vm.uuid from VolumeVO vol, LocalStorageResourceRefVO ref, VmInstanceVO vm where ref.primaryStorageUuid = :psUuid" + " and vol.type = :vtype and ref.resourceUuid = vol.uuid and ref.resourceType = :rtype and ref.hostUuid = :huuid" + " and vm.uuid = vol.vmInstanceUuid"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("vtype", VolumeType.Root); q.setParameter("rtype", VolumeVO.class.getSimpleName()); q.setParameter("huuid", inventory.getUuid()); q.setParameter("psUuid", psUuid); return q.getResultList(); } }.call(); // destroy vms if (!vmUuids.isEmpty()) { List<DestroyVmInstanceMsg> msgs = CollectionUtils.transformToList( vmUuids, new Function<DestroyVmInstanceMsg, String>() { @Override public DestroyVmInstanceMsg call(String uuid) { DestroyVmInstanceMsg msg = new DestroyVmInstanceMsg(); msg.setVmInstanceUuid(uuid); bus.makeTargetServiceIdByResourceUuid(msg, VmInstanceConstant.SERVICE_ID, uuid); return msg; } }); final FutureCompletion completion = new FutureCompletion(); bus.send( msgs, new CloudBusListCallBack(completion) { @Override public void run(List<MessageReply> replies) { for (MessageReply r : replies) { if (!r.isSuccess()) { String vmUuid = vmUuids.get(replies.indexOf(r)); // TODO logger.warn( String.format("failed to destroy the vm[uuid:%s], %s", vmUuid, r.getError())); } } completion.success(); } }); completion.await(TimeUnit.MINUTES.toMillis(15)); } final List<String> volUuids = new Callable<List<String>>() { @Override @Transactional(readOnly = true) public List<String> call() { String sql = "select vol.uuid from VolumeVO vol, LocalStorageResourceRefVO ref where ref.primaryStorageUuid = :psUuid" + " and vol.type = :vtype and ref.resourceUuid = vol.uuid and ref.resourceType = :rtype and ref.hostUuid = :huuid"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("psUuid", psUuid); q.setParameter("vtype", VolumeType.Data); q.setParameter("rtype", VolumeVO.class.getSimpleName()); q.setParameter("huuid", inventory.getUuid()); return q.getResultList(); } }.call(); // delete data volumes if (!volUuids.isEmpty()) { List<DeleteVolumeMsg> msgs = CollectionUtils.transformToList( volUuids, new Function<DeleteVolumeMsg, String>() { @Override public DeleteVolumeMsg call(String uuid) { DeleteVolumeMsg msg = new DeleteVolumeMsg(); msg.setUuid(uuid); msg.setDetachBeforeDeleting(true); bus.makeTargetServiceIdByResourceUuid(msg, VolumeConstant.SERVICE_ID, uuid); return msg; } }); final FutureCompletion completion = new FutureCompletion(); bus.send( msgs, new CloudBusListCallBack(completion) { @Override public void run(List<MessageReply> replies) { for (MessageReply r : replies) { if (!r.isSuccess()) { String uuid = volUuids.get(replies.indexOf(r)); // TODO logger.warn( String.format( "failed to delete the data volume[uuid:%s], %s", uuid, r.getError())); } } completion.success(); } }); completion.await(TimeUnit.MINUTES.toMillis(15)); } }