Search in sources :

Example 26 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project guava by google.

the class CycleDetectingLockFactoryTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    CycleDetectingLockFactory factory = CycleDetectingLockFactory.newInstance(Policies.THROW);
    lockA = factory.newReentrantLock("LockA");
    lockB = factory.newReentrantLock("LockB");
    lockC = factory.newReentrantLock("LockC");
    ReentrantReadWriteLock readWriteLockA = factory.newReentrantReadWriteLock("ReadWriteA");
    ReentrantReadWriteLock readWriteLockB = factory.newReentrantReadWriteLock("ReadWriteB");
    ReentrantReadWriteLock readWriteLockC = factory.newReentrantReadWriteLock("ReadWriteC");
    readLockA = readWriteLockA.readLock();
    readLockB = readWriteLockB.readLock();
    readLockC = readWriteLockC.readLock();
    writeLockA = readWriteLockA.writeLock();
    writeLockB = readWriteLockB.writeLock();
    writeLockC = readWriteLockC.writeLock();
    CycleDetectingLockFactory.WithExplicitOrdering<MyOrder> factory2 = newInstanceWithExplicitOrdering(MyOrder.class, Policies.THROW);
    lock1 = factory2.newReentrantLock(MyOrder.FIRST);
    lock2 = factory2.newReentrantLock(MyOrder.SECOND);
    lock3 = factory2.newReentrantLock(MyOrder.THIRD);
    CycleDetectingLockFactory.WithExplicitOrdering<OtherOrder> factory3 = newInstanceWithExplicitOrdering(OtherOrder.class, Policies.THROW);
    lock01 = factory3.newReentrantLock(OtherOrder.FIRST);
    lock02 = factory3.newReentrantLock(OtherOrder.SECOND);
    lock03 = factory3.newReentrantLock(OtherOrder.THIRD);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 27 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project languagetool by languagetool-org.

the class AbstractLanguageConcurrencyTest method testSpellCheckerFailure.

@Ignore("too slow to run every time")
@Test
public void testSpellCheckerFailure() throws Exception {
    String sampleText = createSampleText();
    Language language = createLanguage();
    int threadCount = Runtime.getRuntime().availableProcessors() * 10;
    int testRuns = 100;
    ReadWriteLock testWaitLock = new ReentrantReadWriteLock();
    Lock testWriteLock = testWaitLock.writeLock();
    testWriteLock.lock();
    failedTests = 0;
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread(new TestRunner(testWaitLock, language, testRuns, sampleText));
        t.start();
        threads.add(t);
    }
    // Release the lock and allow all TestRunner threads to do their work.
    testWriteLock.unlock();
    for (Thread t : threads) {
        t.join();
    }
    Assert.assertEquals(0, failedTests);
}
Also used : Language(org.languagetool.Language) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with ReentrantReadWriteLock

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

the class TestInstrumentedReadWriteLock method testWriteLockLongHoldingReport.

/**
   * Tests the warning when the write lock is held longer than threshold.
   * @throws Exception
   */
@Test(timeout = 10000)
public void testWriteLockLongHoldingReport() 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);
    InstrumentedWriteLock writeLock = new InstrumentedWriteLock(testname, LOG, readWriteLock, 2000, 300, mclock) {

        @Override
        protected void logWarning(long lockHeldTime, long suppressed) {
            wlogged.incrementAndGet();
            wsuppresed.set(suppressed);
        }
    };
    // t = 0
    writeLock.lock();
    time.set(100);
    // t = 100
    writeLock.unlock();
    assertEquals(0, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // t = 100
    writeLock.lock();
    time.set(500);
    // t = 500
    writeLock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // the suppress counting is only changed when
    // log is needed in the test
    // t = 500
    writeLock.lock();
    time.set(900);
    // t = 900
    writeLock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // t = 900
    writeLock.lock();
    time.set(3000);
    // t = 3000
    writeLock.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 29 with ReentrantReadWriteLock

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

the class TestSetTimes method testGetBlockLocationsOnlyUsesReadLock.

/**
   * Test that when access time updates are not needed, the FSNamesystem
   * write lock is not taken by getBlockLocations.
   * Regression test for HDFS-3981.
   */
@Test(timeout = 60000)
public void testGetBlockLocationsOnlyUsesReadLock() throws IOException {
    Configuration conf = new HdfsConfiguration();
    conf.setInt(DFSConfigKeys.DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, 100 * 1000);
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
    ReentrantReadWriteLock spyLock = NameNodeAdapter.spyOnFsLock(cluster.getNamesystem());
    try {
        // Create empty file in the FSN.
        Path p = new Path("/empty-file");
        DFSTestUtil.createFile(cluster.getFileSystem(), p, 0, (short) 1, 0L);
        // getBlockLocations() should not need the write lock, since we just created
        // the file (and thus its access time is already within the 100-second
        // accesstime precision configured above). 
        MockitoUtil.doThrowWhenCallStackMatches(new AssertionError("Should not need write lock"), ".*getBlockLocations.*").when(spyLock).writeLock();
        cluster.getFileSystem().getFileBlockLocations(p, 0, 100);
    } finally {
        cluster.shutdown();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Test(org.junit.Test)

Example 30 with ReentrantReadWriteLock

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

the class TestHAStateTransitions method testTransitionSynchronization.

/**
   * Regression test for HDFS-2693: when doing state transitions, we need to
   * lock the FSNamesystem so that we don't end up doing any writes while it's
   * "in between" states.
   * This test case starts up several client threads which do mutation operations
   * while flipping a NN back and forth from active to standby.
   */
@Test(timeout = 120000)
public void testTransitionSynchronization() throws Exception {
    Configuration conf = new Configuration();
    final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).nnTopology(MiniDFSNNTopology.simpleHATopology()).numDataNodes(0).build();
    try {
        cluster.waitActive();
        ReentrantReadWriteLock spyLock = NameNodeAdapter.spyOnFsLock(cluster.getNameNode(0).getNamesystem());
        Mockito.doAnswer(new GenericTestUtils.SleepAnswer(50)).when(spyLock).writeLock();
        final FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
        TestContext ctx = new TestContext();
        for (int i = 0; i < 50; i++) {
            final int finalI = i;
            ctx.addThread(new RepeatingTestThread(ctx) {

                @Override
                public void doAnAction() throws Exception {
                    Path p = new Path("/test-" + finalI);
                    fs.mkdirs(p);
                    fs.delete(p, true);
                }
            });
        }
        ctx.addThread(new RepeatingTestThread(ctx) {

            @Override
            public void doAnAction() throws Exception {
                cluster.transitionToStandby(0);
                Thread.sleep(50);
                cluster.transitionToActive(0);
            }
        });
        ctx.startThreads();
        ctx.waitFor(20000);
        ctx.stop();
    } finally {
        cluster.shutdown();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) GenericTestUtils(org.apache.hadoop.test.GenericTestUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IOException(java.io.IOException) RepeatingTestThread(org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread) FileSystem(org.apache.hadoop.fs.FileSystem) Test(org.junit.Test)

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