Skip to content

Commit ebe8fa0

Browse files
author
Pearl Dsilva
committed
fix async backup for clvm type pools
1 parent 2aadffa commit ebe8fa0

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
import com.cloud.utils.DateUtil.IntervalType;
166166
import com.cloud.utils.NumbersUtil;
167167
import com.cloud.utils.Pair;
168+
import com.cloud.storage.clvm.ClvmPoolManager;
168169
import com.cloud.utils.Ternary;
169170
import com.cloud.utils.concurrency.NamedThreadFactory;
170171
import com.cloud.utils.db.DB;
@@ -1661,7 +1662,7 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc
16611662
if (backupSnapToSecondary) {
16621663
if (!isKvmAndFileBasedStorage) {
16631664
backupSnapshotToSecondary(payload.getAsyncBackup(), snapshotStrategy, snapshotOnPrimary, payload.getZoneIds(), payload.getStoragePoolIds());
1664-
if (storagePool.getPoolType() == StoragePoolType.CLVM || storagePool.getPoolType() == StoragePoolType.CLVM_NG) {
1665+
if (!payload.getAsyncBackup() && (storagePool.getPoolType() == StoragePoolType.CLVM || storagePool.getPoolType() == StoragePoolType.CLVM_NG)) {
16651666
_snapshotStoreDao.removeBySnapshotStore(snapshotId, snapshotOnPrimary.getDataStore().getId(), snapshotOnPrimary.getDataStore().getRole());
16661667
}
16671668
} else {
@@ -1683,7 +1684,12 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc
16831684
postCreateSnapshot(volume.getId(), snapshotId, payload.getSnapshotPolicyId(), clusterId);
16841685
snapshotZoneDao.addSnapshotToZone(snapshotId, snapshot.getDataCenterId());
16851686

1686-
DataStoreRole dataStoreRole = backupSnapToSecondary ? snapshotHelper.getDataStoreRole(snapshot) : DataStoreRole.Primary;
1687+
DataStoreRole dataStoreRole;
1688+
if (payload.getAsyncBackup() && backupSnapToSecondary && !isKvmAndFileBasedStorage) {
1689+
dataStoreRole = DataStoreRole.Primary;
1690+
} else {
1691+
dataStoreRole = backupSnapToSecondary ? snapshotHelper.getDataStoreRole(snapshot) : DataStoreRole.Primary;
1692+
}
16871693

16881694
List<SnapshotDataStoreVO> snapshotStoreRefs = _snapshotStoreDao.listReadyBySnapshot(snapshotId, dataStoreRole);
16891695
if (CollectionUtils.isEmpty(snapshotStoreRefs)) {
@@ -1821,6 +1827,7 @@ protected void runInContext() {
18211827
SnapshotInfo backupedSnapshot = snapshotStrategy.backupSnapshot(snapshotOnPrimary);
18221828
if (backupedSnapshot != null) {
18231829
snapshotStrategy.postSnapshotCreation(snapshotOnPrimary);
1830+
removeClvmPrimarySnapshotStoreRefIfNeeded(snapshotOnPrimary);
18241831
copyNewSnapshotToZones(snapshotOnPrimary.getId(), snapshotOnPrimary.getDataCenterId(), zoneIds);
18251832
}
18261833
}
@@ -1846,6 +1853,14 @@ private void decriseBackupSnapshotAttempts() {
18461853
}
18471854
}
18481855

1856+
void removeClvmPrimarySnapshotStoreRefIfNeeded(SnapshotInfo snapshotOnPrimary) {
1857+
DataStore dataStore = snapshotOnPrimary.getDataStore();
1858+
StoragePoolVO pool = _storagePoolDao.findById(dataStore.getId());
1859+
if (pool != null && ClvmPoolManager.isClvmPoolType(pool.getPoolType())) {
1860+
_snapshotStoreDao.removeBySnapshotStore(snapshotOnPrimary.getId(), dataStore.getId(), dataStore.getRole());
1861+
}
1862+
}
1863+
18491864
private void updateSnapshotPayload(long storagePoolId, CreateSnapshotPayload payload, boolean isKvmAndFileBasedStorage, StoragePoolType poolType, Long clusterId) {
18501865
StoragePoolVO storagePoolVO = _storagePoolDao.findById(storagePoolId);
18511866

server/src/test/java/com/cloud/storage/snapshot/SnapshotManagerImplTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.org.Grouping;
2727
import com.cloud.storage.DataStoreRole;
2828
import com.cloud.storage.Snapshot;
29+
import com.cloud.storage.Storage.StoragePoolType;
2930
import com.cloud.storage.SnapshotPolicyVO;
3031
import com.cloud.storage.SnapshotVO;
3132
import com.cloud.storage.VolumeVO;
@@ -55,8 +56,10 @@
5556
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
5657
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
5758
import org.apache.cloudstack.framework.async.AsyncCallFuture;
59+
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
5860
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
5961
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
62+
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
6063

6164
import org.junit.Assert;
6265
import org.junit.After;
@@ -105,6 +108,8 @@ public class SnapshotManagerImplTest {
105108
SnapshotScheduler snapshotScheduler;
106109
@Mock
107110
TaggedResourceService taggedResourceService;
111+
@Mock
112+
PrimaryDataStoreDao primaryDataStoreDao;
108113
@InjectMocks
109114
SnapshotManagerImpl snapshotManager = new SnapshotManagerImpl();
110115

@@ -115,6 +120,7 @@ public void setUp() {
115120
snapshotManager._accountMgr = accountManager;
116121
snapshotManager._snapSchedMgr = snapshotScheduler;
117122
snapshotManager.taggedResourceService = taggedResourceService;
123+
snapshotManager._storagePoolDao = primaryDataStoreDao;
118124
}
119125

120126
@After
@@ -610,4 +616,57 @@ public void testDeleteSnapshotPoliciesManualPolicyId() {
610616

611617
snapshotManager.deleteSnapshotPolicies(cmd);
612618
}
619+
620+
@Test
621+
public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_ClvmPool() {
622+
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
623+
DataStore dataStore = Mockito.mock(DataStore.class);
624+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
625+
626+
Mockito.when(snapshot.getDataStore()).thenReturn(dataStore);
627+
Mockito.when(snapshot.getId()).thenReturn(1L);
628+
Mockito.when(dataStore.getId()).thenReturn(2L);
629+
Mockito.when(dataStore.getRole()).thenReturn(DataStoreRole.Primary);
630+
Mockito.when(primaryDataStoreDao.findById(2L)).thenReturn(pool);
631+
Mockito.when(pool.getPoolType()).thenReturn(StoragePoolType.CLVM);
632+
633+
snapshotManager.removeClvmPrimarySnapshotStoreRefIfNeeded(snapshot);
634+
635+
Mockito.verify(snapshotStoreDao).removeBySnapshotStore(1L, 2L, DataStoreRole.Primary);
636+
}
637+
638+
@Test
639+
public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_ClvmNgPool() {
640+
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
641+
DataStore dataStore = Mockito.mock(DataStore.class);
642+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
643+
644+
Mockito.when(snapshot.getDataStore()).thenReturn(dataStore);
645+
Mockito.when(snapshot.getId()).thenReturn(3L);
646+
Mockito.when(dataStore.getId()).thenReturn(4L);
647+
Mockito.when(dataStore.getRole()).thenReturn(DataStoreRole.Primary);
648+
Mockito.when(primaryDataStoreDao.findById(4L)).thenReturn(pool);
649+
Mockito.when(pool.getPoolType()).thenReturn(StoragePoolType.CLVM_NG);
650+
651+
snapshotManager.removeClvmPrimarySnapshotStoreRefIfNeeded(snapshot);
652+
653+
Mockito.verify(snapshotStoreDao).removeBySnapshotStore(3L, 4L, DataStoreRole.Primary);
654+
}
655+
656+
@Test
657+
public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_NonClvmPool() {
658+
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
659+
DataStore dataStore = Mockito.mock(DataStore.class);
660+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
661+
662+
Mockito.when(snapshot.getDataStore()).thenReturn(dataStore);
663+
Mockito.when(dataStore.getId()).thenReturn(5L);
664+
Mockito.when(primaryDataStoreDao.findById(5L)).thenReturn(pool);
665+
Mockito.when(pool.getPoolType()).thenReturn(StoragePoolType.NetworkFilesystem);
666+
667+
snapshotManager.removeClvmPrimarySnapshotStoreRefIfNeeded(snapshot);
668+
669+
Mockito.verify(snapshotStoreDao, Mockito.never()).removeBySnapshotStore(
670+
Mockito.anyLong(), Mockito.anyLong(), Mockito.any());
671+
}
613672
}

0 commit comments

Comments
 (0)