Commit 1de1bdc3 authored by Flavio Paiva Junqueira's avatar Flavio Paiva Junqueira
Browse files

ZOOKEEPER-1837. Fix JMXEnv checks (potential race conditions)

  (Germán Blanco via fpj)



git-svn-id: https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.4@1558951 13f79535-47bb-0310-9956-ffa450edef68
parent 754a4c3c
......@@ -224,6 +224,9 @@ BUGFIXES:
ZOOKEEPER-1238. when the linger time was changed for NIO the patch missed
Netty (Skye Wanderman-Milne via fpj)
ZOOKEEPER-1837. Fix JMXEnv checks (potential race conditions)
(Germán Blanco via fpj)
IMPROVEMENTS:
ZOOKEEPER-1564. Allow JUnit test build with IBM Java
......
......@@ -84,34 +84,43 @@ public class JMXEnv {
* Note that these are components of the name, and in particular
* order matters - you want the more specific name (leafs) specified
* before their parent(s) (since names are hierarchical)
* It waits in a loop up to 60 seconds before failing if there is a
* mismatch.
* @param expectedNames
* @return
* @throws IOException
* @throws MalformedObjectNameException
*/
public static Set<ObjectName> ensureAll(String... expectedNames)
throws IOException
throws IOException, InterruptedException
{
Set<ObjectName> beans;
try {
beans = conn().queryNames(
new ObjectName(CommonNames.DOMAIN + ":*"), null);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
Set<ObjectName> found;
int nTry = 0;
do {
if (nTry++ > 0) {
Thread.sleep(100);
}
try {
beans = conn().queryNames(
new ObjectName(CommonNames.DOMAIN + ":*"), null);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
Set<ObjectName> found = new HashSet<ObjectName>();
for (String name : expectedNames) {
LOG.info("expect:" + name);
for (ObjectName bean : beans) {
if (bean.toString().contains(name)) {
LOG.info("found:" + name + " " + bean);
found.add(bean);
break;
found = new HashSet<ObjectName>();
for (String name : expectedNames) {
LOG.info("expect:" + name);
for (ObjectName bean : beans) {
if (bean.toString().contains(name)) {
LOG.info("found:" + name + " " + bean);
found.add(bean);
break;
}
}
beans.removeAll(found);
}
beans.removeAll(found);
}
} while ((expectedNames.length != found.size()) && (nTry < 600));
TestCase.assertEquals("expected " + Arrays.toString(expectedNames),
expectedNames.length, found.size());
return beans;
......@@ -122,8 +131,6 @@ public class JMXEnv {
* Note that these are components of the name, and in particular
* order matters - you want the more specific name (leafs) specified
* before their parent(s) (since names are hierarchical)
* It waits in a loop up to 5 seconds before failing if there is a
* mismatch.
* @param expectedNames
* @return
* @throws IOException
......@@ -134,10 +141,6 @@ public class JMXEnv {
{
LOG.info("ensureOnly:" + Arrays.toString(expectedNames));
Set<ObjectName> beans = ensureAll(expectedNames);
for (int i = 0; (i < 50) && (beans.size() != 0); i++) {
Thread.sleep(100);
beans = ensureAll(expectedNames);
}
for (ObjectName bean : beans) {
LOG.info("unexpected:" + bean.toString());
}
......@@ -146,23 +149,44 @@ public class JMXEnv {
}
public static void ensureNone(String... expectedNames)
throws IOException
throws IOException, InterruptedException
{
Set<ObjectName> beans;
try {
beans = conn().queryNames(
new ObjectName(CommonNames.DOMAIN + ":*"), null);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
for (String name : expectedNames) {
for (ObjectName bean : beans) {
if (bean.toString().contains(name)) {
LOG.info("didntexpect:" + name);
TestCase.fail(name + " " + bean.toString());
int nTry = 0;
boolean foundUnexpected = false;
String unexpectedName = "";
do {
if (nTry++ > 0) {
Thread.sleep(100);
}
try {
beans = conn().queryNames(
new ObjectName(CommonNames.DOMAIN + ":*"), null);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
foundUnexpected = false;
for (String name : expectedNames) {
for (ObjectName bean : beans) {
if (bean.toString().contains(name)) {
LOG.info("didntexpect:" + name);
foundUnexpected = true;
unexpectedName = name + " " + bean.toString();
break;
}
}
if (foundUnexpected) {
break;
}
}
} while ((foundUnexpected) && (nTry < 600));
if (foundUnexpected) {
LOG.info("List of all beans follows:");
for (ObjectName bean : beans) {
LOG.info("bean:" + bean.toString());
}
TestCase.fail(unexpectedName);
}
}
......
......@@ -161,6 +161,8 @@ public class QuorumUtil {
JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()]));
} catch (IOException e) {
LOG.warn("IOException during JMXEnv operation", e);
} catch (InterruptedException e) {
LOG.warn("InterruptedException during JMXEnv operation", e);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment