Skip to content

Commit 2a60305

Browse files
authored
Fix snapshot chaining on Xen (#12597)
1 parent 7b46749 commit 2a60305

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public interface SnapshotDataStoreDao extends GenericDao<SnapshotDataStoreVO, Lo
5151

5252
SnapshotDataStoreVO findBySnapshotIdAndDataStoreRoleAndState(long snapshotId, DataStoreRole role, ObjectInDataStoreStateMachine.State state);
5353

54+
List<SnapshotDataStoreVO> listBySnapshotIdAndDataStoreRoleAndStateIn(long snapshotId, DataStoreRole role, ObjectInDataStoreStateMachine.State... state);
55+
5456
List<SnapshotDataStoreVO> listReadyByVolumeIdAndCheckpointPathNotNull(long volumeId);
5557

5658
SnapshotDataStoreVO findOneBySnapshotId(long snapshotId, long zoneId);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
6868
protected SearchBuilder<SnapshotDataStoreVO> searchFilteringStoreIdEqStateEqStoreRoleEqIdEqUpdateCountEqSnapshotIdEqVolumeIdEq;
6969
private SearchBuilder<SnapshotDataStoreVO> stateSearch;
7070
private SearchBuilder<SnapshotDataStoreVO> idStateNinSearch;
71+
private SearchBuilder<SnapshotDataStoreVO> idEqRoleEqStateInSearch;
7172
protected SearchBuilder<SnapshotVO> snapshotVOSearch;
7273
private SearchBuilder<SnapshotDataStoreVO> snapshotCreatedSearch;
7374
private SearchBuilder<SnapshotDataStoreVO> dataStoreAndInstallPathSearch;
@@ -151,6 +152,11 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
151152
idStateNinSearch.and(STATE, idStateNinSearch.entity().getState(), SearchCriteria.Op.NOTIN);
152153
idStateNinSearch.done();
153154

155+
idEqRoleEqStateInSearch = createSearchBuilder();
156+
idEqRoleEqStateInSearch.and(SNAPSHOT_ID, idEqRoleEqStateInSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
157+
idEqRoleEqStateInSearch.and(STORE_ROLE, idEqRoleEqStateInSearch.entity().getRole(), SearchCriteria.Op.EQ);
158+
idEqRoleEqStateInSearch.and(STATE, idEqRoleEqStateInSearch.entity().getState(), SearchCriteria.Op.IN);
159+
154160
snapshotVOSearch = snapshotDao.createSearchBuilder();
155161
snapshotVOSearch.and(VOLUME_ID, snapshotVOSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
156162
snapshotVOSearch.done();
@@ -387,6 +393,15 @@ public SnapshotDataStoreVO findBySnapshotIdAndDataStoreRoleAndState(long snapsho
387393
return findOneBy(sc);
388394
}
389395

396+
@Override
397+
public List<SnapshotDataStoreVO> listBySnapshotIdAndDataStoreRoleAndStateIn(long snapshotId, DataStoreRole role, State... state) {
398+
SearchCriteria<SnapshotDataStoreVO> sc = idEqRoleEqStateInSearch.create();
399+
sc.setParameters(SNAPSHOT_ID, snapshotId);
400+
sc.setParameters(STORE_ROLE, role);
401+
sc.setParameters(STATE, (Object[])state);
402+
return listBy(sc);
403+
}
404+
390405
@Override
391406
public SnapshotDataStoreVO findOneBySnapshotId(long snapshotId, long zoneId) {
392407
try (TransactionLegacy transactionLegacy = TransactionLegacy.currentTxn()) {

engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public class DefaultSnapshotStrategy extends SnapshotStrategyBase {
120120
private final List<Snapshot.State> snapshotStatesAbleToDeleteSnapshot = Arrays.asList(Snapshot.State.Destroying, Snapshot.State.Destroyed, Snapshot.State.Error, Snapshot.State.Hidden);
121121

122122
public SnapshotDataStoreVO getSnapshotImageStoreRef(long snapshotId, long zoneId) {
123-
List<SnapshotDataStoreVO> snaps = snapshotStoreDao.listReadyBySnapshot(snapshotId, DataStoreRole.Image);
123+
List<SnapshotDataStoreVO> snaps = snapshotStoreDao.listBySnapshotIdAndDataStoreRoleAndStateIn(snapshotId, DataStoreRole.Image, State.Ready, State.Hidden);
124124
for (SnapshotDataStoreVO ref : snaps) {
125125
if (zoneId == dataStoreMgr.getStoreZoneId(ref.getDataStoreId(), ref.getRole())) {
126126
return ref;

engine/storage/snapshot/src/test/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategyTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,6 @@ public void verifyIfTheSnapshotIsBeingUsedByAnyVolumeTestDetailsIsNotEmptyThrowC
257257

258258
@Test
259259
public void testGetSnapshotImageStoreRefNull() {
260-
SnapshotDataStoreVO ref1 = Mockito.mock(SnapshotDataStoreVO.class);
261-
Mockito.when(ref1.getDataStoreId()).thenReturn(1L);
262-
Mockito.when(ref1.getRole()).thenReturn(DataStoreRole.Image);
263-
Mockito.when(snapshotDataStoreDao.listReadyBySnapshot(Mockito.anyLong(), Mockito.any(DataStoreRole.class))).thenReturn(List.of(ref1));
264-
Mockito.when(dataStoreManager.getStoreZoneId(1L, DataStoreRole.Image)).thenReturn(2L);
265260
Assert.assertNull(defaultSnapshotStrategySpy.getSnapshotImageStoreRef(1L, 1L));
266261
}
267262

@@ -270,7 +265,7 @@ public void testGetSnapshotImageStoreRefNotNull() {
270265
SnapshotDataStoreVO ref1 = Mockito.mock(SnapshotDataStoreVO.class);
271266
Mockito.when(ref1.getDataStoreId()).thenReturn(1L);
272267
Mockito.when(ref1.getRole()).thenReturn(DataStoreRole.Image);
273-
Mockito.when(snapshotDataStoreDao.listReadyBySnapshot(Mockito.anyLong(), Mockito.any(DataStoreRole.class))).thenReturn(List.of(ref1));
268+
Mockito.when(snapshotDataStoreDao.listBySnapshotIdAndDataStoreRoleAndStateIn(Mockito.anyLong(), Mockito.any(DataStoreRole.class), Mockito.any(), Mockito.any())).thenReturn(List.of(ref1));
274269
Mockito.when(dataStoreManager.getStoreZoneId(1L, DataStoreRole.Image)).thenReturn(1L);
275270
Assert.assertNotNull(defaultSnapshotStrategySpy.getSnapshotImageStoreRef(1L, 1L));
276271
}

0 commit comments

Comments
 (0)