Create DB entries done
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/4c110096 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/4c110096 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/4c110096 Branch: refs/heads/vmsync Commit: 4c110096a7641df229f1c533e91b4deca46dbc79 Parents: 6bf9de5 Author: Alex Huang <[email protected]> Authored: Thu May 16 15:11:23 2013 -0700 Committer: Alex Huang <[email protected]> Committed: Thu May 16 15:11:23 2013 -0700 ---------------------------------------------------------------------- .../src/com/cloud/storage/VolumeManager.java | 108 ++++++++++++ .../engine/vm/VirtualMachineOrchestrator.java | 133 ++++++++++----- .../platform/orchestration/CloudOrchestrator.java | 4 +- server/src/com/cloud/storage/VolumeManager.java | 108 ------------ 4 files changed, 202 insertions(+), 151 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4c110096/engine/components-api/src/com/cloud/storage/VolumeManager.java ---------------------------------------------------------------------- diff --git a/engine/components-api/src/com/cloud/storage/VolumeManager.java b/engine/components-api/src/com/cloud/storage/VolumeManager.java new file mode 100644 index 0000000..d198e5d --- /dev/null +++ b/engine/components-api/src/com/cloud/storage/VolumeManager.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.cloud.storage; + +import java.util.Map; + +import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd; +import org.apache.cloudstack.api.command.user.volume.CreateVolumeCmd; +import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd; +import org.apache.cloudstack.api.command.user.volume.MigrateVolumeCmd; +import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd; +import org.apache.cloudstack.api.command.user.volume.UploadVolumeCmd; +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; + +import com.cloud.agent.api.to.VirtualMachineTO; +import com.cloud.deploy.DeployDestination; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientStorageCapacityException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.StorageUnavailableException; +import com.cloud.host.Host; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.storage.Volume.Type; +import com.cloud.user.Account; +import com.cloud.vm.DiskProfile; +import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachineProfile; + +public interface VolumeManager extends VolumeApiService { + + VolumeInfo moveVolume(VolumeInfo volume, long destPoolDcId, Long destPoolPodId, + Long destPoolClusterId, HypervisorType dataDiskHyperType) + throws ConcurrentOperationException; + + VolumeVO uploadVolume(UploadVolumeCmd cmd) + throws ResourceAllocationException; + + VolumeVO allocateDuplicateVolume(VolumeVO oldVol, Long templateId); + + boolean volumeOnSharedStoragePool(VolumeVO volume); + + boolean volumeInactive(Volume volume); + + String getVmNameOnVolume(Volume volume); + + VolumeVO allocVolume(CreateVolumeCmd cmd) + throws ResourceAllocationException; + + VolumeVO createVolume(CreateVolumeCmd cmd); + + VolumeVO resizeVolume(ResizeVolumeCmd cmd) + throws ResourceAllocationException; + + boolean deleteVolume(long volumeId, Account caller) + throws ConcurrentOperationException; + + void destroyVolume(VolumeVO volume); + + DiskProfile allocateRawVolume(Type type, String name, DiskOfferingVO offering, Long size, VMInstanceVO vm, Account owner); + Volume attachVolumeToVM(AttachVolumeCmd command); + + Volume detachVolumeFromVM(DetachVolumeCmd cmmd); + + void release(VirtualMachineProfile<? extends VMInstanceVO> profile); + + void cleanupVolumes(long vmId) throws ConcurrentOperationException; + + Volume migrateVolume(MigrateVolumeCmd cmd); + + <T extends VMInstanceVO> void migrateVolumes(T vm, VirtualMachineTO vmTo, Host srcHost, Host destHost, + Map<VolumeVO, StoragePoolVO> volumeToPool); + + boolean storageMigration( + VirtualMachineProfile<? extends VirtualMachine> vm, + StoragePool destPool); + + void prepareForMigration( + VirtualMachineProfile<? extends VirtualMachine> vm, + DeployDestination dest); + + void prepare(VirtualMachineProfile<? extends VirtualMachine> vm, + DeployDestination dest) throws StorageUnavailableException, + InsufficientStorageCapacityException, ConcurrentOperationException; + + boolean canVmRestartOnAnotherServer(long vmId); + + DiskProfile allocateTemplatedVolume(Type type, String name, + DiskOfferingVO offering, VMTemplateVO template, VMInstanceVO vm, + Account owner); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4c110096/engine/orchestration/src/org/apache/cloudstack/engine/vm/VirtualMachineOrchestrator.java ---------------------------------------------------------------------- diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/vm/VirtualMachineOrchestrator.java b/engine/orchestration/src/org/apache/cloudstack/engine/vm/VirtualMachineOrchestrator.java index 5f1e3d0..63d45a3 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/vm/VirtualMachineOrchestrator.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/vm/VirtualMachineOrchestrator.java @@ -33,7 +33,6 @@ import org.apache.cloudstack.config.ConfigRepo; import org.apache.cloudstack.config.ConfigValue; import org.apache.cloudstack.engine.cloud.entity.VMEntityVO; import org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity; -import org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntityImpl; import org.apache.cloudstack.engine.config.Configs; import org.apache.cloudstack.engine.subsystem.api.storage.StorageOrchestrator; import org.apache.cloudstack.network.NetworkOrchestrator; @@ -50,28 +49,34 @@ import com.cloud.cluster.ManagementServerNode; import com.cloud.dao.EntityManager; import com.cloud.deploy.DeploymentPlan; import com.cloud.domain.dao.DomainDao; +import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.ConnectionException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InvalidParameterValueException; import com.cloud.host.HostVO; import com.cloud.host.Status; -import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.hypervisor.Hypervisor; +import com.cloud.network.NetworkManager; import com.cloud.network.dao.NetworkVO; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.storage.DiskOfferingVO; +import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.Volume.Type; +import com.cloud.storage.VolumeManager; import com.cloud.storage.dao.VMTemplateDao; import com.cloud.user.AccountVO; import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.UserDao; import com.cloud.utils.DateUtil; import com.cloud.utils.Pair; -import com.cloud.utils.component.ComponentContext; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicProfile; -import com.cloud.vm.UserVmVO; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachineManager; import com.cloud.vm.VirtualMachineProfileImpl; @@ -95,7 +100,7 @@ public class VirtualMachineOrchestrator extends ManagerBase { @Inject VmWorkJobDao _workJobDao; @Inject - VirtualMachineManager _itMgr; + VolumeManager _volMgr; protected ConfigValue<Integer> _retry; protected ConfigValue<Integer> _cancelWait; @@ -117,7 +122,11 @@ public class VirtualMachineOrchestrator extends ManagerBase { ServiceOfferingDao _offeringDao; @Inject VMTemplateDao _templateDao; - // FIXME: Hopefull we can remove the above stuff. + @Inject + VirtualMachineManager _itMgr; + @Inject + NetworkManager _networkMgr; + // FIXME: Hopefully we can remove the above stuff. long _nodeId; @@ -129,7 +138,7 @@ public class VirtualMachineOrchestrator extends ManagerBase { String templateId, String hostName, String displayName, - String hypervisor, + Hypervisor.HypervisorType hypervisor, int cpu, int speed, long memory, @@ -147,13 +156,8 @@ public class VirtualMachineOrchestrator extends ManagerBase { } } - VirtualMachineEntityImpl vmEntity = ComponentContext.inject(VirtualMachineEntityImpl.class); - vmEntity.init(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, new ArrayList<String>(networkNicMap.keySet())); - - HypervisorType hypervisorType = HypervisorType.valueOf(hypervisor); - //load vm instance and offerings and call virtualMachineManagerImpl - VMInstanceVO vm = _entityMgr.findByUuid(VMInstanceVO.class, id); + VMEntityVO vm = _entityMgr.findByUuid(VMEntityVO.class, id); // If the template represents an ISO, a disk offering must be passed in, and will be used to create the root disk // Else, a disk offering is optional, and if present will be used to create the data disk @@ -181,20 +185,14 @@ public class VirtualMachineOrchestrator extends ManagerBase { dataDiskOfferings.add(new Pair<DiskOfferingVO, Long>(diskOffering, size)); } - if (_itMgr.allocate(_entityMgr.findById(UserVmVO.class, vm.getId()), - _entityMgr.findById(VMTemplateVO.class, templateId), - offering, - rootDiskOffering, - dataDiskOfferings, - networkIpMap, - null, - plan, - hypervisorType, - _entityMgr.findById(AccountVO.class, new Long(owner))) == null) { + VMTemplateVO template = _entityMgr.findByUuid(VMTemplateVO.class, templateId); + + vm = createDbEntities(vm, plan, template, offering, rootDiskOffering, dataDiskOfferings, networkIpMap, hypervisor, owner); + if (vm == null) { return null; } - return vmEntity; + return new VirtualMachineEntityImpl2(vm); } public VirtualMachineEntity createFromScratch( @@ -203,7 +201,7 @@ public class VirtualMachineOrchestrator extends ManagerBase { String isoId, String hostName, String displayName, - String hypervisor, + Hypervisor.HypervisorType hypervisorType, String os, int cpu, int speed, @@ -214,12 +212,7 @@ public class VirtualMachineOrchestrator extends ManagerBase { Map<String, NicProfile> networkNicMap, DeploymentPlan plan) throws InsufficientCapacityException { - // VirtualMachineEntityImpl vmEntity = new VirtualMachineEntityImpl(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, networks, vmEntityManager); - VirtualMachineEntityImpl vmEntity = ComponentContext.inject(VirtualMachineEntityImpl.class); - vmEntity.init(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, new ArrayList<String>(networkNicMap.keySet())); - - //load vm instance and offerings and call virtualMachineManagerImpl - VMInstanceVO vm = _entityMgr.findByUuid(VMInstanceVO.class, id); + VMEntityVO vm = _entityMgr.findByUuid(VMEntityVO.class, id); Pair<DiskOfferingVO, Long> rootDiskOffering = new Pair<DiskOfferingVO, Long>(null, null); ServiceOfferingVO offering = _entityMgr.findById(ServiceOfferingVO.class, vm.getServiceOfferingId()); @@ -227,9 +220,6 @@ public class VirtualMachineOrchestrator extends ManagerBase { List<Pair<DiskOfferingVO, Long>> dataDiskOfferings = new ArrayList<Pair<DiskOfferingVO, Long>>(); Long diskOfferingId = vm.getDiskOfferingId(); - if (diskOfferingId == null) { - throw new InvalidParameterValueException("Installing from ISO requires a disk offering to be specified for the root disk."); - } DiskOfferingVO diskOffering = _entityMgr.findById(DiskOfferingVO.class, diskOfferingId); if (diskOffering == null) { throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingId); @@ -252,17 +242,78 @@ public class VirtualMachineOrchestrator extends ManagerBase { } } - HypervisorType hypervisorType = HypervisorType.valueOf(hypervisor); + VMTemplateVO template = _entityMgr.findByUuid(VMTemplateVO.class, isoId); - if (_itMgr.allocate(_entityMgr.findById(UserVmVO.class, vm.getId()), - _entityMgr.findById(VMTemplateVO.class, new Long(isoId)), - offering, rootDiskOffering, dataDiskOfferings, networkIpMap, - null, plan, hypervisorType, - _entityMgr.findById(AccountVO.class, new Long(owner))) == null) { + vm = createDbEntities(vm, plan, template, offering, rootDiskOffering, dataDiskOfferings, networkIpMap, hypervisorType, owner); + if (vm == null) { return null; } - return vmEntity; + return new VirtualMachineEntityImpl2(vm); + } + + @DB + protected VMEntityVO createDbEntities(VMEntityVO vm1, DeploymentPlan plan, VMTemplateVO template, ServiceOfferingVO serviceOffering, + Pair<? extends DiskOfferingVO, Long> rootDiskOffering, + List<Pair<DiskOfferingVO, Long>> dataDiskOfferings, List<Pair<NetworkVO, NicProfile>> networks, + Hypervisor.HypervisorType hyperType, String ownerRef) throws InsufficientCapacityException { + assert (plan.getClusterId() == null && plan.getPoolId() == null) : "We currently don't support cluster and pool preset yet"; + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating entries for VM: " + vm1); + } + + AccountVO owner = _entityMgr.findById(AccountVO.class, new Long(ownerRef)); + + VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, vm1.getId()); + VirtualMachineProfileImpl<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vm); + + Transaction txn = Transaction.currentTxn(); + txn.start(); + + try { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating nics for " + vm); + } + + _networkMgr.allocate(vmProfile, networks); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Concurrent operation while trying to allocate resources for the VM", e); + } + + if (dataDiskOfferings == null) { + dataDiskOfferings = new ArrayList<Pair<DiskOfferingVO, Long>>(0); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocaing disks for " + vm); + } + + if (template.getFormat() == ImageFormat.ISO) { + _volMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vm, owner); + } else if (template.getFormat() == ImageFormat.BAREMETAL) { + // Do nothing + } else { + _volMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), template, vm, owner); + } + + for (Pair<DiskOfferingVO, Long> offering : dataDiskOfferings) { + _volMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vm.getId(), offering.first(), offering.second(), vm, owner); + } + + vm1.setDataCenterId(plan.getDataCenterId()); + if (plan.getPodId() != null) { + vm1.setPodId(plan.getPodId()); + } + + vm1 = _entityMgr.persist(vm1); + txn.commit(); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocation completed for VM: " + vm); + } + + return vm1; } public VirtualMachineEntity get(String uuid) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4c110096/engine/orchestration/src/org/apache/cloudstack/platform/orchestration/CloudOrchestrator.java ---------------------------------------------------------------------- diff --git a/engine/orchestration/src/org/apache/cloudstack/platform/orchestration/CloudOrchestrator.java b/engine/orchestration/src/org/apache/cloudstack/platform/orchestration/CloudOrchestrator.java index a958d3a..0a3a44b 100755 --- a/engine/orchestration/src/org/apache/cloudstack/platform/orchestration/CloudOrchestrator.java +++ b/engine/orchestration/src/org/apache/cloudstack/platform/orchestration/CloudOrchestrator.java @@ -128,7 +128,7 @@ public class CloudOrchestrator implements OrchestrationService { templateId, hostName, displayName, - hypervisor, + Hypervisor.HypervisorType.valueOf(hypervisor), cpu, speed, memory, @@ -149,7 +149,7 @@ public class CloudOrchestrator implements OrchestrationService { isoId, hostName, displayName, - hypervisor, + Hypervisor.HypervisorType.valueOf(hypervisor), os, cpu, speed, http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4c110096/server/src/com/cloud/storage/VolumeManager.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/storage/VolumeManager.java b/server/src/com/cloud/storage/VolumeManager.java deleted file mode 100644 index d198e5d..0000000 --- a/server/src/com/cloud/storage/VolumeManager.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.cloud.storage; - -import java.util.Map; - -import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd; -import org.apache.cloudstack.api.command.user.volume.CreateVolumeCmd; -import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd; -import org.apache.cloudstack.api.command.user.volume.MigrateVolumeCmd; -import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd; -import org.apache.cloudstack.api.command.user.volume.UploadVolumeCmd; -import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; -import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; - -import com.cloud.agent.api.to.VirtualMachineTO; -import com.cloud.deploy.DeployDestination; -import com.cloud.exception.ConcurrentOperationException; -import com.cloud.exception.InsufficientStorageCapacityException; -import com.cloud.exception.ResourceAllocationException; -import com.cloud.exception.StorageUnavailableException; -import com.cloud.host.Host; -import com.cloud.hypervisor.Hypervisor.HypervisorType; -import com.cloud.storage.Volume.Type; -import com.cloud.user.Account; -import com.cloud.vm.DiskProfile; -import com.cloud.vm.VMInstanceVO; -import com.cloud.vm.VirtualMachine; -import com.cloud.vm.VirtualMachineProfile; - -public interface VolumeManager extends VolumeApiService { - - VolumeInfo moveVolume(VolumeInfo volume, long destPoolDcId, Long destPoolPodId, - Long destPoolClusterId, HypervisorType dataDiskHyperType) - throws ConcurrentOperationException; - - VolumeVO uploadVolume(UploadVolumeCmd cmd) - throws ResourceAllocationException; - - VolumeVO allocateDuplicateVolume(VolumeVO oldVol, Long templateId); - - boolean volumeOnSharedStoragePool(VolumeVO volume); - - boolean volumeInactive(Volume volume); - - String getVmNameOnVolume(Volume volume); - - VolumeVO allocVolume(CreateVolumeCmd cmd) - throws ResourceAllocationException; - - VolumeVO createVolume(CreateVolumeCmd cmd); - - VolumeVO resizeVolume(ResizeVolumeCmd cmd) - throws ResourceAllocationException; - - boolean deleteVolume(long volumeId, Account caller) - throws ConcurrentOperationException; - - void destroyVolume(VolumeVO volume); - - DiskProfile allocateRawVolume(Type type, String name, DiskOfferingVO offering, Long size, VMInstanceVO vm, Account owner); - Volume attachVolumeToVM(AttachVolumeCmd command); - - Volume detachVolumeFromVM(DetachVolumeCmd cmmd); - - void release(VirtualMachineProfile<? extends VMInstanceVO> profile); - - void cleanupVolumes(long vmId) throws ConcurrentOperationException; - - Volume migrateVolume(MigrateVolumeCmd cmd); - - <T extends VMInstanceVO> void migrateVolumes(T vm, VirtualMachineTO vmTo, Host srcHost, Host destHost, - Map<VolumeVO, StoragePoolVO> volumeToPool); - - boolean storageMigration( - VirtualMachineProfile<? extends VirtualMachine> vm, - StoragePool destPool); - - void prepareForMigration( - VirtualMachineProfile<? extends VirtualMachine> vm, - DeployDestination dest); - - void prepare(VirtualMachineProfile<? extends VirtualMachine> vm, - DeployDestination dest) throws StorageUnavailableException, - InsufficientStorageCapacityException, ConcurrentOperationException; - - boolean canVmRestartOnAnotherServer(long vmId); - - DiskProfile allocateTemplatedVolume(Type type, String name, - DiskOfferingVO offering, VMTemplateVO template, VMInstanceVO vm, - Account owner); -}
