hsato03 commented on code in PR #12086:
URL: https://github.com/apache/cloudstack/pull/12086#discussion_r2884297594


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateSnapshotsBetweenSecondaryStoragesCommandWrapper.java:
##########
@@ -0,0 +1,190 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.hypervisor.kvm.resource.wrapper;
+
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.MigrateSnapshotsBetweenSecondaryStoragesCommand;
+import com.cloud.agent.api.MigrateBetweenSecondaryStoragesCommandAnswer;
+import com.cloud.agent.api.to.DataStoreTO;
+import com.cloud.agent.api.to.DataTO;
+import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
+import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
+import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
+import com.cloud.resource.CommandWrapper;
+import com.cloud.resource.ResourceWrapper;
+import com.cloud.utils.Pair;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.script.Script;
+import org.apache.cloudstack.utils.qemu.QemuImageOptions;
+import org.apache.cloudstack.utils.qemu.QemuImg;
+import org.apache.cloudstack.utils.qemu.QemuImgException;
+import org.apache.cloudstack.utils.qemu.QemuImgFile;
+import org.libvirt.LibvirtException;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+@ResourceWrapper(handles = 
MigrateSnapshotsBetweenSecondaryStoragesCommand.class)
+public class LibvirtMigrateSnapshotsBetweenSecondaryStoragesCommandWrapper 
extends CommandWrapper<MigrateSnapshotsBetweenSecondaryStoragesCommand, Answer, 
LibvirtComputingResource> {
+
+    protected Set<String> filesToRemove;
+    protected List<Pair<Long, String>> resourcesToUpdate;
+    protected String resourceType;
+    protected int wait;
+
+    @Override
+    public Answer execute(MigrateSnapshotsBetweenSecondaryStoragesCommand 
command, LibvirtComputingResource serverResource) {
+        filesToRemove = new HashSet<>();
+        resourcesToUpdate = new ArrayList<>();
+        wait = command.getWait() * 1000;
+
+        DataStoreTO srcDataStore = command.getSrcDataStore();
+        DataStoreTO destDataStore = command.getDestDataStore();
+        KVMStoragePoolManager storagePoolManager = 
serverResource.getStoragePoolMgr();
+
+        Set<KVMStoragePool> imagePools = new HashSet<>();
+        KVMStoragePool destImagePool = 
storagePoolManager.getStoragePoolByURI(destDataStore.getUrl());
+        imagePools.add(destImagePool);
+
+        String imagePoolUrl;
+        KVMStoragePool imagePool = null;
+
+        String parentSnapshotPath = null;
+        boolean parentSnapshotWasMigrated = false;
+        Set<Long> snapshotsIdToMigrate = command.getSnapshotsIdToMigrate();
+
+        DataTO currentSnapshot = null;
+
+        try {
+            for (DataTO snapshot : command.getSnapshotChain()) {
+                currentSnapshot = snapshot;
+                imagePoolUrl = snapshot.getDataStore().getUrl();
+                imagePool = 
storagePoolManager.getStoragePoolByURI(imagePoolUrl);
+                imagePools.add(imagePool);
+
+                String resourceCurrentPath = 
imagePool.getLocalPathFor(snapshot.getPath());
+
+                if (imagePoolUrl.equals(srcDataStore.getUrl()) && 
snapshotsIdToMigrate.contains(snapshot.getId())) {
+                    logger.debug("Migrating snapshot [{}] to destination 
storage pool [{}].", snapshot.getId(), destImagePool.getUuid());
+                    parentSnapshotPath = copyResourceToDestDataStore(snapshot, 
resourceCurrentPath, destImagePool, parentSnapshotPath);
+                    parentSnapshotWasMigrated = true;
+                    logger.debug("Snapshot [{}] migrated successfully. New 
parent path: [{}]", snapshot.getId(), parentSnapshotPath);
+                } else {
+                    if (parentSnapshotWasMigrated) {
+                        logger.debug("Rebasing snapshot [{}] to new parent 
path: [{}]", snapshot.getId(), parentSnapshotPath);
+                        parentSnapshotPath = 
rebaseResourceToNewParentPath(resourceCurrentPath, parentSnapshotPath);
+                    } else {
+                        logger.debug("Skipping migration/rebase for snapshot 
[{}]. Keeping current path.", snapshot.getId());
+                        parentSnapshotPath = resourceCurrentPath;
+                    }
+                    parentSnapshotWasMigrated = false;
+                }
+            }
+            logger.info("Successfully completed migrating snapshot chain 
[{}].", command.getSnapshotChain());
+        }  catch (LibvirtException | QemuImgException e) {
+            logger.error("Exception while migrating snapshots [{}] to 
secondary storage [{}] due to: [{}].", command.getSnapshotChain(), imagePool, 
e.getMessage(), e);
+            return new MigrateBetweenSecondaryStoragesCommandAnswer(command, 
false, String.format("Unable to migrate snapshot with ID [%s].", 
currentSnapshot.getId()), resourcesToUpdate);

Review Comment:
   Considering the workflow executed on the Management Server, it is not 
possible for the snapshot chain to be empty; therefore, the variable 
`currentSnapshot` cannot be null.



-- 
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]

Reply via email to