Search in sources :

Example 31 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class IgniteTxPessimisticOriginatingNodeFailureAbstractSelfTest method checkPrimaryNodeCrash.

/**
 * Checks tx data consistency in case when primary node crashes.
 *
 * @param commmit Whether to commit or rollback a transaction.
 * @throws Exception If failed.
 */
private void checkPrimaryNodeCrash(final boolean commmit) throws Exception {
    Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < 20; i++) keys.add(i);
    final Collection<IgniteKernal> grids = new ArrayList<>();
    ClusterNode primaryNode = grid(1).localNode();
    for (int i = 0; i < gridCount(); i++) {
        if (i != 1)
            grids.add((IgniteKernal) grid(i));
    }
    failingNodeId = primaryNode.id();
    final Map<Integer, String> map = new HashMap<>();
    final String initVal = "initialValue";
    for (Integer key : keys) {
        grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
        map.put(key, String.valueOf(key));
    }
    Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
    IgniteCache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    info("Failing node ID: " + grid(1).localNode().id());
    for (Integer key : keys) {
        Collection<ClusterNode> nodes = new ArrayList<>();
        nodes.addAll(affinity(cache).mapKeyToPrimaryAndBackups(key));
        nodes.remove(primaryNode);
        nodeMap.put(key, nodes);
    }
    info("Starting tx [values=" + map + ", topVer=" + grid(1).context().discovery().topologyVersion() + ']');
    assertNotNull(cache);
    try (Transaction tx = grid(0).transactions().txStart()) {
        cache.getAll(keys);
        // Should not send any messages.
        cache.putAll(map);
        TransactionProxyImpl txProxy = (TransactionProxyImpl) tx;
        GridNearTxLocal txEx = txProxy.tx();
        assertTrue(txEx.pessimistic());
        if (commmit) {
            txEx.prepare(true);
            // Fail the node in the middle of transaction.
            info(">>> Stopping primary node " + primaryNode);
            G.stop(Ignition.ignite(primaryNode.id()).name(), true);
            info(">>> Stopped originating node, finishing transaction: " + primaryNode.id());
            tx.commit();
        } else {
            // Fail the node in the middle of transaction.
            info(">>> Stopping primary node " + primaryNode);
            G.stop(G.ignite(primaryNode.id()).name(), true);
            info(">>> Stopped originating node, finishing transaction: " + primaryNode.id());
            tx.rollback();
        }
    }
    boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (IgniteKernal g : grids) {
                GridCacheAdapter<?, ?> cache = g.internalCache(DEFAULT_CACHE_NAME);
                IgniteTxManager txMgr = cache.isNear() ? ((GridNearCacheAdapter) cache).dht().context().tm() : cache.context().tm();
                int txNum = txMgr.idMapSize();
                if (txNum != 0)
                    return false;
            }
            return true;
        }
    }, 10000);
    assertTrue(txFinished);
    info("Transactions finished.");
    for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
        final Integer key = e.getKey();
        final String val = map.get(key);
        assertFalse(e.getValue().isEmpty());
        for (ClusterNode node : e.getValue()) {
            final UUID checkNodeId = node.id();
            compute(G.ignite(checkNodeId).cluster().forNode(node)).call(new IgniteCallable<Void>() {

                /**
                 */
                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    assertEquals("Failed to check entry value on node: " + checkNodeId, !commmit ? initVal : val, cache.localPeek(key));
                    return null;
                }
            });
        }
    }
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        for (Ignite g : G.allGrids()) assertEquals(!commmit ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) IgniteException(org.apache.ignite.IgniteException) Transaction(org.apache.ignite.transactions.Transaction) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 32 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheNearEvictionSelfTest method testNearEnabledThreeNodes.

/**
 * @throws Exception If failed.
 */
public void testNearEnabledThreeNodes() throws Exception {
    gridCnt = 3;
    startGridsMultiThreaded(gridCnt);
    try {
        final int cnt = 100;
        grid(0).compute().broadcast(new IgniteCallable<Object>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public Object call() throws Exception {
                IgniteCache<Integer, String> c = ignite.cache(DEFAULT_CACHE_NAME);
                for (int i = 0; i < cnt; i++) c.put(i, Integer.toString(i));
                return true;
            }
        });
        for (int i = 0; i < gridCnt; i++) {
            final GridCacheAdapter cache = internalCache(i);
            final GridCacheAdapter near = near(i);
            // Repeatedly check cache sizes because of concurrent cache updates.
            assertTrue(GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    // Every node contains either near, backup, or primary.
                    return cnt == cache.size() + near.nearSize();
                }
            }, getTestTimeout()));
            int keySize = near(i).nearSize();
            assert keySize < cnt : "Key size is not less than count [cnt=" + cnt + ", size=" + keySize + ']';
        }
    } finally {
        stopAllGrids();
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite)

Example 33 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource 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 34 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource 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 35 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testAffinityCall.

/**
 * @throws Exception If failed.
 */
public void testAffinityCall() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    colCfg.setGroupName("testGroup");
    try (final IgniteSet<Integer> set1 = grid(0).set("Set1", colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                set1.affinityCall(new IgniteCallable<Object>() {

                    @Override
                    public Object call() {
                        return null;
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityCall() for non-collocated set: " + set1.name() + ". This operation is supported only for collocated sets.");
    }
    colCfg.setCollocated(true);
    try (final IgniteSet<Integer> set2 = grid(0).set("Set2", colCfg)) {
        set2.add(100);
        final String cacheName = cctx(set2).name();
        Integer res = set2.affinityCall(new IgniteCallable<Integer>() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public Integer call() {
                assertTrue(ignite.cachex(cacheName).affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Set2"));
                return set2.iterator().next();
            }
        });
        assertEquals(100, res.intValue());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Aggregations

IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)46 Ignite (org.apache.ignite.Ignite)38 IgniteException (org.apache.ignite.IgniteException)18 IgniteCache (org.apache.ignite.IgniteCache)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteLogger (org.apache.ignite.IgniteLogger)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 LoggerResource (org.apache.ignite.resources.LoggerResource)6 Collection (java.util.Collection)5 List (java.util.List)5 Callable (java.util.concurrent.Callable)5 Cache (javax.cache.Cache)5 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5