Search in sources :

Example 1 with IgniteQueue

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

the class GridCacheQueueMultiNodeAbstractSelfTest method testPutPollCollocated.

/**
     * @throws Exception If failed.
     */
public void testPutPollCollocated() throws Exception {
    try {
        final String queueName = UUID.randomUUID().toString();
        info("Queue name: " + queueName);
        grid(0).queue(queueName, 5, config(true));
        final CountDownLatch latch = new CountDownLatch(1);
        final Ignite g = startGrid(GRID_CNT + 1);
        IgniteInternalFuture<Object> fut1 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                info(">>> Executing put callable [node=" + g.cluster().localNode().id() + ", thread=" + Thread.currentThread().getName() + ']');
                IgniteQueue<Integer> q = g.queue(queueName, 5, config(true));
                assertTrue(q.isEmpty());
                for (int i = 0; i < ITEMS_CNT; i++) {
                    if (i == q.capacity()) {
                        info(">>> Opening latch...");
                        latch.countDown();
                    }
                    put(q, i);
                }
                info(">>> Finished put callable on node: " + g.cluster().localNode().id());
                return null;
            }
        });
        latch.await();
        final Ignite g1 = startGrid(GRID_CNT + 2);
        IgniteInternalFuture<Object> fut2 = GridTestUtils.runAsync(new Callable<Object>() {

            @SuppressWarnings("BusyWait")
            @Override
            public Object call() throws Exception {
                try {
                    info(">>> Executing poll callable [node=" + g1.cluster().localNode().id() + ", thread=" + Thread.currentThread().getName() + ']');
                    IgniteQueue<Integer> q = g1.queue(queueName, 5, config(true));
                    int cnt = 0;
                    int nullCnt = 0;
                    do {
                        Integer i = q.poll();
                        info("Polled value: " + i);
                        if (i != null) {
                            cnt++;
                            nullCnt = 0;
                        } else {
                            if (nullCnt == 3)
                                throw new Exception("Failed to poll non-null value within 3 attempts.");
                            nullCnt++;
                            Thread.sleep(1000);
                        }
                    } while (cnt < ITEMS_CNT);
                    info("Finished poll callable on node: " + g1.cluster().localNode().id());
                    return null;
                } finally {
                    info("Poll callable finished.");
                }
            }
        });
        fut1.get();
        fut2.get();
        grid(0).queue(queueName, 0, null).close();
    } finally {
        stopGrid(GRID_CNT + 1);
        stopGrid(GRID_CNT + 2);
        checkTopology(GRID_CNT);
        awaitPartitionMapExchange();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 2 with IgniteQueue

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

the class GridCacheQueueApiSelfAbstractTest method testSystemCache.

/**
     * @throws Exception If failed.
     */
public void testSystemCache() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    IgniteQueue queue = grid(0).queue("Queue1", 0, colCfg);
    final CacheConfiguration ccfg = getQueueCache(queue);
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            grid(0).cache(ccfg.getName());
            return null;
        }
    }, IllegalStateException.class, "Failed to get cache because it is a system cache");
    assertNotNull(((IgniteKernal) grid(0)).internalCache(ccfg.getName()));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteException(org.apache.ignite.IgniteException)

Example 3 with IgniteQueue

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

the class GridCacheQueueApiSelfAbstractTest method testFilterNode.

/**
     * @throws Exception If failed.
     */
public void testFilterNode() throws Exception {
    CollectionConfiguration colCfg1 = collectionConfiguration();
    CollectionConfiguration colCfg2 = collectionConfiguration();
    colCfg2.setNodeFilter(CacheConfiguration.ALL_NODES);
    IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg1);
    IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg2);
    assertNotSame(getQueueCache(queue1), getQueueCache(queue2));
    colCfg1.setNodeFilter(CacheConfiguration.ALL_NODES);
    IgniteQueue queue3 = grid(0).queue("Queue3", 0, colCfg1);
    assertEquals(getQueueCache(queue2), getQueueCache(queue3));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 4 with IgniteQueue

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

the class GridCacheQueueApiSelfAbstractTest method testPrepareQueue.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testPrepareQueue() throws Exception {
    // Random sequence names.
    String queueName1 = UUID.randomUUID().toString();
    String queueName2 = UUID.randomUUID().toString();
    CollectionConfiguration colCfg = config(false);
    IgniteQueue queue1 = grid(0).queue(queueName1, 0, colCfg);
    IgniteQueue queue2 = grid(0).queue(queueName2, 0, colCfg);
    IgniteQueue queue3 = grid(0).queue(queueName1, 0, colCfg);
    assertNotNull(queue1);
    assertNotNull(queue2);
    assertNotNull(queue3);
    assert queue1.equals(queue3);
    assert queue3.equals(queue1);
    assert !queue3.equals(queue2);
    queue1.close();
    queue2.close();
    queue3.close();
    assertNull(grid(0).queue(queueName1, 0, null));
    assertNull(grid(0).queue(queueName2, 0, null));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 5 with IgniteQueue

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

the class GridCacheQueueApiSelfAbstractTest method testQueueRemoveMultithreadBounded.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testQueueRemoveMultithreadBounded() throws Exception {
    // Random queue name.
    final String queueName = UUID.randomUUID().toString();
    final IgniteQueue<String> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    final CountDownLatch putLatch = new CountDownLatch(THREAD_NUM);
    final CountDownLatch clearLatch = new CountDownLatch(THREAD_NUM);
    IgniteInternalFuture<?> offerFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            if (log.isDebugEnabled())
                log.debug("Thread has been started." + Thread.currentThread().getName());
            try {
                // Thread must be blocked on put operation.
                for (int i = 0; i < (QUEUE_CAPACITY * THREAD_NUM); i++) queue.offer("anything", 3, TimeUnit.MINUTES);
                fail("Queue failed");
            } catch (IgniteException | IllegalStateException e) {
                putLatch.countDown();
                assert e.getMessage().contains("removed");
                assert queue.removed();
            }
            if (log.isDebugEnabled())
                log.debug("Thread has been stopped." + Thread.currentThread().getName());
            return null;
        }
    }, THREAD_NUM, "offer-thread");
    IgniteInternalFuture<?> closeFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                IgniteQueue<String> queue = grid(0).queue(queueName, 0, null);
                if (queue != null)
                    queue.close();
            } catch (Exception e) {
                fail("Unexpected exception: " + e);
            } finally {
                clearLatch.countDown();
            }
            return null;
        }
    }, THREAD_NUM, "close-thread");
    assert putLatch.await(3, TimeUnit.MINUTES);
    assert clearLatch.await(3, TimeUnit.MINUTES);
    offerFut.get();
    closeFut.get();
    try {
        assert queue.isEmpty() : queue.size();
        fail("Queue must be removed.");
    } catch (IgniteException | IllegalStateException e) {
        assert e.getMessage().contains("removed");
        assert queue.removed();
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException)

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