Skip to content

Commit 7260e8d

Browse files
iHiroakiKawaiSheng Yang
authored andcommitted
CLOUDSTACK-1638: Introduce NetworkMigrationResponder
The location of the virtual machine is provided by DeployDestination, which will be passed in NetworkGuru#reserve and NetworkElement#prepare. During the virtual machine migration, it actually changes DeployDestination and it looks like that it will tell that event to network components as it has NetworkManager#prepareNicForMigration. The problem is that althogh the interface has that method, NetworkManagerImpl does not tell the DeployDestination changes to network components. So IMHO, we need to add calls of NetworkGuru#reserve and NetworkElement#prepare in NetworkManagerImpl#prepareNicForMigration . And then, we also need to add calls NetworkGuru#release and NetworkElement#release after the migration, otherwise the network resources that plugin reserved will be kept even when the vm leaves off. (Sheng Yang: rebase code, add license header) Signed-off-by: Sheng Yang <sheng.yang@citrix.com>
1 parent cf6045f commit 7260e8d

6 files changed

Lines changed: 239 additions & 20 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network;
18+
19+
import com.cloud.deploy.DeployDestination;
20+
import com.cloud.vm.NicProfile;
21+
import com.cloud.vm.ReservationContext;
22+
import com.cloud.vm.VirtualMachine;
23+
import com.cloud.vm.VirtualMachineProfile;
24+
25+
/**
26+
* NetworkGuru and NetworkElements that implement this interface
27+
* will be called during Virtual Machine migration.
28+
*/
29+
public interface NetworkMigrationResponder {
30+
/**
31+
* Prepare for migration.
32+
*
33+
* This method will be called per nic before the vm migration.
34+
* @param nic
35+
* @param network
36+
* @param vm
37+
* @param dest
38+
* @param context
39+
* @return true when operation was successful.
40+
*/
41+
public boolean prepareMigration(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context);
42+
43+
/**
44+
* Cancel for migration preparation.
45+
*
46+
* This method will be called per nic when the entire vm migration
47+
* process failed and need to release the resouces that was
48+
* allocated at the migration preparation.
49+
* @param nic destination nic
50+
* @param network destination network
51+
* @param vm destination vm profile
52+
* @param src The context nic migrates from.
53+
* @param dst The context nic migrates to.
54+
*/
55+
public void rollbackMigration(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, ReservationContext src, ReservationContext dst);
56+
57+
/**
58+
* Commit the migration resource.
59+
*
60+
* This method will be called per nic when the entire vm migration
61+
* process was successful. This is useful to release the resource of
62+
* source deployment where vm has left.
63+
* @param nic source nic
64+
* @param network source network
65+
* @param vm source vm profile
66+
* @param src the context nic migrates from.
67+
* @param dst the context nic migrates to.
68+
*/
69+
public void commitMigration(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, ReservationContext src, ReservationContext dst);
70+
}

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,34 @@ void release(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean fo
123123
Pair<NetworkGuru, NetworkVO> implementNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException,
124124
InsufficientCapacityException;
125125

126-
<T extends VMInstanceVO> void prepareNicForMigration(VirtualMachineProfile<T> vm, DeployDestination dest);
126+
/**
127+
* prepares vm nic change for migration
128+
*
129+
* This method will be called in migration transaction before the vm migration.
130+
* @param vm
131+
* @param dest
132+
*/
133+
void prepareNicForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest);
134+
135+
/**
136+
* commit vm nic change for migration
137+
*
138+
* This method will be called in migration transaction after the successful
139+
* vm migration.
140+
* @param src
141+
* @param dst
142+
*/
143+
void commitNicForMigration(VirtualMachineProfile<? extends VMInstanceVO> src, VirtualMachineProfile<? extends VMInstanceVO> dst);
144+
145+
/**
146+
* rollback vm nic change for migration
147+
*
148+
* This method will be called in migaration transaction after vm migration
149+
* failure.
150+
* @param src
151+
* @param dst
152+
*/
153+
void rollbackNicForMigration(VirtualMachineProfile<? extends VMInstanceVO> src, VirtualMachineProfile<? extends VMInstanceVO> dst);
127154

128155
boolean shutdownNetwork(long networkId, ReservationContext context, boolean cleanupElements);
129156

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,20 +2000,90 @@ public NicProfile prepareNic(VirtualMachineProfile<? extends VMInstanceVO> vmPro
20002000
}
20012001

20022002
@Override
2003-
public <T extends VMInstanceVO> void prepareNicForMigration(VirtualMachineProfile<T> vm, DeployDestination dest) {
2003+
public void prepareNicForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
20042004
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
2005+
ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
20052006
for (NicVO nic : nics) {
20062007
NetworkVO network = _networksDao.findById(nic.getNetworkId());
20072008
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
20082009

20092010
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
20102011
NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate,
20112012
_networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
2013+
if(guru instanceof NetworkMigrationResponder){
2014+
if(!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)){
2015+
s_logger.error("NetworkGuru "+guru+" prepareForMigration failed."); // XXX: Transaction error
2016+
}
2017+
}
2018+
for (NetworkElement element : _networkElements) {
2019+
if(element instanceof NetworkMigrationResponder){
2020+
if(!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)){
2021+
s_logger.error("NetworkElement "+element+" prepareForMigration failed."); // XXX: Transaction error
2022+
}
2023+
}
2024+
}
20122025
guru.updateNicProfile(profile, network);
20132026
vm.addNic(profile);
20142027
}
20152028
}
20162029

2030+
private NicProfile findNicProfileById(VirtualMachineProfile<? extends VMInstanceVO> vm, long id){
2031+
for(NicProfile nic: vm.getNics()){
2032+
if(nic.getId() == id){
2033+
return nic;
2034+
}
2035+
}
2036+
return null;
2037+
}
2038+
2039+
@Override
2040+
public void commitNicForMigration(
2041+
VirtualMachineProfile<? extends VMInstanceVO> src,
2042+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
2043+
for(NicProfile nicSrc: src.getNics()){
2044+
NetworkVO network = _networksDao.findById(nicSrc.getNetworkId());
2045+
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
2046+
NicProfile nicDst = findNicProfileById(dst, nicSrc.getId());
2047+
ReservationContext src_context = new ReservationContextImpl(nicSrc.getReservationId(), null, null);
2048+
ReservationContext dst_context = new ReservationContextImpl(nicDst.getReservationId(), null, null);
2049+
2050+
if(guru instanceof NetworkMigrationResponder){
2051+
((NetworkMigrationResponder) guru).commitMigration(nicSrc, network, src, src_context, dst_context);
2052+
}
2053+
for (NetworkElement element : _networkElements) {
2054+
if(element instanceof NetworkMigrationResponder){
2055+
((NetworkMigrationResponder) element).commitMigration(nicSrc, network, src, src_context, dst_context);
2056+
}
2057+
}
2058+
// update the reservation id
2059+
NicVO nicVo = _nicDao.findById(nicDst.getId());
2060+
nicVo.setReservationId(nicDst.getReservationId());
2061+
_nicDao.persist(nicVo);
2062+
}
2063+
}
2064+
2065+
@Override
2066+
public void rollbackNicForMigration(
2067+
VirtualMachineProfile<? extends VMInstanceVO> src,
2068+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
2069+
for(NicProfile nicDst: dst.getNics()){
2070+
NetworkVO network = _networksDao.findById(nicDst.getNetworkId());
2071+
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
2072+
NicProfile nicSrc = findNicProfileById(src, nicDst.getId());
2073+
ReservationContext src_context = new ReservationContextImpl(nicSrc.getReservationId(), null, null);
2074+
ReservationContext dst_context = new ReservationContextImpl(nicDst.getReservationId(), null, null);
2075+
2076+
if(guru instanceof NetworkMigrationResponder){
2077+
((NetworkMigrationResponder) guru).rollbackMigration(nicDst, network, dst, src_context, dst_context);
2078+
}
2079+
for (NetworkElement element : _networkElements) {
2080+
if(element instanceof NetworkMigrationResponder){
2081+
((NetworkMigrationResponder) element).rollbackMigration(nicDst, network, dst, src_context, dst_context);
2082+
}
2083+
}
2084+
}
2085+
}
2086+
20172087
@Override
20182088
@DB
20192089
public void release(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean forced) throws

‎server/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,11 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
14141414
alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY_MIGRATE;
14151415
}
14161416

1417+
VirtualMachineProfile<VMInstanceVO> vmSrc = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
1418+
for(NicProfile nic: _networkMgr.getNicProfiles(vm)){
1419+
vmSrc.addNic(nic);
1420+
}
1421+
14171422
VirtualMachineProfile<VMInstanceVO> profile = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
14181423
_networkMgr.prepareNicForMigration(profile, dest);
14191424
this.volumeMgr.prepareForMigration(profile, dest);
@@ -1439,6 +1444,7 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
14391444
throw new AgentUnavailableException("Operation timed out", dstHostId);
14401445
} finally {
14411446
if (pfma == null) {
1447+
_networkMgr.rollbackNicForMigration(vmSrc, profile);
14421448
work.setStep(Step.Done);
14431449
_workDao.update(work.getId(), work);
14441450
}
@@ -1447,10 +1453,12 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
14471453
vm.setLastHostId(srcHostId);
14481454
try {
14491455
if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) {
1456+
_networkMgr.rollbackNicForMigration(vmSrc, profile);
14501457
s_logger.info("Migration cancelled because state has changed: " + vm);
14511458
throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm);
14521459
}
14531460
} catch (NoTransitionException e1) {
1461+
_networkMgr.rollbackNicForMigration(vmSrc, profile);
14541462
s_logger.info("Migration cancelled because " + e1.getMessage());
14551463
throw new ConcurrentOperationException("Migration cancelled because " + e1.getMessage());
14561464
}
@@ -1502,6 +1510,7 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
15021510
} finally {
15031511
if (!migrated) {
15041512
s_logger.info("Migration was unsuccessful. Cleaning up: " + vm);
1513+
_networkMgr.rollbackNicForMigration(vmSrc, profile);
15051514

15061515
_alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getInstanceName() + " from host " + fromHost.getName() + " in zone "
15071516
+ dest.getDataCenter().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed. Please check logs.");
@@ -1516,6 +1525,8 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
15161525
} catch (NoTransitionException e) {
15171526
s_logger.warn(e.getMessage());
15181527
}
1528+
}else{
1529+
_networkMgr.commitNicForMigration(vmSrc, profile);
15191530
}
15201531

15211532
work.setStep(Step.Done);

‎server/test/com/cloud/network/MockNetworkManagerImpl.java‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(long networkId, DeployDesti
277277
}
278278

279279

280-
@Override
281-
public <T extends VMInstanceVO> void prepareNicForMigration(VirtualMachineProfile<T> vm, DeployDestination dest) {
282-
// TODO Auto-generated method stub
283-
284-
}
285-
286280
@Override
287281
public boolean destroyNetwork(long networkId, ReservationContext context) {
288282
// TODO Auto-generated method stub
@@ -966,4 +960,27 @@ public DhcpServiceProvider getDhcpServiceProvider(Network network) {
966960
public PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, VlanType type, List<Long> vlanDbIds, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException {
967961
return null; //To change body of implemented methods use File | Settings | File Templates.
968962
}
963+
964+
@Override
965+
public void prepareNicForMigration(
966+
VirtualMachineProfile<? extends VMInstanceVO> vm,
967+
DeployDestination dest) {
968+
// TODO Auto-generated method stub
969+
970+
}
971+
972+
@Override
973+
public void commitNicForMigration(
974+
VirtualMachineProfile<? extends VMInstanceVO> src,
975+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
976+
// TODO Auto-generated method stub
977+
978+
}
979+
980+
@Override
981+
public void rollbackNicForMigration(
982+
VirtualMachineProfile<? extends VMInstanceVO> src,
983+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
984+
// TODO Auto-generated method stub
985+
}
969986
}

‎server/test/com/cloud/vpc/MockNetworkManagerImpl.java‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -845,18 +845,6 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(long networkId, DeployDesti
845845

846846

847847

848-
/* (non-Javadoc)
849-
* @see com.cloud.network.NetworkManager#prepareNicForMigration(com.cloud.vm.VirtualMachineProfile, com.cloud.deploy.DeployDestination)
850-
*/
851-
@Override
852-
public <T extends VMInstanceVO> void prepareNicForMigration(VirtualMachineProfile<T> vm, DeployDestination dest) {
853-
// TODO Auto-generated method stub
854-
855-
}
856-
857-
858-
859-
860848

861849
/* (non-Javadoc)
862850
* @see com.cloud.network.NetworkManager#shutdownNetwork(long, com.cloud.vm.ReservationContext, boolean)
@@ -1471,4 +1459,40 @@ public PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account ow
14711459
}
14721460

14731461

1462+
1463+
1464+
1465+
@Override
1466+
public void prepareNicForMigration(
1467+
VirtualMachineProfile<? extends VMInstanceVO> vm,
1468+
DeployDestination dest) {
1469+
// TODO Auto-generated method stub
1470+
1471+
}
1472+
1473+
1474+
1475+
1476+
1477+
@Override
1478+
public void commitNicForMigration(
1479+
VirtualMachineProfile<? extends VMInstanceVO> src,
1480+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
1481+
// TODO Auto-generated method stub
1482+
1483+
}
1484+
1485+
1486+
1487+
1488+
1489+
@Override
1490+
public void rollbackNicForMigration(
1491+
VirtualMachineProfile<? extends VMInstanceVO> src,
1492+
VirtualMachineProfile<? extends VMInstanceVO> dst) {
1493+
// TODO Auto-generated method stub
1494+
1495+
}
1496+
1497+
14741498
}

0 commit comments

Comments
 (0)