2424
2525import javax .inject .Inject ;
2626
27+ import com .cloud .host .Host ;
28+ import com .cloud .host .HostVO ;
29+ import com .cloud .host .dao .HostDao ;
30+ import com .cloud .utils .Pair ;
2731import org .apache .cloudstack .framework .messagebus .MessageBus ;
2832import org .apache .cloudstack .framework .messagebus .PublishScope ;
2933import org .apache .logging .log4j .Logger ;
@@ -40,54 +44,57 @@ public class VirtualMachinePowerStateSyncImpl implements VirtualMachinePowerStat
4044
4145 @ Inject MessageBus _messageBus ;
4246 @ Inject VMInstanceDao _instanceDao ;
47+ @ Inject HostDao hostDao ;
4348 @ Inject ManagementServiceConfiguration mgmtServiceConf ;
4449
4550 public VirtualMachinePowerStateSyncImpl () {
4651 }
4752
4853 @ Override
49- public void resetHostSyncState (long hostId ) {
50- logger .info ("Reset VM power state sync for host: {}. " , hostId );
51- _instanceDao .resetHostPowerStateTracking (hostId );
54+ public void resetHostSyncState (Host host ) {
55+ logger .info ("Reset VM power state sync for host: {}" , host );
56+ _instanceDao .resetHostPowerStateTracking (host . getId () );
5257 }
5358
5459 @ Override
5560 public void processHostVmStateReport (long hostId , Map <String , HostVmStateReportEntry > report ) {
56- logger .debug ("Process host VM state report. host: {}." , hostId );
61+ HostVO host = hostDao .findById (hostId );
62+ logger .debug ("Process host VM state report. host: {}" , host );
5763
58- Map <Long , VirtualMachine .PowerState > translatedInfo = convertVmStateReport (report );
59- processReport (hostId , translatedInfo , false );
64+ Map <Long , Pair < VirtualMachine .PowerState , VMInstanceVO > > translatedInfo = convertVmStateReport (report );
65+ processReport (host , translatedInfo , false );
6066 }
6167
6268 @ Override
6369 public void processHostVmStatePingReport (long hostId , Map <String , HostVmStateReportEntry > report , boolean force ) {
64- logger .debug ("Process host VM state report from ping process. host: {}." , hostId );
70+ HostVO host = hostDao .findById (hostId );
71+ logger .debug ("Process host VM state report from ping process. host: {}" , host );
6572
66- Map <Long , VirtualMachine .PowerState > translatedInfo = convertVmStateReport (report );
67- processReport (hostId , translatedInfo , force );
73+ Map <Long , Pair < VirtualMachine .PowerState , VMInstanceVO > > translatedInfo = convertVmStateReport (report );
74+ processReport (host , translatedInfo , force );
6875 }
6976
70- private void processReport (long hostId , Map <Long , VirtualMachine .PowerState > translatedInfo , boolean force ) {
77+ private void processReport (HostVO host , Map <Long , Pair < VirtualMachine .PowerState , VMInstanceVO > > translatedInfo , boolean force ) {
7178
72- logger .debug ("Process VM state report. host: {}, number of records in report: {}." , hostId , translatedInfo .size ());
79+ logger .debug ("Process VM state report. host: {}, number of records in report: {}." , host , translatedInfo .size ());
7380
74- for (Map .Entry <Long , VirtualMachine .PowerState > entry : translatedInfo .entrySet ()) {
81+ for (Map .Entry <Long , Pair < VirtualMachine .PowerState , VMInstanceVO > > entry : translatedInfo .entrySet ()) {
7582
76- logger .debug ("VM state report. host: {}, vm id : {}, power state: {}. " , hostId , entry .getKey () , entry .getValue ());
83+ logger .debug ("VM state report. host: {}, vm: {}, power state: {}" , host , entry .getValue (). second () , entry .getValue (). first ());
7784
78- if (_instanceDao .updatePowerState (entry .getKey (), hostId , entry .getValue (), DateUtil .currentGMTTime ())) {
79- logger .debug ("VM state report is updated. host: {}, vm id : {}, power state: {}. " , hostId , entry .getKey () , entry .getValue ());
85+ if (_instanceDao .updatePowerState (entry .getKey (), host . getId () , entry .getValue (). first (), DateUtil .currentGMTTime ())) {
86+ logger .debug ("VM state report is updated. host: {}, vm: {}, power state: {}" , host , entry .getValue (). second () , entry .getValue (). first ());
8087
8188 _messageBus .publish (null , VirtualMachineManager .Topics .VM_POWER_STATE , PublishScope .GLOBAL , entry .getKey ());
8289 } else {
83- logger .trace ("VM power state does not change, skip DB writing. vm id : {}. " , entry .getKey ());
90+ logger .trace ("VM power state does not change, skip DB writing. vm: {}" , entry .getValue (). second ());
8491 }
8592 }
8693
8794 // any state outdates should be checked against the time before this list was retrieved
8895 Date startTime = DateUtil .currentGMTTime ();
8996 // for all running/stopping VMs, we provide monitoring of missing report
90- List <VMInstanceVO > vmsThatAreMissingReport = _instanceDao .findByHostInStates (hostId , VirtualMachine .State .Running ,
97+ List <VMInstanceVO > vmsThatAreMissingReport = _instanceDao .findByHostInStates (host . getId () , VirtualMachine .State .Running ,
9198 VirtualMachine .State .Stopping , VirtualMachine .State .Starting );
9299 java .util .Iterator <VMInstanceVO > it = vmsThatAreMissingReport .iterator ();
93100 while (it .hasNext ()) {
@@ -99,7 +106,7 @@ private void processReport(long hostId, Map<Long, VirtualMachine.PowerState> tra
99106 // here we need to be wary of out of band migration as opposed to other, more unexpected state changes
100107 if (vmsThatAreMissingReport .size () > 0 ) {
101108 Date currentTime = DateUtil .currentGMTTime ();
102- logger .debug ("Run missing VM report. current time: {}" , currentTime .getTime ());
109+ logger .debug ("Run missing VM report for host {} . current time: {}" , host , currentTime .getTime ());
103110
104111 // 2 times of sync-update interval for graceful period
105112 long milliSecondsGracefullPeriod = mgmtServiceConf .getPingInterval () * 2000L ;
@@ -109,70 +116,65 @@ private void processReport(long hostId, Map<Long, VirtualMachine.PowerState> tra
109116 // Make sure powerState is up to date for missing VMs
110117 try {
111118 if (!force && !_instanceDao .isPowerStateUpToDate (instance .getId ())) {
112- logger .warn ("Detected missing VM but power state is outdated, wait for another process report run for VM id : {}. " , instance . getId () );
119+ logger .warn ("Detected missing VM but power state is outdated, wait for another process report run for VM: {}" , instance );
113120 _instanceDao .resetVmPowerStateTracking (instance .getId ());
114121 continue ;
115122 }
116123 } catch (CloudRuntimeException e ) {
117- logger .warn ("Checked for missing powerstate of a none existing vm" , e );
124+ logger .warn ("Checked for missing powerstate of a none existing vm {}" , instance , e );
118125 continue ;
119126 }
120127
121128 Date vmStateUpdateTime = instance .getPowerStateUpdateTime ();
122129 if (vmStateUpdateTime == null ) {
123- logger .warn ("VM power state update time is null, falling back to update time for vm id : {}. " , instance . getId () );
130+ logger .warn ("VM power state update time is null, falling back to update time for vm: {}" , instance );
124131 vmStateUpdateTime = instance .getUpdateTime ();
125132 if (vmStateUpdateTime == null ) {
126- logger .warn ("VM update time is null, falling back to creation time for vm id : {}" , instance . getId () );
133+ logger .warn ("VM update time is null, falling back to creation time for vm: {}" , instance );
127134 vmStateUpdateTime = instance .getCreated ();
128135 }
129136 }
130137
131138 String lastTime = new SimpleDateFormat ("yyyy/MM/dd'T'HH:mm:ss.SSS'Z'" ).format (vmStateUpdateTime );
132- logger .debug ("Detected missing VM. host: {}, vm id: {}({}), power state: {}, last state update: {}"
133- , hostId
134- , instance .getId ()
135- , instance .getUuid ()
136- , VirtualMachine .PowerState .PowerReportMissing
137- , lastTime );
139+ logger .debug ("Detected missing VM. host: {}, vm: {}, power state: {}, last state update: {}" ,
140+ host , instance , VirtualMachine .PowerState .PowerReportMissing , lastTime );
138141
139142 long milliSecondsSinceLastStateUpdate = currentTime .getTime () - vmStateUpdateTime .getTime ();
140143
141144 if (force || milliSecondsSinceLastStateUpdate > milliSecondsGracefullPeriod ) {
142- logger .debug ("vm id : {} - time since last state update({}ms) has passed graceful period. " , instance . getId () , milliSecondsSinceLastStateUpdate );
145+ logger .debug ("vm: {} - time since last state update({}ms) has passed graceful period" , instance , milliSecondsSinceLastStateUpdate );
143146
144147 // this is were a race condition might have happened if we don't re-fetch the instance;
145148 // between the startime of this job and the currentTime of this missing-branch
146149 // an update might have occurred that we should not override in case of out of band migration
147- if (_instanceDao .updatePowerState (instance .getId (), hostId , VirtualMachine .PowerState .PowerReportMissing , startTime )) {
148- logger .debug ("VM state report is updated. host: {}, vm id : {}, power state: PowerReportMissing. " , hostId , instance . getId () );
150+ if (_instanceDao .updatePowerState (instance .getId (), host . getId () , VirtualMachine .PowerState .PowerReportMissing , startTime )) {
151+ logger .debug ("VM state report is updated. host: {}, vm: {}, power state: PowerReportMissing " , host , instance );
149152
150153 _messageBus .publish (null , VirtualMachineManager .Topics .VM_POWER_STATE , PublishScope .GLOBAL , instance .getId ());
151154 } else {
152- logger .debug ("VM power state does not change, skip DB writing. vm id : {}" , instance . getId () );
155+ logger .debug ("VM power state does not change, skip DB writing. vm: {}" , instance );
153156 }
154157 } else {
155- logger .debug ("vm id : {} - time since last state update({}ms) has not passed graceful period yet. " , instance . getId () , milliSecondsSinceLastStateUpdate );
158+ logger .debug ("vm: {} - time since last state update({} ms) has not passed graceful period yet" , instance , milliSecondsSinceLastStateUpdate );
156159 }
157160 }
158161 }
159162
160- logger .debug ("Done with process of VM state report. host: {}" , hostId );
163+ logger .debug ("Done with process of VM state report. host: {}" , host );
161164 }
162165
163- @ Override
164- public Map <Long , VirtualMachine .PowerState > convertVmStateReport (Map <String , HostVmStateReportEntry > states ) {
165- final HashMap <Long , VirtualMachine .PowerState > map = new HashMap <Long , VirtualMachine .PowerState >();
166+ public Map <Long , Pair <VirtualMachine .PowerState , VMInstanceVO >> convertVmStateReport (Map <String , HostVmStateReportEntry > states ) {
167+ final HashMap <Long , Pair <VirtualMachine .PowerState , VMInstanceVO >> map = new HashMap <>();
166168 if (states == null ) {
167169 return map ;
168170 }
169171
170172 for (Map .Entry <String , HostVmStateReportEntry > entry : states .entrySet ()) {
171173 VMInstanceVO vm = findVM (entry .getKey ());
172174 if (vm != null ) {
173- map .put (vm .getId (), entry .getValue ().getState ());
175+ map .put (vm .getId (), new Pair <>( entry .getValue ().getState (), vm ));
174176 } else {
175- logger .debug ("Unable to find matched VM in CloudStack DB. name: {}" , entry .getKey ());
177+ logger .debug ("Unable to find matched VM in CloudStack DB. name: {} powerstate: {} " , entry .getKey (), entry . getValue ());
176178 }
177179 }
178180
0 commit comments