16
16
import java .util .concurrent .locks .Condition ;
17
17
import java .util .concurrent .locks .ReentrantLock ;
18
18
import java .util .function .Consumer ;
19
- import java .util .logging .Level ;
20
19
21
20
import oracle .kubernetes .operator .logging .LoggingFacade ;
22
21
import oracle .kubernetes .operator .logging .LoggingFactory ;
@@ -56,6 +55,7 @@ public final class Fiber implements Runnable, Future<Void>, ComponentRegistry {
56
55
private Packet packet ;
57
56
58
57
public final Engine owner ;
58
+ private final Fiber parent ;
59
59
60
60
/**
61
61
* Is this thread suspended? 0=not suspended, 1=suspended.
@@ -148,7 +148,12 @@ public interface ExitCallback {
148
148
private static final ExitCallback PLACEHOLDER = () -> {};
149
149
150
150
Fiber (Engine engine ) {
151
+ this (engine , null );
152
+ }
153
+
154
+ Fiber (Engine engine , Fiber parent ) {
151
155
this .owner = engine ;
156
+ this .parent = parent ;
152
157
id = iotaGen .incrementAndGet ();
153
158
154
159
// if this is run from another fiber, then we naturally inherit its context
@@ -175,6 +180,10 @@ public void start(Step stepline, Packet packet, CompletionCallback completionCal
175
180
this .completionCallback = completionCallback ;
176
181
this .applyThrowable = null ;
177
182
183
+ if (LOGGER .isFineEnabled ()) {
184
+ LOGGER .fine ("{0} started" , new Object [] { getName () });
185
+ }
186
+
178
187
if (status .get () == NOT_COMPLETE ) {
179
188
owner .addRunnable (this );
180
189
}
@@ -213,20 +222,25 @@ public void resume(Packet resumePacket) {
213
222
*/
214
223
public void resume (Packet resumePacket , CompletionCallback callback ) {
215
224
if (status .get () == NOT_COMPLETE ) {
225
+
226
+ if (LOGGER .isFineEnabled ()) {
227
+ LOGGER .fine ("{0} resumed" , new Object [] { getName () });
228
+ }
229
+
216
230
lock .lock ();
217
231
try {
218
232
if (callback != null ) {
219
233
setCompletionCallback (callback );
220
234
}
221
- if (LOGGER .isLoggable ( Level . FINE )) {
222
- LOGGER .fine ("{0} resuming. Will have suspendedCount={1}" , new Object [] { getName (), suspendedCount - 1 });
235
+ if (LOGGER .isFinerEnabled ( )) {
236
+ LOGGER .finer ("{0} resuming. Will have suspendedCount={1}" , new Object [] { getName (), suspendedCount - 1 });
223
237
}
224
238
packet = resumePacket ;
225
239
if (--suspendedCount == 0 ) {
226
240
owner .addRunnable (this );
227
241
} else {
228
- if (LOGGER .isLoggable ( Level . FINE )) {
229
- LOGGER .fine ("{0} taking no action on resume because suspendedCount != 0: {1}" ,
242
+ if (LOGGER .isFinerEnabled ( )) {
243
+ LOGGER .finer ("{0} taking no action on resume because suspendedCount != 0: {1}" ,
230
244
new Object [] { getName (), suspendedCount });
231
245
}
232
246
}
@@ -245,6 +259,11 @@ public void terminate(Throwable t, Packet packet) {
245
259
if (t == null ) {
246
260
throw new IllegalArgumentException ();
247
261
}
262
+
263
+ if (LOGGER .isFineEnabled ()) {
264
+ LOGGER .fine ("{0} terminated" , new Object [] { getName () });
265
+ }
266
+
248
267
lock .lock ();
249
268
try {
250
269
if (suspendedCount <= 0 ) {
@@ -263,7 +282,7 @@ public void terminate(Throwable t, Packet packet) {
263
282
* @return Child fiber
264
283
*/
265
284
public Fiber createChildFiber () {
266
- Fiber child = owner .createFiber ( );
285
+ Fiber child = owner .createChildFiber ( this );
267
286
268
287
synchronized (this ) {
269
288
if (children == null ) {
@@ -289,6 +308,10 @@ public boolean cancel(boolean mayInterrupt) {
289
308
return false ;
290
309
}
291
310
311
+ if (LOGGER .isFineEnabled ()) {
312
+ LOGGER .fine ("{0} cancelled" , new Object [] { getName () });
313
+ }
314
+
292
315
// synchronized(this) is used as Thread running Fiber will be holding lock
293
316
synchronized (this ) {
294
317
if (mayInterrupt ) {
@@ -375,13 +398,15 @@ public Void get(long timeout, TimeUnit unit) throws InterruptedException, Execut
375
398
private boolean suspend (Holder <Boolean > isRequireUnlock , Consumer <Fiber > onExit ) {
376
399
suspendedCount ++;
377
400
378
- if (LOGGER .isLoggable ( Level . FINE )) {
379
- LOGGER .fine ("{0} suspending. Will have suspendedCount={1}" , new Object [] { getName (), suspendedCount });
401
+ if (LOGGER .isFinerEnabled ( )) {
402
+ LOGGER .finer ("{0} suspending. Will have suspendedCount={1}" , new Object [] { getName (), suspendedCount });
380
403
if (suspendedCount > 1 ) {
381
- LOGGER .fine (
404
+ LOGGER .finer (
382
405
"WARNING - {0} suspended more than resumed. Will require more than one resume to actually resume this fiber." ,
383
406
getName ());
384
407
}
408
+ } else if (LOGGER .isFineEnabled ()) {
409
+ LOGGER .fine ("{0} suspending" , new Object [] { getName () });
385
410
}
386
411
387
412
if (onExit != null ) {
@@ -457,6 +482,11 @@ public void run() {
457
482
// Trigger exitCallback
458
483
synchronized (this ) {
459
484
if (exitCallback != null && exitCallback != PLACEHOLDER ) {
485
+
486
+ if (LOGGER .isFineEnabled ()) {
487
+ LOGGER .fine ("{0} invoking exit callback" , new Object [] { getName () });
488
+ }
489
+
460
490
exitCallback .onExit ();
461
491
}
462
492
exitCallback = PLACEHOLDER ;
@@ -474,7 +504,7 @@ private void completionCheck() {
474
504
// throwable
475
505
int s = status .get ();
476
506
if (s == CANCELLED || (s == NOT_COMPLETE && (applyThrowable != null || (next == null && suspendedCount == 0 )))) {
477
- if (LOGGER .isLoggable ( Level . FINE )) {
507
+ if (LOGGER .isFineEnabled ( )) {
478
508
LOGGER .fine ("{0} completed" , getName ());
479
509
}
480
510
boolean isDone = status .compareAndSet (NOT_COMPLETE , DONE );
@@ -507,8 +537,8 @@ private boolean doRun(Step next) {
507
537
// currentThread is protected by the monitor for this fiber so
508
538
// that it is accessible to cancel() even when the lock is held
509
539
currentThread = Thread .currentThread ();
510
- if (LOGGER .isLoggable ( Level . FINE )) {
511
- LOGGER .fine ("Thread entering _doRun(): {0}" , currentThread );
540
+ if (LOGGER .isFinerEnabled ( )) {
541
+ LOGGER .finer ("Thread entering _doRun(): {0}" , currentThread );
512
542
}
513
543
514
544
old = currentThread .getContextClassLoader ();
@@ -535,8 +565,8 @@ private boolean doRun(Step next) {
535
565
// tracks this state
536
566
Thread thread = Thread .currentThread ();
537
567
thread .setContextClassLoader (old );
538
- if (LOGGER .isLoggable ( Level . FINE )) {
539
- LOGGER .fine ("Thread leaving _doRun(): {0}" , thread );
568
+ if (LOGGER .isFinerEnabled ( )) {
569
+ LOGGER .finer ("Thread leaving _doRun(): {0}" , thread );
540
570
}
541
571
}
542
572
} finally {
@@ -563,8 +593,8 @@ private boolean _doRun(Holder<Boolean> isRequireUnlock) {
563
593
return false ;
564
594
}
565
595
566
- if (LOGGER .isLoggable ( Level . FINE )) {
567
- LOGGER .fine ("{0} {1}.apply({2})" , new Object [] { getName (), next ,
596
+ if (LOGGER .isFinerEnabled ( )) {
597
+ LOGGER .finer ("{0} {1}.apply({2})" , new Object [] { getName (), next ,
568
598
packet != null ? "Packet@" + Integer .toHexString (packet .hashCode ()) : "null" });
569
599
}
570
600
@@ -576,8 +606,8 @@ private boolean _doRun(Holder<Boolean> isRequireUnlock) {
576
606
return false ;
577
607
}
578
608
579
- if (LOGGER .isLoggable ( Level . FINE )) {
580
- LOGGER .fine ("{0} returned with {1 }" , new Object [] { getName (), na });
609
+ if (LOGGER .isFineEnabled ( )) {
610
+ LOGGER .fine ("{0} {1} returned with {2 }" , new Object [] { getName (), next , na });
581
611
}
582
612
583
613
// If resume is called before suspend, then make sure
@@ -615,7 +645,17 @@ private boolean isReady() {
615
645
}
616
646
617
647
private String getName () {
618
- return "engine-" + owner .id + "fiber-" + id ;
648
+ StringBuilder sb = new StringBuilder ();
649
+ if (parent != null ) {
650
+ sb .append (parent .getName ());
651
+ sb .append ("-child-" );
652
+ } else {
653
+ sb .append ("engine-" );
654
+ sb .append (owner .id );
655
+ sb .append ("-fiber-" );
656
+ }
657
+ sb .append (id );
658
+ return sb .toString ();
619
659
}
620
660
621
661
@ Override
@@ -666,6 +706,10 @@ public boolean cancelAndExitCallback(boolean mayInterrupt, ExitCallback exitCall
666
706
// Mark fiber as cancelled, if not already done
667
707
status .compareAndSet (NOT_COMPLETE , CANCELLED );
668
708
709
+ if (LOGGER .isFineEnabled ()) {
710
+ LOGGER .fine ("{0} cancelled" , new Object [] { getName () });
711
+ }
712
+
669
713
synchronized (this ) {
670
714
if (currentThread != null ) {
671
715
if (mayInterrupt ) {
0 commit comments