Skip to content

Commit 79bdb7f

Browse files
committed
[#11050] Replace StopFlag with CompletableFuture.cancel
1 parent 044c8be commit 79bdb7f

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

web/src/main/java/com/navercorp/pinpoint/web/applicationmap/appender/server/DefaultServerInfoAppender.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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()) {

web/src/main/java/com/navercorp/pinpoint/web/applicationmap/nodes/Node.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class Node {
3939
private final Application application;
4040

4141
// avoid NPE
42-
private volatile ServerGroupList serverGroupList = ServerGroupList.empty();
42+
private ServerGroupList serverGroupList = ServerGroupList.empty();
4343

44-
private volatile NodeHistogram nodeHistogram;
44+
private NodeHistogram nodeHistogram;
4545

4646
private boolean authorized = true;
4747
private TimeHistogramFormat timeHistogramFormat = TimeHistogramFormat.V1;

web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ApplicationMapBuilderTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ public void testNoCallData() {
187187
.includeServerInfo(serverGroupListFactory)
188188
.build(application, buildTimeoutMillis);
189189

190-
assertThat(applicationMap.getNodes()).hasSize(1);
191190
assertThat(applicationMap.getNodes()).hasSize(1);
192191
assertThat(applicationMap_parallelAppenders.getNodes()).hasSize(1);
193192
assertThat(applicationMap.getLinks()).isEmpty();
194-
assertThat(applicationMap.getLinks()).isEmpty();
195193
assertThat(applicationMap_parallelAppenders.getLinks()).isEmpty();
196194

197195
ApplicationMapVerifier verifier = new ApplicationMapVerifier(applicationMap);
@@ -218,11 +216,9 @@ public void testEmptyCallData() {
218216
.includeServerInfo(serverGroupListFactory)
219217
.build(linkDataDuplexMap, buildTimeoutMillis);
220218

221-
assertThat(applicationMap.getNodes()).isEmpty();
222219
assertThat(applicationMap.getNodes()).isEmpty();
223220
assertThat(applicationMap_parallelAppenders.getNodes()).isEmpty();
224221

225-
assertThat(applicationMap.getLinks()).isEmpty();
226222
assertThat(applicationMap.getLinks()).isEmpty();
227223
assertThat(applicationMap_parallelAppenders.getLinks()).isEmpty();
228224

@@ -250,11 +246,9 @@ public void testEmptyCallDataSimplfied() {
250246
.includeServerInfo(serverGroupListFactory)
251247
.build(linkDataDuplexMap, buildTimeoutMillis);
252248

253-
assertThat(applicationMap.getNodes()).isEmpty();
254249
assertThat(applicationMap.getNodes()).isEmpty();
255250
assertThat(applicationMap_parallelAppenders.getNodes()).isEmpty();
256251

257-
assertThat(applicationMap.getLinks()).isEmpty();
258252
assertThat(applicationMap.getLinks()).isEmpty();
259253
assertThat(applicationMap_parallelAppenders.getLinks()).isEmpty();
260254

0 commit comments

Comments
 (0)