Search in sources :

Example 31 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class TruncateSelectionTask method map.

/**
 * @param nodes List of ClusterNode
 * @param chromosomeKeys Primary keys for respective chromosomes
 * @return Map of nodes to jobs
 */
public Map map(List<ClusterNode> nodes, List<Long> chromosomeKeys) throws IgniteException {
    Map<ComputeJob, ClusterNode> map = new HashMap<>();
    Affinity affinity = ignite.affinity(GAGridConstants.POPULATION_CACHE);
    // Retrieve enhanced population
    List<List<Long>> enhancedPopulation = getEnhancedPopulation();
    int k = 0;
    for (Long key : chromosomeKeys) {
        TruncateSelectionJob ajob = new TruncateSelectionJob(key, (List) enhancedPopulation.get(k));
        ClusterNode primary = affinity.mapKeyToNode(key);
        map.put(ajob, primary);
        k = k + 1;
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) Affinity(org.apache.ignite.cache.affinity.Affinity) ArrayList(java.util.ArrayList) List(java.util.List)

Example 32 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCommonAbstractTest method findKeys.

/**
 * @param cache Cache.
 * @param cnt Keys count.
 * @param startFrom Start value for keys search.
 * @return Collection of keys for which given cache is primary.
 */
@SuppressWarnings("unchecked")
protected List<Integer> findKeys(IgniteCache<?, ?> cache, final int cnt, final int startFrom, final int type) {
    assert cnt > 0 : cnt;
    final List<Integer> found = new ArrayList<>(cnt);
    final ClusterNode locNode = localNode(cache);
    final Affinity<Integer> aff = (Affinity<Integer>) affinity(cache);
    try {
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                for (int i = startFrom; i < startFrom + 100_000; i++) {
                    Integer key = i;
                    boolean ok;
                    if (type == 0)
                        ok = aff.isPrimary(locNode, key);
                    else if (type == 1)
                        ok = aff.isBackup(locNode, key);
                    else if (type == 2)
                        ok = !aff.isPrimaryOrBackup(locNode, key);
                    else {
                        fail();
                        return false;
                    }
                    if (ok) {
                        if (!found.contains(key))
                            found.add(key);
                        if (found.size() == cnt)
                            return true;
                    }
                }
                return false;
            }
        }, 5000);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    if (found.size() != cnt)
        throw new IgniteException("Unable to find " + cnt + " requied keys.");
    return found;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterNode(org.apache.ignite.cluster.ClusterNode) PA(org.apache.ignite.internal.util.typedef.PA) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 33 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteCacheGroupsTest method checkAffinityMappers.

/**
 * @param node Node.
 */
private void checkAffinityMappers(Ignite node) {
    Affinity aff1 = node.affinity("c1");
    Affinity aff2 = node.affinity("c2");
    Affinity aff3 = node.affinity("c3");
    Affinity aff4 = node.affinity("c4");
    Affinity aff5 = node.affinity("c5");
    Affinity aff6 = node.affinity("c6");
    RendezvousAffinityFunction func = new RendezvousAffinityFunction();
    for (int i = 0; i < 100; i++) {
        MapperTestKey1 k = new MapperTestKey1(i, i + 10);
        assertEquals(i, aff1.partition(k));
        assertEquals(i, aff3.partition(k));
        assertEquals(i + 10, aff2.partition(k));
        assertEquals(i + 10, aff4.partition(k));
        int part;
        if (node.configuration().getMarshaller() instanceof BinaryMarshaller)
            part = func.partition(node.binary().toBinary(k));
        else
            part = func.partition(k);
        assertEquals(part, aff5.partition(k));
        assertEquals(part, aff6.partition(k));
    }
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Affinity(org.apache.ignite.cache.affinity.Affinity) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)

Example 34 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCachePartitionedTopologyChangeSelfTest method checkTxNodeLeft.

/**
 * @throws Exception If failed.
 */
private void checkTxNodeLeft(int nodeType) throws Exception {
    startGridsMultiThreaded(4);
    final IgniteKernal g0 = (IgniteKernal) grid(0);
    final IgniteKernal g1 = (IgniteKernal) grid(1);
    final IgniteKernal g2 = (IgniteKernal) grid(2);
    final IgniteKernal g3 = (IgniteKernal) grid(3);
    IgniteKernal[] nodes = new IgniteKernal[] { g0, g1, g2 };
    final CountDownLatch commitLatch = new CountDownLatch(1);
    UUID leftNodeId = g3.localNode().id();
    try {
        info(">>> Started nodes [g0=" + g0.localNode().id() + ", g1=" + g1.localNode().id() + ", g2=" + g2.localNode().id() + ", g3=" + g3.localNode().id() + ']');
        Collection<IgniteInternalFuture> futs = new ArrayList<>();
        printDistribution(g3);
        for (final IgniteKernal node : nodes) {
            printDistribution(node);
            // Get partitions that does not reside on g0.
            List<Integer> parts = partitions(node, nodeType);
            info(">>> Partitions for node [nodeId=" + node.localNode().id() + ", parts=" + parts + ", type=" + nodeType + ']');
            final Map<Integer, Integer> keysMap = keysFor(node, parts);
            info(">>> Generated keys for node [nodeId=" + node.localNode().id() + ", keysMap=" + keysMap + ']');
            // Start tx for every key in map.
            for (final Integer key : keysMap.values()) {
                futs.add(multithreadedAsync(new Runnable() {

                    @Override
                    public void run() {
                        IgniteCache<Integer, Integer> cache = node.cache(DEFAULT_CACHE_NAME);
                        try {
                            try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                cache.put(key, key);
                                commitLatch.await();
                                tx.commit();
                            }
                        } catch (CacheException e) {
                            info("Failed to run tx for key [key=" + key + ", e=" + e + ']');
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                            info("Got interrupted while waiting for commit latch: " + key);
                        }
                    }
                }, 1));
            }
        }
        final CountDownLatch leaveLatch = new CountDownLatch(1);
        g0.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
                info(">>> Node has left: " + evt.node().id());
                leaveLatch.countDown();
                g0.events().stopLocalListen(this, EVT_NODE_LEFT, EVT_NODE_FAILED);
                return true;
            }
        }, EVT_NODE_LEFT, EVT_NODE_FAILED);
        // Now stop the node.
        stopGrid(getTestIgniteInstanceName(3), true);
        leaveLatch.await();
        // Now check that new transactions will wait for new topology version to become available.
        Collection<IgniteInternalFuture> txFuts = new ArrayList<>(nodes.length);
        for (final Ignite g : nodes) {
            txFuts.add(multithreadedAsync(new Runnable() {

                @Override
                public void run() {
                    IgniteCache<Integer, Integer> cache = g.cache(DEFAULT_CACHE_NAME);
                    int key = (int) Thread.currentThread().getId();
                    try {
                        try (Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            // This method should block until all previous transactions are completed.
                            cache.put(key, key);
                            tx.commit();
                        }
                    } catch (CacheException e) {
                        info(">>> Failed to execute tx on new topology [key=" + key + ", e=" + e + ']');
                    }
                }
            }, 1));
        }
        Thread.sleep(500);
        for (IgniteInternalFuture txFut : txFuts) assertFalse("New transaction was completed before old transactions were committed", txFut.isDone());
        info(">>> Committing pending transactions.");
        commitLatch.countDown();
        for (IgniteInternalFuture fut : futs) fut.get(1000);
        for (IgniteInternalFuture txFut : txFuts) txFut.get(1000);
        for (int i = 0; i < 3; i++) {
            Affinity affinity = grid(i).affinity(DEFAULT_CACHE_NAME);
            ConcurrentMap addedNodes = U.field(affinity, "addedNodes");
            assertFalse(addedNodes.containsKey(leftNodeId));
        }
    } finally {
        info(">>> Shutting down the test.");
        commitLatch.countDown();
        U.sleep(1000);
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Transaction(org.apache.ignite.transactions.Transaction) Event(org.apache.ignite.events.Event) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 35 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method primaryKeys.

/**
 * @param idx Grid index.
 * @param cnt Number of keys.
 * @return Primary keys for grid.
 */
private List<String> primaryKeys(int idx, int cnt) {
    assert cnt > 0;
    Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    List<String> keys = new ArrayList<>(cnt);
    for (int i = 0; i < 10_000; i++) {
        String key = String.valueOf(i);
        if (aff.isPrimary(grid(idx).localNode(), key)) {
            keys.add(key);
            if (keys.size() == cnt)
                break;
        }
    }
    assertEquals(cnt, keys.size());
    return keys;
}
Also used : ArrayList(java.util.ArrayList) Affinity(org.apache.ignite.cache.affinity.Affinity)

Aggregations

Affinity (org.apache.ignite.cache.affinity.Affinity)49 Ignite (org.apache.ignite.Ignite)30 IgniteCache (org.apache.ignite.IgniteCache)29 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)18 Collection (java.util.Collection)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Transaction (org.apache.ignite.transactions.Transaction)16 List (java.util.List)15 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)14 Map (java.util.Map)13 Cache (javax.cache.Cache)13 HashMap (java.util.HashMap)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 Collections (java.util.Collections)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)8 Ignition (org.apache.ignite.Ignition)8 ScanQuery (org.apache.ignite.cache.query.ScanQuery)8