Skip to content

Commit 8209684

Browse files
sureshanapartivishesh92
authored andcommitted
some code improvements for instacne / power state check
1 parent 686b298 commit 8209684

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,10 @@ public Boolean doInTransaction(TransactionStatus status) {
906906
&& (null == instance.getPowerStateUpdateTime()
907907
|| instance.getPowerStateUpdateTime().before(wisdomEra))) {
908908
Long savedPowerHostId = instance.getPowerHostId();
909-
if (instance.getPowerState() != powerState || savedPowerHostId == null
910-
|| savedPowerHostId != powerHostId) {
909+
if (instance.getPowerState() != powerState
910+
|| savedPowerHostId == null
911+
|| savedPowerHostId != powerHostId
912+
|| !isPowerStateInSyncWithInstanceState(powerState, powerHostId, instance)) {
911913
instance.setPowerState(powerState);
912914
instance.setPowerHostId(powerHostId);
913915
instance.setPowerStateUpdateCount(1);
@@ -921,31 +923,6 @@ public Boolean doInTransaction(TransactionStatus status) {
921923
instance.setPowerStateUpdateTime(DateUtil.currentGMTTime());
922924
needToUpdate = true;
923925
update(instanceId, instance);
924-
} else {
925-
switch (instance.getState()) {
926-
case Starting:
927-
case Running:
928-
if (powerState == VirtualMachine.PowerState.PowerOff) {
929-
s_logger.debug(String.format("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s",
930-
instanceId, instance.getHostId(), powerHostId, instance.getState(), powerState));
931-
instance.setPowerStateUpdateTime(DateUtil.currentGMTTime());
932-
instance.setPowerStateUpdateCount(1);
933-
needToUpdate = true;
934-
update(instanceId, instance);
935-
}
936-
break;
937-
case Stopping:
938-
case Stopped:
939-
if (powerState == VirtualMachine.PowerState.PowerOn) {
940-
s_logger.debug(String.format("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s",
941-
instanceId, instance.getHostId(), powerHostId, instance.getState(), powerState));
942-
instance.setPowerStateUpdateTime(DateUtil.currentGMTTime());
943-
instance.setPowerStateUpdateCount(1);
944-
needToUpdate = true;
945-
update(instanceId, instance);
946-
}
947-
break;
948-
}
949926
}
950927
}
951928
}
@@ -954,6 +931,28 @@ public Boolean doInTransaction(TransactionStatus status) {
954931
});
955932
}
956933

934+
private boolean isPowerStateInSyncWithInstanceState(final VirtualMachine.PowerState powerState, final long powerHostId, final VMInstanceVO instance) {
935+
switch (instance.getState()) {
936+
case Starting:
937+
case Running:
938+
if (powerState == VirtualMachine.PowerState.PowerOff) {
939+
s_logger.debug(String.format("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s",
940+
instance.getId(), instance.getHostId(), powerHostId, instance.getState(), powerState));
941+
return false;
942+
}
943+
break;
944+
case Stopping:
945+
case Stopped:
946+
if (powerState == VirtualMachine.PowerState.PowerOn) {
947+
s_logger.debug(String.format("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s",
948+
instance.getId(), instance.getHostId(), powerHostId, instance.getState(), powerState));
949+
return false;
950+
}
951+
break;
952+
}
953+
return true;
954+
}
955+
957956
@Override
958957
public boolean isPowerStateUpToDate(final long instanceId) {
959958
VMInstanceVO instance = findById(instanceId);

engine/schema/src/test/java/com/cloud/vm/dao/VMInstanceDaoImplTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,8 @@
1717

1818
package com.cloud.vm.dao;
1919

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.mockito.Mock;
26-
2720
import static com.cloud.vm.VirtualMachine.State.Running;
2821
import static com.cloud.vm.VirtualMachine.State.Stopped;
29-
3022
import static com.cloud.vm.dao.VMInstanceDaoImpl.MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT;
3123
import static org.junit.Assert.assertFalse;
3224
import static org.junit.Assert.assertTrue;
@@ -37,11 +29,19 @@
3729
import static org.mockito.Mockito.times;
3830
import static org.mockito.Mockito.verify;
3931
import static org.mockito.Mockito.when;
40-
import com.cloud.vm.VMInstanceVO;
32+
33+
import java.util.Date;
34+
35+
import org.joda.time.DateTime;
36+
import org.junit.Before;
37+
import org.junit.Test;
38+
import org.mockito.Mock;
4139
import org.mockito.MockitoAnnotations;
4240
import org.mockito.Spy;
4341

44-
import java.util.Date;
42+
import com.cloud.utils.Pair;
43+
import com.cloud.vm.VMInstanceVO;
44+
import com.cloud.vm.VirtualMachine;
4545

4646
/**
4747
* Created by sudharma_jain on 3/2/17.
@@ -117,6 +117,7 @@ public void testUpdatePowerStateNoChangeFirstUpdate() {
117117
when(vm.getPowerStateUpdateTime()).thenReturn(null);
118118
when(vm.getPowerHostId()).thenReturn(1L);
119119
when(vm.getPowerState()).thenReturn(VirtualMachine.PowerState.PowerOn);
120+
when(vm.getState()).thenReturn(Running);
120121
when(vm.getPowerStateUpdateCount()).thenReturn(1);
121122
doReturn(vm).when(vmInstanceDao).findById(anyLong());
122123
doReturn(true).when(vmInstanceDao).update(anyLong(), any());
@@ -163,8 +164,8 @@ public void testUpdatePowerStateNoChangeMaxUpdatesInvalidStateVmStopped() {
163164

164165
boolean result = vmInstanceDao.updatePowerState(1L, 1L, VirtualMachine.PowerState.PowerOn, new Date());
165166

166-
verify(vm, never()).setPowerState(any());
167-
verify(vm, never()).setPowerHostId(anyLong());
167+
verify(vm, times(1)).setPowerState(any());
168+
verify(vm, times(1)).setPowerHostId(anyLong());
168169
verify(vm, times(1)).setPowerStateUpdateCount(1);
169170
verify(vm, times(1)).setPowerStateUpdateTime(any(Date.class));
170171

@@ -183,12 +184,11 @@ public void testUpdatePowerStateNoChangeMaxUpdatesInvalidStateVmRunning() {
183184

184185
boolean result = vmInstanceDao.updatePowerState(1L, 1L, VirtualMachine.PowerState.PowerOff, new Date());
185186

186-
verify(vm, never()).setPowerState(any());
187-
verify(vm, never()).setPowerHostId(anyLong());
187+
verify(vm, times(1)).setPowerState(any());
188+
verify(vm, times(1)).setPowerHostId(anyLong());
188189
verify(vm, times(1)).setPowerStateUpdateCount(1);
189190
verify(vm, times(1)).setPowerStateUpdateTime(any(Date.class));
190191

191192
assertTrue(result);
192193
}
193-
194194
}

0 commit comments

Comments
 (0)