-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LifeSupport.java
539 lines (495 loc) · 16.7 KB
/
LifeSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.lifecycle;
import java.util.ArrayList;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* Support class for handling collections of Lifecycle instances. Manages the transitions from one state to another.
* <p>
* To use this, first add instances to it that implement the Lifecycle interface. When lifecycle methods on this
* class are called it will try to invoke the same methods on the registered instances.
* <p>
* Components that internally owns other components that has a lifecycle can use this to control them as well.
*/
public class LifeSupport
implements Lifecycle
{
private volatile List<LifecycleInstance> instances = new ArrayList<>();
private volatile LifecycleStatus status = LifecycleStatus.NONE;
private final List<LifecycleListener> listeners = new ArrayList<>();
public LifeSupport()
{
}
/**
* Initialize all registered instances, transitioning from status NONE to STOPPED.
* <p>
* If transition fails, then it goes to STOPPED and then SHUTDOWN, so it cannot be restarted again.
*/
@Override
public synchronized void init()
throws LifecycleException
{
if ( status == LifecycleStatus.NONE )
{
status = changedStatus( this, status, LifecycleStatus.INITIALIZING );
for ( LifecycleInstance instance : instances )
{
try
{
instance.init();
}
catch ( LifecycleException e )
{
status = changedStatus( this, status, LifecycleStatus.STOPPED );
try
{
shutdown();
}
catch ( LifecycleException shutdownErr )
{
e.addSuppressed( shutdownErr );
}
throw e;
}
}
status = changedStatus( this, status, LifecycleStatus.STOPPED );
}
}
/**
* Start all registered instances, transitioning from STOPPED to STARTED.
* <p>
* If it was previously not initialized, it will be initialized first.
* <p>
* If any instance fails to start, the already started instances will be stopped, so
* that the overall status is STOPPED.
*
* @throws LifecycleException
*/
@Override
public synchronized void start()
throws LifecycleException
{
init();
if ( status == LifecycleStatus.STOPPED )
{
status = changedStatus( this, status, LifecycleStatus.STARTING );
for ( LifecycleInstance instance : instances )
{
try
{
instance.start();
}
catch ( LifecycleException e )
{
// TODO perhaps reconsider chaining of exceptions coming from LifeSupports?
status = changedStatus( this, status, LifecycleStatus.STARTED );
try
{
stop();
}
catch ( LifecycleException stopErr )
{
e.addSuppressed( stopErr );
}
throw e;
}
}
status = changedStatus( this, status, LifecycleStatus.STARTED );
}
}
/**
* Stop all registered instances, transitioning from STARTED to STOPPED.
* <p>
* If any instance fails to stop, the rest of the instances will still be stopped,
* so that the overall status is STOPPED.
*/
@Override
public synchronized void stop()
throws LifecycleException
{
if ( status == LifecycleStatus.STARTED )
{
status = changedStatus( this, status, LifecycleStatus.STOPPING );
LifecycleException ex = stopInstances( instances );
status = changedStatus( this, status, LifecycleStatus.STOPPED );
if ( ex != null )
{
throw ex;
}
}
}
private LifecycleException stopInstances( List<LifecycleInstance> instances )
{
LifecycleException ex = null;
for ( int i = instances.size() - 1; i >= 0; i-- )
{
LifecycleInstance lifecycleInstance = instances.get( i );
try
{
lifecycleInstance.stop();
}
catch ( LifecycleException e )
{
if ( ex != null )
{
ex.addSuppressed( e );
}
else
{
ex = e;
}
}
}
return ex;
}
/**
* Shutdown all registered instances, transitioning from either STARTED or STOPPED to SHUTDOWN.
* <p>
* If any instance fails to shutdown, the rest of the instances will still be shut down,
* so that the overall status is SHUTDOWN.
*/
@Override
public synchronized void shutdown()
throws LifecycleException
{
LifecycleException ex = null;
try
{
stop();
}
catch ( LifecycleException e )
{
ex = e;
}
if ( status == LifecycleStatus.STOPPED )
{
status = changedStatus( this, status, LifecycleStatus.SHUTTING_DOWN );
for ( int i = instances.size() - 1; i >= 0; i-- )
{
LifecycleInstance lifecycleInstance = instances.get( i );
try
{
lifecycleInstance.shutdown();
}
catch ( LifecycleException e )
{
if ( ex != null )
{
ex.addSuppressed( e );
}
else
{
ex = e;
}
}
}
status = changedStatus( this, status, LifecycleStatus.SHUTDOWN );
if ( ex != null )
{
throw ex;
}
}
}
/**
* Add a new Lifecycle instance. It will immediately be transitioned
* to the state of this LifeSupport.
*
* @param instance the Lifecycle instance to add
* @param <T> type of the instance
* @return the instance itself
* @throws LifecycleException if the instance could not be transitioned properly
*/
public synchronized <T extends Lifecycle> T add( T instance )
throws LifecycleException
{
assert instance != null;
assert notAlreadyAdded( instance );
LifecycleInstance newInstance = new LifecycleInstance( instance );
List<LifecycleInstance> tmp = new ArrayList<>( instances );
tmp.add( newInstance );
instances = tmp;
bringToState( newInstance );
return instance;
}
private boolean notAlreadyAdded( Lifecycle instance )
{
for ( LifecycleInstance candidate : instances )
{
if ( candidate.instance == instance )
{
throw new IllegalStateException( instance + " already added", candidate.addedWhere );
}
}
return true;
}
public synchronized boolean remove( Lifecycle instance )
{
for ( int i = 0; i < instances.size(); i++ )
{
if ( instances.get( i ).isInstance( instance ) )
{
List<LifecycleInstance> tmp = new ArrayList<>( instances );
LifecycleInstance lifecycleInstance = tmp.remove( i );
lifecycleInstance.shutdown();
instances = tmp;
return true;
}
}
return false;
}
public Iterable<Lifecycle> getLifecycleInstances()
{
return instances.stream().map( ( l ) -> l.instance ).collect( toList() );
}
/**
* Shutdown and throw away all the current instances. After
* this you can add new instances. This method does not change
* the status of the LifeSupport (i.e. if it was started it will remain started)
*/
public synchronized void clear()
{
for ( LifecycleInstance instance : instances )
{
instance.shutdown();
}
instances = new ArrayList<>( );
}
public LifecycleStatus getStatus()
{
return status;
}
public synchronized void addLifecycleListener( LifecycleListener listener )
{
listeners.add( listener );
}
private void bringToState( LifecycleInstance instance )
throws LifecycleException
{
switch ( status )
{
case STARTED:
instance.start();
break;
case STOPPED:
instance.init();
break;
default:
break;
}
}
private LifecycleStatus changedStatus( Lifecycle instance,
LifecycleStatus oldStatus,
LifecycleStatus newStatus
)
{
for ( LifecycleListener listener : listeners )
{
listener.notifyStatusChanged( instance, oldStatus, newStatus );
}
return newStatus;
}
public boolean isRunning()
{
return status == LifecycleStatus.STARTED;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder( );
toString( 0, sb );
return sb.toString();
}
private void toString( int indent, StringBuilder sb )
{
for ( int i = 0; i < indent; i++ )
{
sb.append( ' ' );
}
sb.append( "Lifecycle status:" + status.name() ).append( '\n' );
for ( LifecycleInstance instance : instances )
{
if ( instance.instance instanceof LifeSupport )
{
((LifeSupport) instance.instance).toString( indent + 3, sb );
}
else
{
for ( int i = 0; i < indent + 3; i++ )
{
sb.append( ' ' );
}
sb.append( instance.toString() ).append( '\n' );
}
}
}
private class LifecycleInstance
implements Lifecycle
{
Lifecycle instance;
LifecycleStatus currentStatus = LifecycleStatus.NONE;
Exception addedWhere;
private LifecycleInstance( Lifecycle instance )
{
this.instance = instance;
assert trackInstantiationStackTrace();
}
private boolean trackInstantiationStackTrace()
{
addedWhere = new Exception();
return true;
}
@Override
public void init()
throws LifecycleException
{
if ( currentStatus == LifecycleStatus.NONE )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.INITIALIZING );
try
{
instance.init();
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STOPPED );
}
catch ( Throwable e )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.NONE );
try
{
instance.shutdown();
}
catch ( Throwable se )
{
LifecycleException lifecycleException = new LifecycleException( "Exception during graceful " +
"attempt to shutdown partially initialized component. Please use non suppressed" +
" exception to see original component failure.", se );
e.addSuppressed( lifecycleException );
}
if ( e instanceof LifecycleException )
{
throw (LifecycleException) e;
}
throw new LifecycleException( instance, LifecycleStatus.NONE, LifecycleStatus.STOPPED, e );
}
}
}
@Override
public void start()
throws LifecycleException
{
if ( currentStatus == LifecycleStatus.NONE )
{
init();
}
if ( currentStatus == LifecycleStatus.STOPPED )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STARTING );
try
{
instance.start();
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STARTED );
}
catch ( Throwable e )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STOPPED );
try
{
instance.stop();
}
catch ( Throwable se )
{
LifecycleException lifecycleException = new LifecycleException( "Exception during graceful " +
"attempt to stop partially started component. Please use non suppressed" +
" exception to see original component failure.", se );
e.addSuppressed( lifecycleException );
}
if ( e instanceof LifecycleException )
{
throw (LifecycleException) e;
}
throw new LifecycleException( instance, LifecycleStatus.STOPPED, LifecycleStatus.STARTED, e );
}
}
}
@Override
public void stop()
throws LifecycleException
{
if ( currentStatus == LifecycleStatus.STARTED )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STOPPING );
try
{
instance.stop();
}
catch ( LifecycleException e )
{
throw e;
}
catch ( Throwable e )
{
throw new LifecycleException( instance, LifecycleStatus.STARTED, LifecycleStatus.STOPPED, e );
}
finally
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.STOPPED );
}
}
}
@Override
public void shutdown()
throws LifecycleException
{
if ( currentStatus == LifecycleStatus.STARTED )
{
stop();
}
if ( currentStatus == LifecycleStatus.STOPPED )
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.SHUTTING_DOWN );
try
{
instance.shutdown();
}
catch ( LifecycleException e )
{
throw e;
}
catch ( Throwable e )
{
throw new LifecycleException( instance, LifecycleStatus.STOPPED, LifecycleStatus.SHUTTING_DOWN, e );
}
finally
{
currentStatus = changedStatus( instance, currentStatus, LifecycleStatus.SHUTDOWN );
}
}
}
@Override
public String toString()
{
return instance.toString() + ": " + currentStatus.name();
}
public boolean isInstance( Lifecycle instance )
{
return this.instance == instance;
}
}
}