Skip to content

Commit af6c28b

Browse files
SudharmaJainRohit Yadav
authored andcommitted
CLOUDSTACK-8910: The reserved_capacity field increases suddenly after a vmware host failure
In case of vmware host failure, all the VMs including stopped VMs migrate to the new host. For the Stopped Vms powerhost gets updated. This was triggering HandlePowerStateReport which finally calls updatePowerState updating update_time for the VM. This cause the capacity being reserved for stopped VMs. (cherry picked from commit 9d268c8) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 1b26a48 commit af6c28b

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ public boolean updateState(State oldState, Event event, State newState, VirtualM
456456
// state is same, don't need to update
457457
return true;
458458
}
459+
if(ifStateUnchanged(oldState,newState, oldHostId, newHostId)) {
460+
return true;
461+
}
459462

460463
// lock the target row at beginning to avoid lock-promotion caused deadlock
461464
lockRow(vm.getId(), true);
@@ -503,6 +506,14 @@ public boolean updateState(State oldState, Event event, State newState, VirtualM
503506
return result > 0;
504507
}
505508

509+
boolean ifStateUnchanged(State oldState, State newState, Long oldHostId, Long newHostId ) {
510+
if (oldState == State.Stopped && newState == State.Stopped && newHostId == null && oldHostId == null) {
511+
// No change , no need to update
512+
return true;
513+
}
514+
return false;
515+
}
516+
506517
@Override
507518
public List<VMInstanceVO> listByLastHostId(Long hostId) {
508519
SearchCriteria<VMInstanceVO> sc = AllFieldsSearch.create();
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+
18+
package com.cloud.vm.dao;
19+
20+
import com.cloud.utils.Pair;
21+
import com.cloud.vm.VirtualMachine;
22+
import org.joda.time.DateTime;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.Assert;
26+
import org.mockito.Mock;
27+
28+
import static com.cloud.vm.VirtualMachine.State.Running;
29+
import static com.cloud.vm.VirtualMachine.State.Stopped;
30+
31+
import static org.mockito.Mockito.when;
32+
import com.cloud.vm.VMInstanceVO;
33+
import org.mockito.MockitoAnnotations;
34+
import org.mockito.Spy;
35+
36+
/**
37+
* Created by sudharma_jain on 3/2/17.
38+
*/
39+
40+
public class VMInstanceDaoImplTest {
41+
42+
@Spy
43+
VMInstanceDaoImpl vmInstanceDao = new VMInstanceDaoImpl();
44+
45+
@Mock
46+
VMInstanceVO vm;
47+
48+
@Before
49+
public void setUp() throws Exception {
50+
MockitoAnnotations.initMocks(this);
51+
Long hostId = null;
52+
when(vm.getHostId()).thenReturn(hostId);
53+
when(vm.getUpdated()).thenReturn(5L);
54+
when(vm.getUpdateTime()).thenReturn(DateTime.now().toDate());
55+
}
56+
57+
@Test
58+
public void testUpdateState() throws Exception {
59+
Long destHostId = null;
60+
Pair<Long, Long> opaqueMock = new Pair<Long, Long>(new Long(1), destHostId);
61+
vmInstanceDao.updateState(Stopped, VirtualMachine.Event.FollowAgentPowerOffReport, Stopped, vm , opaqueMock);
62+
}
63+
64+
@Test
65+
public void testIfStateAndHostUnchanged() throws Exception {
66+
Assert.assertEquals(vmInstanceDao.ifStateUnchanged(Stopped, Stopped, null, null), true);
67+
Assert.assertEquals(vmInstanceDao.ifStateUnchanged(Stopped, Running, null, null), false);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)