2
2
3
3
import java .util .Collections ;
4
4
import java .util .List ;
5
+ import java .util .concurrent .CancellationException ;
6
+ import java .util .concurrent .ExecutionException ;
7
+ import java .util .concurrent .ScheduledFuture ;
5
8
import java .util .concurrent .TimeUnit ;
9
+ import java .util .concurrent .TimeoutException ;
6
10
11
+ import javax .enterprise .concurrent .ManagedScheduledExecutorService ;
7
12
import javax .inject .Inject ;
8
13
9
14
import org .apache .commons .lang .StringUtils ;
37
42
import org .ovirt .engine .core .dao .VdsStaticDao ;
38
43
import org .ovirt .engine .core .dao .gluster .GlusterDBUtils ;
39
44
import org .ovirt .engine .core .utils .EngineLocalConfig ;
40
- import org .ovirt .engine .core .utils .ThreadUtils ;
41
45
import org .ovirt .engine .core .utils .lock .EngineLock ;
42
46
import org .ovirt .engine .core .utils .threadpool .ThreadPoolUtil ;
47
+ import org .ovirt .engine .core .utils .threadpool .ThreadPools ;
43
48
import org .ovirt .engine .core .vdsbroker .ResourceManager ;
49
+ import org .ovirt .engine .core .vdsbroker .vdsbroker .IVdsServer ;
50
+ import org .ovirt .engine .core .vdsbroker .vdsbroker .VDSInfoReturn ;
51
+
44
52
45
53
public abstract class VdsCommand <T extends VdsActionParameters > extends CommandBase <T > {
46
54
47
55
protected String _failureMessage = null ;
56
+ private ScheduledFuture <?> reachableFuture ;
48
57
49
58
@ Inject
50
59
protected AuditLogDirector auditLogDirector ;
@@ -68,6 +77,9 @@ public abstract class VdsCommand<T extends VdsActionParameters> extends CommandB
68
77
private AlertDirector alertDirector ;
69
78
@ Inject
70
79
private VdsStaticDao vdsStaticDao ;
80
+ @ Inject
81
+ @ ThreadPools (ThreadPools .ThreadPoolType .EngineScheduledThreadPool )
82
+ private ManagedScheduledExecutorService executor ;
71
83
72
84
/**
73
85
* Constructor for command creation when compensation is applied on startup
@@ -112,14 +124,46 @@ protected void runSleepOnReboot(boolean synchronous, final VDSStatus status) {
112
124
}
113
125
}
114
126
127
+ /**
128
+ * Enables timeout on the thread until max timeout time is exceeded or a connection is made with the rebooting device
129
+ */
115
130
private void sleepOnReboot (final VDSStatus status ) {
116
- int sleepTimeInSec = Config .<Integer > getValue (ConfigValues .ServerRebootTimeout );
117
- log .info ("Waiting {} seconds, for server to finish reboot process." ,
118
- sleepTimeInSec );
119
131
resourceManager .getVdsManager (getVdsId ()).setInServerRebootTimeout (true );
120
- ThreadUtils .sleep (TimeUnit .SECONDS .toMillis (sleepTimeInSec ));
121
- resourceManager .getVdsManager (getVdsId ()).setInServerRebootTimeout (false );
122
- setVdsStatus (status );
132
+ int serverRebootMax = Config .<Integer > getValue (ConfigValues .ServerRebootTimeout );
133
+ int retryTime = Config .<Integer > getValue (ConfigValues .ServerRebootSleepTime );
134
+ try {
135
+ reachableFuture
136
+ = executor .scheduleAtFixedRate (() -> isReachable (), retryTime , retryTime , TimeUnit .SECONDS );
137
+ reachableFuture .get (serverRebootMax , TimeUnit .SECONDS );
138
+ } catch (InterruptedException e ) {
139
+ log .info ("Trying to reconnect with host {} after reboot failed due to {}" , getVdsId (), e .toString ());
140
+ } catch (ExecutionException e ) {
141
+ log .info ("Problem during execution of reconnection with host {} after reboot due to {}" , getVdsId (), e .toString ());
142
+ } catch (TimeoutException e ) {
143
+ log .info ("Unable to connect to host {} after {} seconds" , getVdsId (), serverRebootMax );
144
+ } catch (CancellationException e ) {
145
+ log .info ("Future cancelled due to ability to connect to host {} after reboot." , getVdsId ());
146
+ } finally {
147
+ resourceManager .getVdsManager (getVdsId ()).setInServerRebootTimeout (false );
148
+ setVdsStatus (status );
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Checks if the host is ready to reconnect
154
+ * if the status equals 0 it means the vds is done and ready to reconnect, so the thread can be interrupted
155
+ */
156
+ private void isReachable () {
157
+ try {
158
+ IVdsServer serv = resourceManager .getVdsManager (getVdsId ()).getVdsProxy ();
159
+ VDSInfoReturn info = serv .getVdsStats ();
160
+ log .info ("Status of host {} is {}" , getVdsId (), info .status .toString ());
161
+ if (info .status .code == 0 ) {
162
+ reachableFuture .cancel (false );
163
+ }
164
+ } catch (Throwable t ) {
165
+ log .error ("Error encountered {}" , t .toString ());
166
+ }
123
167
}
124
168
125
169
/**
0 commit comments