@@ -55,29 +55,45 @@ public DefaultServerInfoAppender(ServerGroupListFactory serverGroupListFactory,
5555 }
5656
5757 @ Override
58- public void appendServerInfo (final Range range , final NodeList source , final LinkDataDuplexMap linkDataDuplexMap , final long timeoutMillis ) {
58+ public void appendServerInfo (final Range range , final NodeList source , final LinkDataDuplexMap linkDataDuplexMap , long timeoutMillis ) {
5959 if (source == null ) {
6060 return ;
6161 }
62+ if (-1 == timeoutMillis ) {
63+ timeoutMillis = Long .MAX_VALUE ;
64+ }
65+
6266 Collection <Node > nodes = source .getNodeList ();
6367 if (CollectionUtils .isEmpty (nodes )) {
6468 return ;
6569 }
6670
6771 final CompletableFuture <ServerGroupList >[] futures = getServerGroupListFutures (range , nodes , linkDataDuplexMap );
68- if (-1 == timeoutMillis ) {
69- // Returns the result value when complete
70- CompletableFuture .allOf (futures ).join ();
71- } else {
72- try {
73- CompletableFuture .allOf (futures ).get (timeoutMillis , TimeUnit .MILLISECONDS );
74- } catch (Throwable e ) {
75- CompletableFuture .allOf (futures ).cancel (false );
76- String cause = "an error occurred while adding server info" ;
77- if (e instanceof TimeoutException ) {
78- cause += " build timed out. timeout=" + timeoutMillis + "ms" ;
79- }
80- throw new RuntimeException (cause , e );
72+
73+ try {
74+ CompletableFuture .allOf (futures ).get (timeoutMillis , TimeUnit .MILLISECONDS );
75+ } catch (Throwable e ) {
76+ CompletableFuture .allOf (futures ).cancel (false );
77+ String cause = "an error occurred while adding server info" ;
78+ if (e instanceof TimeoutException ) {
79+ cause += " build timed out. timeout=" + timeoutMillis + "ms" ;
80+ }
81+ throw new RuntimeException (cause , e );
82+ }
83+
84+ int index = 0 ;
85+ for (Node node : nodes ) {
86+ if (skipServerInfo (node )) {
87+ continue ;
88+ }
89+ final CompletableFuture <ServerGroupList > future = futures [index ++];
90+ if (future .isCompletedExceptionally ()) {
91+ logger .warn ("Failed to get server info for node {}" , node );
92+ node .setServerGroupList (serverGroupListFactory .createEmptyNodeInstanceList ());
93+ } else {
94+ ServerGroupList serverGroupList = future .getNow (null );
95+ logger .trace ("ServerGroupList: {}" , serverGroupList );
96+ node .setServerGroupList (serverGroupList );
8197 }
8298 }
8399 }
@@ -86,33 +102,21 @@ public void appendServerInfo(final Range range, final NodeList source, final Lin
86102 private CompletableFuture <ServerGroupList >[] getServerGroupListFutures (Range range , Collection <Node > nodes , LinkDataDuplexMap linkDataDuplexMap ) {
87103 List <CompletableFuture <ServerGroupList >> serverGroupListFutures = new ArrayList <>();
88104 for (Node node : nodes ) {
89- if (node .getServiceType ().isUnknown ()) {
90- // we do not know the server info for unknown nodes
105+ if (skipServerInfo (node )) {
91106 continue ;
92107 }
93108 CompletableFuture <ServerGroupList > serverGroupListFuture = getServerGroupListFuture (range , node , linkDataDuplexMap );
94109 serverGroupListFutures .add (serverGroupListFuture );
95110 }
96-
97111 return serverGroupListFutures .toArray (new CompletableFuture [0 ]);
98112 }
99113
100- private CompletableFuture <ServerGroupList > getServerGroupListFuture (Range range , Node node , LinkDataDuplexMap linkDataDuplexMap ) {
101- CompletableFuture <ServerGroupList > serverGroupListFuture = getServerGroupListFuture0 (node , range , linkDataDuplexMap );
102- serverGroupListFuture .whenComplete ((serverGroupList , throwable ) -> {
103- if (throwable != null ) {
104- // error
105- logger .warn ("Failed to get server info for node {}" , node , throwable );
106- node .setServerGroupList (serverGroupListFactory .createEmptyNodeInstanceList ());
107- } else {
108- logger .trace ("ServerGroupList: {}" , serverGroupList );
109- node .setServerGroupList (serverGroupList );
110- }
111- });
112- return serverGroupListFuture ;
113- }
114+ private boolean skipServerInfo (Node node ) {
115+ // we do not know the server info for unknown nodes
116+ return node .getServiceType ().isUnknown ();
117+ }
114118
115- private CompletableFuture <ServerGroupList > getServerGroupListFuture0 ( Node node , Range range , LinkDataDuplexMap linkDataDuplexMap ) {
119+ private CompletableFuture <ServerGroupList > getServerGroupListFuture ( Range range , Node node , LinkDataDuplexMap linkDataDuplexMap ) {
116120 final Application application = node .getApplication ();
117121 final ServiceType nodeServiceType = application .getServiceType ();
118122 if (nodeServiceType .isWas ()) {
0 commit comments