@@ -526,10 +526,10 @@ public Task create(final Task.Type type, final Link link, final byte[] data) {
526526 }
527527
528528 protected void reconnect (final Link link ) {
529- reconnect (link , null , false );
529+ reconnect (link , null , null , false );
530530 }
531531
532- protected void reconnect (final Link link , String host , boolean forTransfer ) {
532+ protected void reconnect (final Link link , String preferredHost , List < String > avoidHostList , boolean forTransfer ) {
533533 if (!(forTransfer || _reconnectAllowed )) {
534534 return ;
535535 }
@@ -565,24 +565,29 @@ protected void reconnect(final Link link, String host, boolean forTransfer) {
565565 _shell .getBackoffAlgorithm ().waitBeforeRetry ();
566566 }
567567
568+ String host = preferredHost ;
569+ if (StringUtils .isEmpty (host )) {
570+ host = _shell .getNextHost ();
571+ }
572+
568573 do {
569- if (StringUtils .isEmpty (host )) {
570- host = _shell .getNextHost ();
571- }
572- _connection = new NioClient ("Agent" , host , _shell .getPort (), _shell .getWorkers (), this );
573- logger .info ("Reconnecting to host:{}" , host );
574- try {
575- _connection .start ();
576- } catch (final NioConnectionException e ) {
577- logger .info ("Attempted to re-connect to the server, but received an unexpected exception, trying again..." , e );
578- _connection .stop ();
574+ if (CollectionUtils .isEmpty (avoidHostList ) || !avoidHostList .contains (host )) {
575+ _connection = new NioClient ("Agent" , host , _shell .getPort (), _shell .getWorkers (), this );
576+ logger .info ("Reconnecting to host:{}" , host );
579577 try {
580- _connection .cleanUp ();
581- } catch (final IOException ex ) {
582- logger .warn ("Fail to clean up old connection. {}" , ex );
578+ _connection .start ();
579+ } catch (final NioConnectionException e ) {
580+ logger .info ("Attempted to re-connect to the server, but received an unexpected exception, trying again..." , e );
581+ _connection .stop ();
582+ try {
583+ _connection .cleanUp ();
584+ } catch (final IOException ex ) {
585+ logger .warn ("Fail to clean up old connection. {}" , ex );
586+ }
583587 }
584588 }
585589 _shell .getBackoffAlgorithm ().waitBeforeRetry ();
590+ host = _shell .getNextHost ();
586591 } while (!_connection .isStartup ());
587592 _shell .updateConnectedHost ();
588593 logger .info ("Connected to the host: {}" , _shell .getConnectedHost ());
@@ -874,19 +879,25 @@ private void migrateAgentConnection(List<String> avoidMsList) {
874879 throw new CloudRuntimeException ("No other Management Server hosts to migrate" );
875880 }
876881
877- final String preferredHost = msHostsList .get (0 );
882+ String preferredHost = null ;
883+ for (String msHost : msHostsList ) {
884+ try (final Socket socket = new Socket ()) {
885+ socket .connect (new InetSocketAddress (msHost , _shell .getPort ()), 5000 );
886+ preferredHost = msHost ;
887+ break ;
888+ } catch (final IOException e ) {
889+ throw new CloudRuntimeException ("Management server host: " + msHost + " is not reachable, to migrate connection" );
890+ }
891+ }
878892
879- try (final Socket socket = new Socket ()) {
880- socket .connect (new InetSocketAddress (preferredHost , _shell .getPort ()), 5000 );
881- } catch (final IOException e ) {
882- throw new CloudRuntimeException ("Preferred management server host: " + preferredHost + " is not reachable, to migrate" );
893+ if (preferredHost == null ) {
894+ throw new CloudRuntimeException ("Management server host(s) are not reachable, to migrate connection" );
883895 }
884896
885- logger .debug ("Preferred management server host " + preferredHost + " is found to be reachable, trying to reconnect" );
886- _reconnectAllowed = true ;
897+ logger .debug ("Management server host " + preferredHost + " is found to be reachable, trying to reconnect" );
887898 _shell .resetHostCounter ();
888899 _shell .setConnectionTransfer (true );
889- reconnect (_link , preferredHost , true );
900+ reconnect (_link , preferredHost , avoidMsList , true );
890901 }
891902
892903 public void processResponse (final Response response , final Link link ) {
0 commit comments