Search in sources :

Example 6 with IgniteQueue

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

the class GridCachePartitionedQueueEntryMoveSelfTest method startAdditionalNodes.

/**
     * Start additional nodes above {@link #GRID_CNT}.
     *
     * @param cnt Number of additional nodes.
     * @param queueName Queue name.
     * @throws Exception If failed.
     */
private void startAdditionalNodes(int cnt, String queueName) throws Exception {
    IgniteQueue queue = ignite(0).queue(queueName, 0, null);
    CacheConfiguration cCfg = getQueueCache(queue);
    Collection<ClusterNode> aff1 = ignite(0).affinity(cCfg.getName()).mapKeyToPrimaryAndBackups(queueName);
    for (int i = 0, id = GRID_CNT; i < cnt; i++) {
        startGrid(id++);
        awaitPartitionMapExchange();
        Collection<ClusterNode> aff2 = ignite(0).affinity(cCfg.getName()).mapKeyToPrimaryAndBackups(queueName);
        if (!aff1.iterator().next().equals(aff2.iterator().next())) {
            info("Moved queue to new primary node [oldAff=" + aff1 + ", newAff=" + aff2 + ']');
            return;
        }
    }
    throw new IgniteCheckedException("Unable to move the queue to a new primary node");
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteQueue(org.apache.ignite.IgniteQueue) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with IgniteQueue

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

the class GridCachePartitionedQueueCreateMultiNodeSelfTest method testQueueCreation.

/**
     * @throws Exception If failed.
     */
public void testQueueCreation() throws Exception {
    final AtomicInteger idx = new AtomicInteger();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int idx0 = idx.getAndIncrement();
            Thread.currentThread().setName("createQueue-" + idx0);
            final Ignite ignite = startGrid(idx0);
            UUID locNodeId = ignite.cluster().localNode().id();
            info("Started grid: " + locNodeId);
            info("Creating queue: " + locNodeId);
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ignite.queue("queue", 1, config(true));
                    return null;
                }
            }, 10, "create-queue-" + ignite.name());
            IgniteQueue<String> q = ignite.queue("queue", 1, config(true));
            assert q != null;
            info("Putting first value: " + locNodeId);
            q.offer("val", 1000, MILLISECONDS);
            info("Putting second value: " + locNodeId);
            boolean res2 = q.offer("val1", 1000, MILLISECONDS);
            assert !res2;
            info("Thread finished: " + locNodeId);
            return null;
        }
    }, 10);
    fut.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) Callable(java.util.concurrent.Callable)

Example 8 with IgniteQueue

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

the class GridCacheQueueMultiNodeAbstractSelfTest method testIterator.

/**
     * @throws Exception If failed.
     */
public void testIterator() throws Exception {
    final String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName);
    try (IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false))) {
        assertTrue(queue.isEmpty());
        grid(0).compute().broadcast(new AddAllJob(queueName, RETRIES));
        assertEquals(GRID_CNT * RETRIES, queue.size());
        Collection<ClusterNode> nodes = grid(0).cluster().nodes();
        for (ClusterNode node : nodes) {
            Collection<Integer> queueElements = compute(grid(0).cluster().forNode(node)).call(new IgniteCallable<Collection<Integer>>() {

                @IgniteInstanceResource
                private Ignite grid;

                /** {@inheritDoc} */
                @Override
                public Collection<Integer> call() throws Exception {
                    Collection<Integer> values = new ArrayList<>();
                    grid.log().info("Running job [node=" + grid.cluster().localNode().id() + "]");
                    IgniteQueue<Integer> locQueue = grid.queue(queueName, 0, null);
                    grid.log().info("Queue size " + locQueue.size());
                    for (Integer element : locQueue) values.add(element);
                    grid.log().info("Returning: " + values);
                    return values;
                }
            });
            assertTrue(F.eqOrdered(queue, queueElements));
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteQueue(org.apache.ignite.IgniteQueue) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite)

Example 9 with IgniteQueue

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

the class GridCacheQueueMultiNodeConsistencySelfTest method checkCacheQueue.

/**
     * Starts {@code GRID_CNT} nodes, broadcasts {@code AddAllJob} to them then starts new grid and
     * reads cache queue content and finally asserts queue content is the same.
     *
     * @throws Exception If failed.
     */
private void checkCacheQueue() throws Exception {
    startGrids(GRID_CNT);
    final String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue0 = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    assertTrue(queue0.isEmpty());
    grid(0).compute().broadcast(new AddAllJob(queueName, RETRIES));
    assertEquals(GRID_CNT * RETRIES, queue0.size());
    if (stopRandomGrid)
        stopGrid(1 + new Random().nextInt(GRID_CNT));
    if (forceRepartition) {
        for (int i = 0; i < GRID_CNT; i++) {
            IgniteKernal ignite = (IgniteKernal) grid(i);
            boolean found = false;
            for (GridCacheContext ctx : ignite.context().cache().context().cacheContexts()) {
                if (ctx.name() != null && ctx.name().startsWith("datastructures")) {
                    ctx.cache().rebalance().get();
                    found = true;
                }
            }
            assertTrue(found);
        }
    }
    Ignite newIgnite = startGrid(GRID_CNT + 1);
    // Intentionally commented code cause in this way inconsistent queue problem doesn't appear.
    // IgniteQueue<Integer> newQueue = newGrid.cache().queue(queueName);
    // assertTrue(CollectionUtils.isEqualCollection(queue0, newQueue));
    Collection<Integer> locQueueContent = compute(newIgnite.cluster().forLocal()).call(new IgniteCallable<Collection<Integer>>() {

        @IgniteInstanceResource
        private Ignite grid;

        /** {@inheritDoc} */
        @Override
        public Collection<Integer> call() throws Exception {
            Collection<Integer> values = new ArrayList<>();
            grid.log().info("Running job [node=" + grid.cluster().localNode().id() + ", job=" + this + "]");
            IgniteQueue<Integer> locQueue = grid.queue(queueName, QUEUE_CAPACITY, config(false));
            grid.log().info("Queue size " + locQueue.size());
            for (Integer element : locQueue) values.add(element);
            return values;
        }
    });
    assertTrue(CollectionUtils.isEqualCollection(queue0, locQueueContent));
    queue0.close();
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AddAllJob(org.apache.ignite.internal.processors.cache.datastructures.GridCacheQueueMultiNodeAbstractSelfTest.AddAllJob) Random(java.util.Random) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteQueue(org.apache.ignite.IgniteQueue) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite)

Example 10 with IgniteQueue

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

the class GridCacheQueueApiSelfAbstractTest method testNotReuseCache.

/**
     * @throws Exception If failed.
     */
public void testNotReuseCache() throws Exception {
    CollectionConfiguration colCfg1 = collectionConfiguration();
    CollectionConfiguration colCfg2 = collectionConfiguration();
    if (colCfg2.getAtomicityMode() == ATOMIC)
        colCfg2.setAtomicityMode(TRANSACTIONAL);
    else
        colCfg2.setAtomicityMode(ATOMIC);
    IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg1);
    IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg2);
    assertNotSame(getQueueCache(queue1), getQueueCache(queue2));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

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