提交 e2227d90 编写于 作者: E egahlin

6543856: MonitorVmStartTerminate.sh fails intermittently

Reviewed-by: sla, dholmes
上级 44aebaa3
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
* questions. * questions.
*/ */
import java.util.concurrent.CountDownLatch;
import java.util.regex.*; import java.util.regex.*;
import java.util.*; import java.util.*;
import java.net.URISyntaxException; import java.net.URISyntaxException;
...@@ -33,8 +34,6 @@ public class MonitorVmStartTerminate { ...@@ -33,8 +34,6 @@ public class MonitorVmStartTerminate {
private static final int SLEEPERS = 10; private static final int SLEEPERS = 10;
private static final int SLEEPTIME = 5000; // sleep time for a sleeper private static final int SLEEPTIME = 5000; // sleep time for a sleeper
private static final int EXECINTERVAL = 3000; // wait time between exec's private static final int EXECINTERVAL = 3000; // wait time between exec's
private static final int JOINTIME = (SLEEPERS * EXECINTERVAL)
+ SLEEPTIME * 2;
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
...@@ -46,7 +45,8 @@ public class MonitorVmStartTerminate { ...@@ -46,7 +45,8 @@ public class MonitorVmStartTerminate {
MonitoredHost host = MonitoredHost.getMonitoredHost("localhost"); MonitoredHost host = MonitoredHost.getMonitoredHost("localhost");
host.setInterval(200); host.setInterval(200);
SleeperListener listener = new SleeperListener(host, sleeperPattern); Matcher matcher = Pattern.compile(sleeperPattern).matcher("");
SleeperListener listener = new SleeperListener(host, matcher, SLEEPERS);
host.addHostListener(listener); host.addHostListener(listener);
SleeperStarter ss = new SleeperStarter(SLEEPERS, EXECINTERVAL, SleeperStarter ss = new SleeperStarter(SLEEPERS, EXECINTERVAL,
...@@ -56,212 +56,193 @@ public class MonitorVmStartTerminate { ...@@ -56,212 +56,193 @@ public class MonitorVmStartTerminate {
System.out.println("Waiting for " System.out.println("Waiting for "
+ SLEEPERS + " sleepers to terminate"); + SLEEPERS + " sleepers to terminate");
try { try {
ss.join(JOINTIME); ss.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println("Timed out waiting for sleepers"); throw new Exception("Timed out waiting for sleepers");
}
if (listener.getStarted() != SLEEPERS) {
throw new RuntimeException(
"Too few sleepers started: "
+ " started = " + listener.getStarted()
+ " SLEEPERS = " + SLEEPERS);
}
if (listener.getStarted() != listener.getTerminated()) {
throw new RuntimeException(
"Started count != terminated count: "
+ " started = " + listener.getStarted()
+ " terminated = " + listener.getTerminated());
} }
listener.waitForSleepersToStart();
listener.waitForSleepersToTerminate();
} }
}
class SleeperListener implements HostListener { public static class SleeperListener implements HostListener {
private static final boolean DEBUG = false;
int started; private final List<Integer> targets = new ArrayList<>();
int terminated; private final CountDownLatch terminateLatch;
MonitoredHost host; private final CountDownLatch startLatch;
Matcher patternMatcher; private final MonitoredHost host;
ArrayList targets; private final Matcher patternMatcher;
public SleeperListener(MonitoredHost host, String sleeperPattern) { public SleeperListener(MonitoredHost host, Matcher matcher, int count) {
this.host = host; this.host = host;
Pattern pattern = Pattern.compile(sleeperPattern); this.patternMatcher = matcher;
patternMatcher = pattern.matcher(""); this.terminateLatch = new CountDownLatch(count);
targets = new ArrayList(); this.startLatch = new CountDownLatch(count);
} }
private void printList(Iterator i, String msg) { public void waitForSleepersToTerminate() throws InterruptedException {
System.out.println(msg + ":"); terminateLatch.await();
while (i.hasNext()) {
Integer lvmid = (Integer)i.next();
try {
VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue());
MonitoredVm target = host.getMonitoredVm(vmid);
StringMonitor cmdMonitor =
(StringMonitor)target.findByName("sun.rt.javaCommand");
String cmd = cmdMonitor.stringValue();
System.out.println("\t" + lvmid.intValue() + ": "
+ "\"" + cmd + "\"" + ": ");
} catch (URISyntaxException e) {
System.err.println("Unexpected URISyntaxException: "
+ e.getMessage());
} catch (MonitorException e) {
System.out.println("\t" + lvmid.intValue()
+ ": error reading monitoring data: "
+ " target possibly terminated?");
}
} }
}
public void waitForSleepersToStart() throws InterruptedException {
startLatch.await();
}
private int addStarted(Iterator i) { private void printList(Set<Integer> list, String msg) {
int found = 0; System.out.println(msg + ":");
while (i.hasNext()) { for (Integer lvmid : list) {
try { try {
Integer lvmid = (Integer)i.next(); VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue());
VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue()); MonitoredVm target = host.getMonitoredVm(vmid);
MonitoredVm target = host.getMonitoredVm(vmid);
StringMonitor cmdMonitor =
(StringMonitor)target.findByName("sun.rt.javaCommand");
String cmd = cmdMonitor.stringValue();
System.out.println("\t" + lvmid.intValue() + ": "
+ "\"" + cmd + "\"" + ": ");
} catch (URISyntaxException e) {
System.err.println("Unexpected URISyntaxException: "
+ e.getMessage());
} catch (MonitorException e) {
System.out.println("\t" + lvmid.intValue()
+ ": error reading monitoring data: "
+ " target possibly terminated?");
}
}
}
StringMonitor cmdMonitor =
(StringMonitor)target.findByName("sun.rt.javaCommand");
String cmd = cmdMonitor.stringValue();
patternMatcher.reset(cmd); private int addStarted(Set<Integer> started) {
System.out.print("Started: " + lvmid.intValue() int found = 0;
+ ": " + "\"" + cmd + "\"" + ": "); for (Integer lvmid : started) {
try {
VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue());
MonitoredVm target = host.getMonitoredVm(vmid);
StringMonitor cmdMonitor =
(StringMonitor)target.findByName("sun.rt.javaCommand");
String cmd = cmdMonitor.stringValue();
patternMatcher.reset(cmd);
System.out.print("Started: " + lvmid.intValue()
+ ": " + "\"" + cmd + "\"" + ": ");
if (patternMatcher.matches()) {
System.out.println("matches pattern - recorded");
targets.add(lvmid);
found++;
}
else {
System.out.println("does not match pattern - ignored");
}
} catch (URISyntaxException e) {
System.err.println("Unexpected URISyntaxException: "
+ e.getMessage());
} catch (MonitorException e) {
System.err.println("Unexpected MonitorException: "
+ e.getMessage());
}
}
return found;
}
if (patternMatcher.matches()) { private int removeTerminated(Set<Integer> terminated) {
System.out.println("matches pattern - recorded"); int found = 0;
targets.add(lvmid); for (Integer lvmid : terminated) {
/*
* we don't attempt to attach to the target here as it's
* now dead and has no jvmstat share memory file. Just see
* if the process id is among those that we saved when we
* started the targets (note - duplicated allowed and somewhat
* expected on windows);
*/
System.out.print("Terminated: " + lvmid.intValue() + ": ");
if (targets.contains(lvmid)) {
System.out.println("matches pattern - termination recorded");
targets.remove(lvmid);
found++; found++;
} }
else { else {
System.out.println("does not match pattern - ignored"); System.out.println("does not match pattern - ignored");
} }
} catch (URISyntaxException e) {
System.err.println("Unexpected URISyntaxException: "
+ e.getMessage());
} catch (MonitorException e) {
System.err.println("Unexpected MonitorException: "
+ e.getMessage());
} }
return found;
} }
return found;
}
private int removeTerminated(Iterator i) { @SuppressWarnings("unchecked")
int found = 0; public void vmStatusChanged(VmStatusChangeEvent ev) {
while (i.hasNext()) { printList(ev.getActive(), "Active");
Integer lvmid = (Integer)i.next(); printList(ev.getStarted(), "Started");
/* printList(ev.getTerminated(), "Terminated");
* we don't attempt to attach to the target here as it's
* now dead and has no jvmstat share memory file. Just see
* if the process id is among those that we saved when we
* started the targets (note - duplicated allowed and somewhat
* expected on windows);
*/
System.out.print("Terminated: " + lvmid.intValue() + ": ");
if (targets.contains(lvmid)) {
System.out.println("matches pattern - termination recorded");
targets.remove(lvmid);
found++;
}
else {
System.out.println("does not match pattern - ignored");
}
}
return found;
}
public synchronized int getStarted() { int recentlyStarted = addStarted(ev.getStarted());
return started; int recentlyTerminated = removeTerminated(ev.getTerminated());
}
public synchronized int getTerminated() { for (int i = 0; i < recentlyTerminated; i++) {
return terminated; terminateLatch.countDown();
} }
for (int i = 0; i < recentlyStarted; i++) {
public void vmStatusChanged(VmStatusChangeEvent ev) { startLatch.countDown();
if (DEBUG) { }
printList(ev.getActive().iterator(), "Active");
printList(ev.getStarted().iterator(), "Started");
printList(ev.getTerminated().iterator(), "Terminated");
} }
int recentlyStarted = addStarted(ev.getStarted().iterator()); public void disconnected(HostEvent ev) {
int recentlyTerminated = removeTerminated(
ev.getTerminated().iterator());
synchronized (this) {
started += recentlyStarted;
terminated += recentlyTerminated;
} }
} }
public void disconnected(HostEvent ev) { public static class SleeperStarter extends Thread {
}
}
class SleeperStarter extends Thread {
JavaProcess[] processes; private final JavaProcess[] processes;
int execInterval; private final int execInterval;
String args; private final String args;
public SleeperStarter(int sleepers, int execInterval, String args) { public SleeperStarter(int sleepers, int execInterval, String args) {
this.execInterval = execInterval; this.execInterval = execInterval;
this.args = args; this.args = args;
this.processes = new JavaProcess[sleepers]; this.processes = new JavaProcess[sleepers];
} }
private synchronized int active() { private synchronized int active() {
int active = processes.length; int active = processes.length;
for(int i = 0; i < processes.length; i++) { for(JavaProcess jp : processes) {
try { try {
int exitValue = processes[i].exitValue(); jp.exitValue();
active--; active--;
} catch (IllegalThreadStateException e) { } catch (IllegalThreadStateException e) {
// process hasn't exited yet // process hasn't exited yet
}
} }
return active;
} }
return active;
}
public void run() {
System.out.println("Starting " + processes.length + " sleepers");
String[] classpath = { public void run() {
"-classpath", System.out.println("Starting " + processes.length + " sleepers");
System.getProperty("java.class.path")
}; String[] classpath = {
"-classpath",
for (int i = 0; i < processes.length; i++) { System.getProperty("java.class.path")
try { };
System.out.println("Starting Sleeper " + i);
synchronized(this) { for (int i = 0; i < processes.length; i++) {
processes[i] = new JavaProcess("Sleeper", args + " " + i); try {
processes[i].addOptions(classpath); System.out.println("Starting Sleeper " + i);
synchronized(this) {
processes[i] = new JavaProcess("Sleeper", args + " " + i);
processes[i].addOptions(classpath);
}
processes[i].start();
Thread.sleep(execInterval);
} catch (InterruptedException ignore) {
} catch (IOException e) {
System.err.println(
"IOException trying to start Sleeper " + i + ": "
+ e.getMessage());
} }
processes[i].start();
Thread.sleep(execInterval);
} catch (InterruptedException ignore) {
} catch (IOException e) {
System.err.println(
"IOException trying to start Sleeper " + i + ": "
+ e.getMessage());
} }
}
// spin waiting for the processes to terminate
while (active() > 0) ;
// give final termination event a change to propogate to // spin waiting for the processes to terminate
// the HostListener while (active() > 0) ;
try { Thread.sleep(2000); } catch (InterruptedException ignore) { } }
} }
} }
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
# #
# @test # @test
# @ignore until 6543856 is fixed
# @bug 4990825 # @bug 4990825
# @summary attach to external but local JVM processes # @summary attach to external but local JVM processes
# @library ../../testlibrary # @library ../../testlibrary
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
*/ */
import java.io.*; import java.io.*;
import java.util.Properties;
public class JavaProcess { public class JavaProcess {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册