Skip to content

Commit 3a9ebbf

Browse files
João JandreJoaoJandre
authored andcommitted
fix chain on different storages
1 parent 4e6cacd commit 3a9ebbf

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/SnapshotDataStoreDaoImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ public SnapshotDataStoreVO findParent(DataStoreRole role, Long storeId, Long zon
338338
}
339339

340340
sc.setParameters(VOLUME_ID, volumeId);
341-
sc.setParameters(STORE_ROLE, role.toString());
341+
if (role != null) {
342+
sc.setParameters(STORE_ROLE, role.toString());
343+
}
342344
sc.setParameters(STATE, ObjectInDataStoreStateMachine.State.Ready.name());
343345
if (storeId != null) {
344346
sc.setParameters(STORE_ID, new Long[]{storeId});

engine/storage/src/main/java/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.cloud.hypervisor.Hypervisor;
2323
import com.cloud.storage.ImageStore;
2424
import com.cloud.storage.snapshot.SnapshotManager;
25-
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
2625
import org.apache.logging.log4j.Logger;
2726
import org.apache.logging.log4j.LogManager;
2827
import org.springframework.stereotype.Component;
@@ -141,18 +140,16 @@ public DataObject create(DataObject obj, DataStore dataStore) {
141140
ss.setSize(snapshotInfo.getSize()); // this is the virtual size of snapshot in primary storage.
142141
ss.setPhysicalSize(snapshotInfo.getSize()); // this physical size will get updated with actual size once the snapshot backup is done.
143142
Long clusterId = hostDao.findClusterIdByVolumeInfo(snapshotInfo.getBaseVolume());
144-
boolean kvmIncrementalSnapshot = SnapshotManager.kvmIncrementalSnapshot.valueIn(clusterId);
145-
SnapshotDataStoreVO snapshotDataStoreVO = snapshotDataStoreDao.findParent(dataStore.getRole(), dataStore.getId(), null, snapshotInfo.getVolumeId(), kvmIncrementalSnapshot, snapshotInfo.getHypervisorType());
146-
snapshotDataStoreVO = tryToGetSnapshotOnSecondaryIfNotOnPrimaryAndIsKVM(dataStore, snapshotInfo, snapshotDataStoreVO, kvmIncrementalSnapshot, snapshotInfo.getHypervisorType());
147-
if (snapshotDataStoreVO != null) {
143+
SnapshotDataStoreVO parentSnapshotDataStoreVO = findParent(dataStore, clusterId, snapshotInfo);
144+
if (parentSnapshotDataStoreVO != null) {
148145
//Double check the snapshot is removed or not
149-
SnapshotVO parentSnap = snapshotDao.findById(snapshotDataStoreVO.getSnapshotId());
150-
if (parentSnap != null && !snapshotDataStoreVO.isEndOfChain()) {
151-
ss.setParentSnapshotId(snapshotDataStoreVO.getSnapshotId());
152-
} else if (snapshotDataStoreVO.isEndOfChain()) {
146+
SnapshotVO parentSnap = snapshotDao.findById(parentSnapshotDataStoreVO.getSnapshotId());
147+
if (parentSnap != null && !parentSnapshotDataStoreVO.isEndOfChain()) {
148+
ss.setParentSnapshotId(parentSnapshotDataStoreVO.getSnapshotId());
149+
} else if (parentSnapshotDataStoreVO.isEndOfChain()) {
153150
logger.debug("Snapshot [{}] will begin a new chain, as the last one has finished.", ss.getSnapshotId());
154151
} else {
155-
logger.debug("find inconsistent db for snapshot " + snapshotDataStoreVO.getSnapshotId());
152+
logger.debug("find inconsistent db for snapshot " + parentSnapshotDataStoreVO.getSnapshotId());
156153
}
157154
}
158155
ss.setState(ObjectInDataStoreStateMachine.State.Allocated);
@@ -219,10 +216,29 @@ public DataObject create(DataObject obj, DataStore dataStore) {
219216
return this.get(obj, dataStore, null);
220217
}
221218

222-
private SnapshotDataStoreVO tryToGetSnapshotOnSecondaryIfNotOnPrimaryAndIsKVM(DataStore dataStore, SnapshotInfo snapshotInfo, SnapshotDataStoreVO snapshotDataStoreVO,
223-
boolean kvmIncrementalSnapshot, Hypervisor.HypervisorType hypervisorType) {
224-
if (snapshotDataStoreVO == null && Hypervisor.HypervisorType.KVM.equals(snapshotInfo.getHypervisorType()) && DataStoreRole.Primary.equals(dataStore.getRole())) {
225-
snapshotDataStoreVO = snapshotDataStoreDao.findParent(DataStoreRole.Image, null, ((PrimaryDataStore)dataStore).getDataCenterId(), snapshotInfo.getVolumeId(), kvmIncrementalSnapshot, hypervisorType);
219+
private SnapshotDataStoreVO findParent(DataStore dataStore, Long clusterId, SnapshotInfo snapshotInfo) {
220+
boolean kvmIncrementalSnapshot = SnapshotManager.kvmIncrementalSnapshot.valueIn(clusterId);
221+
SnapshotDataStoreVO snapshotDataStoreVO;
222+
if (Hypervisor.HypervisorType.KVM.equals(snapshotInfo.getHypervisorType()) && kvmIncrementalSnapshot) {
223+
snapshotDataStoreVO = snapshotDataStoreDao.findParent(null, null, null, snapshotInfo.getVolumeId(),
224+
kvmIncrementalSnapshot, snapshotInfo.getHypervisorType());
225+
snapshotDataStoreVO = returnNullIfNotOnSameTypeOfStoreRole(snapshotInfo, snapshotDataStoreVO);
226+
} else {
227+
snapshotDataStoreVO = snapshotDataStoreDao.findParent(dataStore.getRole(), dataStore.getId(), null, snapshotInfo.getVolumeId(),
228+
kvmIncrementalSnapshot, snapshotInfo.getHypervisorType());
229+
}
230+
return snapshotDataStoreVO;
231+
}
232+
233+
private SnapshotDataStoreVO returnNullIfNotOnSameTypeOfStoreRole(SnapshotInfo snapshotInfo, SnapshotDataStoreVO snapshotDataStoreVO) {
234+
if (snapshotDataStoreVO == null) {
235+
return snapshotDataStoreVO;
236+
}
237+
if ((snapshotInfo.getImageStore() != null && !snapshotDataStoreVO.getRole().isImageStore()) ||
238+
(snapshotInfo.getImageStore() == null && snapshotDataStoreVO.getRole().isImageStore())) {
239+
snapshotDataStoreVO.setEndOfChain(true);
240+
snapshotDataStoreDao.update(snapshotDataStoreVO.getId(), snapshotDataStoreVO);
241+
return null;
226242
}
227243
return snapshotDataStoreVO;
228244
}

0 commit comments

Comments
 (0)