/** * Map Jobs to nodes using data affinity. * * @param nodes Cluster Nodes * @param chromosomeKeys Primary keys for respective chromosomes * @return A 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); Map<ClusterNode, Collection<Long>> nodeKeys = affinity.mapKeysToNodes(chromosomeKeys); for (Map.Entry<ClusterNode, Collection<Long>> entry : nodeKeys.entrySet()) { ClusterNode aNode = entry.getKey(); map = setupCrossOver(aNode, (List<Long>)entry.getValue(), map); } return map; }
/** {@inheritDoc} */ @Override public Map<ClusterNode, Collection<K>> mapKeysToNodes(@Nullable Collection<? extends K> keys) { CacheOperationContext old = gate.enter(null); try { return delegate.mapKeysToNodes(keys); } finally { gate.leave(old); } }
@Override public Object call() throws Exception { return affinity.mapKeysToNodes(Collections.singleton(key)); } }, IgniteException.class, EXPECTED_MSG);
/** * Check mapKeysToNodes methods. * * @param testAff Affinity1. * @param aff Affinity2. */ private void checkMapKeysToNodes(Affinity testAff, Affinity aff) { List<Integer> keys = new ArrayList<>(10000); for (int i = 0; i < 10000; ++i) keys.add(i); checkEqualMaps(testAff.mapKeysToNodes(keys), aff.mapKeysToNodes(keys)); }
/** {@inheritDoc} */ @Override public Map<ClusterNode, Collection<K>> call() throws Exception { return affinity().mapKeysToNodes(keys); } }
/** * */ @Test public void testMapKeysToNodes() { grid(0).affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(Arrays.asList("1", "2")); }
/** * @throws Exception In case of error. */ @Test public void testMapKeysToNodes() throws Exception { IgniteCache<String, Integer> cache = jcache(); cache.put("key1", 1); cache.put("key2", 2); Map<ClusterNode, Collection<String>> map = grid(0).<String>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(F.asList("key1", "key2")); assert map.size() == 1; Collection<String> keys = map.get(dfltIgnite.cluster().localNode()); assert keys != null; assert keys.size() == 2; for (String key : keys) assert "key1".equals(key) || "key2".equals(key); map = grid(0).<String>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(F.asList("key1", "key2")); assert map.size() == 1; keys = map.get(dfltIgnite.cluster().localNode()); assert keys != null; assert keys.size() == 2; for (String key : keys) assert "key1".equals(key) || "key2".equals(key); }
Map<ClusterNode, Collection<Integer>> cacheMap = affinity(cache).mapKeysToNodes(keys);
Collection<Object> keys = PlatformUtils.readCollection(reader); Map<ClusterNode, Collection<Object>> map = aff.mapKeysToNodes(keys);
Map<ClusterNode, Collection<String>> mapped = grid(0).<String>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(map.keySet()); mapped = grid(0).<String>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(map.keySet());
/** * @throws Exception if failed. */ @Test public void testUnloadEvents() throws Exception { final Ignite g1 = startGrid("g1"); Collection<Integer> allKeys = new ArrayList<>(EVENTS_COUNT); IgniteCache<Integer, String> cache1 = g1.cache(DEFAULT_CACHE_NAME); IgniteCache<Integer, String> cache2 = g1.cache(DEFAULT_CACHE_NAME_EVTS_DISABLED); for (int i = 0; i < EVENTS_COUNT; i++) { cache1.put(i, "val"); // Events should not be fired by this put. cache2.put(i, "val"); allKeys.add(i); } Ignite g2 = startGrid("g2"); awaitPartitionMapExchange(true, true, null); Map<ClusterNode, Collection<Object>> keysMap = g1.affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(allKeys); Collection<Object> g2Keys = keysMap.get(g2.cluster().localNode()); assertNotNull(g2Keys); assertFalse("There are no keys assigned to g2", g2Keys.isEmpty()); Collection<Event> objEvts = g1.events().localQuery(F.<Event>alwaysTrue(), EVT_CACHE_REBALANCE_OBJECT_UNLOADED); checkObjectUnloadEvents(objEvts, g1, g2Keys); Collection <Event> partEvts = g1.events().localQuery(F.<Event>alwaysTrue(), EVT_CACHE_REBALANCE_PART_UNLOADED); checkPartitionUnloadEvents(partEvts, g1, dht(g2.cache(DEFAULT_CACHE_NAME)).topology().localPartitions()); }
/** * @throws IgniteCheckedException If failed. */ @Test public void testAffinity() throws Exception { Ignite g1 = grid(1); Ignite g2 = grid(2); assert caches(g1).isEmpty(); assert F.first(caches(g2)).getCacheMode() == PARTITIONED; awaitPartitionMapExchange(); Map<ClusterNode, Collection<String>> map = g1.<String>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(F.asList("1")); assertNotNull(map); assertEquals("Invalid map size: " + map.size(), 1, map.size()); assertEquals(F.first(map.keySet()), g2.cluster().localNode()); UUID id1 = g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode("2").id(); assertNotNull(id1); assertEquals(g2.cluster().localNode().id(), id1); UUID id2 = g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode("3").id(); assertNotNull(id2); assertEquals(g2.cluster().localNode().id(), id2); }
/** * @throws IgniteCheckedException If failed. */ @Test public void testMappedAffinity() throws IgniteCheckedException { Ignite g1 = grid(1); Ignite g2 = grid(2); Ignite g3 = grid(3); assert g1.configuration().getCacheConfiguration().length == 0; assert g2.configuration().getCacheConfiguration()[0].getCacheMode() == PARTITIONED; assert g3.configuration().getCacheConfiguration()[0].getCacheMode() == PARTITIONED; ClusterNode first = g2.cluster().localNode(); ClusterNode second = g3.cluster().localNode(); //When MockCacheAfinity and MockCacheAffinityKeyMapper are set to cache configuration we expect the following. //Key 0 is mapped to partition 0, first node. //Key 1 is mapped to partition 1, second node. //key 2 is mapped to partition 0, first node because mapper substitutes key 2 with affinity key 0. Map<ClusterNode, Collection<Integer>> map = g1.<Integer>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(F.asList(0)); assertNotNull(map); assertEquals("Invalid map size: " + map.size(), 1, map.size()); assertEquals(F.first(map.keySet()), first); UUID id1 = g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1).id(); assertNotNull(id1); assertEquals(second.id(), id1); UUID id2 = g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2).id(); assertNotNull(id2); assertEquals(first.id(), id2); }
Map<ClusterNode, Collection<String>> mapped = grid(0).<String>affinity(cacheName()).mapKeysToNodes(map.keySet()); mapped = grid(0).<String>affinity(cacheName()).mapKeysToNodes(map.keySet());
Map<ClusterNode, Collection<Integer>> map = g1.<Integer>affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(F.asList(0));
/** {@inheritDoc} */ @Override public Map<ClusterNode, Collection<K>> mapKeysToNodes(@Nullable Collection<? extends K> keys) { CacheOperationContext old = gate.enter(null); try { return delegate.mapKeysToNodes(keys); } finally { gate.leave(old); } }
Collection<Object> keys = PlatformUtils.readCollection(reader); Map<ClusterNode, Collection<Object>> map = aff.mapKeysToNodes(keys);