Search in sources :

Example 11 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class GridCacheQueueApiSelfAbstractTest method testReuseCache.

/**
     * @throws Exception If failed.
     */
public void testReuseCache() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg);
    IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg);
    assertEquals(getQueueCache(queue1), getQueueCache(queue2));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 12 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class IgniteDataStructureUniqueNameTest method testUniqueName.

/**
     * @param singleGrid If {@code true} uses single grid.
     * @throws Exception If failed.
     */
private void testUniqueName(final boolean singleGrid) throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final int DS_TYPES = 9;
    final int THREADS = DS_TYPES * 3;
    for (int iter = 0; iter < 20; iter++) {
        log.info("Iteration: " + iter);
        List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        for (int i = 0; i < THREADS; i++) {
            final int idx = i;
            IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        Thread.currentThread().setName("test thread-" + idx);
                        barrier.await();
                        Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
                        Object res;
                        switch(idx % DS_TYPES) {
                            case 0:
                                log.info("Create atomic long, grid: " + ignite.name());
                                res = ignite.atomicLong(name, 0, true);
                                break;
                            case 1:
                                log.info("Create atomic sequence, grid: " + ignite.name());
                                res = ignite.atomicSequence(name, 0, true);
                                break;
                            case 2:
                                log.info("Create atomic stamped, grid: " + ignite.name());
                                res = ignite.atomicStamped(name, 0, true, true);
                                break;
                            case 3:
                                log.info("Create atomic latch, grid: " + ignite.name());
                                res = ignite.countDownLatch(name, 0, true, true);
                                break;
                            case 4:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 5:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 6:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            case 7:
                                log.info("Create atomic semaphore, grid: " + ignite.name());
                                res = ignite.semaphore(name, 0, false, true);
                                break;
                            case 8:
                                log.info("Create atomic reentrant lock, grid: " + ignite.name());
                                res = ignite.reentrantLock(name, true, true, true);
                                break;
                            default:
                                fail();
                                return null;
                        }
                        log.info("Thread created: " + res);
                        return res;
                    } catch (IgniteException e) {
                        log.info("Failed: " + e);
                        return e;
                    }
                }
            });
            futs.add(fut);
        }
        Closeable dataStructure = null;
        int createdCnt = 0;
        for (IgniteInternalFuture<Object> fut : futs) {
            Object res = fut.get();
            if (res instanceof IgniteException || res instanceof IgniteCheckedException)
                continue;
            assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
            log.info("Data structure created: " + dataStructure);
            createdCnt++;
            if (dataStructure != null)
                assertEquals(dataStructure.getClass(), res.getClass());
            else
                dataStructure = (Closeable) res;
        }
        assertNotNull(dataStructure);
        assertEquals(3, createdCnt);
        dataStructure.close();
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteSet(org.apache.ignite.IgniteSet) IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteLock(org.apache.ignite.IgniteLock)

Example 13 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class IgniteClientDataStructuresAbstractTest method testQueue.

/**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
private void testQueue(Ignite creator, final Ignite other) throws Exception {
    assertNull(creator.queue("q1", 0, null));
    assertNull(other.queue("q1", 0, null));
    try (IgniteQueue<Integer> queue = creator.queue("q1", 0, new CollectionConfiguration())) {
        assertNotNull(queue);
        queue.add(1);
        assertEquals(1, queue.poll().intValue());
        IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                U.sleep(1000);
                IgniteQueue<Integer> queue0 = other.queue("q1", 0, null);
                assertEquals(0, queue0.size());
                log.info("Add in queue.");
                queue0.add(2);
                return null;
            }
        });
        log.info("Try take.");
        assertEquals(2, queue.take().intValue());
        log.info("Finished take.");
        fut.get();
    }
    assertNull(creator.queue("q1", 0, null));
    assertNull(other.queue("q1", 0, null));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 14 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestQueue.

/**
     * Tests the queue.
     *
     * @param topWorker Topology change worker.
     * @throws Exception If failed.
     */
private void doTestQueue(ConstantTopologyChangeWorker topWorker) throws Exception {
    int queueMaxSize = 100;
    try (IgniteQueue<Integer> s = grid(0).queue(STRUCTURE_NAME, 0, config(false))) {
        s.put(1);
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {

            @Override
            public Object apply(Ignite ignite) {
                IgniteQueue<Integer> queue = ignite.queue(STRUCTURE_NAME, 0, null);
                assertNotNull(queue);
                Integer val = queue.peek();
                assertNotNull(val);
                assert val > 0;
                return null;
            }
        });
        int val = s.peek();
        while (!fut.isDone()) {
            if (s.size() == queueMaxSize) {
                int last = 0;
                for (int i = 0, size = s.size() - 1; i < size; i++) {
                    int cur = s.poll();
                    if (i == 0) {
                        last = cur;
                        continue;
                    }
                    assertEquals(last, cur - 1);
                    last = cur;
                }
            }
            s.put(++val);
        }
        fut.get();
        val = s.peek();
        for (Ignite g : G.allGrids()) assertEquals(val, (int) g.<Integer>queue(STRUCTURE_NAME, 0, null).peek());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite)

Example 15 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method dataStructureOperationsTest.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void dataStructureOperationsTest() throws Exception {
    clientMode = true;
    final Ignite client = startGrid(serverCount());
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check atomic long.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.atomicLong("testAtomic", 41, true);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.atomicLong("testAtomic", 41, true);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteAtomicLong atomicLong = (IgniteAtomicLong) o;
            assertEquals(42, atomicLong.incrementAndGet());
            return true;
        }
    }), // Check set.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.set("testSet", new CollectionConfiguration());
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.set("testSet", new CollectionConfiguration());
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteSet set = (IgniteSet) o;
            String val = "testVal";
            set.add(val);
            assertEquals(1, set.size());
            assertTrue(set.contains(val));
            return true;
        }
    }), // Check ignite queue.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.queue("TestQueue", 10, new CollectionConfiguration());
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.queue("TestQueue", 10, new CollectionConfiguration());
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteQueue queue = (IgniteQueue) o;
            String val = "Test";
            queue.add(val);
            assertEquals(val, queue.poll());
            return true;
        }
    })));
    clientMode = false;
}
Also used : IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteSet(org.apache.ignite.IgniteSet) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

IgniteQueue (org.apache.ignite.IgniteQueue)16 Ignite (org.apache.ignite.Ignite)8 CollectionConfiguration (org.apache.ignite.configuration.CollectionConfiguration)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 Collection (java.util.Collection)2 Callable (java.util.concurrent.Callable)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteSet (org.apache.ignite.IgniteSet)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 UUID (java.util.UUID)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 CacheException (javax.cache.CacheException)1