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);
}
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);
}
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"));
}
Aggregations