Skip to content

Commit b042167

Browse files
committed
Handle pr review comments
1 parent 78fc3e0 commit b042167

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ public void onPublishMessage(String serderAddress, String subject, Object args)
628628
params.put(Config.RouterAggregationCommandEachTimeout.toString(), _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
629629
params.put(Config.MigrateWait.toString(), _configDao.getValue(Config.MigrateWait.toString()));
630630
_agentManager.propagateChangeToAgents(params);
631-
} else if (VMLeaseManagerImpl.InstanceLeaseEnabled.key().equals(globalSettingUpdated) && !VMLeaseManagerImpl.InstanceLeaseEnabled.value()) {
632-
vmLeaseManager.cancelLeaseOnExistingInstances();
631+
} else if (VMLeaseManagerImpl.InstanceLeaseEnabled.key().equals(globalSettingUpdated)) {
632+
vmLeaseManager.onLeaseFeatureToggle();
633633
}
634634
}
635635
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6341,7 +6341,7 @@ protected void applyLeaseOnUpdateInstance(UserVm instance, Integer leaseDuration
63416341
// vm must have associated lease during deployment
63426342
UserVmDetailVO vmDetail = userVmDetailsDao.findDetail(instance.getId(), VmDetailConstants.INSTANCE_LEASE_EXPIRY_DATE);
63436343
if (vmDetail == null || StringUtils.isEmpty(vmDetail.getValue())) {
6344-
logger.debug("Lease wont be applied on instance with id: {}, it doesn't have " +
6344+
logger.debug("Lease won't be applied on instance with id: {}, it doesn't have " +
63456345
"leased associated during deployment", instanceUuid);
63466346
return;
63476347
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@ enum ExpiryAction {
3232

3333
ConfigKey<Long> InstanceLeaseSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
3434
"instance.lease.scheduler.interval", "3600", "VM Lease Scheduler interval in seconds",
35-
true, List.of(ConfigKey.Scope.Global));
35+
false, List.of(ConfigKey.Scope.Global));
3636

3737
ConfigKey<Long> InstanceLeaseAlertSchedule = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
3838
"instance.lease.alertscheduler.interval", "86400", "Lease Alert Scheduler interval in seconds",
39-
true, List.of(ConfigKey.Scope.Global));
39+
false, List.of(ConfigKey.Scope.Global));
4040

4141
ConfigKey<Long> InstanceLeaseExpiryAlertDaysBefore = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
4242
"instance.lease.alert.daysbefore", "7", "Indicates how many days in advance the alert will be triggered before expiry.",
4343
true, List.of(ConfigKey.Scope.Global));
4444

45-
/**
46-
* This method will cancel lease on instances running under lease
47-
* will be primarily used when feature gets disabled
48-
*/
49-
void cancelLeaseOnExistingInstances();
45+
void onLeaseFeatureToggle();
5046
}

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,27 @@ public void setAsyncJobDispatcher(final AsyncJobDispatcher dispatcher) {
107107

108108
@Override
109109
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
110-
try {
111-
vmLeaseExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("VMLeasePollExecutor"));
112-
vmLeaseAlertExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("VMLeaseAlertPollExecutor"));
113-
} catch (final Exception e) {
114-
throw new ConfigurationException("Unable to to configure VMLeaseManagerImpl");
110+
if (InstanceLeaseEnabled.value()) {
111+
scheduleLeaseExecutors();
115112
}
116113
return true;
117114
}
118115

119116
@Override
120117
public boolean start() {
121-
vmLeaseExecutor.scheduleAtFixedRate(new VMLeaseSchedulerTask(),5L, InstanceLeaseSchedulerInterval.value(), TimeUnit.SECONDS);
122-
vmLeaseAlertExecutor.scheduleAtFixedRate(new VMLeaseAlertSchedulerTask(), 5L, InstanceLeaseAlertSchedule.value(), TimeUnit.SECONDS);
123118
return true;
124119
}
125120

126121
@Override
127122
public boolean stop() {
128-
vmLeaseExecutor.shutdown();
129-
vmLeaseAlertExecutor.shutdown();
123+
shutDownLeaseExecutors();
130124
return true;
131125
}
132126

133-
@Override
127+
/**
128+
* This method will cancel lease on instances running under lease
129+
* will be primarily used when feature gets disabled
130+
*/
134131
public void cancelLeaseOnExistingInstances() {
135132
List<UserVmJoinVO> leaseExpiringForInstances = userVmJoinDao.listLeaseInstancesExpiringInDays(-1);
136133
logger.debug("Total instances found for lease cancellation: {}", leaseExpiringForInstances.size());
@@ -142,6 +139,45 @@ public void cancelLeaseOnExistingInstances() {
142139
}
143140
}
144141

142+
@Override
143+
public void onLeaseFeatureToggle() {
144+
boolean isLeaseFeatureEnabled = VMLeaseManagerImpl.InstanceLeaseEnabled.value();
145+
if (isLeaseFeatureEnabled) {
146+
scheduleLeaseExecutors();
147+
} else {
148+
cancelLeaseOnExistingInstances();
149+
shutDownLeaseExecutors();
150+
}
151+
}
152+
153+
private void scheduleLeaseExecutors() {
154+
if (vmLeaseExecutor == null || vmLeaseExecutor.isShutdown()) {
155+
logger.debug("Scheduling lease executor");
156+
vmLeaseExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("VMLeasePollExecutor"));
157+
vmLeaseExecutor.scheduleAtFixedRate(new VMLeaseSchedulerTask(),5L, InstanceLeaseSchedulerInterval.value(), TimeUnit.SECONDS);
158+
}
159+
160+
if (vmLeaseAlertExecutor == null || vmLeaseAlertExecutor.isShutdown()) {
161+
logger.debug("Scheduling lease alert executor");
162+
vmLeaseAlertExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("VMLeaseAlertPollExecutor"));
163+
vmLeaseAlertExecutor.scheduleAtFixedRate(new VMLeaseAlertSchedulerTask(), 5L, InstanceLeaseAlertSchedule.value(), TimeUnit.SECONDS);
164+
}
165+
}
166+
167+
private void shutDownLeaseExecutors() {
168+
if (vmLeaseExecutor != null) {
169+
logger.debug("Shutting down lease executor");
170+
vmLeaseExecutor.shutdown();
171+
vmLeaseExecutor = null;
172+
}
173+
174+
if (vmLeaseAlertExecutor != null) {
175+
logger.debug("Shutting down lease alert executor");
176+
vmLeaseAlertExecutor.shutdown();
177+
vmLeaseAlertExecutor = null;
178+
}
179+
}
180+
145181
class VMLeaseSchedulerTask extends ManagedContextRunnable {
146182
@Override
147183
protected void runInContext() {

0 commit comments

Comments
 (0)