8787import java .util .concurrent .atomic .AtomicBoolean ;
8888import java .util .concurrent .atomic .AtomicReference ;
8989import java .util .function .BiFunction ;
90+ import java .util .function .BooleanSupplier ;
9091import java .util .function .Consumer ;
9192import java .util .function .Function ;
9293import java .util .function .LongConsumer ;
@@ -103,6 +104,7 @@ class StreamEnvironment implements Environment {
103104 private static final Logger LOGGER = LoggerFactory .getLogger (StreamEnvironment .class );
104105
105106 private final EventLoopGroup eventLoopGroup ;
107+ private final boolean privateEventLoopGroup ;
106108 private final ScheduledExecutorService scheduledExecutorService ;
107109 private final ScheduledExecutorService locatorReconnectionScheduledExecutorService ;
108110 private final boolean privateScheduleExecutorService ;
@@ -272,17 +274,16 @@ class StreamEnvironment implements Environment {
272274
273275 if (clientParametersPrototype .eventLoopGroup == null ) {
274276 this .eventLoopGroup = Utils .eventLoopGroup ();
277+ this .privateEventLoopGroup = true ;
275278 shutdownService .wrap (() -> closeEventLoopGroup (this .eventLoopGroup ));
276- this .clientParametersPrototype =
277- clientParametersPrototype .duplicate ().eventLoopGroup (this .eventLoopGroup );
278279 } else {
279- this .eventLoopGroup = null ;
280- this .clientParametersPrototype =
281- clientParametersPrototype
282- .duplicate ()
283- .eventLoopGroup (clientParametersPrototype .eventLoopGroup );
280+ this .eventLoopGroup = clientParametersPrototype .eventLoopGroup ;
281+ this .privateEventLoopGroup = false ;
284282 }
285283
284+ this .clientParametersPrototype =
285+ clientParametersPrototype .duplicate ().eventLoopGroup (this .eventLoopGroup );
286+
286287 this .producersCoordinator =
287288 new ProducersCoordinator (
288289 this ,
@@ -356,13 +357,19 @@ class StreamEnvironment implements Environment {
356357 clientFactory ,
357358 this .locatorReconnectionScheduledExecutorService ,
358359 this .recoveryBackOffDelayPolicy ,
359- l .label ());
360+ l .label (),
361+ this ::shouldTryLocatorConnection );
360362 }
361363 });
362364 }
363365 };
364366 if (lazyInit ) {
365- this .locatorInitializationSequence = locatorInitSequence ;
367+ this .locatorInitializationSequence =
368+ () -> {
369+ if (this .shouldTryLocatorConnection ()) {
370+ locatorInitSequence .run ();
371+ }
372+ };
366373 } else {
367374 locatorInitSequence .run ();
368375 locatorsInitialized .set (true );
@@ -408,7 +415,8 @@ private ShutdownListener shutdownListener(
408415 clientFactory ,
409416 this .locatorReconnectionScheduledExecutorService ,
410417 delayPolicy ,
411- label );
418+ label ,
419+ this ::shouldTryLocatorConnection );
412420 } else {
413421 LOGGER .debug ("Locator connection '{}' closing normally" , label );
414422 }
@@ -425,7 +433,8 @@ private static void scheduleLocatorConnection(
425433 Function <Client .ClientParameters , Client > clientFactory ,
426434 ScheduledExecutorService scheduler ,
427435 BackOffDelayPolicy delayPolicy ,
428- String locatorLabel ) {
436+ String locatorLabel ,
437+ BooleanSupplier shouldRetry ) {
429438 LOGGER .debug (
430439 "Scheduling locator '{}' connection with delay policy {}" , locatorLabel , delayPolicy );
431440 try {
@@ -452,6 +461,7 @@ private static void scheduleLocatorConnection(
452461 .description ("Locator '%s' connection" , locatorLabel )
453462 .scheduler (scheduler )
454463 .delayPolicy (delayPolicy )
464+ .retry (ignored -> shouldRetry .getAsBoolean ())
455465 .build ()
456466 .thenAccept (locator ::client )
457467 .exceptionally (
@@ -777,14 +787,19 @@ public void close() {
777787 if (this .locatorReconnectionScheduledExecutorService != null ) {
778788 this .locatorReconnectionScheduledExecutorService .shutdownNow ();
779789 }
780- closeEventLoopGroup (this .eventLoopGroup );
790+ if (this .privateEventLoopGroup ) {
791+ closeEventLoopGroup (this .eventLoopGroup );
792+ }
781793 }
782794 }
783795
796+ private boolean shouldTryLocatorConnection () {
797+ return !this .closed .get () && !this .eventLoopGroup .isShuttingDown ();
798+ }
799+
784800 private static void closeEventLoopGroup (EventLoopGroup eventLoopGroup ) {
785801 try {
786- if (eventLoopGroup != null
787- && (!eventLoopGroup .isShuttingDown () || !eventLoopGroup .isShutdown ())) {
802+ if (!eventLoopGroup .isShuttingDown ()) {
788803 LOGGER .debug ("Closing Netty event loop group" );
789804 eventLoopGroup .shutdownGracefully (1 , 10 , SECONDS ).get (10 , SECONDS );
790805 }
0 commit comments