Search in sources :

Example 11 with GridConcurrentHashSet

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

the class IgniteLockAbstractSelfTest method testLockInterruptibly.

/**
     * @throws Exception If failed.
     */
private void testLockInterruptibly(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.lockInterruptibly();
            } catch (IgniteInterruptedException ignored) {
                assertFalse(Thread.currentThread().isInterrupted());
                isInterrupted = true;
            } finally {
                // Assert that thread was interrupted.
                assertTrue(isInterrupted);
                // Assert that locked is still owned by main thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread doesn't own the lock.
                assertFalse(lock0.isHeldByCurrentThread());
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(1000);
    }
    for (Thread t : startedThreads) t.interrupt();
    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)

Example 12 with GridConcurrentHashSet

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

the class HadoopIgfs20FileSystemAbstractSelfTest 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(primaryFsUri), "/dir");
    fs.mkdir(dir, FsPermission.getDefault(), true);
    final Path file = new Path(dir, "file");
    fs.create(file, EnumSet.noneOf(CreateFlag.class), Options.CreateOpts.perms(FsPermission.getDefault())).close();
    final AtomicInteger cnt = new AtomicInteger();
    final Collection<Integer> errs = new GridConcurrentHashSet<>(THREAD_CNT, 1.0f, THREAD_CNT);
    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, EnumSet.of(CreateFlag.OVERWRITE), Options.CreateOpts.perms(FsPermission.getDefault()));
                os.write(data);
            } catch (IOException ignore) {
                errs.add(idx);
            } finally {
                U.awaitQuiet(barrier);
                U.closeQuiet(os);
            }
        }
    }, THREAD_CNT);
    // Only one thread could obtain write lock on the file.
    assert errs.size() == THREAD_CNT - 1 : "Invalid errors count [expected=" + (THREAD_CNT - 1) + ", actual=" + errs.size() + ']';
    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) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException)

Example 13 with GridConcurrentHashSet

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

the class SocketStreamerSelfTest method test.

/**
     * @param converter Converter.
     * @param r Runnable..
     */
private void test(@Nullable SocketMessageConverter<Message> converter, @Nullable byte[] delim, Runnable r, boolean oneMessagePerTuple) throws Exception {
    SocketStreamer<Message, Integer, String> sockStmr = null;
    Ignite ignite = grid(0);
    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
    cache.clear();
    try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
        stmr.allowOverwrite(true);
        stmr.autoFlushFrequency(10);
        sockStmr = new SocketStreamer<>();
        sockStmr.setIgnite(ignite);
        sockStmr.setStreamer(stmr);
        sockStmr.setPort(port);
        sockStmr.setDelimiter(delim);
        if (oneMessagePerTuple) {
            sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<Message, Integer, String>() {

                @Override
                public Map.Entry<Integer, String> extract(Message msg) {
                    return new IgniteBiTuple<>(msg.key, msg.val);
                }
            });
        } else {
            sockStmr.setMultipleTupleExtractor(new StreamMultipleTupleExtractor<Message, Integer, String>() {

                @Override
                public Map<Integer, String> extract(Message msg) {
                    Map<Integer, String> answer = new HashMap<>();
                    for (int value : msg.values) {
                        answer.put(value, Integer.toString(value));
                    }
                    return answer;
                }
            });
        }
        if (converter != null)
            sockStmr.setConverter(converter);
        final CountDownLatch latch = new CountDownLatch(CNT);
        final GridConcurrentHashSet<CacheEvent> evts = new GridConcurrentHashSet<>();
        IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {

            @Override
            public boolean apply(UUID uuid, CacheEvent evt) {
                evts.add(evt);
                latch.countDown();
                return true;
            }
        };
        ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(locLsnr, null, EVT_CACHE_OBJECT_PUT);
        sockStmr.start();
        r.run();
        latch.await();
        for (int i = 0; i < CNT; i++) {
            Object val = cache.get(i);
            String exp = Integer.toString(i);
            if (!exp.equals(val))
                log.error("Unexpected cache value [key=" + i + ", exp=" + exp + ", val=" + val + ", evts=" + evts + ']');
            assertEquals(exp, val);
        }
        assertEquals(CNT, cache.size(CachePeekMode.PRIMARY));
    } finally {
        if (sockStmr != null)
            sockStmr.stop();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with GridConcurrentHashSet

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

the class HadoopIgfs20FileSystemAbstractSelfTest 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(primaryFsUri), "/dir");
    fs.mkdir(dir, FsPermission.getDefault(), true);
    final Path file = new Path(dir, "file");
    fs.create(file, EnumSet.noneOf(CreateFlag.class), Options.CreateOpts.perms(FsPermission.getDefault())).close();
    final AtomicInteger cnt = new AtomicInteger();
    final Collection<Integer> errs = new GridConcurrentHashSet<>(THREAD_CNT, 1.0f, THREAD_CNT);
    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.create(file, EnumSet.of(CreateFlag.APPEND), Options.CreateOpts.perms(FsPermission.getDefault()));
                os.write(data);
            } catch (IOException ignore) {
                errs.add(idx);
            } finally {
                U.awaitQuiet(barrier);
                U.closeQuiet(os);
            }
        }
    }, THREAD_CNT);
    // 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) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException)

Example 15 with GridConcurrentHashSet

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

the class GridMessagingSelfTest method testSendReceiveMessageWithEnumTopic.

/**
     * Tests simple message sending-receiving with enumerated topic.
     *
     * @throws Exception If error occurs.
     */
public void testSendReceiveMessageWithEnumTopic() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    //to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch rcvLatch = new CountDownLatch(3);
    ignite1.message().localListen(TestTopic.TOPIC_1, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_1 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_1.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(TestTopic.TOPIC_2, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_2 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_2.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_2);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=default]");
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_3.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: default");
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite1.cluster().forLocal();
    message(rNode1).send(TestTopic.TOPIC_1, MSG_1);
    message(rNode1).send(TestTopic.TOPIC_2, MSG_2);
    message(rNode1).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

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