use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.
the class TestDatasetVolumeChecker method testCheckOneVolume.
/**
* Test {@link DatasetVolumeChecker#checkVolume} propagates the
* check to the delegate checker.
*
* @throws Exception
*/
@Test(timeout = 10000)
public void testCheckOneVolume() throws Exception {
LOG.info("Executing {}", testName.getMethodName());
final FsVolumeSpi volume = makeVolumes(1, expectedVolumeHealth).get(0);
final DatasetVolumeChecker checker = new DatasetVolumeChecker(new HdfsConfiguration(), new FakeTimer());
checker.setDelegateChecker(new DummyChecker());
final AtomicLong numCallbackInvocations = new AtomicLong(0);
/**
* Request a check and ensure it triggered {@link FsVolumeSpi#check}.
*/
boolean result = checker.checkVolume(volume, new DatasetVolumeChecker.Callback() {
@Override
public void call(Set<FsVolumeSpi> healthyVolumes, Set<FsVolumeSpi> failedVolumes) {
numCallbackInvocations.incrementAndGet();
if (expectedVolumeHealth != null && expectedVolumeHealth != FAILED) {
assertThat(healthyVolumes.size(), is(1));
assertThat(failedVolumes.size(), is(0));
} else {
assertThat(healthyVolumes.size(), is(0));
assertThat(failedVolumes.size(), is(1));
}
}
});
// Ensure that the check was invoked at least once.
verify(volume, times(1)).check(anyObject());
if (result) {
assertThat(numCallbackInvocations.get(), is(1L));
}
}
use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.
the class TestInstrumentedReadWriteLock method testReadLockLongHoldingReport.
/**
* Tests the warning when the read lock is held longer than threshold.
* @throws Exception
*/
@Test(timeout = 10000)
public void testReadLockLongHoldingReport() throws Exception {
String testname = name.getMethodName();
final AtomicLong time = new AtomicLong(0);
Timer mclock = new Timer() {
@Override
public long monotonicNow() {
return time.get();
}
};
final AtomicLong wlogged = new AtomicLong(0);
final AtomicLong wsuppresed = new AtomicLong(0);
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
InstrumentedReadLock readLock = new InstrumentedReadLock(testname, LOG, readWriteLock, 2000, 300, mclock) {
@Override
protected void logWarning(long lockHeldTime, long suppressed) {
wlogged.incrementAndGet();
wsuppresed.set(suppressed);
}
};
// t = 0
readLock.lock();
time.set(100);
// t = 100
readLock.unlock();
assertEquals(0, wlogged.get());
assertEquals(0, wsuppresed.get());
// t = 100
readLock.lock();
time.set(500);
// t = 500
readLock.unlock();
assertEquals(1, wlogged.get());
assertEquals(0, wsuppresed.get());
// the suppress counting is only changed when
// log is needed in the test
// t = 500
readLock.lock();
time.set(900);
// t = 900
readLock.unlock();
assertEquals(1, wlogged.get());
assertEquals(0, wsuppresed.get());
// t = 900
readLock.lock();
time.set(3000);
// t = 3000
readLock.unlock();
assertEquals(2, wlogged.get());
assertEquals(1, wsuppresed.get());
}
use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.
the class TestInstrumentedLock method testLockLongHoldingReport.
/**
* Test the lock logs warning when lock held time is greater than threshold
* and not log warning otherwise.
* @throws Exception
*/
@Test(timeout = 10000)
public void testLockLongHoldingReport() throws Exception {
String testname = name.getMethodName();
final AtomicLong time = new AtomicLong(0);
Timer mclock = new Timer() {
@Override
public long monotonicNow() {
return time.get();
}
};
Lock mlock = mock(Lock.class);
final AtomicLong wlogged = new AtomicLong(0);
final AtomicLong wsuppresed = new AtomicLong(0);
InstrumentedLock lock = new InstrumentedLock(testname, LOG, mlock, 2000, 300, mclock) {
@Override
void logWarning(long lockHeldTime, long suppressed) {
wlogged.incrementAndGet();
wsuppresed.set(suppressed);
}
};
// do not log warning when the lock held time is short
// t = 0
lock.lock();
time.set(200);
// t = 200
lock.unlock();
assertEquals(0, wlogged.get());
assertEquals(0, wsuppresed.get());
// t = 200
lock.lock();
time.set(700);
// t = 700
lock.unlock();
assertEquals(1, wlogged.get());
assertEquals(0, wsuppresed.get());
// despite the lock held time is greater than threshold
// suppress the log warning due to the logging gap
// (not recorded in wsuppressed until next log message)
// t = 700
lock.lock();
time.set(1100);
// t = 1100
lock.unlock();
assertEquals(1, wlogged.get());
assertEquals(0, wsuppresed.get());
// log a warning message when the lock held time is greater the threshold
// and the logging time gap is satisfied. Also should display suppressed
// previous warnings.
time.set(2400);
// t = 2400
lock.lock();
time.set(2800);
// t = 2800
lock.unlock();
assertEquals(2, wlogged.get());
assertEquals(1, wsuppresed.get());
}
use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.
the class InstrumentationService method init.
@Override
@SuppressWarnings("unchecked")
public void init() throws ServiceException {
timersSize = getServiceConfig().getInt(CONF_TIMERS_SIZE, 10);
counterLock = new ReentrantLock();
timerLock = new ReentrantLock();
variableLock = new ReentrantLock();
samplerLock = new ReentrantLock();
Map<String, VariableHolder> jvmVariables = new ConcurrentHashMap<String, VariableHolder>();
counters = new ConcurrentHashMap<String, Map<String, AtomicLong>>();
timers = new ConcurrentHashMap<String, Map<String, Timer>>();
variables = new ConcurrentHashMap<String, Map<String, VariableHolder>>();
samplers = new ConcurrentHashMap<String, Map<String, Sampler>>();
samplersList = new ArrayList<Sampler>();
all = new LinkedHashMap<String, Map<String, ?>>();
all.put("os-env", System.getenv());
all.put("sys-props", (Map<String, ?>) (Map) System.getProperties());
all.put("jvm", jvmVariables);
all.put("counters", (Map) counters);
all.put("timers", (Map) timers);
all.put("variables", (Map) variables);
all.put("samplers", (Map) samplers);
jvmVariables.put("free.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
@Override
public Long getValue() {
return Runtime.getRuntime().freeMemory();
}
}));
jvmVariables.put("max.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
@Override
public Long getValue() {
return Runtime.getRuntime().maxMemory();
}
}));
jvmVariables.put("total.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
@Override
public Long getValue() {
return Runtime.getRuntime().totalMemory();
}
}));
}
use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.
the class InstrumentationService method incr.
@Override
public void incr(String group, String name, long count) {
AtomicLong counter = getToAdd(group, name, AtomicLong.class, counterLock, counters);
counter.addAndGet(count);
}
Aggregations