From eb301bb3ecb9ed63cfd609b24d6eb909d94bd0d1 Mon Sep 17 00:00:00 2001 From: Daniel Templeton Date: Fri, 2 Dec 2016 12:05:29 -0800 Subject: [PATCH] YARN-5901. Fix race condition in TestGetGroups beforeclass setup() (Contributed by Haibo Chen via Daniel Templeton) --- .../hadoop/yarn/client/TestGetGroups.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestGetGroups.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestGetGroups.java index e947ece8a7d..71f02ed9149 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestGetGroups.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestGetGroups.java @@ -20,16 +20,21 @@ package org.apache.hadoop.yarn.client; import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.Service; import org.apache.hadoop.service.Service.STATE; +import org.apache.hadoop.service.ServiceStateChangeListener; import org.apache.hadoop.tools.GetGroupsTestBase; import org.apache.hadoop.util.Tool; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; @@ -42,30 +47,37 @@ public class TestGetGroups extends GetGroupsTestBase { private static Configuration conf; @BeforeClass - public static void setUpResourceManager() throws IOException, InterruptedException { + public static void setUpResourceManager() throws InterruptedException { conf = new YarnConfiguration(); resourceManager = new ResourceManager() { @Override protected void doSecureLogin() throws IOException { }; }; + + // a reliable way to wait for resource manager to start + final CountDownLatch rmStartedSignal = new CountDownLatch(1); + ServiceStateChangeListener rmStateChangeListener = + new ServiceStateChangeListener() { + @Override + public void stateChanged(Service service) { + if (service.getServiceState() == STATE.STARTED) { + rmStartedSignal.countDown(); + } + } + }; + resourceManager.registerServiceListener(rmStateChangeListener); + resourceManager.init(conf); new Thread() { public void run() { resourceManager.start(); }; }.start(); - int waitCount = 0; - while (resourceManager.getServiceState() == STATE.INITED - && waitCount++ < 10) { - LOG.info("Waiting for RM to start..."); - Thread.sleep(1000); - } - if (resourceManager.getServiceState() != STATE.STARTED) { - throw new IOException( - "ResourceManager failed to start. Final state is " - + resourceManager.getServiceState()); - } + + boolean rmStarted = rmStartedSignal.await(60000L, TimeUnit.MILLISECONDS); + Assert.assertTrue("ResourceManager failed to start up.", rmStarted); + LOG.info("ResourceManager RMAdmin address: " + conf.get(YarnConfiguration.RM_ADMIN_ADDRESS)); }