Skip to content

Commit 9494fd6

Browse files
committed
fixed database update on snapshot with multiple volumes and an api change
1 parent b744824 commit 9494fd6

File tree

3 files changed

+1208
-2
lines changed

3 files changed

+1208
-2
lines changed

engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/ScaleIOVMSnapshotStrategy.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.cloudstack.storage.datastore.util.ScaleIOUtil;
4141
import org.apache.cloudstack.storage.to.VolumeObjectTO;
4242
import org.apache.commons.collections.CollectionUtils;
43+
import org.apache.cloudstack.storage.datastore.api.Volume;
4344

4445
import com.cloud.agent.api.VMSnapshotTO;
4546
import com.cloud.alert.AlertManager;
@@ -201,11 +202,44 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
201202
List<VMSnapshotDetailsVO> vmSnapshotDetails = new ArrayList<VMSnapshotDetailsVO>();
202203
vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "SnapshotGroupId", snapshotGroupId, false));
203204

204-
for (int index = 0; index < volumeIds.size(); index++) {
205+
/* for (int index = 0; index < volumeIds.size(); index++) {
205206
String volumeSnapshotName = srcVolumeDestSnapshotMap.get(ScaleIOUtil.getVolumePath(volumeTOs.get(index).getPath()));
206207
String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(volumeIds.get(index), volumeSnapshotName);
207208
vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "Vol_" + volumeTOs.get(index).getId() + "_Snapshot", pathWithScaleIOVolumeName, false));
208209
}
210+
*/
211+
// Invert the srcVolumeDestSnapshotMap: snapshotName -> srcVolumePath
212+
// Invert the srcVolumeDestSnapshotMap: snapshotName -> srcVolumePath
213+
Map<String, String> snapshotNameToSrcPathMap = new HashMap<>();
214+
for (Map.Entry<String, String> entry : srcVolumeDestSnapshotMap.entrySet()) {
215+
snapshotNameToSrcPathMap.put(entry.getValue(), entry.getKey());
216+
}
217+
218+
for (String snapshotVolumeId : volumeIds) {
219+
// Use getVolume() to fetch snapshot volume details and get its name
220+
Volume snapshotVolume = client.getVolume(snapshotVolumeId);
221+
if (snapshotVolume == null) {
222+
throw new CloudRuntimeException("Cannot find snapshot volume with id: " + snapshotVolumeId);
223+
}
224+
String snapshotName = snapshotVolume.getName();
225+
226+
// Match back to source volume path
227+
String srcVolumePath = snapshotNameToSrcPathMap.get(snapshotName);
228+
if (srcVolumePath == null) {
229+
throw new CloudRuntimeException("Cannot match snapshot " + snapshotName + " to a source volume");
230+
}
231+
232+
// Find the matching VolumeObjectTO by path
233+
VolumeObjectTO matchedVolume = volumeTOs.stream()
234+
.filter(v -> ScaleIOUtil.getVolumePath(v.getPath()).equals(srcVolumePath))
235+
.findFirst()
236+
.orElseThrow(() -> new CloudRuntimeException("Cannot find source volume for path: " + srcVolumePath));
237+
238+
String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(snapshotVolumeId, snapshotName);
239+
vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(),
240+
"Vol_" + matchedVolume.getId() + "_Snapshot",
241+
pathWithScaleIOVolumeName, false));
242+
}
209243

210244
vmSnapshotDetailsDao.saveDetails(vmSnapshotDetails);
211245
}

0 commit comments

Comments
 (0)