Search in sources :

Example 31 with FakeTimer

use of org.apache.hadoop.util.FakeTimer in project hadoop by apache.

the class TestThrottledAsyncCheckerTimeout method testDiskCheckTimeout.

@Test(timeout = 1000)
public void testDiskCheckTimeout() throws Exception {
    LOG.info("Executing {}", testName.getMethodName());
    final DummyCheckable target = new DummyCheckable();
    final FakeTimer timer = new FakeTimer();
    ThrottledAsyncChecker<Boolean, Boolean> checker = new ThrottledAsyncChecker<>(timer, 0, DISK_CHECK_TIMEOUT, getExecutorService());
    // Acquire lock to halt checker. Release after timeout occurs.
    lock.lock();
    final Optional<ListenableFuture<Boolean>> olf = checker.schedule(target, true);
    final AtomicLong numCallbackInvocationsSuccess = new AtomicLong(0);
    final AtomicLong numCallbackInvocationsFailure = new AtomicLong(0);
    AtomicBoolean callbackResult = new AtomicBoolean(false);
    final Throwable[] throwable = new Throwable[1];
    assertTrue(olf.isPresent());
    Futures.addCallback(olf.get(), new FutureCallback<Boolean>() {

        @Override
        public void onSuccess(Boolean result) {
            numCallbackInvocationsSuccess.incrementAndGet();
            callbackResult.set(true);
        }

        @Override
        public void onFailure(Throwable t) {
            throwable[0] = t;
            numCallbackInvocationsFailure.incrementAndGet();
            callbackResult.set(true);
        }
    });
    while (!callbackResult.get()) {
        // Wait for the callback
        Thread.sleep(DISK_CHECK_TIMEOUT);
    }
    lock.unlock();
    assertThat(numCallbackInvocationsFailure.get(), is(1L));
    assertThat(numCallbackInvocationsSuccess.get(), is(0L));
    assertTrue(throwable[0] instanceof TimeoutException);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeTimer(org.apache.hadoop.util.FakeTimer) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 32 with FakeTimer

use of org.apache.hadoop.util.FakeTimer in project hadoop by apache.

the class TestThrottledAsyncCheckerTimeout method testTimeoutExceptionIsNotThrownForGoodDisk.

@Test(timeout = 1000)
public void testTimeoutExceptionIsNotThrownForGoodDisk() throws Exception {
    LOG.info("Executing {}", testName.getMethodName());
    final DummyCheckable target = new DummyCheckable();
    final FakeTimer timer = new FakeTimer();
    ThrottledAsyncChecker<Boolean, Boolean> checker = new ThrottledAsyncChecker<>(timer, 0, DISK_CHECK_TIMEOUT, getExecutorService());
    final Optional<ListenableFuture<Boolean>> olf = checker.schedule(target, true);
    AtomicBoolean callbackResult = new AtomicBoolean(false);
    final Throwable[] throwable = new Throwable[1];
    assertTrue(olf.isPresent());
    Futures.addCallback(olf.get(), new FutureCallback<Boolean>() {

        @Override
        public void onSuccess(Boolean result) {
            callbackResult.set(true);
        }

        @Override
        public void onFailure(Throwable t) {
            throwable[0] = t;
            callbackResult.set(true);
        }
    });
    while (!callbackResult.get()) {
        // Wait for the callback
        Thread.sleep(DISK_CHECK_TIMEOUT);
    }
    assertTrue(throwable[0] == null);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeTimer(org.apache.hadoop.util.FakeTimer) Test(org.junit.Test)

Example 33 with FakeTimer

use of org.apache.hadoop.util.FakeTimer in project hadoop by apache.

the class TestFSNamesystemLock method testFSReadLockLongHoldingReport.

/**
   * Test when FSNamesystem read lock is held for a long time,
   * logger will report it.
   */
@Test(timeout = 45000)
public void testFSReadLockLongHoldingReport() throws Exception {
    final long readLockReportingThreshold = 100L;
    final long readLockSuppressWarningInterval = 10000L;
    final String readLockLogStmt = "FSNamesystem read lock held for ";
    Configuration conf = new Configuration();
    conf.setLong(DFSConfigKeys.DFS_NAMENODE_READ_LOCK_REPORTING_THRESHOLD_MS_KEY, readLockReportingThreshold);
    conf.setTimeDuration(DFSConfigKeys.DFS_LOCK_SUPPRESS_WARNING_INTERVAL_KEY, readLockSuppressWarningInterval, TimeUnit.MILLISECONDS);
    final FakeTimer timer = new FakeTimer();
    final FSNamesystemLock fsnLock = new FSNamesystemLock(conf, null, timer);
    timer.advance(readLockSuppressWarningInterval);
    LogCapturer logs = LogCapturer.captureLogs(FSNamesystem.LOG);
    GenericTestUtils.setLogLevel(FSNamesystem.LOG, Level.INFO);
    // Don't report if the read lock is held for a short time
    fsnLock.readLock();
    fsnLock.readUnlock();
    assertFalse(logs.getOutput().contains(GenericTestUtils.getMethodName()) && logs.getOutput().contains(readLockLogStmt));
    // Report the first read lock warning if it is held for a long time
    fsnLock.readLock();
    timer.advance(readLockReportingThreshold + 10);
    logs.clearOutput();
    fsnLock.readUnlock();
    assertTrue(logs.getOutput().contains(GenericTestUtils.getMethodName()) && logs.getOutput().contains(readLockLogStmt));
    // Track but do not Report if the write lock is held for a long time but
    // time since last report does not exceed the suppress warning interval
    fsnLock.readLock();
    timer.advance(readLockReportingThreshold + 10);
    logs.clearOutput();
    fsnLock.readUnlock();
    assertFalse(logs.getOutput().contains(GenericTestUtils.getMethodName()) && logs.getOutput().contains(readLockLogStmt));
    // Track but do not Report if it's held for a long time when re-entering
    // read lock but time since last report does not exceed the suppress
    // warning interval
    fsnLock.readLock();
    timer.advance(readLockReportingThreshold / 2 + 1);
    fsnLock.readLock();
    timer.advance(readLockReportingThreshold / 2 + 1);
    logs.clearOutput();
    fsnLock.readUnlock();
    assertFalse(logs.getOutput().contains(GenericTestUtils.getMethodName()) || logs.getOutput().contains(readLockLogStmt));
    logs.clearOutput();
    fsnLock.readUnlock();
    assertFalse(logs.getOutput().contains(GenericTestUtils.getMethodName()) && logs.getOutput().contains(readLockLogStmt));
    // Report if it's held for a long time (and time since last report
    // exceeds the suppress warning interval) while another thread also has the
    // read lock. Let one thread hold the lock long enough to activate an
    // alert, then have another thread grab the read lock to ensure that this
    // doesn't reset the timing.
    timer.advance(readLockSuppressWarningInterval);
    logs.clearOutput();
    final CountDownLatch barrier = new CountDownLatch(1);
    final CountDownLatch barrier2 = new CountDownLatch(1);
    Thread t1 = new Thread() {

        @Override
        public void run() {
            try {
                fsnLock.readLock();
                timer.advance(readLockReportingThreshold + 1);
                // Allow for t2 to acquire the read lock
                barrier.countDown();
                // Wait until t2 has the read lock
                barrier2.await();
                fsnLock.readUnlock();
            } catch (InterruptedException e) {
                fail("Interrupted during testing");
            }
        }
    };
    Thread t2 = new Thread() {

        @Override
        public void run() {
            try {
                // Wait until t1 finishes sleeping
                barrier.await();
                fsnLock.readLock();
                // Allow for t1 to unlock
                barrier2.countDown();
                fsnLock.readUnlock();
            } catch (InterruptedException e) {
                fail("Interrupted during testing");
            }
        }
    };
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    // Look for the differentiating class names in the stack trace
    String stackTracePatternString = String.format("INFO.+%s(.+\n){5}\\Q%%s\\E\\.run", readLockLogStmt);
    Pattern t1Pattern = Pattern.compile(String.format(stackTracePatternString, t1.getClass().getName()));
    assertTrue(t1Pattern.matcher(logs.getOutput()).find());
    Pattern t2Pattern = Pattern.compile(String.format(stackTracePatternString, t2.getClass().getName()));
    assertFalse(t2Pattern.matcher(logs.getOutput()).find());
    assertTrue(logs.getOutput().contains("Number of suppressed read-lock reports: 2"));
}
Also used : Pattern(java.util.regex.Pattern) Configuration(org.apache.hadoop.conf.Configuration) LogCapturer(org.apache.hadoop.test.GenericTestUtils.LogCapturer) CountDownLatch(java.util.concurrent.CountDownLatch) FakeTimer(org.apache.hadoop.util.FakeTimer) Test(org.junit.Test)

Aggregations

FakeTimer (org.apache.hadoop.util.FakeTimer)33 Test (org.junit.Test)30 Groups (org.apache.hadoop.security.Groups)10 HdfsConfiguration (org.apache.hadoop.hdfs.HdfsConfiguration)9 Configuration (org.apache.hadoop.conf.Configuration)8 StorageLocation (org.apache.hadoop.hdfs.server.datanode.StorageLocation)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 IOException (java.io.IOException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 TimeoutException (java.util.concurrent.TimeoutException)2 LogCapturer (org.apache.hadoop.test.GenericTestUtils.LogCapturer)2 Before (org.junit.Before)2 Optional (com.google.common.base.Optional)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1