Skip to content

Commit 8dfddfa

Browse files
author
Datadog Syncup Service
committed
Merge branch 'upstream-master'
2 parents 5693c68 + 4070736 commit 8dfddfa

File tree

2 files changed

+98
-10
lines changed

2 files changed

+98
-10
lines changed

src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/MonitoredHost.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,7 +50,7 @@
5050
* @see HostListener
5151
*/
5252
public abstract class MonitoredHost {
53-
private static Map<HostIdentifier, MonitoredHost> monitoredHosts =
53+
private static final Map<HostIdentifier, MonitoredHost> monitoredHosts =
5454
new HashMap<HostIdentifier, MonitoredHost>();
5555

5656
/*
@@ -133,13 +133,6 @@ public static MonitoredHost getMonitoredHost(VmIdentifier vmid)
133133
return getMonitoredHost(hostId);
134134
}
135135

136-
137-
/*
138-
* Load the MonitoredHostServices
139-
*/
140-
private static ServiceLoader<MonitoredHostService> monitoredHostServiceLoader =
141-
ServiceLoader.load(MonitoredHostService.class, MonitoredHostService.class.getClassLoader());
142-
143136
/**
144137
* Factory method to construct a MonitoredHost instance to manage the
145138
* connection to the host indicated by {@code hostId}.
@@ -167,9 +160,12 @@ public static MonitoredHost getMonitoredHost(HostIdentifier hostId)
167160

168161
hostId = resolveHostId(hostId);
169162

170-
for (MonitoredHostService mhs : monitoredHostServiceLoader) {
163+
ServiceLoader<MonitoredHostService> services = ServiceLoader.load(
164+
MonitoredHostService.class, MonitoredHostService.class.getClassLoader());
165+
for (MonitoredHostService mhs : services) {
171166
if (mhs.getScheme().equals(hostId.getScheme())) {
172167
mh = mhs.getMonitoredHost(hostId);
168+
break;
173169
}
174170
}
175171

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.concurrent.Callable;
28+
import java.util.concurrent.ExecutorService;
29+
import java.util.concurrent.Executors;
30+
import java.util.concurrent.Future;
31+
32+
import sun.jvmstat.monitor.MonitoredHost;
33+
import sun.jvmstat.monitor.VmIdentifier;
34+
35+
/*
36+
* @test
37+
* @bug 8320687
38+
* @summary verify that sun.jvmstat.monitor.MonitoredHost.getMonitoredHost() doesn't
39+
* unexpectedly throw an exception when invoked by concurrent threads
40+
*
41+
* @run main/othervm ConcurrentGetMonitoredHost
42+
*/
43+
public class ConcurrentGetMonitoredHost {
44+
45+
/*
46+
* Launches multiple concurrent threads and invokes MonitoredHost.getMonitoredHost()
47+
* in each of these threads and expects the call to return successfully without any
48+
* exceptions.
49+
*/
50+
public static void main(final String[] args) throws Exception {
51+
final String pidStr = "12345";
52+
final VmIdentifier vmid = new VmIdentifier(pidStr);
53+
final int numTasks = 100;
54+
final List<Task> tasks = new ArrayList<>();
55+
for (int i = 0; i < numTasks; i++) {
56+
tasks.add(new Task(vmid));
57+
}
58+
System.out.println("Submitting " + numTasks + " concurrent tasks to" +
59+
" get MonitoredHost for " + vmid);
60+
try {
61+
final ExecutorService executor = Executors.newCachedThreadPool();
62+
// wait for all tasks to complete
63+
final List<Future<MonitoredHost>> results = executor.invokeAll(tasks);
64+
// verify each one successfully completed and each of
65+
// the returned MonitoredHost is not null
66+
for (final Future<MonitoredHost> result : results) {
67+
final MonitoredHost mh = result.get();
68+
if (mh == null) {
69+
throw new AssertionError("MonitoredHost.getMonitoredHost() returned" +
70+
" null for vmid " + vmid);
71+
}
72+
}
73+
} catch (Exception e) {
74+
}
75+
System.out.println("All " + numTasks + " completed successfully");
76+
}
77+
78+
// a task which just calls MonitoredHost.getMonitoredHost(VmIdentifier) and
79+
// returns the resultant MonitoredHost
80+
private static final class Task implements Callable<MonitoredHost> {
81+
private final VmIdentifier vmid;
82+
83+
private Task(final VmIdentifier vmid) {
84+
this.vmid = Objects.requireNonNull(vmid);
85+
}
86+
87+
@Override
88+
public MonitoredHost call() throws Exception {
89+
return MonitoredHost.getMonitoredHost(this.vmid);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)