Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package org.apache.cloudstack.storage.motion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.inject.Inject;

Expand All @@ -30,6 +33,7 @@
import org.apache.cloudstack.engine.subsystem.api.storage.DataMotionStrategy;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.StorageStrategyFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
Expand All @@ -42,6 +46,7 @@

import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VolumeDao;
Expand Down Expand Up @@ -122,20 +127,41 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmT

DataMotionStrategy strategy = storageStrategyFactory.getDataMotionStrategy(volumeMap, srcHost, destHost);
if (strategy == null) {
List<String> volumeIds = new LinkedList<String>();
for (final VolumeInfo volumeInfo : volumeMap.keySet()) {
volumeIds.add(volumeInfo.getUuid());
String errMsg;
if (HypervisorType.KVM.equals(srcHost.getHypervisorType())) {
errMsg = getKvmLiveMigrationUnsupportedMessage(volumeMap, srcHost, destHost);
} else {
List<String> volumeIds = new LinkedList<String>();
for (final VolumeInfo volumeInfo : volumeMap.keySet()) {
volumeIds.add(volumeInfo.getUuid());
}
errMsg = "Can't find strategy to move data. " + "Source Host: " + srcHost.getName() + ", Destination Host: " + destHost.getName() +
", Volume UUIDs: " + StringUtils.join(volumeIds, ",");
}

String errMsg = "Can't find strategy to move data. " + "Source Host: " + srcHost.getName() + ", Destination Host: " + destHost.getName() +
", Volume UUIDs: " + StringUtils.join(volumeIds, ",");
invokeCallback(errMsg, callback);
return;
}

strategy.copyAsync(volumeMap, vmTo, srcHost, destHost, callback);
}

private String getKvmLiveMigrationUnsupportedMessage(Map<VolumeInfo, DataStore> volumeMap, Host srcHost, Host destHost) {
List<String> volumes = new ArrayList<>();
for (Map.Entry<VolumeInfo, DataStore> entry : volumeMap.entrySet()) {
volumes.add(String.format("%s from %s to %s", entry.getKey().getUuid(), describePool(entry.getKey().getDataStore()), describePool(entry.getValue())));
}
Collections.sort(volumes);
return String.format("KVM cannot live migrate the volumes of this running VM from host %s to host %s between these storage pools: %s. " +
"Stop the VM and migrate the volumes while it is stopped.", srcHost.getName(), destHost.getName(), StringUtils.join(volumes, ", "));
}

private String describePool(DataStore dataStore) {
if (dataStore instanceof PrimaryDataStoreInfo) {
return String.format("%s (%s)", dataStore.getName(), Objects.toString(((PrimaryDataStoreInfo)dataStore).getPoolType(), "unknown type"));
}
return dataStore == null ? "unknown pool" : dataStore.getName();
}

private void invokeCallback(String errMsg, AsyncCompletionCallback<CopyCommandResult> callback) {
CopyCmdAnswer copyCmdAnswer = new CopyCmdAnswer(errMsg);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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 org.apache.cloudstack.storage.motion;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.StorageStrategyFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.Storage.StoragePoolType;

@RunWith(MockitoJUnitRunner.class)
public class DataMotionServiceImplTest {

@InjectMocks
private DataMotionServiceImpl dataMotionService;

@Mock
private StorageStrategyFactory storageStrategyFactory;
@Mock
private AsyncCompletionCallback<CopyCommandResult> callback;
@Mock
private Host srcHost;
@Mock
private Host destHost;

private final Map<VolumeInfo, DataStore> volumeMap = new LinkedHashMap<>();

@Before
public void setUp() {
Mockito.when(srcHost.getName()).thenReturn("host1");
Mockito.when(destHost.getName()).thenReturn("host2");
}

private void addRbdVolumes() {
volumeMap.put(mockVolume("root-uuid", mockPool("ceph1", StoragePoolType.RBD)), mockPool("nfs1", StoragePoolType.NetworkFilesystem));
volumeMap.put(mockVolume("data-uuid", mockPool("ceph1", StoragePoolType.RBD)), mockPool("ceph2", StoragePoolType.RBD));
}

private PrimaryDataStore mockPool(String name, StoragePoolType type) {
PrimaryDataStore pool = Mockito.mock(PrimaryDataStore.class);
Mockito.lenient().when(pool.getName()).thenReturn(name);
Mockito.lenient().when(pool.getPoolType()).thenReturn(type);
return pool;
}

private VolumeInfo mockVolume(String uuid, DataStore srcPool) {
VolumeInfo volume = Mockito.mock(VolumeInfo.class);
Mockito.when(volume.getUuid()).thenReturn(uuid);
Mockito.lenient().when(volume.getDataStore()).thenReturn(srcPool);
return volume;
}

private String migrateAndGetError() {
dataMotionService.copyAsync(volumeMap, null, srcHost, destHost, callback);
ArgumentCaptor<CopyCommandResult> captor = ArgumentCaptor.forClass(CopyCommandResult.class);
Mockito.verify(callback).complete(captor.capture());
return captor.getValue().getResult();
}

@Test
public void copyAsyncKvmNoStrategyNamesPoolTypesAndSaysToStopVm() {
Mockito.when(srcHost.getHypervisorType()).thenReturn(HypervisorType.KVM);
addRbdVolumes();

Assert.assertEquals("KVM cannot live migrate the volumes of this running VM from host host1 to host host2 between these storage pools: "
+ "data-uuid from ceph1 (RBD) to ceph2 (RBD), root-uuid from ceph1 (RBD) to nfs1 (NetworkFilesystem). "
+ "Stop the VM and migrate the volumes while it is stopped.", migrateAndGetError());
}

@Test
public void copyAsyncKvmNoStrategyUnknownPoolType() {
Mockito.when(srcHost.getHypervisorType()).thenReturn(HypervisorType.KVM);
DataStore imageStore = Mockito.mock(DataStore.class);
Mockito.when(imageStore.getName()).thenReturn("store1");
volumeMap.put(mockVolume("vol-uuid", null), imageStore);
volumeMap.put(mockVolume("vol2-uuid", mockPool("pool1", null)), mockPool("pool2", null));

Assert.assertEquals("KVM cannot live migrate the volumes of this running VM from host host1 to host host2 between these storage pools: "
+ "vol-uuid from unknown pool to store1, vol2-uuid from pool1 (unknown type) to pool2 (unknown type). "
+ "Stop the VM and migrate the volumes while it is stopped.", migrateAndGetError());
}

@Test
public void copyAsyncNonKvmNoStrategyKeepsGenericError() {
Mockito.when(srcHost.getHypervisorType()).thenReturn(HypervisorType.XenServer);
addRbdVolumes();

Assert.assertEquals("Can't find strategy to move data. Source Host: host1, Destination Host: host2, Volume UUIDs: root-uuid,data-uuid", migrateAndGetError());
}
}
Loading