|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.cloudstack.platform.subsystem.api.storage.DataStore; |
| 24 | +import org.apache.cloudstack.platform.subsystem.api.storage.StorageProvider; |
| 25 | +import org.apache.cloudstack.platform.subsystem.api.storage.VolumeStrategy; |
| 26 | +import org.apache.cloudstack.storage.volume.VolumeManager; |
| 27 | +import org.apache.log4j.Logger; |
| 28 | + |
| 29 | +import com.cloud.deploy.DeploymentPlan; |
| 30 | +import com.cloud.offering.DiskOffering; |
| 31 | +import com.cloud.storage.DiskOfferingVO; |
| 32 | +import com.cloud.storage.StoragePool; |
| 33 | +import com.cloud.storage.Volume; |
| 34 | +import com.cloud.storage.VolumeHostVO; |
| 35 | +import com.cloud.storage.VolumeVO; |
| 36 | +import com.cloud.storage.dao.DiskOfferingDao; |
| 37 | +import com.cloud.storage.dao.StoragePoolDao; |
| 38 | +import com.cloud.storage.dao.VolumeDao; |
| 39 | +import com.cloud.storage.dao.VolumeHostDao; |
| 40 | +import com.cloud.utils.component.Inject; |
| 41 | +import com.cloud.utils.db.DB; |
| 42 | +import com.cloud.utils.db.Transaction; |
| 43 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 44 | +import com.cloud.utils.fsm.NoTransitionException; |
| 45 | +import com.cloud.vm.VirtualMachine; |
| 46 | +import com.cloud.vm.dao.VMInstanceDao; |
| 47 | + |
| 48 | +public class StorageOrchestratorImpl implements StorageOrchestrator { |
| 49 | + private static final Logger s_logger = Logger.getLogger(StorageOrchestratorImpl.class); |
| 50 | + @Inject |
| 51 | + StoragePoolDao _storagePoolDao; |
| 52 | + @Inject |
| 53 | + StorageProviderManager _spManager; |
| 54 | + @Inject |
| 55 | + VolumeDao _volumeDao; |
| 56 | + @Inject |
| 57 | + VMInstanceDao _vmDao; |
| 58 | + @Inject |
| 59 | + DiskOfferingDao _diskOfferingDao; |
| 60 | + @Inject |
| 61 | + VolumeHostDao _volumeHostDao; |
| 62 | + @Inject |
| 63 | + StorageProviderManager _storageProviderMgr; |
| 64 | + @Inject |
| 65 | + VolumeManager _volumeMgr; |
| 66 | + |
| 67 | + protected Volume copyVolumeFromBackupStorage(VolumeVO volume, DataStore destStore, String reservationId) { |
| 68 | + StorageProvider sp = _storageProviderMgr.getBackupStorageProvider(volume.getDataCenterId()); |
| 69 | + |
| 70 | + VolumeHostVO volumeHostVO = _volumeHostDao.findByVolumeId(volume.getId()); |
| 71 | + long poolId = volumeHostVO.getHostId(); |
| 72 | + DataStore srcStore = _storageProviderMgr.getDataStore(poolId); |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + protected Volume migrateVolume(VolumeVO volume, DataStore srcStore, DataStore destStore, String reservationId) throws NoTransitionException { |
| 79 | + VolumeStrategy vs = srcStore.getVolumeStrategy(); |
| 80 | + |
| 81 | + Transaction txn = Transaction.currentTxn(); |
| 82 | + txn.start(); |
| 83 | + volume.setReservationId(reservationId); |
| 84 | + volume = _volumeMgr.processEvent(volume, Volume.Event.MigrationRequested); |
| 85 | + Volume destVolume = _volumeMgr.allocateDuplicateVolume(volume); |
| 86 | + destVolume = _volumeMgr.processEvent(destVolume, Volume.Event.CreateRequested); |
| 87 | + txn.commit(); |
| 88 | + |
| 89 | + vs.migrateVolume(volume, srcStore, destVolume, destStore); |
| 90 | + |
| 91 | + txn.start(); |
| 92 | + volume = _volumeMgr.processEvent(volume, Volume.Event.OperationSucceeded); |
| 93 | + destVolume = _volumeMgr.processEvent(destVolume, Volume.Event.OperationSucceeded); |
| 94 | + txn.commit(); |
| 95 | + _volumeDao.remove(volume.getId()); |
| 96 | + return destVolume; |
| 97 | + } |
| 98 | + |
| 99 | + @DB |
| 100 | + protected void prepareVolumes(List<VolumeVO> vols, Long destPoolId, String reservationId) throws NoTransitionException { |
| 101 | + DataStore destStore = null; |
| 102 | + if (destPoolId != null) { |
| 103 | + destStore = _storageProviderMgr.getDataStore(destPoolId); |
| 104 | + } |
| 105 | + |
| 106 | + for (VolumeVO volume : vols) { |
| 107 | + if (volume.getPoolId() == null && destStore == null) { |
| 108 | + throw new CloudRuntimeException("Volume has no pool associate and also no storage pool assigned in DeployDestination, Unable to create."); |
| 109 | + } |
| 110 | + if (destStore == null) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + DataStore srcStore = _storageProviderMgr.getDataStore(volume.getPoolId()); |
| 115 | + boolean needToCreateVolume = false; |
| 116 | + boolean needToRecreateVolume = false; |
| 117 | + boolean needToMigrateVolume = false; |
| 118 | + boolean needToCopyFromSec = false; |
| 119 | + |
| 120 | + Volume.State state = volume.getState(); |
| 121 | + if (state == Volume.State.Allocated) { |
| 122 | + needToCreateVolume = true; |
| 123 | + } else if (state == Volume.State.UploadOp) { |
| 124 | + needToCopyFromSec = true; |
| 125 | + } else if (destStore.getId() != srcStore.getId()) { |
| 126 | + if (s_logger.isDebugEnabled()) { |
| 127 | + s_logger.debug("Mismatch in storage pool " + destStore.getId() + " assigned by deploymentPlanner and the one associated with volume " + volume); |
| 128 | + } |
| 129 | + |
| 130 | + if (Volume.Type.ROOT == volume.getVolumeType()) { |
| 131 | + needToMigrateVolume = true; |
| 132 | + } else { |
| 133 | + if (destStore.getCluterId() != srcStore.getCluterId()) { |
| 134 | + needToMigrateVolume = true; |
| 135 | + } else if (!srcStore.isSharedStorage() && srcStore.getId() != destStore.getId()) { |
| 136 | + needToMigrateVolume = true; |
| 137 | + } else { |
| 138 | + continue; |
| 139 | + } |
| 140 | + } |
| 141 | + } else { |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + VolumeStrategy vs = srcStore.getVolumeStrategy(); |
| 146 | + if (needToCreateVolume) { |
| 147 | + volume.setReservationId(reservationId); |
| 148 | + volume = _volumeMgr.processEvent(volume, Volume.Event.CreateRequested); |
| 149 | + |
| 150 | + vs.createVolume(volume, destStore); |
| 151 | + |
| 152 | + volume = _volumeMgr.processEvent(volume, Volume.Event.OperationSucceeded); |
| 153 | + } else if (needToMigrateVolume) { |
| 154 | + migrateVolume(volume, srcStore, destStore, reservationId); |
| 155 | + } else if (needToCopyFromSec) { |
| 156 | + _volumeMgr.processEvent(volume, Volume.Event.CopyRequested); |
| 157 | + } else if (needToRecreateVolume) { |
| 158 | + |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public void prepare(long vmId, DeploymentPlan plan, String reservationId) { |
| 164 | + VirtualMachine vm = _vmDao.findById(vmId); |
| 165 | + |
| 166 | + |
| 167 | + List<VolumeVO> vols = _volumeDao.findUsableVolumesForInstance(vm.getId()); |
| 168 | + if (s_logger.isDebugEnabled()) { |
| 169 | + s_logger.debug("Prepare " + vols.size() + " volumes for " + vm.getInstanceName()); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + public void release(long vmId, String reservationId) { |
| 177 | + // TODO Auto-generated method stub |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + public void destroy(List<Long> disks, String reservationId) { |
| 182 | + // TODO Auto-generated method stub |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + public void cancel(String reservationId) { |
| 187 | + // TODO Auto-generated method stub |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + public void prepareAttachDiskToVM(long disk, long vm, String reservationId) { |
| 192 | + // TODO Auto-generated method stub |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments