sidshas03 commented on code in PR #14697:
URL: https://github.com/apache/cloudstack/pull/14697#discussion_r4607930045
##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java:
##########
@@ -3609,4 +3665,231 @@ public KVMStoragePool
getTemplateSourcePoolUsingMigrationOptions(KVMStoragePool
return
storagePoolMgr.getStoragePool(migrationOptions.getSrcPoolType(),
migrationOptions.getSrcPoolUuid());
}
+
+ /**
+ * Write a marker file before creating a snapshot overlay.
+ * Marker format:
poolUuid|volumePath|snapshotName|vmName|diskLabel|overlayPath|timestamp
+ */
+ protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath,
String snapshotName) {
+ writeSnapshotMarker(pool, volumePath, snapshotName, null, null, null);
+ }
+
+ protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath,
String snapshotName,
+ String vmName, String diskLabel, String
overlayPath) {
+ try {
+ File markerDir = new File(SNAPSHOT_MARKER_DIR);
+ if (!markerDir.exists()) {
+ markerDir.mkdirs();
+ }
+ String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_"
+ volumePath.replace("/", "_") + "_" + snapshotName;
+ File markerFile = new File(markerDir, markerName);
+ String content = String.join("|",
+ pool.getUuid(),
+ volumePath,
+ snapshotName,
+ StringUtils.defaultString(vmName),
+ StringUtils.defaultString(diskLabel),
+ StringUtils.defaultString(overlayPath),
+ String.valueOf(System.currentTimeMillis()));
+ try (FileOutputStream fos = new FileOutputStream(markerFile)) {
+ fos.write(content.getBytes());
+ }
+ logger.debug("Wrote snapshot marker: {}",
markerFile.getAbsolutePath());
+ } catch (IOException e) {
+ logger.warn("Failed to write snapshot marker for volume {}
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+ }
+ }
+
+ protected void updateSnapshotMarker(KVMStoragePool pool, String
volumePath, String snapshotName,
+ String vmName, String diskLabel,
String overlayPath) {
+ writeSnapshotMarker(pool, volumePath, snapshotName, vmName, diskLabel,
overlayPath);
+ }
+
+ /**
+ * Remove the snapshot marker after successful merge.
+ */
+ protected void removeSnapshotMarker(KVMStoragePool pool, String
volumePath, String snapshotName) {
+ try {
+ String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_"
+ volumePath.replace("/", "_") + "_" + snapshotName;
+ File markerFile = new File(SNAPSHOT_MARKER_DIR, markerName);
+ if (markerFile.exists()) {
+ markerFile.delete();
+ logger.debug("Removed snapshot marker: {}",
markerFile.getAbsolutePath());
+ }
+ } catch (Exception e) {
+ logger.warn("Failed to remove snapshot marker for volume {}
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+ }
+ }
+
+ /**
+ * Reconcile incomplete snapshots by checking for marker files on agent
startup.
+ * If a marker exists, the agent died during a snapshot and the merge may
not have completed.
+ *
+ * Strategy per marker:
+ * - Overlay gone: snapshot completed or was cleaned up, remove marker
+ * - VM still running on overlay: blockcommit --active --pivot back to base
+ * - VM already on base: overlay is orphaned, remove marker
+ * - Domain gone, overlay exists: offline qemu-img commit
+ * - Base modified since marker: cannot safely commit, alert and keep
marker
+ */
+ protected void reconcileSnapshotMarkers() {
Review Comment:
You are right, it was not wired up. Added the call in configure() at the
end, so it runs when the storage processor initializes on agent startup.
##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java:
##########
@@ -3609,4 +3665,231 @@ public KVMStoragePool
getTemplateSourcePoolUsingMigrationOptions(KVMStoragePool
return
storagePoolMgr.getStoragePool(migrationOptions.getSrcPoolType(),
migrationOptions.getSrcPoolUuid());
}
+
+ /**
+ * Write a marker file before creating a snapshot overlay.
+ * Marker format:
poolUuid|volumePath|snapshotName|vmName|diskLabel|overlayPath|timestamp
+ */
+ protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath,
String snapshotName) {
+ writeSnapshotMarker(pool, volumePath, snapshotName, null, null, null);
+ }
+
+ protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath,
String snapshotName,
+ String vmName, String diskLabel, String
overlayPath) {
+ try {
+ File markerDir = new File(SNAPSHOT_MARKER_DIR);
+ if (!markerDir.exists()) {
+ markerDir.mkdirs();
+ }
+ String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_"
+ volumePath.replace("/", "_") + "_" + snapshotName;
+ File markerFile = new File(markerDir, markerName);
+ String content = String.join("|",
+ pool.getUuid(),
+ volumePath,
+ snapshotName,
+ StringUtils.defaultString(vmName),
+ StringUtils.defaultString(diskLabel),
+ StringUtils.defaultString(overlayPath),
+ String.valueOf(System.currentTimeMillis()));
+ try (FileOutputStream fos = new FileOutputStream(markerFile)) {
+ fos.write(content.getBytes());
+ }
+ logger.debug("Wrote snapshot marker: {}",
markerFile.getAbsolutePath());
+ } catch (IOException e) {
+ logger.warn("Failed to write snapshot marker for volume {}
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+ }
+ }
+
+ protected void updateSnapshotMarker(KVMStoragePool pool, String
volumePath, String snapshotName,
+ String vmName, String diskLabel,
String overlayPath) {
+ writeSnapshotMarker(pool, volumePath, snapshotName, vmName, diskLabel,
overlayPath);
+ }
+
+ /**
+ * Remove the snapshot marker after successful merge.
+ */
+ protected void removeSnapshotMarker(KVMStoragePool pool, String
volumePath, String snapshotName) {
+ try {
+ String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_"
+ volumePath.replace("/", "_") + "_" + snapshotName;
+ File markerFile = new File(SNAPSHOT_MARKER_DIR, markerName);
+ if (markerFile.exists()) {
+ markerFile.delete();
+ logger.debug("Removed snapshot marker: {}",
markerFile.getAbsolutePath());
+ }
+ } catch (Exception e) {
+ logger.warn("Failed to remove snapshot marker for volume {}
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+ }
+ }
+
+ /**
+ * Reconcile incomplete snapshots by checking for marker files on agent
startup.
+ * If a marker exists, the agent died during a snapshot and the merge may
not have completed.
+ *
+ * Strategy per marker:
+ * - Overlay gone: snapshot completed or was cleaned up, remove marker
+ * - VM still running on overlay: blockcommit --active --pivot back to base
+ * - VM already on base: overlay is orphaned, remove marker
+ * - Domain gone, overlay exists: offline qemu-img commit
+ * - Base modified since marker: cannot safely commit, alert and keep
marker
+ */
+ protected void reconcileSnapshotMarkers() {
+ File markerDir = new File(SNAPSHOT_MARKER_DIR);
+ if (!markerDir.exists() || !markerDir.isDirectory()) {
+ return;
+ }
+
+ File[] markers = markerDir.listFiles((dir, name) ->
name.startsWith(SNAPSHOT_MARKER_PREFIX));
+ if (markers == null || markers.length == 0) {
+ return;
+ }
+
+ logger.info("Found {} snapshot marker(s) to reconcile",
markers.length);
+ for (File marker : markers) {
+ reconcileSingleMarker(marker);
+ }
+ }
+
+ protected void reconcileSingleMarker(File marker) {
Review Comment:
Same as above! reconcileSnapshotMarkers() is now called from configure(), so
the whole chain is live.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]