1818package com .cloud .vm ;
1919
2020import java .net .URI ;
21+ import java .sql .PreparedStatement ;
22+ import java .sql .ResultSet ;
23+ import java .sql .SQLException ;
2124import java .util .ArrayList ;
2225import java .util .Collection ;
2326import java .util .Date ;
2427import java .util .HashMap ;
2528import java .util .List ;
2629import java .util .Map ;
30+ import java .util .TimeZone ;
2731import java .util .concurrent .Executors ;
2832import java .util .concurrent .ScheduledExecutorService ;
2933import java .util .concurrent .TimeUnit ;
@@ -3412,11 +3416,11 @@ private void HandlePownerStateReport(Object target, String subject, String sende
34123416 if (vm != null ) {
34133417 switch (vm .getPowerState ()) {
34143418 case PowerOn :
3415- HandlePowerOnReportWithNoPendingJobsOnVM (vm );
3419+ handlePowerOnReportWithNoPendingJobsOnVM (vm );
34163420 break ;
34173421
34183422 case PowerOff :
3419- HandlePowerOffReportWithNoPendingJobsOnVM (vm );
3423+ handlePowerOffReportWithNoPendingJobsOnVM (vm );
34203424 break ;
34213425
34223426 // PowerUnknown shouldn't be reported, it is a derived
@@ -3434,7 +3438,7 @@ private void HandlePownerStateReport(Object target, String subject, String sende
34343438 }
34353439 }
34363440
3437- private void HandlePowerOnReportWithNoPendingJobsOnVM (VMInstanceVO vm ) {
3441+ private void handlePowerOnReportWithNoPendingJobsOnVM (VMInstanceVO vm ) {
34383442 //
34393443 // 1) handle left-over transitional VM states
34403444 // 2) handle out of band VM live migration
@@ -3493,7 +3497,7 @@ private void HandlePowerOnReportWithNoPendingJobsOnVM(VMInstanceVO vm) {
34933497 }
34943498 }
34953499
3496- private void HandlePowerOffReportWithNoPendingJobsOnVM (VMInstanceVO vm ) {
3500+ private void handlePowerOffReportWithNoPendingJobsOnVM (VMInstanceVO vm ) {
34973501
34983502 // TODO :
34993503 // 1) handle left-over transitional VM states
@@ -3527,15 +3531,69 @@ private void HandlePowerOffReportWithNoPendingJobsOnVM(VMInstanceVO vm) {
35273531
35283532 private void scanStalledVMInTransitionState (long hostId ) {
35293533 //
3530- // TODO check VM that is stuck in Starting, Stopping, Migrating states, we won't check
3534+ // Check VM that is stuck in Starting, Stopping, Migrating states, we won't check
35313535 // VMs in expunging state (this need to be handled specially)
35323536 //
35333537 // checking condition
35343538 // 1) no pending VmWork job
3535- // 2) no power state update for some time
3536- // 3) on hostId host
3539+ // 2) on hostId host and host is UP
3540+ //
3541+ // When host is UP, soon or later we will get a report from the host about the VM,
3542+ // however, if VM is missing from the host report (it may happen in out of band changes
3543+ // or from designed behave of XS/KVM)
3544+ //
35373545
35383546
3547+ }
3548+
3549+ // TODO, use sql query directly for quick prototype, need to refactor to use joins and search builders
3550+ // if it supports
3551+ @ DB
3552+ private List <Long > listStalledVMInTransitionStateOnUpHost (long hostId , Date cutTime ) {
3553+ String sql = "SELECT i.* FROM vm_instance as i, host as h WHERE h.status = 'UP' " +
3554+ "AND h.id = ? AND i.power_state_update_time < ? AND i.host_id = h.id " +
3555+ "AND (i.state ='Starting' OR i.state='Stopping' OR i.state='Migrating') " +
3556+ "AND i.id NOT IN (SELECT vm_instance_id FROM vm_work_job)" ;
35393557
3558+ List <Long > l = new ArrayList <Long >();
3559+ Transaction txn = Transaction .currentTxn ();;
3560+ PreparedStatement pstmt = null ;
3561+ try {
3562+ pstmt = txn .prepareAutoCloseStatement (sql );
3563+
3564+ pstmt .setLong (1 , hostId );
3565+ pstmt .setString (2 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), cutTime ));
3566+ ResultSet rs = pstmt .executeQuery ();
3567+ while (rs .next ()) {
3568+ l .add (rs .getLong (1 ));
3569+ }
3570+ } catch (SQLException e ) {
3571+ } catch (Throwable e ) {
3572+ }
3573+ return l ;
3574+ }
3575+
3576+ @ DB
3577+ private List <Long > listStalledVMInTransitionStateOnDisconnectedHosts (Date cutTime ) {
3578+ String sql = "SELECT i.* FROM vm_instance as i, host as h WHERE h.status != 'UP' " +
3579+ "AND i.power_state_update_time < ? AND i.host_id = h.id " +
3580+ "AND (i.state ='Starting' OR i.state='Stopping' OR i.state='Migrating') " +
3581+ "AND i.id NOT IN (SELECT vm_instance_id FROM vm_work_job)" ;
3582+
3583+ List <Long > l = new ArrayList <Long >();
3584+ Transaction txn = Transaction .currentTxn ();;
3585+ PreparedStatement pstmt = null ;
3586+ try {
3587+ pstmt = txn .prepareAutoCloseStatement (sql );
3588+
3589+ pstmt .setString (1 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), cutTime ));
3590+ ResultSet rs = pstmt .executeQuery ();
3591+ while (rs .next ()) {
3592+ l .add (rs .getLong (1 ));
3593+ }
3594+ } catch (SQLException e ) {
3595+ } catch (Throwable e ) {
3596+ }
3597+ return l ;
35403598 }
35413599}
0 commit comments