Search in sources :

Example 36 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project deltaspike by apache.

the class LockSupplierStorage method newLock.

@Override
public ReadWriteLock newLock(final AnnotatedMethod<?> method, final boolean fair) {
    final String name = method.getJavaMember().getDeclaringClass().getName();
    ReadWriteLock lock = locks.get(name);
    if (lock == null) {
        lock = new ReentrantReadWriteLock(fair);
        final ReadWriteLock existing = locks.putIfAbsent(name, lock);
        if (existing != null) {
            lock = existing;
        }
    }
    return lock;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 37 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock 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 38 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock 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 39 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jdk8u_jdk by JetBrains.

the class Basic method realMain.

private static void realMain(String[] args) throws Throwable {
    Thread.currentThread().setName("mainThread");
    //----------------------------------------------------------------
    try {
        final StampedLock sl = new StampedLock();
        check(!sl.isReadLocked());
        check(!sl.isWriteLocked());
        long stamp = sl.tryOptimisticRead();
        check(stamp != 0L);
        check(sl.validate(stamp));
        check(!sl.validate(0));
        stamp = sl.writeLock();
        try {
            check(sl.validate(stamp));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
            check(sl.tryReadLock() == 0L);
            check(sl.tryReadLock(100, MILLISECONDS) == 0L);
            check(sl.tryOptimisticRead() == 0L);
            check(sl.tryWriteLock() == 0L);
            check(sl.tryWriteLock(100, MILLISECONDS) == 0L);
            check(!sl.tryUnlockRead());
            check(sl.tryConvertToWriteLock(stamp) == stamp);
            try {
                sl.unlockRead(stamp);
                fail("Expected unlockRead to throw when not holding read lock");
            } catch (IllegalMonitorStateException x) {
                pass();
            }
            check(sl.validate(stamp));
        } finally {
            sl.unlockWrite(stamp);
        }
        check(!sl.isWriteLocked());
        stamp = sl.readLock();
        try {
            check(sl.validate(stamp));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(sl.tryOptimisticRead() != 0L);
            check(sl.tryWriteLock() == 0L);
            check(sl.tryWriteLock(100, MILLISECONDS) == 0L);
            check(!sl.tryUnlockWrite());
            check(sl.tryConvertToReadLock(stamp) == stamp);
            try {
                sl.unlockWrite(stamp);
                fail("Expected unlockWrite to throw when not holding read lock");
            } catch (IllegalMonitorStateException x) {
                pass();
            }
            check(sl.validate(stamp));
        } finally {
            sl.unlockRead(stamp);
        }
        check(!sl.isReadLocked());
        stamp = sl.tryReadLock(100, MILLISECONDS);
        try {
            check(stamp != 0L);
        } finally {
            sl.unlockRead(stamp);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        Phaser gate = new Phaser(102);
        Iterator<Writer> writers = writerIterator(sl, gate);
        Iterator<Reader> readers = readerIterator(sl, gate);
        for (int i = 0; i < 10; i++) {
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            check(sl.tryOptimisticRead() != 0L);
            Locker[] wThreads = new Locker[100];
            ;
            for (int j = 0; j < 100; j++) wThreads[j] = writers.next();
            for (int j = 0; j < 100; j++) wThreads[j].start();
            Reader reader = readers.next();
            reader.start();
            toTheStartingGate(gate);
            reader.join();
            for (int j = 0; j < 100; j++) wThreads[j].join();
            for (int j = 0; j < 100; j++) checkResult(wThreads[j], null);
            checkResult(reader, null);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        Phaser gate = new Phaser(102);
        Iterator<Writer> writers = writerIterator(sl, gate);
        Iterator<Reader> readers = readerIterator(sl, gate);
        for (int i = 0; i < 10; i++) {
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            check(sl.tryOptimisticRead() != 0L);
            Locker[] rThreads = new Locker[100];
            ;
            for (int j = 0; j < 100; j++) rThreads[j] = readers.next();
            for (int j = 0; j < 100; j++) rThreads[j].start();
            Writer writer = writers.next();
            writer.start();
            toTheStartingGate(gate);
            writer.join();
            for (int j = 0; j < 100; j++) rThreads[j].join();
            for (int j = 0; j < 100; j++) checkResult(rThreads[j], null);
            checkResult(writer, null);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        boolean view = false;
        StampedLock sl = new StampedLock();
        for (long timeout : new long[] { -1L, 30L, -1L, 30L }) {
            long stamp = sl.writeLock();
            try {
                Reader r = interruptibleReader(sl, timeout, SECONDS, null, view);
                r.start();
                // allow r to block
                Thread.sleep(2000);
                r.interrupt();
                r.join();
                checkResult(r, InterruptedException.class);
            } finally {
                sl.unlockWrite(stamp);
            }
            stamp = sl.readLock();
            try {
                Writer w = interruptibleWriter(sl, timeout, SECONDS, null, view);
                w.start();
                // allow w to block
                Thread.sleep(2000);
                w.interrupt();
                w.join();
                checkResult(w, InterruptedException.class);
            } finally {
                sl.unlockRead(stamp);
            }
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            check(sl.tryOptimisticRead() != 0L);
            if (timeout == 30L)
                view = true;
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        for (long timeout : new long[] { 0L, 5L }) {
            long stamp = sl.writeLock();
            try {
                check(sl.tryReadLock(timeout, SECONDS) == 0L);
            } finally {
                sl.unlockWrite(stamp);
            }
            stamp = sl.readLock();
            try {
                check(sl.tryWriteLock(timeout, SECONDS) == 0L);
            } finally {
                sl.unlockRead(stamp);
            }
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            check(sl.tryOptimisticRead() != 0L);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        Iterator<Writer> writers = writerIterator(sl, null);
        Iterator<Reader> readers = readerIterator(sl, null);
        for (int i = 0; i < 10; i++) {
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            long stamp = sl.tryOptimisticRead();
            check(stamp != 0L);
            check(sl.tryConvertToOptimisticRead(stamp) == stamp);
            Reader r = readers.next();
            r.start();
            r.join();
            checkResult(r, null);
            check(sl.validate(stamp));
            check(sl.tryConvertToOptimisticRead(stamp) == stamp);
            Writer w = writers.next();
            w.start();
            w.join();
            checkResult(w, null);
            check(sl.validate(stamp) == false);
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        for (int i = 0; i < 2; i++) {
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(!sl.tryUnlockRead());
            check(!sl.tryUnlockWrite());
            long stamp = sl.tryOptimisticRead();
            check(stamp != 0L);
            check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
            check(sl.validate(stamp));
            check(sl.isReadLocked());
            check(sl.tryWriteLock() == 0L);
            check(sl.tryWriteLock(1L, SECONDS) == 0L);
            check((stamp = sl.tryConvertToWriteLock(stamp)) != 0L);
            check(sl.validate(stamp));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
            check(sl.tryReadLock(1L, SECONDS) == 0L);
            if (i != 0) {
                sl.unlockWrite(stamp);
                continue;
            }
            // convert down
            check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
            check(sl.validate(stamp));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(sl.tryWriteLock() == 0L);
            check(sl.tryWriteLock(1L, SECONDS) == 0L);
            check((stamp = sl.tryConvertToOptimisticRead(stamp)) != 0L);
            check(sl.validate(stamp));
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            check(sl.validate(stamp));
        }
    } catch (Throwable t) {
        unexpected(t);
    }
    //----------------------------------------------------------------
    try {
        StampedLock sl = new StampedLock();
        Lock rl = sl.asReadLock();
        Lock wl = sl.asWriteLock();
        for (int i = 0; i < 2; i++) {
            rl.lock();
            try {
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1L, SECONDS) == 0L);
            } finally {
                rl.unlock();
            }
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            wl.lock();
            try {
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1L, SECONDS) == 0L);
            } finally {
                wl.unlock();
            }
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            ReadWriteLock rwl = sl.asReadWriteLock();
            rl = rwl.readLock();
            wl = rwl.writeLock();
        }
    } catch (Throwable t) {
        unexpected(t);
    }
}
Also used : Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) StampedLock(java.util.concurrent.locks.StampedLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) StampedLock(java.util.concurrent.locks.StampedLock) Phaser(java.util.concurrent.Phaser)

Example 40 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project hive by apache.

the class QueryTracker method registerFragment.

/**
 * Register a new fragment for a specific query
 */
QueryFragmentInfo registerFragment(QueryIdentifier queryIdentifier, String appIdString, String dagIdString, String dagName, String hiveQueryIdString, int dagIdentifier, String vertexName, int fragmentNumber, int attemptNumber, String user, SignableVertexSpec vertex, Token<JobTokenIdentifier> appToken, String fragmentIdString, LlapTokenInfo tokenInfo, final LlapNodeId amNodeId) throws IOException {
    ReadWriteLock dagLock = getDagLock(queryIdentifier);
    // Note: This is a readLock to prevent a race with queryComplete. Operations
    // and mutations within this lock need to be on concurrent structures.
    dagLock.readLock().lock();
    try {
        if (completedDagMap.contains(queryIdentifier)) {
            // Cleanup the dag lock here, since it may have been created after the query completed
            dagSpecificLocks.remove(queryIdentifier);
            String message = "Dag " + dagName + " already complete. Rejecting fragment [" + vertexName + ", " + fragmentNumber + ", " + attemptNumber + "]";
            LOG.info(message);
            throw new RuntimeException(message);
        }
        // out of the request provided that it's signed.
        if (tokenInfo == null) {
            tokenInfo = LlapTokenChecker.getTokenInfo(clusterId);
        }
        boolean isExistingQueryInfo = true;
        QueryInfo queryInfo = queryInfoMap.get(queryIdentifier);
        if (queryInfo == null) {
            if (UserGroupInformation.isSecurityEnabled()) {
                Preconditions.checkNotNull(tokenInfo.userName);
            }
            queryInfo = new QueryInfo(queryIdentifier, appIdString, dagIdString, dagName, hiveQueryIdString, dagIdentifier, user, getSourceCompletionMap(queryIdentifier), localDirsBase, localFs, tokenInfo.userName, tokenInfo.appId, amNodeId, vertex.getTokenIdentifier(), appToken, vertex.getIsExternalSubmission());
            QueryInfo old = queryInfoMap.putIfAbsent(queryIdentifier, queryInfo);
            if (old != null) {
                queryInfo = old;
            } else {
                isExistingQueryInfo = false;
            }
        }
        if (isExistingQueryInfo) {
            // We already retrieved the incoming info, check without UGI.
            LlapTokenChecker.checkPermissions(tokenInfo, queryInfo.getTokenUserName(), queryInfo.getTokenAppId(), queryInfo.getQueryIdentifier());
        }
        queryIdentifierToHiveQueryId.putIfAbsent(queryIdentifier, hiveQueryIdString);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Registering request for {} with the ShuffleHandler", queryIdentifier);
        }
        if (!vertex.getIsExternalSubmission()) {
            ShuffleHandler.get().registerDag(appIdString, dagIdentifier, appToken, user, queryInfo.getLocalDirs());
        }
        return queryInfo.registerFragment(vertexName, fragmentNumber, attemptNumber, vertex, fragmentIdString);
    } finally {
        dagLock.readLock().unlock();
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)45 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)36 Lock (java.util.concurrent.locks.Lock)21 Test (org.junit.Test)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 ArrayList (java.util.ArrayList)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Ignore (org.junit.Ignore)2 Member (cz.metacentrum.perun.core.api.Member)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1