Skip to content

Commit 466e6d1

Browse files
sanjaytripathiyadvr
authored andcommitted
CLOUDSTACK-8097: Failed to create volume snapshot after vm live migration across clusters.
(cherry picked from commit 0c4128e) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent d807bf0 commit 466e6d1

6 files changed

Lines changed: 113 additions & 23 deletions

File tree

‎engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
5858
import org.apache.cloudstack.storage.command.CommandResult;
5959
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
60+
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
6061
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
6162
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
6263
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
@@ -95,6 +96,7 @@
9596
import com.cloud.storage.Volume;
9697
import com.cloud.storage.Volume.Type;
9798
import com.cloud.storage.VolumeVO;
99+
import com.cloud.storage.dao.SnapshotDao;
98100
import com.cloud.storage.dao.VolumeDao;
99101
import com.cloud.storage.dao.VolumeDetailsDao;
100102
import com.cloud.template.TemplateManager;
@@ -141,6 +143,10 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati
141143
@Inject
142144
protected VolumeDao _volumeDao;
143145
@Inject
146+
protected SnapshotDao _snapshotDao;
147+
@Inject
148+
protected SnapshotDataStoreDao _snapshotDataStoreDao;
149+
@Inject
144150
protected ResourceLimitService _resourceLimitMgr;
145151
@Inject
146152
VolumeDetailsDao _volDetailDao;
@@ -916,8 +922,14 @@ public Volume migrateVolume(Volume volume, StoragePool destPool) throws StorageU
916922
try {
917923
VolumeApiResult result = future.get();
918924
if (result.isFailed()) {
919-
s_logger.error("migrate volume failed:" + result.getResult());
920-
throw new StorageUnavailableException("migrate volume failed: " + result.getResult(), destPool.getId());
925+
s_logger.error("Migrate volume failed:" + result.getResult());
926+
throw new StorageUnavailableException("Migrate volume failed: " + result.getResult(), destPool.getId());
927+
} else {
928+
// update the volumeId for snapshots on secondary
929+
if (!_snapshotDao.listByVolumeId(vol.getId()).isEmpty()) {
930+
_snapshotDao.updateVolumeIds(vol.getId(), result.getVolume().getId());
931+
_snapshotDataStoreDao.updateVolumeIds(vol.getId(), result.getVolume().getId());
932+
}
921933
}
922934
return result.getVolume();
923935
} catch (InterruptedException e) {

‎engine/schema/src/com/cloud/storage/dao/SnapshotDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ public interface SnapshotDao extends GenericDao<SnapshotVO, Long>, StateDao<Snap
5757

5858
List<SnapshotVO> listAllByStatus(Snapshot.State... status);
5959

60+
void updateVolumeIds(long oldVolId, long newVolId);
61+
6062
}

‎engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.cloud.utils.db.Filter;
4242
import com.cloud.utils.db.GenericDaoBase;
4343
import com.cloud.utils.db.GenericSearchBuilder;
44+
import com.cloud.utils.db.UpdateBuilder;
4445
import com.cloud.utils.db.JoinBuilder.JoinType;
4546
import com.cloud.utils.db.SearchBuilder;
4647
import com.cloud.utils.db.SearchCriteria;
@@ -326,4 +327,13 @@ public boolean updateState(State currentState, Event event, State nextState, Sna
326327
return true;
327328
}
328329

330+
@Override
331+
public void updateVolumeIds(long oldVolId, long newVolId) {
332+
SearchCriteria<SnapshotVO> sc = VolumeIdSearch.create();
333+
sc.setParameters("volumeId", oldVolId);
334+
SnapshotVO snapshot = createForUpdate();
335+
snapshot.setVolumeId(newVolId);
336+
UpdateBuilder ub = getUpdateBuilder(snapshot);
337+
update(ub, sc, null);
338+
}
329339
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ public interface SnapshotDataStoreDao extends GenericDao<SnapshotDataStoreVO, Lo
5656
void updateStoreRoleToCache(long storeId);
5757

5858
SnapshotDataStoreVO findLatestSnapshotForVolume(Long volumeId, DataStoreRole role);
59+
60+
SnapshotDataStoreVO findOldestSnapshotForVolume(Long volumeId, DataStoreRole role);
61+
62+
void updateVolumeIds(long oldVolId, long newVolId);
5963
}

‎engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/XenserverSnapshotStrategy.java‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
import com.cloud.storage.Snapshot;
4747
import com.cloud.storage.SnapshotVO;
4848
import com.cloud.storage.Volume;
49+
import com.cloud.storage.VolumeVO;
4950
import com.cloud.storage.dao.SnapshotDao;
51+
import com.cloud.storage.dao.VolumeDao;
5052
import com.cloud.storage.snapshot.SnapshotManager;
5153
import com.cloud.utils.NumbersUtil;
5254
import com.cloud.utils.db.DB;
@@ -68,6 +70,8 @@ public class XenserverSnapshotStrategy extends SnapshotStrategyBase {
6870
@Inject
6971
SnapshotDao snapshotDao;
7072
@Inject
73+
VolumeDao volumeDao;
74+
@Inject
7175
SnapshotDataFactory snapshotDataFactory;
7276

7377
@Override
@@ -105,31 +109,44 @@ public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
105109

106110
// determine full snapshot backup or not
107111

108-
109112
boolean fullBackup = true;
110113
SnapshotDataStoreVO parentSnapshotOnBackupStore = snapshotStoreDao.findLatestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Image);
111114
HypervisorType hypervisorType = snapshot.getBaseVolume().getHypervisorType();
112115
if (parentSnapshotOnBackupStore != null && hypervisorType == Hypervisor.HypervisorType.XenServer) { // CS does incremental backup only for XenServer
113-
int _deltaSnapshotMax = NumbersUtil.parseInt(configDao.getValue("snapshot.delta.max"),
114-
SnapshotManager.DELTAMAX);
115-
int deltaSnap = _deltaSnapshotMax;
116-
int i;
117-
118-
for (i = 1; i < deltaSnap; i++) {
119-
Long prevBackupId = parentSnapshotOnBackupStore.getParentSnapshotId();
120-
if (prevBackupId == 0) {
121-
break;
122-
}
123-
parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(prevBackupId, DataStoreRole.Image);
124-
if (parentSnapshotOnBackupStore == null) {
125-
break;
126-
}
127-
}
128116

129-
if (i >= deltaSnap) {
130-
fullBackup = true;
131-
} else {
132-
fullBackup = false;
117+
// In case of volume migration from one pool to other pool, CS should take full snapshot to avoid any issues with delta chain,
118+
// to check if this is a migrated volume, compare the current pool id of volume and store_id of oldest snapshot on primary for this volume.
119+
// Why oldest? Because at this point CS has two snapshot on primary entries for same volume, one with old pool_id and other one with
120+
// current pool id. So, verify and if volume found to be migrated, delete snapshot entry with previous pool store_id.
121+
SnapshotDataStoreVO oldestSnapshotOnPrimary = snapshotStoreDao.findOldestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
122+
VolumeVO volume = volumeDao.findById(snapshot.getVolumeId());
123+
if (oldestSnapshotOnPrimary != null) {
124+
if (oldestSnapshotOnPrimary.getDataStoreId() == volume.getPoolId()) {
125+
int _deltaSnapshotMax = NumbersUtil.parseInt(configDao.getValue("snapshot.delta.max"),
126+
SnapshotManager.DELTAMAX);
127+
int deltaSnap = _deltaSnapshotMax;
128+
int i;
129+
130+
for (i = 1; i < deltaSnap; i++) {
131+
Long prevBackupId = parentSnapshotOnBackupStore.getParentSnapshotId();
132+
if (prevBackupId == 0) {
133+
break;
134+
}
135+
parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(prevBackupId, DataStoreRole.Image);
136+
if (parentSnapshotOnBackupStore == null) {
137+
break;
138+
}
139+
}
140+
141+
if (i >= deltaSnap) {
142+
fullBackup = true;
143+
} else {
144+
fullBackup = false;
145+
}
146+
} else {
147+
// if there is an snapshot entry for previousPool(primary storage) of migrated volume, delete it becasue CS created one more snapshot entry for current pool
148+
snapshotStoreDao.remove(oldestSnapshotOnPrimary.getId());
149+
}
133150
}
134151
}
135152

‎engine/storage/src/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,18 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
5454
private SearchBuilder<SnapshotDataStoreVO> snapshotSearch;
5555
private SearchBuilder<SnapshotDataStoreVO> storeSnapshotSearch;
5656
private SearchBuilder<SnapshotDataStoreVO> snapshotIdSearch;
57+
private SearchBuilder<SnapshotDataStoreVO> volumeIdSearch;
5758

5859
private final String parentSearch = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where store_id = ? "
5960
+ " and store_role = ? and volume_id = ? and state = 'Ready'" + " order by created DESC " + " limit 1";
6061
private final String findLatestSnapshot = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where " +
6162
" store_role = ? and volume_id = ? and state = 'Ready'" +
6263
" order by created DESC " +
6364
" limit 1";
65+
private final String findOldestSnapshot = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where " +
66+
" store_role = ? and volume_id = ? and state = 'Ready'" +
67+
" order by created ASC " +
68+
" limit 1";
6469

6570
@Override
6671
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@@ -110,6 +115,10 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
110115
snapshotIdSearch.and("snapshot_id", snapshotIdSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
111116
snapshotIdSearch.done();
112117

118+
volumeIdSearch = createSearchBuilder();
119+
volumeIdSearch.and("volume_id", volumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
120+
volumeIdSearch.done();
121+
113122
return true;
114123
}
115124

@@ -225,7 +234,34 @@ public SnapshotDataStoreVO findLatestSnapshotForVolume(Long volumeId, DataStoreR
225234
return findByStoreSnapshot(role, sid, snid);
226235
}
227236
} catch (SQLException e) {
228-
s_logger.debug("Failed to find parent snapshot: " + e.toString());
237+
s_logger.debug("Failed to find latest snapshot for volume: " + volumeId + " due to: " + e.toString());
238+
} finally {
239+
try {
240+
if (pstmt != null)
241+
pstmt.close();
242+
} catch (SQLException e) {
243+
}
244+
}
245+
return null;
246+
}
247+
248+
@Override
249+
public SnapshotDataStoreVO findOldestSnapshotForVolume(Long volumeId, DataStoreRole role) {
250+
TransactionLegacy txn = TransactionLegacy.currentTxn();
251+
PreparedStatement pstmt = null;
252+
ResultSet rs = null;
253+
try {
254+
pstmt = txn.prepareStatement(findOldestSnapshot);
255+
pstmt.setString(1, role.toString());
256+
pstmt.setLong(2, volumeId);
257+
rs = pstmt.executeQuery();
258+
while (rs.next()) {
259+
long sid = rs.getLong(1);
260+
long snid = rs.getLong(3);
261+
return findByStoreSnapshot(role, sid, snid);
262+
}
263+
} catch (SQLException e) {
264+
s_logger.debug("Failed to find oldest snapshot for volume: " + volumeId + " due to: " + e.toString());
229265
} finally {
230266
try {
231267
if (pstmt != null)
@@ -367,4 +403,13 @@ public void updateStoreRoleToCache(long storeId) {
367403
}
368404
}
369405

406+
@Override
407+
public void updateVolumeIds(long oldVolId, long newVolId) {
408+
SearchCriteria<SnapshotDataStoreVO> sc = volumeIdSearch.create();
409+
sc.setParameters("volume_id", oldVolId);
410+
SnapshotDataStoreVO snapshot = createForUpdate();
411+
snapshot.setVolumeId(newVolId);
412+
UpdateBuilder ub = getUpdateBuilder(snapshot);
413+
update(ub, sc, null);
414+
}
370415
}

0 commit comments

Comments
 (0)