41
41
import org .apache .cloudstack .framework .config .Configurable ;
42
42
import org .apache .cloudstack .framework .jobs .AsyncJobManager ;
43
43
import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
44
+ import org .apache .cloudstack .management .ManagementServerHost ;
44
45
import org .apache .cloudstack .management .ManagementServerHost .State ;
45
46
import org .apache .cloudstack .maintenance .command .CancelMaintenanceManagementServerHostCommand ;
46
47
import org .apache .cloudstack .maintenance .command .CancelShutdownManagementServerHostCommand ;
@@ -245,6 +246,9 @@ public void triggerShutdown() {
245
246
this .shutdownTriggered = true ;
246
247
prepareForShutdown (true );
247
248
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
249
+ if (msHost == null ) {
250
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
251
+ }
248
252
msHostDao .updateState (msHost .getId (), State .ShuttingDown );
249
253
}
250
254
@@ -269,12 +273,18 @@ private void prepareForShutdown(boolean postTrigger) {
269
273
public void prepareForShutdown () {
270
274
prepareForShutdown (false );
271
275
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
276
+ if (msHost == null ) {
277
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
278
+ }
272
279
msHostDao .updateState (msHost .getId (), State .PreparingForShutDown );
273
280
}
274
281
275
282
@ Override
276
283
public void cancelShutdown () {
277
284
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
285
+ if (msHost == null ) {
286
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
287
+ }
278
288
if (!this .preparingForShutdown && !(State .PreparingForShutDown .equals (msHost .getState ()) || State .ReadyToShutDown .equals (msHost .getState ()))) {
279
289
throw new CloudRuntimeException ("Shutdown has not been triggered" );
280
290
}
@@ -295,36 +305,48 @@ public void prepareForMaintenance(String lbAlorithm, boolean forced) {
295
305
if (this .preparingForMaintenance ) {
296
306
throw new CloudRuntimeException ("Maintenance has already been initiated" );
297
307
}
308
+
309
+ ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
310
+ if (msHost == null ) {
311
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
312
+ }
298
313
this .preparingForMaintenance = true ;
299
314
this .maintenanceStartTime = System .currentTimeMillis ();
300
315
this .lbAlgorithm = lbAlorithm ;
301
316
jobManager .disableAsyncJobs ();
302
317
onPreparingForMaintenance ();
303
318
waitForPendingJobs (forced );
304
- ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
305
319
msHostDao .updateState (msHost .getId (), State .PreparingForMaintenance );
306
320
}
307
321
308
322
@ Override
309
323
public void cancelMaintenance () {
310
324
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
325
+ if (msHost == null ) {
326
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
327
+ }
311
328
if (!this .preparingForMaintenance && !(State .Maintenance .equals (msHost .getState ()) || State .PreparingForMaintenance .equals (msHost .getState ()))) {
312
329
throw new CloudRuntimeException ("Maintenance has not been initiated" );
313
330
}
314
331
resetMaintenanceParams ();
315
332
resetShutdownParams ();
316
333
jobManager .enableAsyncJobs ();
317
334
cancelWaitForPendingJobs ();
318
- if (msHost != null ) {
319
- if (State .PreparingForMaintenance .equals (msHost .getState ())) {
320
- onCancelPreparingForMaintenance ();
321
- }
322
- if (State .Maintenance .equals (msHost .getState ())) {
323
- onCancelMaintenance ();
324
- }
325
- }
326
-
327
335
msHostDao .updateState (msHost .getId (), State .Up );
336
+ ScheduledExecutorService cancelMaintenanceService = Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("CancelMaintenance-Job" ));
337
+ cancelMaintenanceService .schedule (() -> {
338
+ cancelMaintenanceTask (msHost .getState ());
339
+ }, 0 , TimeUnit .SECONDS );
340
+ cancelMaintenanceService .shutdown ();
341
+ }
342
+
343
+ private void cancelMaintenanceTask (ManagementServerHost .State msState ) {
344
+ if (State .PreparingForMaintenance .equals (msState )) {
345
+ onCancelPreparingForMaintenance ();
346
+ }
347
+ if (State .Maintenance .equals (msState )) {
348
+ onCancelMaintenance ();
349
+ }
328
350
}
329
351
330
352
private void waitForPendingJobs (boolean forceMaintenance ) {
@@ -509,6 +531,9 @@ public void cancelPreparingForMaintenance(ManagementServerHostVO msHost) {
509
531
jobManager .enableAsyncJobs ();
510
532
if (msHost == null ) {
511
533
msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
534
+ if (msHost == null ) {
535
+ throw new CloudRuntimeException ("Invalid node id for the management server" );
536
+ }
512
537
}
513
538
onCancelPreparingForMaintenance ();
514
539
msHostDao .updateState (msHost .getId (), State .Up );
@@ -595,6 +620,10 @@ protected void runInContext() {
595
620
if (forceMaintenance ) {
596
621
logger .debug ("Maintenance window timeout, MS is forced to Maintenance Mode" );
597
622
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
623
+ if (msHost == null ) {
624
+ logger .warn ("Unable to find the management server, invalid node id" );
625
+ return ;
626
+ }
598
627
msHostDao .updateState (msHost .getId (), State .Maintenance );
599
628
managementServerMaintenanceManager .onMaintenance ();
600
629
managementServerMaintenanceManager .cancelWaitForPendingJobs ();
@@ -627,6 +656,10 @@ protected void runInContext() {
627
656
}
628
657
if (managementServerMaintenanceManager .isPreparingForMaintenance ()) {
629
658
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
659
+ if (msHost == null ) {
660
+ logger .warn ("Unable to find the management server, invalid node id" );
661
+ return ;
662
+ }
630
663
if (totalAgents == 0 ) {
631
664
logger .info ("MS is in Maintenance Mode" );
632
665
msHostDao .updateState (msHost .getId (), State .Maintenance );
@@ -654,14 +687,16 @@ protected void runInContext() {
654
687
logger .warn (String .format ("Unable to prepare for maintenance, cannot transfer direct agents on this management server node %d (id: %s)" , ManagementServerNode .getManagementServerId (), msHost .getUuid ()));
655
688
managementServerMaintenanceManager .cancelPreparingForMaintenance (msHost );
656
689
managementServerMaintenanceManager .cancelWaitForPendingJobs ();
657
- return ;
658
690
}
659
691
} else if (managementServerMaintenanceManager .isPreparingForShutdown ()) {
660
692
logger .info ("MS is Ready To Shutdown" );
661
693
ManagementServerHostVO msHost = msHostDao .findByMsid (ManagementServerNode .getManagementServerId ());
694
+ if (msHost == null ) {
695
+ logger .warn ("Unable to find the management server, invalid node id" );
696
+ return ;
697
+ }
662
698
msHostDao .updateState (msHost .getId (), State .ReadyToShutDown );
663
699
managementServerMaintenanceManager .cancelWaitForPendingJobs ();
664
- return ;
665
700
}
666
701
} catch (final Exception e ) {
667
702
logger .error ("Error trying to check/run pending jobs task" , e );
0 commit comments