33
33
import java .util .concurrent .ConcurrentHashMap ;
34
34
import java .util .concurrent .ConcurrentMap ;
35
35
import java .util .concurrent .atomic .AtomicLong ;
36
+ import java .util .function .Supplier ;
36
37
37
38
/**
38
39
* The Engine which does the peer health checking work. Based on the config passed, it probes the peer once in specified
@@ -48,20 +49,30 @@ public class ConnectionHealthCheckerImpl implements ConnectionHealthChecker {
48
49
49
50
private final SetOnceFlag shutdown = new SetOnceFlag ();
50
51
private final SetOnceFlag started = new SetOnceFlag ();
51
-
52
- public ConnectionHealthCheckerImpl (HealthCheckerConfig healthCheckerConfig , TCConnectionManager connManager ) {
52
+
53
+ public ConnectionHealthCheckerImpl (HealthCheckerConfig healthCheckerConfig , TCConnectionManager connManager , Supplier < Boolean > reachable ) {
53
54
Assert .assertNotNull (healthCheckerConfig );
54
55
Assert .eval (healthCheckerConfig .isHealthCheckerEnabled ());
55
56
logger = LoggerFactory .getLogger (ConnectionHealthCheckerImpl .class .getName () + ": "
56
57
+ healthCheckerConfig .getHealthCheckerName ());
57
58
monitorThread = new Timer (healthCheckerConfig .getHealthCheckerName () + " - HealthCheck-Timer" , true );
58
- monitorThreadEngine = getHealthMonitorThreadEngine (healthCheckerConfig , connManager , logger );
59
+ monitorThreadEngine = getHealthMonitorThreadEngine (healthCheckerConfig , connManager , reachable , logger );
59
60
}
60
61
61
- protected HealthCheckerMonitorThreadEngine getHealthMonitorThreadEngine (HealthCheckerConfig config ,
62
+ private HealthCheckerMonitorThreadEngine getHealthMonitorThreadEngine (HealthCheckerConfig config ,
62
63
TCConnectionManager connectionManager ,
63
- Logger loger ) {
64
- return new HealthCheckerMonitorThreadEngine (config , connectionManager , loger );
64
+ Supplier <Boolean > reachable ,
65
+ Logger logger ) {
66
+ return new HealthCheckerMonitorThreadEngine (config , connectionManager , ()-> {
67
+ if (reachable .get ()) {
68
+ return true ;
69
+ } else {
70
+ stop ();
71
+ connectionManager .asynchCloseAllConnections ();
72
+ connectionManager .shutdown ();
73
+ return false ;
74
+ }
75
+ }, logger );
65
76
}
66
77
67
78
@ Override
@@ -149,19 +160,20 @@ static class HealthCheckerMonitorThreadEngine extends TimerTask {
149
160
private final long pingInterval ;
150
161
private final int pingProbes ;
151
162
private final long checkTimeInterval ;
152
- private final SetOnceFlag stop = new SetOnceFlag ();
153
163
private final HealthCheckerConfig config ;
154
164
private final Logger logger ;
155
165
private final TCConnectionManager connectionManager ;
166
+ private final Supplier <Boolean > reachable ;
156
167
private final AtomicLong lastCheckTime = new AtomicLong (System .currentTimeMillis ());
157
168
158
169
public HealthCheckerMonitorThreadEngine (HealthCheckerConfig healthCheckerConfig ,
159
- TCConnectionManager connectionManager , Logger logger ) {
170
+ TCConnectionManager connectionManager , Supplier < Boolean > reachable , Logger logger ) {
160
171
this .pingIdleTime = healthCheckerConfig .getPingIdleTimeMillis ();
161
172
this .pingInterval = healthCheckerConfig .getPingIntervalMillis ();
162
173
this .pingProbes = healthCheckerConfig .getPingProbes ();
163
174
this .checkTimeInterval = healthCheckerConfig .getCheckTimeInterval ();
164
175
this .connectionManager = connectionManager ;
176
+ this .reachable = reachable ;
165
177
this .config = healthCheckerConfig ;
166
178
167
179
Assert .assertNotNull (logger );
@@ -193,7 +205,6 @@ protected ConnectionHealthCheckerContext getHealthCheckerContext(MessageTranspor
193
205
}
194
206
195
207
public void stop () {
196
- stop .attemptSet ();
197
208
this .cancel ();
198
209
}
199
210
@@ -202,42 +213,44 @@ public void run() {
202
213
// same interval for all connections
203
214
final boolean canCheckTime = canCheckTime ();
204
215
205
- Iterator <MessageTransportBase > connectionIterator = connectionMap .values ().iterator ();
206
- while (connectionIterator .hasNext ()) {
207
- MessageTransportBase mtb = connectionIterator .next ();
208
-
209
- TCConnection conn = mtb .getConnection ();
210
- if (conn == null || !mtb .isConnected ()) {
211
- logger .info ("[" + (conn == null ? null : conn .getRemoteAddress ().toString ())
212
- + "] is not connected. Health Monitoring for this node is now disabled." );
213
- connectionIterator .remove ();
214
- continue ;
215
- }
216
-
217
- if (mtb .getReceiveLayer () == null ) {
218
- logger .info ("[" + (conn == null ? null : conn .getRemoteAddress ().toString ())
219
- + "] is no longer referenced. Closing the connection" );
220
- mtb .disconnect ();
221
- connectionIterator .remove ();
222
- continue ;
223
- }
216
+ if (reachable .get ()) {
217
+ Iterator <MessageTransportBase > connectionIterator = connectionMap .values ().iterator ();
218
+ while (connectionIterator .hasNext ()) {
219
+ MessageTransportBase mtb = connectionIterator .next ();
224
220
225
- ConnectionHealthCheckerContext connContext = mtb .getHealthCheckerContext ();
226
- if ((conn .getIdleReceiveTime () >= this .pingIdleTime )) {
221
+ TCConnection conn = mtb .getConnection ();
222
+ if (conn == null || !mtb .isConnected ()) {
223
+ logger .info ("[" + (conn == null ? null : conn .getRemoteAddress ().toString ())
224
+ + "] is not connected. Health Monitoring for this node is now disabled." );
225
+ connectionIterator .remove ();
226
+ continue ;
227
+ }
227
228
228
- if (!connContext .probeIfAlive ()) {
229
- // Connection is dead. Disconnect the transport.
230
- logger .error ("Declared connection dead " + mtb .getConnectionID () + " idle time "
231
- + conn .getIdleReceiveTime () + "ms" );
229
+ if (mtb .getReceiveLayer () == null ) {
230
+ logger .info ("[" + (conn == null ? null : conn .getRemoteAddress ().toString ())
231
+ + "] is no longer referenced. Closing the connection" );
232
232
mtb .disconnect ();
233
233
connectionIterator .remove ();
234
+ continue ;
235
+ }
236
+
237
+ ConnectionHealthCheckerContext connContext = mtb .getHealthCheckerContext ();
238
+ if ((conn .getIdleReceiveTime () >= this .pingIdleTime )) {
239
+
240
+ if (!connContext .probeIfAlive ()) {
241
+ // Connection is dead. Disconnect the transport.
242
+ logger .error ("Declared connection dead " + mtb .getConnectionID () + " idle time "
243
+ + conn .getIdleReceiveTime () + "ms" );
244
+ mtb .disconnect ();
245
+ connectionIterator .remove ();
246
+ }
247
+ } else {
248
+ connContext .refresh ();
249
+ }
250
+ // is there any significant time difference between hosts ?
251
+ if (canCheckTime ) {
252
+ connContext .checkTime ();
234
253
}
235
- } else {
236
- connContext .refresh ();
237
- }
238
- // is there any significant time difference between hosts ?
239
- if (canCheckTime ) {
240
- connContext .checkTime ();
241
254
}
242
255
}
243
256
0 commit comments