Search in sources :

Example 26 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project storm by apache.

the class Time method simulatedSleepUntilNanos.

private static void simulatedSleepUntilNanos(long targetTimeNanos) throws InterruptedException {
    try {
        synchronized (sleepTimesLock) {
            if (threadSleepTimesNanos == null) {
                LOG.debug("{} is still sleeping after simulated time disabled.", Thread.currentThread(), new RuntimeException("STACK TRACE"));
                throw new InterruptedException();
            }
            threadSleepTimesNanos.put(Thread.currentThread(), new AtomicLong(targetTimeNanos));
        }
        while (simulatedCurrTimeNanos.get() < targetTimeNanos) {
            synchronized (sleepTimesLock) {
                if (threadSleepTimesNanos == null) {
                    LOG.debug("{} is still sleeping after simulated time disabled.", Thread.currentThread(), new RuntimeException("STACK TRACE"));
                    throw new InterruptedException();
                }
            }
            long autoAdvance = autoAdvanceNanosOnSleep.get();
            if (autoAdvance > 0) {
                advanceTimeNanos(autoAdvance);
            }
            Thread.sleep(10);
        }
    } finally {
        synchronized (sleepTimesLock) {
            if (simulating.get() && threadSleepTimesNanos != null) {
                threadSleepTimesNanos.remove(Thread.currentThread());
            }
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 27 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project storm by apache.

the class WorkerBackpressureThreadTest method testNormalEvent.

@Test
public void testNormalEvent() throws Exception {
    Object trigger = new Object();
    AtomicLong workerData = new AtomicLong(0);
    WorkerBackpressureCallback callback = new WorkerBackpressureCallback() {

        @Override
        public void onEvent(Object obj) {
            ((AtomicLong) obj).getAndDecrement();
        }
    };
    WorkerBackpressureThread workerBackpressureThread = new WorkerBackpressureThread(trigger, workerData, callback);
    workerBackpressureThread.start();
    WorkerBackpressureThread.notifyBackpressureChecker(trigger);
    long start = System.currentTimeMillis();
    while (workerData.get() == 0) {
        assertTrue("Timeout", (System.currentTimeMillis() - start) < 1000);
        Thread.sleep(100);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 28 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project storm by apache.

the class DisruptorQueueBackpressureTest method testBackPressureCallback.

@Test
public void testBackPressureCallback() throws Exception {
    final DisruptorQueue queue = createQueue("testBackPressure", CAPACITY);
    queue.setEnableBackpressure(true);
    queue.setHighWaterMark(HIGH_WATERMARK);
    queue.setLowWaterMark(LOW_WATERMARK);
    final AtomicBoolean throttleOn = new AtomicBoolean(false);
    // we need to record the cursor because the DisruptorQueue does not update the readPos during batch consuming
    final AtomicLong consumerCursor = new AtomicLong(-1);
    DisruptorBackpressureCallbackImpl cb = new DisruptorBackpressureCallbackImpl(queue, throttleOn, consumerCursor);
    queue.registerBackpressureCallback(cb);
    for (int i = 0; i < MESSAGES; i++) {
        queue.publish(String.valueOf(i));
    }
    queue.consumeBatchWhenAvailable(new EventHandler<Object>() {

        @Override
        public void onEvent(Object o, long l, boolean b) throws Exception {
            consumerCursor.set(l);
        }
    });
    Assert.assertEquals("Check the calling time of throttle on. ", queue.getHighWaterMark(), cb.highWaterMarkCalledPopulation);
    Assert.assertEquals("Checking the calling time of throttle off. ", queue.getLowWaterMark(), cb.lowWaterMarkCalledPopulation);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 29 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project zookeeper by apache.

the class CreateTTLTest method testModifying.

@Test
public void testModifying() throws IOException, KeeperException, InterruptedException {
    Stat stat = new Stat();
    zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100);
    Assert.assertEquals(0, stat.getEphemeralOwner());
    final AtomicLong fakeElapsed = new AtomicLong(0);
    ContainerManager containerManager = newContainerManager(fakeElapsed);
    containerManager.checkContainers();
    Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false));
    for (int i = 0; i < 10; ++i) {
        fakeElapsed.set(50);
        zk.setData("/foo", new byte[i + 1], -1);
        containerManager.checkContainers();
        Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false));
    }
    fakeElapsed.set(200);
    containerManager.checkContainers();
    Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Stat(org.apache.zookeeper.data.Stat) Test(org.junit.Test)

Example 30 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project zookeeper by apache.

the class CreateTTLTest method testMulti.

@Test
public void testMulti() throws IOException, KeeperException, InterruptedException {
    Op createTtl = Op.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, 100);
    Op createTtlSequential = Op.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 200);
    Op createNonTtl = Op.create("/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    List<OpResult> results = zk.multi(Arrays.asList(createTtl, createTtlSequential, createNonTtl));
    String sequentialPath = ((OpResult.CreateResult) results.get(1)).getPath();
    final AtomicLong fakeElapsed = new AtomicLong(0);
    ContainerManager containerManager = newContainerManager(fakeElapsed);
    containerManager.checkContainers();
    Assert.assertNotNull("node should not have been deleted yet", zk.exists("/a", false));
    Assert.assertNotNull("node should not have been deleted yet", zk.exists(sequentialPath, false));
    Assert.assertNotNull("node should never be deleted", zk.exists("/c", false));
    fakeElapsed.set(110);
    containerManager.checkContainers();
    Assert.assertNull("node should have been deleted", zk.exists("/a", false));
    Assert.assertNotNull("node should not have been deleted yet", zk.exists(sequentialPath, false));
    Assert.assertNotNull("node should never be deleted", zk.exists("/c", false));
    fakeElapsed.set(210);
    containerManager.checkContainers();
    Assert.assertNull("node should have been deleted", zk.exists("/a", false));
    Assert.assertNull("node should have been deleted", zk.exists(sequentialPath, false));
    Assert.assertNotNull("node should never be deleted", zk.exists("/c", false));
}
Also used : Op(org.apache.zookeeper.Op) AtomicLong(java.util.concurrent.atomic.AtomicLong) OpResult(org.apache.zookeeper.OpResult) Test(org.junit.Test)

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