diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/AdHocLogDumper.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/AdHocLogDumper.java index d2e4c74fd3..f33ddcdb83 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/AdHocLogDumper.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/AdHocLogDumper.java @@ -26,6 +26,8 @@ import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.log4j.*; +import com.google.common.annotations.VisibleForTesting; + import java.io.File; import java.io.IOException; import java.util.*; @@ -41,7 +43,7 @@ public class AdHocLogDumper { private Map appenderLevels; private Level currentLogLevel; public static final String AD_HOC_DUMPER_APPENDER = "ad-hoc-dumper-appender"; - private static boolean logFlag = false; + private static volatile boolean logFlag = false; private static final Object lock = new Object(); public AdHocLogDumper(String name, String targetFilename) { @@ -107,6 +109,11 @@ public void dumpLogs(String level, int timePeriod) } } + @VisibleForTesting + public static boolean getState() { + return logFlag; + } + class RestoreLogLevel extends TimerTask { @Override public void run() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java index c32962996e..50a4849e92 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java @@ -41,7 +41,9 @@ import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.io.TwoDArrayWritable; import org.apache.hadoop.service.Service.STATE; +import org.apache.hadoop.test.GenericTestUtils.SleepAnswer; import org.apache.hadoop.util.VersionInfo; import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest; import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse; @@ -62,6 +64,7 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.AppsInfo; import org.apache.hadoop.yarn.server.security.ApplicationACLsManager; +import org.apache.hadoop.yarn.util.AdHocLogDumper; import org.apache.hadoop.yarn.util.YarnVersionInfo; import org.apache.hadoop.yarn.webapp.ForbiddenException; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; @@ -671,7 +674,7 @@ public void testDumpingSchedulerLogs() throws Exception { // nothing should happen webSvc.dumpSchedulerLogs("1", mockHsr); - Thread.sleep(1000); + waitforLogDump(50); checkSchedulerLogFileAndCleanup(); conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true); @@ -709,7 +712,7 @@ public String getName() { } }); webSvc.dumpSchedulerLogs("1", mockHsr); - Thread.sleep(1000); + waitforLogDump(50); checkSchedulerLogFileAndCleanup(); } @@ -727,4 +730,14 @@ private void checkSchedulerLogFileAndCleanup() { assertTrue("scheduler log file doesn't exist", logFile.exists()); FileUtils.deleteQuietly(logFile); } + + private void waitforLogDump(int tickcount) throws InterruptedException { + while (tickcount > 0) { + Thread.sleep(100); + if (!AdHocLogDumper.getState()) { + return; + } + tickcount--; + } + } }