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 @@ -19,6 +19,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -1037,7 +1038,7 @@ public Map<Long, VirtualMachine.PowerState> updatePowerState(
return notUpdated;
}
StringBuilder sql = new StringBuilder("UPDATE `cloud`.`vm_instance` SET " +
"`power_host` = ?, `power_state_update_time` = now(), `power_state` = CASE ");
"`power_host` = ?, `power_state_update_time` = ?, `power_state` = CASE ");
updateCounts.keySet().forEach(key -> {
sql.append("WHEN id = ").append(key).append(" THEN '").append(instancePowerStates.get(key)).append("' ");
});
Expand All @@ -1052,6 +1053,7 @@ public Map<Long, VirtualMachine.PowerState> updatePowerState(
TransactionLegacy txn = TransactionLegacy.currentTxn();
try (PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql.toString())) {
pstmt.setLong(1, powerHostId);
pstmt.setTimestamp(2, new Timestamp(DateUtil.currentGMTTime().getTime()), gmtCalendar());
pstmt.executeUpdate();
} catch (SQLException e) {
logger.error("Unable to execute update power states SQL from VMs {} due to: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,37 @@
import static com.cloud.vm.VirtualMachine.State.Running;
import static com.cloud.vm.VirtualMachine.State.Stopped;
import static com.cloud.vm.dao.VMInstanceDaoImpl.MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import com.cloud.host.dao.HostDao;
import org.joda.time.DateTime;
import org.junit.After;
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.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
Expand All @@ -50,6 +59,7 @@
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import org.mockito.junit.MockitoJUnitRunner;
Expand Down Expand Up @@ -210,6 +220,51 @@
assertTrue(result);
}

@Test
public void testUpdatePowerStatesBindsGmtUpdateTimeInsteadOfDbNow() throws Exception {
TimeZone defaultTimeZone = TimeZone.getDefault();
try {
for (String timeZone : List.of("UTC", "Europe/Amsterdam", "America/New_York", "Asia/Kolkata", "Pacific/Auckland")) {
TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
verifyBulkUpdatePowerStatesBindsGmtUpdateTime(timeZone);
}
} finally {
TimeZone.setDefault(defaultTimeZone);
}
}

private void verifyBulkUpdatePowerStatesBindsGmtUpdateTime(String timeZone) throws Exception {
VMInstanceVO instance = Mockito.mock(VMInstanceVO.class);

Check warning on line 237 in engine/schema/src/test/java/com/cloud/vm/dao/VMInstanceDaoImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaEW2BHxIIA1DvOUfPRv&open=AaEW2BHxIIA1DvOUfPRv&pullRequest=14343
when(instance.getId()).thenReturn(1L);
when(instance.getPowerState()).thenReturn(VirtualMachine.PowerState.PowerOn);
doReturn(List.of(instance)).when(vmInstanceDao).listSelectPowerStateByIds(any());
TransactionLegacy txn = Mockito.mock(TransactionLegacy.class);

Check warning on line 241 in engine/schema/src/test/java/com/cloud/vm/dao/VMInstanceDaoImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaEW2BHxIIA1DvOUfPRw&open=AaEW2BHxIIA1DvOUfPRw&pullRequest=14343
PreparedStatement pstmt = Mockito.mock(PreparedStatement.class);

Check warning on line 242 in engine/schema/src/test/java/com/cloud/vm/dao/VMInstanceDaoImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaEW2BHxIIA1DvOUfPRx&open=AaEW2BHxIIA1DvOUfPRx&pullRequest=14343
ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
when(txn.prepareAutoCloseStatement(sqlCaptor.capture())).thenReturn(pstmt);
ArgumentCaptor<Timestamp> timeCaptor = ArgumentCaptor.forClass(Timestamp.class);
ArgumentCaptor<Calendar> calendarCaptor = ArgumentCaptor.forClass(Calendar.class);

long before = System.currentTimeMillis();
Map<Long, VirtualMachine.PowerState> notUpdated;
try (MockedStatic<TransactionLegacy> ignored = Mockito.mockStatic(TransactionLegacy.class)) {
ignored.when(TransactionLegacy::currentTxn).thenReturn(txn);
notUpdated = vmInstanceDao.updatePowerState(Map.of(1L, VirtualMachine.PowerState.PowerOff), 2L, new Date());
}
long after = System.currentTimeMillis();

assertTrue(timeZone, notUpdated.isEmpty());
String sql = sqlCaptor.getValue();
assertFalse(timeZone, sql.toLowerCase().contains("now()"));
assertTrue(timeZone, sql.contains("`power_state_update_time` = ?"));
verify(pstmt).setLong(1, 2L);
verify(pstmt).setTimestamp(eq(2), timeCaptor.capture(), calendarCaptor.capture());
verify(pstmt).executeUpdate();
long written = timeCaptor.getValue().getTime();
assertTrue(timeZone, written >= before && written <= after);
assertEquals(timeZone, "GMT", calendarCaptor.getValue().getTimeZone().getID());
}

@Test
public void testSearchRemovedByRemoveDate() {
SearchBuilder<VMInstanceVO> sb = Mockito.mock(SearchBuilder.class);
Expand Down
Loading