Search in sources :

Example 6 with AtomicLong

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));
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) FakeTimer(org.apache.hadoop.util.FakeTimer) Test(org.junit.Test)

Example 7 with AtomicLong

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());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Test(org.junit.Test)

Example 8 with AtomicLong

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());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 9 with AtomicLong

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();
        }
    }));
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with AtomicLong

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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)678 Test (org.junit.Test)261 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)93 IOException (java.io.IOException)82 CountDownLatch (java.util.concurrent.CountDownLatch)68 ArrayList (java.util.ArrayList)65 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)64 HashMap (java.util.HashMap)47 Map (java.util.Map)45 Random (java.util.Random)43 List (java.util.List)42 AtomicReference (java.util.concurrent.atomic.AtomicReference)40 File (java.io.File)34 ExecutorService (java.util.concurrent.ExecutorService)24 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)23 HashSet (java.util.HashSet)21 Test (org.testng.annotations.Test)21 InetSocketAddress (java.net.InetSocketAddress)16 InputStream (java.io.InputStream)13 Set (java.util.Set)13