@@ -505,49 +505,58 @@ private static void scheduleDomainStatusUpdating(DomainPresenceInfo info) {
505505
506506 AtomicInteger unchangedCount = new AtomicInteger (0 );
507507 AtomicReference <ScheduledFuture <?>> statusUpdater = info .getStatusUpdater ();
508- ScheduledFuture <?> existing = statusUpdater .get ();
509- if (existing == null || !validateExisting (initialShortDelay , existing )) {
510- Runnable command = new Runnable () {
511- public void run () {
512- Runnable r = this ; // resolve visibility
513- Packet packet = new Packet ();
514- packet .getComponents ().put (ProcessingConstants .DOMAIN_COMPONENT_NAME , Component .createFor (info , version , config ));
515- Step strategy = DomainStatusUpdater .createStatusStep (statusUpdateTimeoutSeconds , null );
516- domainUpdaters .startFiberIfNoCurrentFiber (domainUID , strategy , packet , new CompletionCallback () {
517- @ Override
518- public void onCompletion (Packet packet ) {
519- Boolean isStatusUnchanged = (Boolean ) packet .get (ProcessingConstants .STATUS_UNCHANGED );
520- long delay = initialShortDelay ;
521- if (Boolean .TRUE .equals (isStatusUnchanged )) {
522- if (unchangedCount .incrementAndGet () > unchangedCountToDelayStatusRecheck ) {
523- delay = eventualLongDelay ;
524- }
525- } else {
526- unchangedCount .set (0 );
508+ Runnable command = new Runnable () {
509+ public void run () {
510+ Runnable r = this ; // resolve visibility
511+ Packet packet = new Packet ();
512+ packet .getComponents ().put (ProcessingConstants .DOMAIN_COMPONENT_NAME , Component .createFor (info , version , config ));
513+ Step strategy = DomainStatusUpdater .createStatusStep (statusUpdateTimeoutSeconds , null );
514+ domainUpdaters .startFiberIfNoCurrentFiber (domainUID , strategy , packet , new CompletionCallback () {
515+ @ Override
516+ public void onCompletion (Packet packet ) {
517+ Boolean isStatusUnchanged = (Boolean ) packet .get (ProcessingConstants .STATUS_UNCHANGED );
518+ ScheduledFuture <?> existing = null ;
519+ if (Boolean .TRUE .equals (isStatusUnchanged )) {
520+ if (unchangedCount .incrementAndGet () == unchangedCountToDelayStatusRecheck ) {
521+ // slow down retries because of sufficient unchanged statuses
522+ existing = statusUpdater .getAndSet (
523+ engine .getExecutor ().scheduleWithFixedDelay (r , eventualLongDelay , eventualLongDelay , TimeUnit .SECONDS ));
524+ }
525+ } else {
526+ // reset to trying after shorter delay because of changed status
527+ unchangedCount .set (0 );
528+ existing = statusUpdater .getAndSet (
529+ engine .getExecutor ().scheduleWithFixedDelay (r , initialShortDelay , initialShortDelay , TimeUnit .SECONDS ));
530+ if (existing != null ) {
531+ existing .cancel (false );
527532 }
528- // retry after delay
529- statusUpdater .set (engine .getExecutor ().schedule (r , delay , TimeUnit .SECONDS ));
530533 }
531-
532- @ Override
533- public void onThrowable (Packet packet , Throwable throwable ) {
534- LOGGER .severe (MessageKeys .EXCEPTION , throwable );
535- // retry after delay
536- statusUpdater .set (engine .getExecutor ().schedule (r , initialShortDelay , TimeUnit .SECONDS ));
534+ if (existing != null ) {
535+ existing .cancel (false );
537536 }
538- });
539- }
540- };
541- statusUpdater .set (engine .getExecutor ().schedule (command , initialShortDelay , TimeUnit .SECONDS ));
537+ }
538+
539+ @ Override
540+ public void onThrowable (Packet packet , Throwable throwable ) {
541+ LOGGER .severe (MessageKeys .EXCEPTION , throwable );
542+ // retry to trying after shorter delay because of exception
543+ unchangedCount .set (0 );
544+ ScheduledFuture <?> existing = statusUpdater .getAndSet (
545+ engine .getExecutor ().scheduleWithFixedDelay (r , initialShortDelay , initialShortDelay , TimeUnit .SECONDS ));
546+ if (existing != null ) {
547+ existing .cancel (false );
548+ }
549+ }
550+ });
551+ }
552+ };
553+ ScheduledFuture <?> existing = statusUpdater .getAndSet (
554+ engine .getExecutor ().scheduleWithFixedDelay (command , initialShortDelay , initialShortDelay , TimeUnit .SECONDS ));
555+ if (existing != null ) {
556+ existing .cancel (false );
542557 }
543558 }
544559
545- private static boolean validateExisting (long initialShortDelay , ScheduledFuture <?> existing ) {
546- if (existing .isCancelled ())
547- return false ;
548- return existing .getDelay (TimeUnit .SECONDS ) <= initialShortDelay ;
549- }
550-
551560 private static void cancelDomainStatusUpdating (DomainPresenceInfo info ) {
552561 ScheduledFuture <?> statusUpdater = info .getStatusUpdater ().getAndSet (null );
553562 if (statusUpdater != null ) {
0 commit comments