/
CellNucleus.java
1172 lines (1061 loc) · 39.5 KB
/
CellNucleus.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dmg.cells.nucleus;
import com.google.common.base.Throwables;
import com.google.common.collect.Queues;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import dmg.util.Pinboard;
import dmg.util.logback.FilterThresholds;
import dmg.util.logback.RootFilterThresholds;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.consumingIterable;
import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
import static org.dcache.util.MathUtils.addWithInfinity;
import static org.dcache.util.MathUtils.subWithInfinity;
/**
*
*
* @author Patrick Fuhrmann
* @version 0.1, 15 Feb 1998
*/
public class CellNucleus implements ThreadFactory
{
private final static Logger LOGGER =
LoggerFactory.getLogger(CellNucleus.class);
private static final int PINBOARD_DEFAULT_SIZE = 200;
private static final int INITIAL = 0;
private static final int ACTIVE = 1;
private static final int REMOVING = 2;
private static final int DEAD = 3;
private static CellGlue __cellGlue;
private final String _cellName;
private final String _cellType;
private final ThreadGroup _threads;
private final AtomicInteger _threadCounter = new AtomicInteger();
private final Cell _cell;
private final Date _creationTime = new Date();
private final AtomicInteger _state = new AtomicInteger(INITIAL);
// have to be synchronized map
private final Map<UOID, CellLock> _waitHash = new HashMap<>();
private String _cellClass;
private volatile ExecutorService _messageExecutor;
private final AtomicInteger _eventQueueSize = new AtomicInteger();
/**
* Timer for periodic low-priority maintenance tasks. Shared among
* all cell instances. Since a Timer is single-threaded,
* it is important that the timer is not used for long-running or
* blocking tasks, nor for time critical tasks.
*/
private static final Timer _timer = new Timer("Cell maintenance task timer", true);
/**
* Task for calling the Cell nucleus message timeout mechanism.
*/
private TimerTask _timeoutTask;
private Pinboard _pinboard;
private FilterThresholds _loggingThresholds;
private final Queue<Runnable> _deferredTasks = Queues.synchronizedQueue(new ArrayDeque<Runnable>());
public CellNucleus(Cell cell, String name) {
this(cell, name, "Generic");
}
public CellNucleus(Cell cell, String name, String type)
{
setPinboard(new Pinboard(PINBOARD_DEFAULT_SIZE));
if (__cellGlue == null) {
//
// the cell gluon hasn't yet been created
// (we insist in creating a SystemCell first.)
//
if (cell instanceof SystemCell) {
__cellGlue = new CellGlue(name);
_cellName = "System";
_cellType = "System";
__cellGlue.setSystemNucleus(this);
} else {
throw new
IllegalArgumentException("System must be first Cell");
}
} else {
//
// we don't accept more then one System.cells
//
if (cell instanceof SystemCell) {
throw new
IllegalArgumentException("System already exists");
} else {
String cellName = name.replace('@', '+');
if ((cellName == null) ||
(cellName.equals(""))) {
cellName = "*";
}
if (cellName.charAt(cellName.length() - 1) == '*') {
if (cellName.length() == 1) {
cellName = "$-"+getUnique();
} else {
cellName = cellName.substring(0,cellName.length()-1)+
"-"+getUnique();
}
}
_cellName = cellName;
_cellType = type;
}
}
_cell = cell;
_cellClass = _cell.getClass().getName();
/* Instantiate management component for log filtering.
*/
CellNucleus parentNucleus =
CellNucleus.getLogTargetForCell(MDC.get(CDC.MDC_CELL));
FilterThresholds parentThresholds =
(parentNucleus.isSystemNucleus() || parentNucleus == this)
? RootFilterThresholds.getInstance()
: parentNucleus.getLoggingThresholds();
setLoggingThresholds(new FilterThresholds(parentThresholds));
_threads = new ThreadGroup(__cellGlue.getMasterThreadGroup(), _cellName + "-threads");
_messageExecutor =
new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
this);
LOGGER.info("Created {}", name);
}
/**
* Start the timeout task.
*
* Cells rely on periodic calls to executeMaintenanceTasks to implement
* message timeouts. This method starts a task which calls
* executeMaintenanceTasks every 20 seconds.
*/
private void startTimeoutTask()
{
if (_timeoutTask != null) {
throw new IllegalStateException("Timeout task is already running");
}
_timeoutTask = new TimerTask() {
@Override
public void run()
{
try (CDC ignored = CDC.reset(CellNucleus.this)) {
try {
executeMaintenanceTasks();
} catch (Throwable e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
}
}
};
_timer.schedule(_timeoutTask, 20000, 20000);
}
/**
* Returns the CellNucleus to which log messages tagged with a
* given cell are associated.
*/
public static CellNucleus getLogTargetForCell(String cell)
{
CellNucleus nucleus = null;
if (__cellGlue != null) {
if (cell != null) {
nucleus = __cellGlue.getCell(cell);
}
if (nucleus == null) {
nucleus = __cellGlue.getSystemNucleus();
}
}
return nucleus;
}
void setSystemNucleus(CellNucleus nucleus) {
__cellGlue.setSystemNucleus(nucleus);
}
boolean isSystemNucleus() {
return this == __cellGlue.getSystemNucleus();
}
public String getCellName() { return _cellName; }
public String getCellType() { return _cellType; }
public String getCellClass()
{
return _cellClass;
}
public void setCellClass(String cellClass)
{
_cellClass = cellClass;
}
public CellAddressCore getThisAddress() {
return new CellAddressCore(_cellName, __cellGlue.getCellDomainName());
}
public String getCellDomainName() {
return __cellGlue.getCellDomainName();
}
public List<String> getCellNames() { return __cellGlue.getCellNames(); }
public CellInfo getCellInfo(String name) {
return __cellGlue.getCellInfo(name);
}
public CellInfo getCellInfo() {
return __cellGlue.getCellInfo(getCellName());
}
public Map<String, Object> getDomainContext()
{
return __cellGlue.getCellContext();
}
public Reader getDomainContextReader(String contextName)
throws FileNotFoundException {
Object o = __cellGlue.getCellContext(contextName);
if (o == null) {
throw new
FileNotFoundException("Context not found : " + contextName);
}
return new StringReader(o.toString());
}
public void setDomainContext(String contextName, Object context) {
__cellGlue.getCellContext().put(contextName, context);
}
public Object getDomainContext(String str) {
return __cellGlue.getCellContext(str);
}
public String [] [] getClassProviders() {
return __cellGlue.getClassProviders();
}
public synchronized void setClassProvider(String selection, String provider) {
__cellGlue.setClassProvider(selection, provider);
}
Cell getThisCell() { return _cell; }
CellInfo _getCellInfo() {
CellInfo info = new CellInfo();
info.setCellName(getCellName());
info.setDomainName(getCellDomainName());
info.setCellType(getCellType());
info.setCreationTime(_creationTime);
try {
info.setCellVersion(_cell.getCellVersion());
} catch(Exception e) {}
try {
info.setPrivateInfo(_cell.getInfo());
} catch(Exception e) {
info.setPrivateInfo("Not yet/No more available\n");
}
try {
info.setShortInfo(_cell.toString());
} catch(Exception e) {
info.setShortInfo("Not yet/No more available");
}
info.setCellClass(_cellClass);
try {
info.setEventQueueSize(getEventQueueSize());
info.setState(_state.get());
info.setThreadCount(_threads.activeCount());
} catch(Exception e) {
info.setEventQueueSize(0);
info.setState(0);
info.setThreadCount(0);
}
return info;
}
public void setLoggingThresholds(FilterThresholds thresholds)
{
_loggingThresholds = thresholds;
}
public FilterThresholds getLoggingThresholds()
{
return _loggingThresholds;
}
public synchronized void setPinboard(Pinboard pinboard)
{
_pinboard = pinboard;
}
public synchronized Pinboard getPinboard()
{
return _pinboard;
}
/**
* Executor used for incoming message delivery.
*/
public synchronized void setMessageExecutor(ExecutorService executor)
{
checkNotNull(executor);
int state = _state.get();
checkState(state != REMOVING && state != DEAD);
_messageExecutor.shutdown();
_messageExecutor = executor;
}
public void sendMessage(CellMessage msg,
boolean locally,
boolean remotely)
throws SerializationException,
NoRouteToCellException
{
if (!msg.isStreamMode()) {
msg.touch();
}
EventLogger.sendBegin(this, msg, "async");
try {
__cellGlue.sendMessage(this, msg, locally, remotely);
} finally {
EventLogger.sendEnd(msg);
}
}
/**
* Sends <code>envelope</code> and waits <code>timeout</code>
* milliseconds for an answer to arrive. The answer will bypass
* the ordinary queuing mechanism and will be delivered before any
* other asynchronous message. The answer need to have the
* getLastUOID set to the UOID of the message send with
* sendAndWait. If the answer does not arrive withing the specified
* time interval, the method returns <code>null</code> and the
* answer will be handled as if it was an ordinary asynchronous
* message.
*
* This method mostly exists for backwards compatibility. dCache code
* should use CellStub or CellEndpoint.
*
* @param envelope the cell message to be sent.
* @param timeout milliseconds to wait for an answer.
* @return the answer or null if the timeout was reached.
* @throws SerializationException if the payload object of this
* message is not serializable.
* @throws NoRouteToCellException if the destination
* could not be reached.
* @throws ExecutionException if an exception was returned.
*/
public CellMessage sendAndWait(CellMessage envelope, long timeout)
throws SerializationException, NoRouteToCellException, InterruptedException, ExecutionException
{
final SettableFuture<CellMessage> future = SettableFuture.create();
sendMessage(envelope, true, true,
new CellMessageAnswerable()
{
@Override
public void answerArrived(CellMessage request, CellMessage answer)
{
future.set(answer);
}
@Override
public void exceptionArrived(CellMessage request, Exception exception)
{
future.setException(exception);
}
@Override
public void answerTimedOut(CellMessage request)
{
future.set(null);
}
}, sameThreadExecutor(), timeout);
try {
return future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
return null;
} catch (ExecutionException e) {
Throwables.propagateIfInstanceOf(e.getCause(), NoRouteToCellException.class);
Throwables.propagateIfInstanceOf(e.getCause(), SerializationException.class);
throw e;
}
}
public Map<UOID,CellLock > getWaitQueue()
{
synchronized (_waitHash) {
return new HashMap<>(_waitHash);
}
}
private int executeMaintenanceTasks()
{
Collection<CellLock> expired = new ArrayList<>();
long now = System.currentTimeMillis();
int size;
synchronized (_waitHash) {
Iterator<CellLock> i = _waitHash.values().iterator();
while (i.hasNext()) {
CellLock lock = i.next();
if (lock.getTimeout() < now) {
expired.add(lock);
i.remove();
}
}
size = _waitHash.size();
}
//
// _waitHash can't be used here. Otherwise
// we will end up in a deadlock (NO LOCKS WHILE CALLING CALLBACKS)
//
for (final CellLock lock: expired) {
try (CDC ignored = lock.getCdc().restore()) {
try {
lock.getExecutor().execute(new Runnable()
{
@Override
public void run()
{
CellMessage envelope = lock.getMessage();
try {
lock.getCallback().answerTimedOut(envelope);
EventLogger.sendEnd(envelope);
} catch (RejectedExecutionException e) {
/* May happen when the callback itself tries to schedule the call
* on an executor. Put the request back and let it time out.
*/
synchronized (_waitHash) {
_waitHash.put(envelope.getUOID(), lock);
}
LOGGER.warn("Failed to invoke callback: {}", e.toString());
}
}
});
} catch (RejectedExecutionException e) {
/* Put it back and deal with it later.
*/
synchronized (_waitHash) {
_waitHash.put(lock.getMessage().getUOID(), lock);
}
LOGGER.warn("Failed to invoke callback: {}", e.toString());
} catch (RuntimeException e) {
/* Don't let a problem in the callback prevent us from
* expiring all messages.
*/
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
}
}
// Execute delayed operations
for (Runnable task : consumingIterable(_deferredTasks)) {
task.run();
}
return size;
}
/**
* Sends <code>msg</code>.
*
* The <code>callback</code> argument specifies an object which is informed
* as soon as an has answer arrived or if the timeout has expired.
*
* The callback is run in the supplied executor. The executor may
* execute the callback inline, but such an executor must only be
* used if the callback is non-blocking, and the callback should
* refrain from CPU heavy operations. Care should be taken that
* the executor isn't blocked by tasks waiting for the callback;
* such tasks could lead to a deadlock.
*
* @param msg the cell message to be sent.
* @param local whether to attempt delivery to cells in the same domain
* @param remote whether to attempt delivery to cells in other domains
* @param callback specifies an object class which will be informed
* as soon as the message arrives.
* @param executor the executor to run the callback in
* @param timeout is the timeout in msec.
* @exception SerializationException if the payload object of this
* message is not serializable.
*/
public void sendMessage(final CellMessage msg,
boolean local,
boolean remote,
final CellMessageAnswerable callback,
Executor executor,
long timeout)
throws SerializationException
{
if (!msg.isStreamMode()) {
msg.touch();
}
msg.setTtl(timeout);
final UOID uoid = msg.getUOID();
final CellLock lock = new CellLock(msg, callback, executor, timeout);
EventLogger.sendBegin(this, msg, "callback");
synchronized (_waitHash) {
_waitHash.put(uoid, lock);
}
try {
__cellGlue.sendMessage(this, msg, local, remote);
} catch (SerializationException e) {
synchronized (_waitHash) {
_waitHash.remove(uoid);
}
EventLogger.sendEnd(msg);
throw e;
} catch (final Exception e) {
synchronized (_waitHash) {
_waitHash.remove(uoid);
}
try {
executor.execute(new Runnable()
{
@Override
public void run()
{
try {
callback.exceptionArrived(msg, e);
EventLogger.sendEnd(msg);
} catch (RejectedExecutionException e) {
/* May happen when the callback itself tries to schedule the call
* on an executor. Put the request back and let it time out.
*/
synchronized (_waitHash) {
_waitHash.put(uoid, lock);
}
LOGGER.error("Failed to invoke callback: {}", e.toString());
}
}
});
} catch (RejectedExecutionException e1) {
/* Put it back and let it time out.
*/
synchronized (_waitHash) {
_waitHash.put(uoid, lock);
}
LOGGER.error("Failed to invoke callback: {}", e1.toString());
}
}
}
public void addCellEventListener(CellEventListener listener) {
__cellGlue.addCellEventListener(this, listener);
}
public void export() { __cellGlue.export(this); }
/**
*
* The kill method schedules the specified cell for deletion.
* The actual remove operation will run in a different
* thread. So on return of this method the cell may
* or may not be alive.
*/
public void kill() { __cellGlue.kill(this); }
/**
*
* The kill method schedules this Cell for deletion.
* The actual remove operation will run in a different
* thread. So on return of this method the cell may
* or may not be alive.
*/
public void kill(String cellName) throws IllegalArgumentException {
__cellGlue.kill(this, cellName);
}
/**
* List the threads of some cell to stdout. This is
* indended for diagnostic information.
*/
public void listThreadGroupOf(String cellName) {
__cellGlue.threadGroupList(cellName);
}
/**
* Print diagnostic information about currently running
* threads at warn level.
*/
public void threadGroupList() {
Thread[] threads = new Thread[_threads.activeCount()];
_threads.enumerate(threads);
for(Thread thread : threads) {
LOGGER.warn("Thread: {} [{}{}{}] ({}) {}",
thread.getName(),
(thread.isAlive() ? "A" : "-"),
(thread.isDaemon() ? "D" : "-"),
(thread.isInterrupted() ? "I" : "-"),
thread.getPriority(),
thread.getState());
for(StackTraceElement s : thread.getStackTrace()) {
LOGGER.warn(" {}", s);
}
}
}
/**
* Blocks until the given cell is dead.
*
* @throws InterruptedException if another thread interrupted the
* current thread before or while the current thread was waiting
* for a notification. The interrupted status of the current
* thread is cleared when this exception is thrown.
* @return True if the cell died, false in case of a timeout.
*/
public boolean join(String cellName)
throws InterruptedException
{
return __cellGlue.join(cellName, 0);
}
/**
* Blocks until the given cell is dead.
*
* @param timeout the maximum time to wait in milliseconds.
* @throws InterruptedException if another thread interrupted the
* current thread before or while the current thread was waiting
* for a notification. The interrupted status of the current
* thread is cleared when this exception is thrown.
* @return True if the cell died, false in case of a timeout.
*/
public boolean join(String cellName, long timeout)
throws InterruptedException
{
return __cellGlue.join(cellName, timeout);
}
/**
* Returns the non-daemon threads of a thread group.
*/
private Collection<Thread> getNonDaemonThreads(ThreadGroup group)
{
Thread[] threads = new Thread[group.activeCount()];
int count = group.enumerate(threads);
Collection<Thread> nonDaemonThreads = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Thread thread = threads[i];
if (!thread.isDaemon()) {
nonDaemonThreads.add(thread);
}
}
return nonDaemonThreads;
}
/**
* Waits for at most timeout milliseconds for the termination of a
* set of threads.
*
* @return true if all threads terminated, false otherwise
*/
private boolean joinThreads(Collection<Thread> threads, long timeout)
throws InterruptedException
{
long deadline = addWithInfinity(System.currentTimeMillis(), timeout);
for (Thread thread: threads) {
if (thread.isAlive()) {
long wait = subWithInfinity(deadline, System.currentTimeMillis());
if (wait <= 0) {
return false;
}
thread.join(wait);
if (thread.isAlive()) {
return false;
}
}
}
return true;
}
/**
* Interrupts a set of threads.
*/
private void killThreads(Collection<Thread> threads)
{
for (Thread thread: threads) {
if (thread.isAlive()) {
LOGGER.debug("killerThread : interrupting {}", thread.getName());
thread.interrupt();
}
}
}
private Runnable wrapLoggingContext(final Runnable runnable)
{
return new Runnable() {
@Override
public void run() {
try (CDC ignored = CDC.reset(CellNucleus.this)) {
runnable.run();
}
}
};
}
private <T> Callable<T> wrapLoggingContext(final Callable<T> callable)
{
return new Callable<T>() {
@Override
public T call() throws Exception {
try (CDC ignored = CDC.reset(CellNucleus.this)) {
return callable.call();
}
}
};
}
/**
* Submits a task for execution on the message thread.
*/
<T> Future<T> invokeOnMessageThread(Callable<T> task)
{
return _messageExecutor.submit(wrapLoggingContext(task));
}
void invokeLater(Runnable runnable)
{
_deferredTasks.add(runnable);
}
@Override @Nonnull
public Thread newThread(@Nonnull Runnable target)
{
return newThread(target, getCellName() + "-" + _threadCounter.getAndIncrement());
}
@Nonnull
public Thread newThread(@Nonnull Runnable target, @Nonnull String name)
{
return new Thread(_threads, wrapLoggingContext(target), name);
}
//
// package
//
Thread [] getThreads(String cellName) {
return __cellGlue.getThreads(cellName);
}
public ThreadGroup getThreadGroup() { return _threads; }
Thread [] getThreads() {
if (_threads == null) {
return new Thread[0];
}
int threadCount = _threads.activeCount();
Thread [] list = new Thread[threadCount];
int rc = _threads.enumerate(list);
if (rc == list.length) {
return list;
}
Thread [] ret = new Thread[rc];
System.arraycopy(list, 0, ret, 0, rc);
return ret;
}
int getUnique() { return __cellGlue.getUnique(); }
int getEventQueueSize()
{
return _eventQueueSize.get();
}
void addToEventQueue(MessageEvent ce) {
//
//
if (ce instanceof RoutedMessageEvent) {
if (_cell instanceof CellTunnel) {
//
// nothing to do (no transformation needed)
//
} else {
//
// originally this case has not been forseen,
// but it appeared rather useful. It allows alias
// cells which serves several different cells names.
// mainly useful for debuggin purposes (see alias
// package.
//
ce = new MessageEvent(ce.getMessage());
}
}
CellMessage msg = ce.getMessage();
LOGGER.trace("addToEventQueue : message arrived : {}", msg);
CellLock lock;
synchronized (_waitHash) {
lock = _waitHash.remove(msg.getLastUOID());
}
if (lock != null) {
//
// we were waiting for you (sync or async)
//
LOGGER.trace("addToEventQueue : lock found for : {}", msg);
try {
_eventQueueSize.incrementAndGet();
lock.getExecutor().execute(new CallbackTask(lock, msg));
} catch (RejectedExecutionException e) {
_eventQueueSize.decrementAndGet();
/* Put it back; the timeout handler will eventually take care of it.
*/
synchronized (_waitHash) {
_waitHash.put(msg.getLastUOID(), lock);
}
LOGGER.error("Message queue overflow. Dropping {}", ce);
}
} else {
try {
EventLogger.queueBegin(ce);
_eventQueueSize.incrementAndGet();
_messageExecutor.execute(new DeliverMessageTask(ce));
} catch (RejectedExecutionException e) {
EventLogger.queueEnd(ce);
_eventQueueSize.decrementAndGet();
LOGGER.error("Message queue overflow. Dropping {}", ce);
}
}
}
public void start()
{
checkState(_state.compareAndSet(INITIAL, ACTIVE));
//
// make ourself known to the world
//
__cellGlue.addCell(_cellName, this);
startTimeoutTask();
}
void shutdown(KillEvent event)
{
try (CDC ignored = CDC.reset(CellNucleus.this)) {
LOGGER.trace("Received {}", event);
checkState(_state.compareAndSet(INITIAL, REMOVING) || _state.compareAndSet(ACTIVE, REMOVING));
/* Shut down message executor.
*/
ExecutorService executor;
synchronized (this) {
executor = _messageExecutor;
}
if (!MoreExecutors.shutdownAndAwaitTermination(executor, 2, TimeUnit.SECONDS)) {
LOGGER.warn("Failed to flush message queue during shutdown.");
}
/* Stop executing deferred tasks.
*/
if (_timeoutTask != null) {
_timeoutTask.cancel();
}
/* Shut down cell.
*/
try {
_cell.prepareRemoval(event);
} catch (Throwable e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
/* Shut down remaining threads.
*/
LOGGER.debug("Waiting for all threads in {} to finish", _threads);
try {
Collection<Thread> threads = getNonDaemonThreads(_threads);
/* Some threads shut down asynchronously. Give them
* one second before we start to kill them.
*/
while (!joinThreads(threads, 1000)) {
killThreads(threads);
}
_threads.destroy();
} catch (IllegalThreadStateException e) {
_threads.setDaemon(true);
} catch (InterruptedException e) {
LOGGER.warn("Interrupted while waiting for threads");
}
/* Declare the cell as dead.
*/
__cellGlue.destroy(CellNucleus.this);
_state.set(DEAD);
}
}
////////////////////////////////////////////////////////////
//
// create new cell by different arguments
// String, String [], Socket
// can choose between systemLoader only or
// Domain loader.
//
public Cell createNewCell(String cellClass,
String cellName,
String cellArgs,
boolean systemOnly)
throws ClassNotFoundException,
NoSuchMethodException,
SecurityException,
InstantiationException,
InvocationTargetException,
IllegalAccessException,
ClassCastException
{
try {
Object [] args = new Object[1];
args[0] = cellArgs;
return __cellGlue._newInstance(
cellClass, cellName, args, systemOnly);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
throw e;
}
}
public Class<?> loadClass(String className) throws ClassNotFoundException
{
return __cellGlue.loadClass(className);
}
public Cell createNewCell(String className,
String cellName,
String [] argsClassNames,
Object [] args)
throws ClassNotFoundException,
NoSuchMethodException,
InstantiationException,
IllegalAccessException,
InvocationTargetException,
ClassCastException
{
if (argsClassNames == null) {
return __cellGlue._newInstance(
className, cellName, args, false);
} else {
return __cellGlue._newInstance(