Search in sources :

Example 6 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class IgniteHadoopFileSystemAbstractSelfTest method testMultithreadedAppend.

/**
     * Ensure that when running in multithreaded mode only one append() operation succeed.
     *
     * @throws Exception If failed.
     */
public void testMultithreadedAppend() throws Exception {
    Path dir = new Path(new Path(PRIMARY_URI), "/dir");
    assert fs.mkdirs(dir);
    final Path file = new Path(dir, "file");
    fs.create(file).close();
    final AtomicInteger cnt = new AtomicInteger();
    final Collection<Integer> errs = new GridConcurrentHashSet<>(THREAD_CNT, 1.0f, THREAD_CNT);
    final AtomicBoolean err = new AtomicBoolean();
    multithreaded(new Runnable() {

        @Override
        public void run() {
            int idx = cnt.getAndIncrement();
            byte[] data = new byte[256];
            Arrays.fill(data, (byte) idx);
            U.awaitQuiet(barrier);
            FSDataOutputStream os = null;
            try {
                os = fs.append(file);
            } catch (IOException ignore) {
                errs.add(idx);
            }
            U.awaitQuiet(barrier);
            try {
                if (os != null)
                    os.write(data);
            } catch (IOException ignore) {
                err.set(true);
            } finally {
                U.closeQuiet(os);
            }
        }
    }, THREAD_CNT);
    assert !err.get();
    // Only one thread could obtain write lock on the file.
    assert errs.size() == THREAD_CNT - 1;
    int idx = -1;
    for (int i = 0; i < THREAD_CNT; i++) {
        if (!errs.remove(i)) {
            idx = i;
            break;
        }
    }
    byte[] expData = new byte[256];
    Arrays.fill(expData, (byte) idx);
    FSDataInputStream is = fs.open(file);
    byte[] data = new byte[256];
    is.read(data);
    is.close();
    assert Arrays.equals(expData, data);
}
Also used : Path(org.apache.hadoop.fs.Path) IgfsPath(org.apache.ignite.igfs.IgfsPath) IOException(java.io.IOException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 7 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class IgniteHadoopFileSystemAbstractSelfTest method testMultithreadedCreate.

/**
     * Ensure that when running in multithreaded mode only one create() operation succeed.
     *
     * @throws Exception If failed.
     */
public void testMultithreadedCreate() throws Exception {
    Path dir = new Path(new Path(PRIMARY_URI), "/dir");
    assert fs.mkdirs(dir);
    final Path file = new Path(dir, "file");
    fs.create(file).close();
    final AtomicInteger cnt = new AtomicInteger();
    final Collection<Integer> errs = new GridConcurrentHashSet<>(THREAD_CNT, 1.0f, THREAD_CNT);
    final AtomicBoolean err = new AtomicBoolean();
    multithreaded(new Runnable() {

        @Override
        public void run() {
            int idx = cnt.getAndIncrement();
            byte[] data = new byte[256];
            Arrays.fill(data, (byte) idx);
            FSDataOutputStream os = null;
            try {
                os = fs.create(file, true);
            } catch (IOException ignore) {
                errs.add(idx);
            }
            U.awaitQuiet(barrier);
            try {
                if (os != null)
                    os.write(data);
            } catch (IOException ignore) {
                err.set(true);
            } finally {
                U.closeQuiet(os);
            }
        }
    }, THREAD_CNT);
    assert !err.get();
    // Only one thread could obtain write lock on the file.
    assert errs.size() == THREAD_CNT - 1;
    int idx = -1;
    for (int i = 0; i < THREAD_CNT; i++) {
        if (!errs.remove(i)) {
            idx = i;
            break;
        }
    }
    byte[] expData = new byte[256];
    Arrays.fill(expData, (byte) idx);
    FSDataInputStream is = fs.open(file);
    byte[] data = new byte[256];
    is.read(data);
    is.close();
    assert Arrays.equals(expData, data) : "Expected=" + Arrays.toString(expData) + ", actual=" + Arrays.toString(data);
}
Also used : Path(org.apache.hadoop.fs.Path) IgfsPath(org.apache.ignite.igfs.IgfsPath) IOException(java.io.IOException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 8 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class IgniteLockAbstractSelfTest method testHasConditionQueuedThreads.

/**
     * @throws Exception If failed.
     */
private void testHasConditionQueuedThreads(final boolean fair) throws Exception {
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    final int totalThreads = 5;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    final Set<Thread> finishedThreads = new GridConcurrentHashSet<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            IgniteCondition cond = lock0.getOrCreateCondition("cond");
            lock0.lock();
            startedThreads.add(Thread.currentThread());
            // Wait until every thread tries to lock.
            do {
                cond.await();
                Thread.sleep(1000);
            } while (startedThreads.size() != totalThreads);
            try {
                info("Acquired in separate thread. Number of threads waiting on condition: " + lock0.getWaitQueueLength(cond));
                assertTrue(lock0.isHeldByCurrentThread());
                assertFalse(lock0.hasQueuedThread(Thread.currentThread()));
                finishedThreads.add(Thread.currentThread());
                if (startedThreads.size() != finishedThreads.size()) {
                    assertTrue(lock0.hasWaiters(cond));
                }
                for (Thread t : startedThreads) {
                    if (!finishedThreads.contains(t))
                        assertTrue(lock0.hasWaiters(cond));
                }
                assertTrue(lock0.getWaitQueueLength(cond) == (startedThreads.size() - finishedThreads.size()));
            } finally {
                cond.signal();
                lock0.unlock();
                assertFalse(lock0.isHeldByCurrentThread());
            }
            return null;
        }
    }, totalThreads);
    IgniteCondition cond = lock0.getOrCreateCondition("cond");
    lock0.lock();
    try {
        // Wait until all threads are waiting on condition.
        while (lock0.getWaitQueueLength(cond) != totalThreads) {
            lock0.unlock();
            Thread.sleep(1000);
            lock0.lock();
        }
        // Signal once to get things started.
        cond.signal();
    } finally {
        lock0.unlock();
    }
    fut.get();
    assertFalse(lock0.hasQueuedThreads());
    for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
    lock0.close();
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteLock(org.apache.ignite.IgniteLock) IgniteCondition(org.apache.ignite.IgniteCondition) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 9 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class IgniteLockAbstractSelfTest method testLock.

/**
     * @throws Exception If failed.
     */
private void testLock(final boolean fair) throws Exception {
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    final int totalThreads = 2;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    lock0.lock();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            boolean isInterrupted = false;
            try {
                lock0.lock();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
                fail("Lock() method is uninterruptible.");
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that interrupted flag is set and clear it in order to call unlock().
                assertTrue(Thread.interrupted());
                // Assert that lock is still owned by this thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertTrue(lock0.isHeldByCurrentThread());
                // Release lock.
                lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(500);
    }
    for (Thread t : startedThreads) t.interrupt();
    lock0.unlock();
    fut.get();
    assertFalse(lock0.isLocked());
    for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
    lock0.close();
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteLock(org.apache.ignite.IgniteLock) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 10 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class IgniteLockAbstractSelfTest method testTryLockTimed.

/**
     * @throws Exception If failed.
     */
private void testTryLockTimed(final boolean fair) throws Exception {
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    final int totalThreads = 2;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    lock0.lock();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            boolean isInterrupted = false;
            boolean locked = false;
            try {
                locked = lock0.tryLock(100, TimeUnit.MILLISECONDS);
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that tryLock returned false.
                assertFalse(locked);
                // Assert that lock is still owned by main thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread doesn't own the lock.
                assertFalse(lock0.isHeldByCurrentThread());
                // Release lock.
                if (locked)
                    lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    fut.get();
    lock0.unlock();
    assertFalse(lock0.isLocked());
    for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
    lock0.close();
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteLock(org.apache.ignite.IgniteLock) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Aggregations

GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)22 IOException (java.io.IOException)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)10 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)8 IgniteLock (org.apache.ignite.IgniteLock)8 ExpectedException (org.junit.rules.ExpectedException)8 UUID (java.util.UUID)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)4 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)4 Path (org.apache.hadoop.fs.Path)4 Ignite (org.apache.ignite.Ignite)4 IgfsPath (org.apache.ignite.igfs.IgfsPath)4 ArrayList (java.util.ArrayList)3