JoaoJandre commented on code in PR #12086: URL: https://github.com/apache/cloudstack/pull/12086#discussion_r3139567572
########## plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateSnapshotsBetweenSecondaryStoragesCommandWrapper.java: ########## @@ -0,0 +1,193 @@ +// +// 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 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> snapshotIdsToMigrate = command.getSnapshotIdsToMigrate(); + + 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()) && snapshotIdsToMigrate.contains(snapshot.getId())) { + logger.debug("Migrating snapshot [{}] to destination storage pool [{}].", snapshot.getId(), destImagePool.getUuid()); + parentSnapshotPath = copyResourceToDestDataStore(snapshot, resourceCurrentPath, destImagePool, parentSnapshotPath); + parentSnapshotWasMigrated = !parentSnapshotPath.equals(resourceCurrentPath); + logger.debug("Snapshot [{}] migrated successfully. New parent path: [{}].", snapshot.getId(), parentSnapshotPath); Review Comment: ```suggestion logger.debug("Snapshot [{}] migrated successfully. New path: [{}].", snapshot.getId(), parentSnapshotPath); ``` This is the path of the current snapshot that was migrated, correct? ########## engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/StorageOrchestrator.java: ########## @@ -346,20 +370,102 @@ protected Map<Long, Pair<Long, Long>> migrateAway( executor.setCorePoolSize((int) (totalJobs)); } - MigrateDataTask task = new MigrateDataTask(chosenFileForMigration, srcDatastore, dataStoreManager.getDataStore(destDatastoreId, DataStoreRole.Image)); - if (chosenFileForMigration instanceof SnapshotInfo ) { + DataStore destDataStore = dataStoreManager.getDataStore(destDatastoreId, DataStoreRole.Image); + + boolean isKvmIncrementalSnapshot = chosenFileForMigration instanceof SnapshotInfo && ((SnapshotInfo) chosenFileForMigration).isKvmIncrementalSnapshot() && snapshotChains.containsKey(chosenFileForMigration); + + if (isKvmIncrementalSnapshot) { + MigrateKvmIncrementalSnapshotTask task = new MigrateKvmIncrementalSnapshotTask(chosenFileForMigration, snapshotChains, srcDatastore, destDataStore, snapshotIdsToMigrate); + futures.add(submitKvmIncrementalMigration(srcDatastore.getScope().getScopeId(), task)); + logger.debug("Incremental snapshot migration {} submitted to incremental pool.", chosenFileForMigration.getUuid()); + } else { + createMigrateDataTask(chosenFileForMigration, snapshotChains, templateChains, srcDatastore, destDataStore, executor, futures); + } + + return storageCapacities; + } + + private AsyncCallFuture<DataObjectResult> migrateKvmIncrementalSnapshotChain(DataObject chosenFileForMigration, Map<DataObject, Pair<List<SnapshotInfo>, Long>> snapshotChains, DataStore srcDatastore, DataStore destDataStore, Set<Long> snapshotIdsToMigrate) { + return Transaction.execute((TransactionCallback<AsyncCallFuture<DataObjectResult>>) status -> { + MigrateBetweenSecondaryStoragesCommandAnswer answer = null; + AsyncCallFuture<DataObjectResult> future = new AsyncCallFuture<>(); + DataObjectResult result = new DataObjectResult(chosenFileForMigration); + + try { + List<SnapshotInfo> snapshotChain = snapshotChains.get(chosenFileForMigration).first(); + MigrateSnapshotsBetweenSecondaryStoragesCommand migrateBetweenSecondaryStoragesCmd = new MigrateSnapshotsBetweenSecondaryStoragesCommand(snapshotChain.stream().map(DataObject::getTO).collect(Collectors.toList()), srcDatastore.getTO(), destDataStore.getTO(), snapshotIdsToMigrate); + + HostVO host = getAvailableHost(((SnapshotInfo) chosenFileForMigration).getDataCenterId()); + if (host == null) { + throw new CloudRuntimeException("No suitable hosts found to send migrate command."); + } + + migrateBetweenSecondaryStoragesCmd.setWait(StorageManager.AgentMaxDataMigrationWaitTime.valueIn(host.getClusterId())); + answer = (MigrateBetweenSecondaryStoragesCommandAnswer) agentManager.send(host.getId(), migrateBetweenSecondaryStoragesCmd); + if (answer == null || !answer.getResult()) { + logger.warn("Unable to migrate snapshots [{}].", snapshotChain); + throw new CloudRuntimeException("Unable to migrate KVM incremental snapshots to another secondary storage"); + } + } catch (final OperationTimedoutException | AgentUnavailableException e) { + throw new CloudRuntimeException("Error while migrating KVM incremental snapshot chain. Check the logs for more information.", e); Review Comment: Was this addressed? ########## engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/DataMigrationUtility.java: ########## @@ -297,16 +311,30 @@ protected List<DataObject> getAllReadySnapshotsAndChains(DataStore srcDataStore, logger.debug("Not migrating snapshot [{}] because its hypervisor type is simulator.", snapshot); continue; } - if (snapshot.getParentSnapshotId() != 0) { + boolean isKvmIncrementalSnapshot = snapshot.getKvmCheckpointPath() != null; + if (snapshot.getParentSnapshotId() != 0 && !isKvmIncrementalSnapshot) { continue; // The child snapshot will be migrated in the for loop below. } - SnapshotInfo snap = snapshotFactory.getSnapshot(snapshotVO.getSnapshotId(), snapshot.getDataStoreId(), snapshot.getRole()); - if (snap == null) { - logger.debug("Not migrating snapshot [{}] because we could not get its information.", snapshot); + if (snapshotIdsAlreadyInChain.contains(snapshotVO.getId())) { + logger.debug("Skipping snapshot {} since it is already in snapshot chain.", snapshot); continue; } - files.add(snap); - idsForMigration.add(snapshotId); + + if (isKvmIncrementalSnapshot) { + List<SnapshotInfo> kvmIncrementalSnapshotChain = createKvmIncrementalSnapshotChain(snapshot); + SnapshotInfo parent = kvmIncrementalSnapshotChain.get(0); + snapshotIdsAlreadyInChain.addAll(kvmIncrementalSnapshotChain.stream().map(DataObject::getId).collect(Collectors.toSet())); + snapshotChains.put(parent, new Pair<>(kvmIncrementalSnapshotChain, getTotalChainSize(kvmIncrementalSnapshotChain.stream().filter(snapInfo -> snapshotIdsToMigrate.contains(snapInfo.getId())).collect(Collectors.toList())))); + files.add(parent); + } else { Review Comment: Why do we add the parent snapshot and not the snapshot itself? -- 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]
