Search in sources :

Example 41 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.

the class DeadlockDetectorIntegrationTest method testReadLockDeadlock.

@Ignore("ReadWriteLock deadlock detection is not currently supported by DeadlockDetector")
@Test
public void testReadLockDeadlock() throws Exception {
    final ReadWriteLock lock1 = new ReentrantReadWriteLock();
    final ReadWriteLock lock2 = new ReentrantReadWriteLock();
    Thread thread1 = new Thread() {

        @Override
        public void run() {
            stuckThreads.add(Thread.currentThread());
            lock1.readLock().lock();
            Thread thread2 = new Thread() {

                @Override
                public void run() {
                    stuckThreads.add(Thread.currentThread());
                    lock2.readLock().lock();
                    try {
                        lock1.writeLock().tryLock(10, TimeUnit.SECONDS);
                    } catch (InterruptedException ignore) {
                    }
                    lock2.readLock().unlock();
                }
            };
            thread2.start();
            try {
                Thread.sleep(1000);
                lock2.writeLock().tryLock(10, TimeUnit.SECONDS);
            } catch (InterruptedException ignore) {
            }
            lock1.readLock().unlock();
        }
    };
    thread1.start();
    Thread.sleep(2000);
    DeadlockDetector detector = new DeadlockDetector();
    detector.addDependencies(DeadlockDetector.collectAllDependencies("here"));
    LinkedList<Dependency> deadlocks = detector.findDeadlock();
    System.out.println("deadlocks=" + deadlocks);
    assertEquals(4, detector.findDeadlock().size());
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 42 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.

the class ParallelGatewaySenderQueueJUnitTest method createParallelGatewaySenderQueue.

@Before
public void createParallelGatewaySenderQueue() {
    cache = mock(GemFireCacheImpl.class);
    sender = mock(AbstractGatewaySender.class);
    CancelCriterion cancelCriterion = mock(CancelCriterion.class);
    when(sender.getCancelCriterion()).thenReturn(cancelCriterion);
    when(sender.getCache()).thenReturn(cache);
    when(sender.getMaximumQueueMemory()).thenReturn(100);
    when(sender.getLifeCycleLock()).thenReturn(new ReentrantReadWriteLock());
    metaRegionFactory = mock(MetaRegionFactory.class);
    queue = new ParallelGatewaySenderQueue(sender, Collections.emptySet(), 0, 1, metaRegionFactory);
}
Also used : CancelCriterion(org.apache.geode.CancelCriterion) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) AbstractGatewaySender(org.apache.geode.internal.cache.wan.AbstractGatewaySender) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) MetaRegionFactory(org.apache.geode.internal.cache.wan.parallel.ParallelGatewaySenderQueue.MetaRegionFactory) Before(org.junit.Before)

Example 43 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.

the class BucketRegionJUnitTest method setInternalRegionArguments.

@Override
protected void setInternalRegionArguments(InternalRegionArguments ira) {
    // PR specific
    PartitionedRegion pr = mock(PartitionedRegion.class);
    BucketAdvisor ba = mock(BucketAdvisor.class);
    ReadWriteLock primaryMoveLock = new ReentrantReadWriteLock();
    Lock activeWriteLock = primaryMoveLock.readLock();
    when(ba.getActiveWriteLock()).thenReturn(activeWriteLock);
    when(ba.isPrimary()).thenReturn(true);
    ira.setPartitionedRegion(pr).setPartitionedRegionBucketRedundancy(1).setBucketAdvisor(ba);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 44 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project ignite by apache.

the class GridOffheapSnapTreeSelfTest method testMemoryMultithreaded.

/**
     * Test for memory leaks.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings("TypeMayBeWeakened")
public void testMemoryMultithreaded() throws Exception {
    final TestPointerFactory f = new TestPointerFactory(1000);
    final GridUnsafeMemory mem = new GridUnsafeMemory(25000);
    final GridUnsafeGuard guard = new GridUnsafeGuard();
    final GridOffHeapSnapTreeMap<TestPointer, TestPointer> m = new GridOffHeapSnapTreeMap<>(f, f, mem, guard);
    final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    for (int j = 1; j < 20; j++) {
        final int max = Math.min(1000, j * 20);
        multithreaded(new Runnable() {

            @SuppressWarnings({ "LockAcquiredButNotSafelyReleased", "StatementWithEmptyBody", "UnusedDeclaration", "unchecked" })
            @Override
            public void run() {
                Random rnd = new Random();
                for (int i = 0; i < 100000; i++) {
                    int x = 1 + rnd.nextInt(max);
                    TestPointer fx = f.createPointer(x);
                    boolean put = rnd.nextInt(2) != 0;
                    lock.readLock().lock();
                    guard.begin();
                    try {
                        if (put)
                            m.put(fx, fx);
                        else
                            m.remove(fx);
                    } finally {
                        lock.readLock().unlock();
                        guard.end();
                    }
                    if (i % 100 == 0) {
                        lock.writeLock().lock();
                        GridOffHeapSnapTreeMap<TestPointer, TestPointer> m2;
                        try {
                            m.validate();
                            m2 = m.clone();
                            assert m2.equals(m);
                        } finally {
                            lock.writeLock().unlock();
                        }
                        m2.validate();
                        for (GridOffHeapSmartPointer p : m2.values()) assertTrue(((TestPointer) p).refs.get() >= 2);
                        m2.close();
                    }
                }
            }
        }, 29);
        m.validate();
        assertEquals(m.size(), m.nodes(true));
        assertFalse(mem.allocatedSize() == 0);
        X.println(String.valueOf(mem.allocatedSize()));
        int refs = 0;
        for (TestPointer ptr : f.ptrs) refs += ptr.refs.get();
        assertEquals(m.size() * 2 + (m.nodes(false) - m.size()), refs);
        if (j % 2 == 0)
            continue;
        guard.begin();
        try {
            for (int i = 1; i <= max; i++) m.remove(f.createPointer(i));
        } finally {
            guard.end();
        }
        assertEquals(0, m.size());
        assertTrue(m.isEmpty());
        assertEquals(0, m.nodes(false));
        for (TestPointer ptr : f.ptrs) {
            refs = ptr.refs.get();
            assertEquals(0, refs);
        }
    }
    m.close();
    assertEquals(0, mem.allocatedSize());
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Random(java.util.Random)

Example 45 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project ignite by apache.

the class QueueSizeCounterMultiThreadedTest method testQueueSizeCounter.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testQueueSizeCounter() throws Exception {
    final ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>();
    final AtomicInteger sizeCnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicBoolean guard = new AtomicBoolean();
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @SuppressWarnings({ "BusyWait" })
        @Nullable
        @Override
        public Object call() throws Exception {
            int cleanUps = 0;
            while (!done.get()) {
                lock.readLock().lock();
                try {
                    q.add(1);
                    sizeCnt.incrementAndGet();
                } finally {
                    lock.readLock().unlock();
                }
                if (sizeCnt.get() > 100 && guard.compareAndSet(false, true)) {
                    lock.writeLock().lock();
                    try {
                        for (Integer i = q.poll(); i != null; i = q.poll()) sizeCnt.decrementAndGet();
                        cleanUps++;
                        assert sizeCnt.get() == 0 : "Invalid count [cnt=" + sizeCnt.get() + ", size=" + q.size() + ", entries=" + q + ']';
                    } finally {
                        lock.writeLock().unlock();
                        guard.set(false);
                    }
                }
            }
            X.println("Cleanups count (per thread): " + cleanUps);
            return null;
        }
    }, 100, "test-thread");
    Thread.sleep(3 * 60 * 1000);
    done.set(true);
    fut1.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)52 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)6 Lock (java.util.concurrent.locks.Lock)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 HashMap (java.util.HashMap)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 IOException (java.io.IOException)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 PostLoad (javax.persistence.PostLoad)2 Configuration (org.apache.hadoop.conf.Configuration)2 Path (org.apache.hadoop.fs.Path)2