Skip to content

Commit 62397cc

Browse files
committed
validate integration tests, minor ui changes and log messages
1 parent aff544f commit 62397cc

File tree

17 files changed

+112
-124
lines changed

17 files changed

+112
-124
lines changed

server/src/main/java/com/cloud/api/query/dao/ServiceOfferingJoinDaoImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public ServiceOfferingResponse newServiceOfferingResponse(ServiceOfferingJoinVO
177177
}
178178
}
179179

180-
if (VMLeaseManagerImpl.InstanceLeaseEnabled.value() && offering.getLeaseDuration() != null && offering.getLeaseDuration() != -1L) {
180+
if (VMLeaseManagerImpl.InstanceLeaseEnabled.value() && offering.getLeaseDuration() != null && offering.getLeaseDuration() >= -1L) {
181181
offeringResponse.setLeaseDuration(offering.getLeaseDuration());
182182
offeringResponse.setLeaseExpiryAction(offering.getLeaseExpiryAction());
183183
}

server/src/main/java/com/cloud/api/query/dao/UserVmJoinDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoi
4747
List<UserVmJoinVO> listByAccountServiceOfferingTemplateAndNotInState(long accountId,
4848
List<VirtualMachine.State> states, List<Long> offeringIds, List<Long> templateIds);
4949

50-
List<UserVmJoinVO> listExpiredInstancesIds();
50+
List<UserVmJoinVO> listLeaseExpiredInstances();
5151

52-
List<UserVmJoinVO> listExpiringInstancesInDays(int days);
52+
List<UserVmJoinVO> listLeaseInstancesExpiringInDays(int days);
5353
}

server/src/main/java/com/cloud/api/query/dao/UserVmJoinDaoImpl.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ protected UserVmJoinDaoImpl() {
138138
leaseOverInstanceSearch.done();
139139

140140
leaseExpiringInstanceSearch = createSearchBuilder();
141-
leaseExpiringInstanceSearch.and("leaseExpiringToday", leaseExpiringInstanceSearch.entity().getLeaseExpiryDate(), Op.GTEQ);
141+
leaseExpiringInstanceSearch.selectFields(leaseExpiringInstanceSearch.entity().getId(), leaseExpiringInstanceSearch.entity().getUuid(),
142+
leaseExpiringInstanceSearch.entity().getPodId(), leaseExpiringInstanceSearch.entity().getDataCenterId());
143+
leaseExpiringInstanceSearch.and("leaseCurrentDate", leaseExpiringInstanceSearch.entity().getLeaseExpiryDate(), Op.GTEQ);
142144
leaseExpiringInstanceSearch.and("leaseExpiresOnDate", leaseExpiringInstanceSearch.entity().getLeaseExpiryDate(), Op.LT);
143145
leaseExpiringInstanceSearch.done();
144146

@@ -751,21 +753,21 @@ public List<UserVmJoinVO> listByAccountServiceOfferingTemplateAndNotInState(long
751753
}
752754

753755
@Override
754-
public List<UserVmJoinVO> listExpiredInstancesIds() {
756+
public List<UserVmJoinVO> listLeaseExpiredInstances() {
755757
SearchCriteria<UserVmJoinVO> sc = leaseOverInstanceSearch.create();
756758
sc.setParameters("leaseExpired", new Date());
757759
return listBy(sc);
758760
}
759761

760762
@Override
761-
public List<UserVmJoinVO> listExpiringInstancesInDays(int days) {
763+
public List<UserVmJoinVO> listLeaseInstancesExpiringInDays(int days) {
762764
SearchCriteria<UserVmJoinVO> sc = leaseExpiringInstanceSearch.create();
763765
Date currentDate = new Date();
764766
Calendar calendar = Calendar.getInstance();
765767
calendar.setTime(currentDate);
766768
calendar.add(Calendar.DAY_OF_MONTH, days);
767769
Date nextDate = calendar.getTime();
768-
sc.setParameters("leaseExpiringToday", currentDate);
770+
sc.setParameters("leaseCurrentDate", currentDate);
769771
sc.setParameters("leaseExpiresOnDate", nextDate);
770772
return listBy(sc);
771773
}

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3500,7 +3500,7 @@ public static String validateAndGetLeaseExpiryAction(Long leaseDuration, String
35003500
throw new InvalidParameterValueException("Provide values for both: leaseduration and leaseexpiryaction");
35013501
}
35023502

3503-
if (leaseDuration < 0L) {
3503+
if (leaseDuration < 1L) {
35043504
throw new InvalidParameterValueException("Invalid value provided for leaseDuration, accepts only positive number");
35053505
}
35063506

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
import java.net.URLDecoder;
364364
import java.text.SimpleDateFormat;
365365
import java.time.LocalDate;
366+
import java.time.LocalTime;
366367
import java.time.ZoneId;
367368
import java.util.ArrayList;
368369
import java.util.Arrays;
@@ -6366,7 +6367,7 @@ protected void applyLeaseOnUpdateInstance(UserVm instance, Long leaseDuration, S
63666367

63676368
private void addLeaseDetailsForInstance(UserVm vm, Long leaseDuration, String leaseExpiryAction) {
63686369
LocalDate today = LocalDate.now();
6369-
Date leaseExpiryDate = Date.from(today.plusDays(leaseDuration).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
6370+
Date leaseExpiryDate = Date.from(today.plusDays(leaseDuration).atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
63706371
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
63716372
String formattedLeaseExpiryDate = sdf.format(leaseExpiryDate);
63726373
userVmDetailsDao.addDetail(vm.getId(), VmDetailConstants.INSTANCE_LEASE_EXPIRY_DATE, formattedLeaseExpiryDate, false);

server/src/main/java/org/apache/cloudstack/vm/lease/VMLeaseManager.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ enum ExpiryAction {
3636
true, List.of(ConfigKey.Scope.Account, ConfigKey.Scope.Domain));
3737

3838
ConfigKey<String> InstanceLeaseExpiryAction = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, String.class,
39-
"instance.lease.expiryaction", "stop", "Default action to be taken at instance lease expiry",
39+
"instance.lease.expiryaction", "STOP", "Default action to be taken at instance lease expiry",
4040
true, List.of(ConfigKey.Scope.Account, ConfigKey.Scope.Domain));
4141

4242
ConfigKey<Long> InstanceLeaseSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
43-
"instance.lease.scheduler.interval", "60", "VM Lease Scheduler interval in seconds",
43+
"instance.lease.scheduler.interval", "86400", "VM Lease Scheduler interval in seconds",
4444
true, List.of(ConfigKey.Scope.Global));
4545

4646
ConfigKey<Long> InstanceLeaseAlertSchedule = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
47-
"instance.lease.alertscheduler.interval", "3600", "Lease Alert Scheduler interval in seconds",
47+
"instance.lease.alertscheduler.interval", "86400", "Lease Alert Scheduler interval in seconds",
4848
true, List.of(ConfigKey.Scope.Global));
4949

5050
ConfigKey<Long> InstanceLeaseAlertStartsAt = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,

server/src/main/java/org/apache/cloudstack/vm/lease/VMLeaseManagerImpl.java

+33-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.cloud.api.query.dao.UserVmJoinDao;
2424
import com.cloud.api.query.vo.UserVmJoinVO;
2525
import com.cloud.event.ActionEventUtils;
26+
import com.cloud.user.Account;
2627
import com.cloud.user.User;
28+
import com.cloud.utils.DateUtil;
2729
import com.cloud.utils.StringUtils;
2830
import com.cloud.utils.component.ComponentContext;
2931
import com.cloud.utils.component.ManagerBase;
@@ -39,10 +41,12 @@
3941
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
4042
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
4143
import org.apache.cloudstack.managed.context.ManagedContextTimerTask;
44+
import org.apache.commons.lang3.time.DateUtils;
4245

4346
import javax.inject.Inject;
4447
import java.util.ArrayList;
4548
import java.util.Arrays;
49+
import java.util.Calendar;
4650
import java.util.Date;
4751
import java.util.HashMap;
4852
import java.util.List;
@@ -138,17 +142,32 @@ protected void alert() {
138142
return;
139143
}
140144

141-
List<UserVmJoinVO> leaseExpiringForInstances = userVmJoinDao.listExpiringInstancesInDays(InstanceLeaseAlertStartsAt.value().intValue());
142-
for (UserVmJoinVO instance : leaseExpiringForInstances) {
143-
alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_USERVM, instance.getDataCenterId(), instance.getPodId(),
144-
"Lease expiring for instance id: " + instance.getUuid(), "Lease expiring for instance");
145+
GlobalLock scanLock = GlobalLock.getInternLock("VMLeaseAlertScheduler");
146+
try {
147+
if (scanLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) {
148+
try {
149+
List<UserVmJoinVO> leaseExpiringForInstances = userVmJoinDao.listLeaseInstancesExpiringInDays(InstanceLeaseAlertStartsAt.value().intValue());
150+
for (UserVmJoinVO instance : leaseExpiringForInstances) {
151+
alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_USERVM, instance.getDataCenterId(), instance.getPodId(),
152+
"Lease expiring for instance id: " + instance.getUuid(), "Lease expiring for instance");
153+
}
154+
} finally {
155+
scanLock.unlock();
156+
}
157+
}
158+
} finally {
159+
scanLock.releaseRef();
145160
}
146161
}
147162

148163
@Override
149-
public void poll(Date currentTimestamp) {
150-
// as feature is disabled, no action is required
164+
public void poll(Date timestamp) {
165+
Date currentTimestamp = DateUtils.round(timestamp, Calendar.MINUTE);
166+
String displayTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, currentTimestamp);
167+
logger.debug("VMLeaseScheduler.poll is being called at {}", displayTime);
168+
151169
if (!InstanceLeaseEnabled.value()) {
170+
logger.debug("Instance lease feature is disabled, no action is required");
152171
return;
153172
}
154173

@@ -168,16 +187,15 @@ public void poll(Date currentTimestamp) {
168187

169188
protected void reallyRun() {
170189
// fetch user_instances having leaseDuration configured and has expired
171-
List<UserVmJoinVO> leaseExpiredInstances = userVmJoinDao.listExpiredInstancesIds();
190+
List<UserVmJoinVO> leaseExpiredInstances = userVmJoinDao.listLeaseExpiredInstances();
172191
List<Long> actionableInstanceIds = new ArrayList<>();
173-
// iterate over them and ignore if delete protection is enabled
192+
// iterate over and skip ones with delete protection
174193
for (UserVmJoinVO userVmVO : leaseExpiredInstances) {
175194
if (userVmVO.isDeleteProtection() != null && userVmVO.isDeleteProtection()) {
176195
logger.debug("Ignoring instance with id: {} as deleteProtection is enabled", userVmVO.getUuid());
177196
continue;
178197
}
179198
// state check, include instances not yet stopped or destroyed
180-
// it can be done in fetch_user_instances as well
181199
if (!Arrays.asList(Destroyed, Expunging, Unknown, VirtualMachine.State.Error).contains(userVmVO.getState())) {
182200
actionableInstanceIds.add(userVmVO.getId());
183201
}
@@ -208,7 +226,7 @@ protected void reallyRun() {
208226
failedToSubmitInstanceIds.add(instanceId);
209227
}
210228
}
211-
logger.debug("Successfully submitted jobs for ids: {}", submittedJobIds);
229+
logger.debug("Successfully submitted lease expiry jobs with ids: {}", submittedJobIds);
212230
if (!failedToSubmitInstanceIds.isEmpty()) {
213231
logger.debug("Lease scheduler failed to submit jobs for instance ids: {}", failedToSubmitInstanceIds);
214232
}
@@ -227,7 +245,7 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
227245
}
228246
default: {
229247
logger.error("Invalid configuration for instance.lease.expiryaction for vm id: {}, " +
230-
"valid values are: \"stop\" and \"destroy\"", instance.getUuid());
248+
"valid values are: \"STOP\" and \"DESTROY\"", instance.getUuid());
231249
}
232250
}
233251
return null;
@@ -236,8 +254,8 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
236254
long executeStopInstanceJob(UserVmJoinVO vm, boolean isForced, long eventId) {
237255
final Map<String, String> params = new HashMap<>();
238256
params.put(ApiConstants.ID, String.valueOf(vm.getId()));
239-
params.put("ctxUserId", "1");
240-
params.put("ctxAccountId", String.valueOf(vm.getAccountId()));
257+
params.put("ctxUserId", String.valueOf(User.UID_SYSTEM));
258+
params.put("ctxAccountId", String.valueOf(Account.ACCOUNT_ID_SYSTEM));
241259
params.put(ApiConstants.CTX_START_EVENT_ID, String.valueOf(eventId));
242260
params.put(ApiConstants.FORCED, String.valueOf(isForced));
243261
final StopVMCmd cmd = new StopVMCmd();
@@ -252,8 +270,8 @@ long executeStopInstanceJob(UserVmJoinVO vm, boolean isForced, long eventId) {
252270
long executeDestroyInstanceJob(UserVmJoinVO vm, boolean isForced, long eventId) {
253271
final Map<String, String> params = new HashMap<>();
254272
params.put(ApiConstants.ID, String.valueOf(vm.getId()));
255-
params.put("ctxUserId", "1");
256-
params.put("ctxAccountId", String.valueOf(vm.getAccountId()));
273+
params.put("ctxUserId", String.valueOf(User.UID_SYSTEM));
274+
params.put("ctxAccountId", String.valueOf(Account.ACCOUNT_ID_SYSTEM));
257275
params.put(ApiConstants.CTX_START_EVENT_ID, String.valueOf(eventId));
258276
params.put(ApiConstants.FORCED, String.valueOf(isForced));
259277

server/src/test/java/org/apache/cloudstack/vm/lease/VMLeaseManagerImplTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void testStart() {
110110
public void testAlert() {
111111
UserVmJoinVO vm = createMockVm(1L, VM_UUID, VM_NAME, VirtualMachine.State.Running, false);
112112
List<UserVmJoinVO> expiringVms = Arrays.asList(vm);
113-
when(userVmJoinDao.listExpiringInstancesInDays(anyInt())).thenReturn(expiringVms);
113+
when(userVmJoinDao.listLeaseInstancesExpiringInDays(anyInt())).thenReturn(expiringVms);
114114
vmLeaseManager.alert();
115115
verify(alertManager).sendAlert(
116116
eq(AlertManager.AlertType.ALERT_TYPE_USERVM),
@@ -123,7 +123,7 @@ public void testAlert() {
123123

124124
@Test
125125
public void testReallyRunNoExpiredInstances() {
126-
when(userVmJoinDao.listExpiredInstancesIds()).thenReturn(new ArrayList<>());
126+
when(userVmJoinDao.listLeaseExpiredInstances()).thenReturn(new ArrayList<>());
127127
vmLeaseManager.reallyRun();
128128
verify(asyncJobManager, never()).submitAsyncJob(any(AsyncJobVO.class));
129129
}
@@ -132,7 +132,7 @@ public void testReallyRunNoExpiredInstances() {
132132
public void testReallyRunWithDeleteProtection() {
133133
UserVmJoinVO vm = createMockVm(1L, VM_UUID, VM_NAME, VirtualMachine.State.Running, true);
134134
List<UserVmJoinVO> expiredVms = Arrays.asList(vm);
135-
when(userVmJoinDao.listExpiredInstancesIds()).thenReturn(expiredVms);
135+
when(userVmJoinDao.listLeaseExpiredInstances()).thenReturn(expiredVms);
136136
vmLeaseManager.reallyRun();
137137
// Verify no jobs were submitted because of delete protection
138138
verify(asyncJobManager, never()).submitAsyncJob(any(AsyncJobVO.class));
@@ -147,7 +147,7 @@ public void testReallyRunIgnoredStates() {
147147
UserVmJoinVO vmError = createMockVm(4L, "vm-uuid4", "vm-name4", VirtualMachine.State.Error, false);
148148

149149
List<UserVmJoinVO> expiredVms = Arrays.asList(vmDestroyed, vmExpunging, vmUnknown, vmError);
150-
when(userVmJoinDao.listExpiredInstancesIds()).thenReturn(expiredVms);
150+
when(userVmJoinDao.listLeaseExpiredInstances()).thenReturn(expiredVms);
151151
vmLeaseManager.reallyRun();
152152
// Verify no jobs were submitted because all VMs are in ignored states
153153
verify(asyncJobManager, never()).submitAsyncJob(any(AsyncJobVO.class));
@@ -157,7 +157,7 @@ public void testReallyRunIgnoredStates() {
157157
public void testReallyRunStopAction() {
158158
UserVmJoinVO vm = createMockVm(1L, VM_UUID, VM_NAME, VirtualMachine.State.Running, false);
159159
List<UserVmJoinVO> expiredVms = Arrays.asList(vm);
160-
when(userVmJoinDao.listExpiredInstancesIds()).thenReturn(expiredVms);
160+
when(userVmJoinDao.listLeaseExpiredInstances()).thenReturn(expiredVms);
161161
when(userVmJoinDao.findById(1L)).thenReturn(vm);
162162
doReturn(1L).when(vmLeaseManager).executeStopInstanceJob(eq(vm), eq(true), anyLong());
163163
try (MockedStatic<ActionEventUtils> utilities = Mockito.mockStatic(ActionEventUtils.class)) {
@@ -173,7 +173,7 @@ public void testReallyRunStopAction() {
173173
public void testReallyRunDestroyAction() {
174174
UserVmJoinVO vm = createMockVm(1L, VM_UUID, VM_NAME, VirtualMachine.State.Running, false, DESTORY);
175175
List<UserVmJoinVO> expiredVms = Arrays.asList(vm);
176-
when(userVmJoinDao.listExpiredInstancesIds()).thenReturn(expiredVms);
176+
when(userVmJoinDao.listLeaseExpiredInstances()).thenReturn(expiredVms);
177177
when(userVmJoinDao.findById(1L)).thenReturn(vm);
178178
doReturn(1L).when(vmLeaseManager).executeDestroyInstanceJob(eq(vm), eq(true), anyLong());
179179
try (MockedStatic<ActionEventUtils> utilities = Mockito.mockStatic(ActionEventUtils.class)) {

0 commit comments

Comments
 (0)