/
RemoveSnapshotSingleDiskCommandBase.java
368 lines (327 loc) · 17 KB
/
RemoveSnapshotSingleDiskCommandBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package org.ovirt.engine.core.bll.snapshots;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.LockMessagesMatchUtil;
import org.ovirt.engine.core.bll.context.CommandContext;
import org.ovirt.engine.core.bll.storage.disk.image.BaseImagesCommand;
import org.ovirt.engine.core.bll.tasks.CommandCoordinatorUtil;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.ActionParametersBase;
import org.ovirt.engine.core.common.action.ActionReturnValue;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.action.DestroyImageParameters;
import org.ovirt.engine.core.common.action.ImagesContainterParametersBase;
import org.ovirt.engine.core.common.action.LockProperties;
import org.ovirt.engine.core.common.action.RemoveMemoryVolumesParameters;
import org.ovirt.engine.core.common.action.RemoveSnapshotSingleDiskParameters;
import org.ovirt.engine.core.common.businessentities.CommandEntity;
import org.ovirt.engine.core.common.businessentities.Snapshot;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.storage.DiskBackup;
import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
import org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData;
import org.ovirt.engine.core.common.businessentities.storage.Image;
import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
import org.ovirt.engine.core.common.businessentities.storage.VolumeClassification;
import org.ovirt.engine.core.common.businessentities.storage.VolumeFormat;
import org.ovirt.engine.core.common.errors.EngineException;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.common.locks.LockingGroup;
import org.ovirt.engine.core.common.utils.Pair;
import org.ovirt.engine.core.common.vdscommands.GetImageInfoVDSCommandParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.BaseDiskDao;
import org.ovirt.engine.core.dao.DiskImageDao;
import org.ovirt.engine.core.dao.ImageDao;
import org.ovirt.engine.core.dao.SnapshotDao;
import org.ovirt.engine.core.utils.ovf.OvfManager;
import org.ovirt.engine.core.utils.ovf.OvfReaderException;
import org.ovirt.engine.core.utils.transaction.TransactionSupport;
public abstract class RemoveSnapshotSingleDiskCommandBase<T extends ImagesContainterParametersBase> extends BaseImagesCommand<T> {
@Inject
protected OvfManager ovfManager;
@Inject
private SnapshotDao snapshotDao;
@Inject
private ImageDao imageDao;
@Inject
private BaseDiskDao baseDiskDao;
@Inject
private DiskImageDao diskImageDao;
@Inject
private CommandCoordinatorUtil commandCoordinatorUtil;
protected RemoveSnapshotSingleDiskCommandBase(T parameters, CommandContext cmdContext) {
super(parameters, cmdContext);
}
@Override
public Map<String, String> getJobMessageProperties() {
if (jobProperties == null) {
jobProperties = super.getJobMessageProperties();
jobProperties.put(VdcObjectType.Disk.name().toLowerCase(), getDiskImage().getDiskAlias());
jobProperties.put("sourcesnapshot",
getSnapshotDescriptionById(getDiskImage().getVmSnapshotId()));
jobProperties.put("destinationsnapshot",
getSnapshotDescriptionById(getDestinationDiskImage().getVmSnapshotId()));
}
return jobProperties;
}
protected DiskImage getImageInfoFromVdsm(final DiskImage targetImage) {
try {
VDSReturnValue ret = runVdsCommand(
VDSCommandType.GetImageInfo,
new GetImageInfoVDSCommandParameters(targetImage.getStoragePoolId(),
targetImage.getStorageIds().get(0),
targetImage.getId(),
targetImage.getImageId()));
return (DiskImage) ret.getReturnValue();
} catch (EngineException e) {
log.warn("Failed to get info of volume '{}' using GetImageInfo", targetImage.getImageId(), e);
return null;
}
}
protected void updateDiskImageDynamic(final DiskImage imageFromVdsm, final DiskImage targetImage) {
// Update image's actual size in DB
if (imageFromVdsm != null) {
completeImageData(imageFromVdsm);
} else {
log.warn("Could not update DiskImage's size with ID '{}'",
targetImage.getImageId());
}
}
protected DestroyImageParameters buildDestroyImageParameters(Guid imageGroupId, List<Guid> imageList, ActionType actionType) {
DestroyImageParameters parameters = new DestroyImageParameters(
getVdsId(),
getVmId(),
getDiskImage().getStoragePoolId(),
getDiskImage().getStorageIds().get(0),
imageGroupId,
imageList,
getDiskImage().isWipeAfterDelete(),
false);
parameters.setParentCommand(actionType);
parameters.setEndProcedure(ActionParametersBase.EndProcedure.COMMAND_MANAGED);
parameters.setParentParameters(getParameters());
return parameters;
}
/**
* Updates (but does not persist) the parameters.childCommands list to ensure the current
* child command is present. This is necessary in various entry points called externally
* (e.g. by endAction()), which can be called after a child command is started but before
* the main proceedCommandExecution() loop has persisted the updated child list.
*/
protected void syncChildCommandList(RemoveSnapshotSingleDiskParameters parameters) {
// Both ColdMergeSnapshotSingleDiskCommand and RemoveSnapshotSingleDiskLiveCommand, hold
// a list of the executed children. The list is constructed based on the number of children
// provided by CoCo. The number of children will also be incremented if child commands fail.
// In this method, we build a mapping between the command step and the command Id. As the last
// command in the children command list is the current step being executed, we always put that
// command as the value of the current step.
List<Guid> childCommandIds = commandCoordinatorUtil
.getChildCommandIds(getCommandId())
.stream()
.map(guid -> commandCoordinatorUtil.getCommandEntity(guid))
.sorted(Comparator.comparing(CommandEntity::getCreatedAt))
.map(CommandEntity::getId)
.collect(Collectors.toList());
if (childCommandIds.size() != parameters.getChildCommands().size()) {
parameters.getChildCommands().put(parameters.getCommandStep(), childCommandIds.get(childCommandIds.size()-1));
}
}
protected Guid getCurrentChildId(RemoveSnapshotSingleDiskParameters parameters) {
return parameters.getChildCommands().get(parameters.getCommandStep());
}
protected DiskImage getActiveDiskImage() {
Guid snapshotId = snapshotDao.getId(getVmId(), Snapshot.SnapshotType.ACTIVE);
return diskImageDao.getDiskSnapshotForVmSnapshot(getDiskImage().getId(), snapshotId);
}
/**
* After merging the snapshots, update the image and snapshot records in the
* database to reflect the changes. This handles either forward or backwards
* merge (detected). It will either then remove the images, or mark them
* illegal (to handle the case where image deletion failed).
*
* @param removeImages Remove the images from the database, or if false, only
* mark them illegal
*/
protected void syncDbRecords(DiskImage imageFromVdsm, Set<Guid> imagesToUpdate, boolean removeImages) {
TransactionSupport.executeInNewTransaction(() -> {
// If deletion failed after a backwards merge, the snapshots' images need to be swapped
// as they would upon success. Instead of removing them, mark them illegal.
DiskImage baseImage = getDiskImage();
DiskImage topImage = getDestinationDiskImage();
// The vdsm merge verb may decide to perform a forward or backward merge.
if (topImage == null) {
log.info("No merge destination image, not updating image/snapshot association");
} else {
handleBackwardMerge(topImage, baseImage, imageFromVdsm);
}
if (imagesToUpdate == null) {
log.error("Failed to update orphaned images in db: image list could not be retrieved");
return null;
}
for (Guid imageId : imagesToUpdate) {
if (removeImages) {
imageDao.remove(imageId);
} else {
// The (illegal && no-parent && no-children) status indicates an orphaned image.
Image image = imageDao.get(imageId);
image.setStatus(ImageStatus.ILLEGAL);
image.setParentId(Guid.Empty);
imageDao.update(image);
}
}
return null;
});
}
protected Pair<ActionType, DestroyImageParameters> buildDestroyCommand(ActionType actionToRun,
ActionType parentCommand,
List<Guid> images) {
return new Pair<>(actionToRun, buildDestroyImageParameters(getActiveDiskImage().getId(),
images, parentCommand));
}
private void handleBackwardMerge(DiskImage topImage, DiskImage baseImage, DiskImage imageFromVdsm) {
// For backwards merge, the prior base image now has the data associated with the newer
// snapshot we want to keep. Re-associate this older image with the newer snapshot.
// The base snapshot is deleted if everything went well. In case it's not deleted, we
// hijack it to preserve a link to the broken image. This makes the image discoverable
// so that we can retry the deletion later, yet doesn't corrupt the VM image chain.
List<DiskImage> children = diskImageDao.getAllSnapshotsForParent(topImage.getImageId());
if (!children.isEmpty()) {
DiskImage childImage = children.get(0);
childImage.setParentId(baseImage.getImageId());
imageDao.update(childImage.getImage());
}
Image oldTopImage = topImage.getImage();
topImage.setImage(baseImage.getImage());
baseImage.setImage(oldTopImage);
Guid oldTopSnapshotId = topImage.getImage().getSnapshotId();
topImage.getImage().setSnapshotId(baseImage.getImage().getSnapshotId());
baseImage.getImage().setSnapshotId(oldTopSnapshotId);
boolean oldTopIsActive = topImage.getImage().isActive();
topImage.getImage().setActive(baseImage.getImage().isActive());
VolumeClassification baseImageVolumeClassification =
VolumeClassification.getVolumeClassificationByActiveFlag(baseImage.getImage().isActive());
topImage.getImage().setVolumeClassification(baseImageVolumeClassification);
baseImage.getImage().setActive(oldTopIsActive);
VolumeClassification oldTopVolumeClassification =
VolumeClassification.getVolumeClassificationByActiveFlag(oldTopIsActive);
topImage.getImage().setVolumeClassification(oldTopVolumeClassification);
// If we remain with a RAW disk after removing a snapshot, backup property must be set back to none.
if (oldTopImage.getVolumeFormat() == VolumeFormat.COW && topImage.getVolumeFormat() == VolumeFormat.RAW
&& topImage.getImage().isActive()) {
baseImage.setBackup(DiskBackup.None);
}
topImage.setSize(baseImage.getSize());
topImage.setActualSizeInBytes(imageFromVdsm.getActualSizeInBytes());
topImage.setImageStatus(ImageStatus.OK);
baseDiskDao.update(topImage);
imageDao.update(topImage.getImage());
updateDiskImageDynamic(imageFromVdsm, topImage);
baseDiskDao.update(baseImage);
imageDao.update(baseImage.getImage());
removeTopImageMemoryIfNeeded(topImage);
updateVmConfigurationForImageChange(topImage.getImage().getSnapshotId(),
baseImage.getImageId(), topImage);
updateVmConfigurationForImageRemoval(baseImage.getImage().getSnapshotId(),
topImage.getImageId());
}
private boolean isRemoveTopImageMemoryNeeded(Snapshot snapshot) {
if (snapshot.containsMemory()) {
VM vmSnapshot = new VM();
FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(vmSnapshot);
try {
ovfManager.importVm(snapshot.getVmConfiguration(), vmSnapshot, fullEntityOvfData);
if (Version.getLowest()
.greater(vmSnapshot.getStaticData().getClusterCompatibilityVersionOrigin())) {
return true;
}
} catch (OvfReaderException e) {
log.error("Failed to read snapshot '{}' configuration", snapshot.getId());
}
}
return false;
}
private void removeTopImageMemoryIfNeeded(DiskImage topImage) {
// If the top image snapshot created in a cluster version < 3.6, the memory of the snapshot
// is not supported by the engine.
// The engine updates the snapshot's cluster compatibility version to the lowest supported version (3.6)
// to be able to update the snapshot OVF.
// Therefore, the memory of the snapshot should be removed to prevent a preview of unsupported snapshot memory.
Snapshot topSnapshot = snapshotDao.get(topImage.getImage().getSnapshotId());
if (isRemoveTopImageMemoryNeeded(topSnapshot)) {
ActionReturnValue retVal = runInternalAction(
ActionType.RemoveMemoryVolumes,
new RemoveMemoryVolumesParameters(topSnapshot, getVmId()),
cloneContextAndDetachFromParent());
if (!retVal.getSucceeded()) {
log.error("Failed to remove memory volumes '{}, {}'",
topSnapshot.getMemoryDiskId(),
topSnapshot.getMetadataDiskId());
}
}
}
private void updateVmConfigurationForImageChange(final Guid snapshotId, final Guid oldImageId, final DiskImage newImage) {
try {
lockVmSnapshotsWithWait(getVm());
TransactionSupport.executeInNewTransaction(() -> {
Snapshot s = snapshotDao.get(snapshotId);
s = imagesHandler.prepareSnapshotConfigWithAlternateImage(s, oldImageId, newImage, ovfManager);
snapshotDao.update(s);
return null;
});
} finally {
if (getSnapshotsEngineLock() != null) {
lockManager.releaseLock(getSnapshotsEngineLock());
}
}
}
private void updateVmConfigurationForImageRemoval(final Guid snapshotId, final Guid imageId) {
try {
lockVmSnapshotsWithWait(getVm());
TransactionSupport.executeInNewTransaction(() -> {
Snapshot s = snapshotDao.get(snapshotId);
s = imagesHandler.prepareSnapshotConfigWithoutImageSingleImage(s, imageId, ovfManager);
snapshotDao.update(s);
return null;
});
} finally {
if (getSnapshotsEngineLock() != null) {
lockManager.releaseLock(getSnapshotsEngineLock());
}
}
}
@Override
protected Map<String, Pair<String, String>> getSharedLocks() {
// Lock the template image to prevent having it teared down
if (!Guid.Empty.equals(getDiskImage().getImageTemplateId())) {
return Collections.singletonMap(getDiskImage().getImageTemplateId().toString(),
LockMessagesMatchUtil.makeLockingPair(LockingGroup.TEMPLATE,
EngineMessage.VM_TEMPLATE_IMAGE_IS_LOCKED));
}
return null;
}
@Override
protected void endWithFailure() {
// TODO: FILL! We should determine what to do in case of
// failure (is everything rolled-backed? rolled-forward?
// some and some?).
setSucceeded(true);
}
private String getSnapshotDescriptionById(Guid snapshotId) {
Snapshot snapshot = snapshotDao.get(snapshotId);
return snapshot != null ? snapshot.getDescription() : StringUtils.EMPTY;
}
@Override
protected LockProperties applyLockProperties(LockProperties lockProperties) {
return lockProperties.withScope(LockProperties.Scope.Command);
}
}