11
11
import java .util .Map ;
12
12
import java .util .Optional ;
13
13
import java .util .concurrent .ThreadFactory ;
14
+ import java .util .concurrent .TimeUnit ;
14
15
import java .util .concurrent .atomic .AtomicBoolean ;
15
16
import java .util .function .Consumer ;
16
17
import javax .annotation .Nonnull ;
28
29
import io .kubernetes .client .util .Watchable ;
29
30
import oracle .kubernetes .operator .TuningParameters .WatchTuning ;
30
31
import oracle .kubernetes .operator .builders .WatchBuilder ;
32
+ import oracle .kubernetes .operator .calls .CallResponse ;
31
33
import oracle .kubernetes .operator .helpers .CallBuilder ;
34
+ import oracle .kubernetes .operator .helpers .DomainPresenceInfo ;
32
35
import oracle .kubernetes .operator .helpers .KubernetesUtils ;
33
36
import oracle .kubernetes .operator .helpers .LegalNames ;
34
37
import oracle .kubernetes .operator .helpers .PodHelper ;
35
38
import oracle .kubernetes .operator .helpers .ResponseStep ;
36
39
import oracle .kubernetes .operator .logging .LoggingFacade ;
37
40
import oracle .kubernetes .operator .logging .LoggingFactory ;
38
41
import oracle .kubernetes .operator .logging .MessageKeys ;
42
+ import oracle .kubernetes .operator .steps .DefaultResponseStep ;
39
43
import oracle .kubernetes .operator .watcher .WatchListener ;
44
+ import oracle .kubernetes .operator .work .NextAction ;
45
+ import oracle .kubernetes .operator .work .Packet ;
40
46
import oracle .kubernetes .operator .work .Step ;
41
47
48
+ import static oracle .kubernetes .operator .ProcessingConstants .SERVER_NAME ;
49
+ import static oracle .kubernetes .operator .logging .MessageKeys .EXECUTE_MAKE_RIGHT_DOMAIN ;
50
+ import static oracle .kubernetes .operator .logging .MessageKeys .LOG_WAITING_COUNT ;
51
+
42
52
/**
43
53
* Watches for changes to pods.
44
54
*/
@@ -305,6 +315,8 @@ public Step waitForDelete(V1Pod pod, Step next) {
305
315
306
316
private abstract static class WaitForPodStatusStep extends WaitForReadyStep <V1Pod > {
307
317
318
+ public static final int RECHECK_DEBUG_COUNT = 10 ;
319
+
308
320
private WaitForPodStatusStep (V1Pod pod , Step next ) {
309
321
super (pod , next );
310
322
}
@@ -322,6 +334,67 @@ V1ObjectMeta getMetadata(V1Pod pod) {
322
334
Step createReadAsyncStep (String name , String namespace , String domainUid , ResponseStep <V1Pod > responseStep ) {
323
335
return new CallBuilder ().readPodAsync (name , namespace , domainUid , responseStep );
324
336
}
337
+
338
+ protected DefaultResponseStep <V1Pod > resumeIfReady (Callback callback ) {
339
+ return new DefaultResponseStep <>(getNext ()) {
340
+ @ Override
341
+ public NextAction onSuccess (Packet packet , CallResponse <V1Pod > callResponse ) {
342
+
343
+ DomainPresenceInfo info = packet .getSpi (DomainPresenceInfo .class );
344
+ String serverName = (String )packet .get (SERVER_NAME );
345
+ String resource = initialResource == null ? resourceName : getMetadata (initialResource ).getName ();
346
+ if ((info != null ) && (callResponse != null )) {
347
+ Optional .ofNullable (callResponse .getResult ()).ifPresent (result ->
348
+ info .setServerPodFromEvent (getPodLabel (result ), result ));
349
+ if (onReadNotFoundForCachedResource (getServerPod (info , serverName ), isNotFoundOnRead (callResponse ))) {
350
+ LOGGER .fine (EXECUTE_MAKE_RIGHT_DOMAIN , serverName , callback .getRecheckCount ());
351
+ removeCallback (resource , callback );
352
+ return doNext (NEXT_STEP_FACTORY .createMakeDomainRightStep (callback , info , getNext ()), packet );
353
+ }
354
+ }
355
+
356
+ if (isReady (callResponse .getResult ()) || callback .didResumeFiber ()) {
357
+ callback .proceedFromWait (callResponse .getResult ());
358
+ return null ;
359
+ }
360
+
361
+ if (shouldWait ()) {
362
+ if ((callback .getRecheckCount () % RECHECK_DEBUG_COUNT ) == 0 ) {
363
+ LOGGER .fine (LOG_WAITING_COUNT , serverName , callback .getRecheckCount ());
364
+ }
365
+ // Watch backstop recheck count is less than or equal to the configured recheck count, delay.
366
+ return doDelay (createReadAndIfReadyCheckStep (callback ), packet ,
367
+ getWatchBackstopRecheckDelaySeconds (), TimeUnit .SECONDS );
368
+ } else {
369
+ LOGGER .fine (EXECUTE_MAKE_RIGHT_DOMAIN , serverName , callback .getRecheckCount ());
370
+ removeCallback (resource , callback );
371
+ // Watch backstop recheck count is more than configured recheck count, proceed to make-right step.
372
+ return doNext (NEXT_STEP_FACTORY .createMakeDomainRightStep (callback , info , getNext ()), packet );
373
+ }
374
+ }
375
+
376
+ private String getPodLabel (V1Pod pod ) {
377
+ return Optional .ofNullable (pod )
378
+ .map (V1Pod ::getMetadata )
379
+ .map (V1ObjectMeta ::getLabels )
380
+ .map (m -> m .get (LabelConstants .SERVERNAME_LABEL ))
381
+ .orElse (null );
382
+ }
383
+
384
+ private V1Pod getServerPod (DomainPresenceInfo info , String serverName ) {
385
+ return Optional .ofNullable (serverName ).map (info ::getServerPod ).orElse (null );
386
+ }
387
+
388
+ private boolean isNotFoundOnRead (CallResponse callResponse ) {
389
+ return callResponse .getResult () == null ;
390
+ }
391
+
392
+ private boolean shouldWait () {
393
+ return callback .incrementAndGetRecheckCount () <= getWatchBackstopRecheckCount ();
394
+ }
395
+ };
396
+ }
397
+
325
398
}
326
399
327
400
private class WaitForPodReadyStep extends WaitForPodStatusStep {
@@ -360,13 +433,25 @@ protected void removeCallback(String podName, Consumer<V1Pod> callback) {
360
433
protected void logWaiting (String name ) {
361
434
LOGGER .fine (MessageKeys .WAITING_FOR_POD_READY , name );
362
435
}
436
+
437
+ @ Override
438
+ protected boolean onReadNotFoundForCachedResource (V1Pod cachedPod , boolean isNotFoundOnRead ) {
439
+ // Return true if cached pod is not null but pod not found in explicit read, false otherwise.
440
+ return (cachedPod != null ) && isNotFoundOnRead ;
441
+ }
442
+
363
443
}
364
444
365
445
private class WaitForPodDeleteStep extends WaitForPodStatusStep {
366
446
private WaitForPodDeleteStep (V1Pod pod , Step next ) {
367
447
super (pod , next );
368
448
}
369
449
450
+ @ Override
451
+ protected boolean onReadNotFoundForCachedResource (V1Pod cachedPod , boolean isNotFoundOnRead ) {
452
+ return false ;
453
+ }
454
+
370
455
// A pod is considered deleted when reading its value from Kubernetes returns null.
371
456
@ Override
372
457
protected boolean isReady (V1Pod result ) {
0 commit comments