/** * Maps affinity key to node. * * @param affinityKey Affinity key to map. * @return Primary node for this key. */ public Collection<ClusterNode> affinityNodes(Object affinityKey) { return dataCache.affinity().mapKeyToPrimaryAndBackups(affinityKey); }
/** * @param aff Affinity. * @param key Key. * @return Nodes. */ private static Collection<? extends ClusterNode> nodes(Affinity<Object> aff, Object key) { return aff.mapKeyToPrimaryAndBackups(key); }
/** * @param aff Affinity. * @param key Key. * @return Nodes. */ private static Collection<? extends ClusterNode> nodes(Affinity<Object> aff, Object key) { return aff.mapKeyToPrimaryAndBackups(key); }
@Override public Object call() throws Exception { return affinity.mapKeyToPrimaryAndBackups(key); } }, IgniteException.class, EXPECTED_MSG);
/** * @param aff Affinity. * @param key Key. * @return Nodes. */ private static Collection<? extends ClusterNode> nodes(Affinity<Object> aff, Object key) { return aff.mapKeyToPrimaryAndBackups(key); }
/** {@inheritDoc} */ @Override public Collection<ClusterNode> mapKeyToPrimaryAndBackups(K key) { CacheOperationContext old = gate.enter(null); try { return delegate.mapKeyToPrimaryAndBackups(key); } finally { gate.leave(old); } }
/** {@inheritDoc} */ @Override public Collection<ClusterNode> call() throws Exception { return affinity().mapKeyToPrimaryAndBackups(key); } }
/** * @param entries Entries. * @param g Grid. * @return Affinity nodes. */ private Set<UUID> affinityNodes(Iterable<Cache.Entry<Integer, Integer>> entries, Ignite g) { Set<UUID> nodes = new HashSet<>(); for (Cache.Entry<Integer, Integer> entry : entries) nodes.add(g.affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(entry.getKey()).iterator().next().id()); return nodes; }
/** * @param key Key. * @return Primary node for the given key. */ private Collection<ClusterNode> keyNodes(Object key) { return grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key); }
/** * @param key Key. * @return Primary node for the key. */ @Nullable private Collection<Ignite> backupGrids(Integer key) { Collection<ClusterNode> nodes = affinity(0).mapKeyToPrimaryAndBackups(key); Collection<ClusterNode> backups = CU.backups(nodes); return F.viewReadOnly(backups, new C1<ClusterNode, Ignite>() { @Override public Ignite apply(ClusterNode node) { return G.ignite(node.id()); } }); }
/** * @param key Key. * @param cacheName Cache name. * @return Ignite instance which has backup cache for given key. */ protected Ignite backupNode(Object key, String cacheName) { List<Ignite> allGrids = Ignition.allGrids(); assertFalse("There are no alive nodes.", F.isEmpty(allGrids)); Ignite ignite = allGrids.get(0); Affinity<Object> aff = ignite.affinity(cacheName); Collection<ClusterNode> nodes = aff.mapKeyToPrimaryAndBackups(key); assertTrue("Expected more than one node for key [key=" + key + ", nodes=" + nodes + ']', nodes.size() > 1); Iterator<ClusterNode> it = nodes.iterator(); it.next(); // Skip primary. return grid(it.next()); }
/** * Check mapKeyToNode, mapKeyToPrimaryAndBackups methods. * * @param testAff Affinity1. * @param aff Affinity2. */ private void checkMapKeyToNode(Affinity testAff, Affinity aff) { for (int i = 0; i < 10000; i++) { assertEquals(testAff.mapKeyToNode(i).id(), aff.mapKeyToNode(i).id()); checkEqualCollection(testAff.mapKeyToPrimaryAndBackups(i), aff.mapKeyToPrimaryAndBackups(i)); } }
/** {@inheritDoc} */ @Override protected int testKey() { int key = 0; IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME); while (true) { Collection<ClusterNode> nodes = affinity(cache).mapKeyToPrimaryAndBackups(key); if (nodes.contains(grid(0).localNode())) key++; else break; } return key; }
/** {@inheritDoc} */ @Override protected int testKey() { int key = 0; IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME); while (true) { Collection<ClusterNode> nodes = affinity(cache).mapKeyToPrimaryAndBackups(key); if (nodes.contains(grid(0).localNode())) key++; else break; } return key; }
@Override public void run(int idx) throws Exception { for (int i = 0; i < cnt; i++) { String key = String.valueOf(i); GridCacheContext<String, Integer> ctx = ((IgniteKernal)ignite).<String, Integer>internalCache(cacheName).context(); GridCacheEntryEx entry = ctx.isNear() ? ctx.near().dht().peekEx(key) : ctx.cache().peekEx(key); if (ignite.affinity(cacheName).mapKeyToPrimaryAndBackups(key).contains(((IgniteKernal)ignite).localNode())) { assertNotNull(entry); assertTrue(entry.deleted()); } else assertNull(entry); } } }
/** {@inheritDoc} */ @Override public void run(int idx) throws Exception { for (int i = 0; i < cnt; i++) { String key = String.valueOf(i); GridCacheContext<String, Integer> ctx = ((IgniteKernal)ignite).<String, Integer>internalCache(DEFAULT_CACHE_NAME).context(); GridCacheEntryEx entry = ctx.isNear() ? ctx.near().dht().peekEx(key) : ctx.cache().peekEx(key); if (ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key).contains(((IgniteKernal)ignite).localNode())) { assertNotNull(entry); assertTrue(entry.deleted()); } else assertNull(entry); } } }
/** * @param key Key. * @return Near cache for key. */ protected IgniteCache<Integer, Integer> nearCache(Integer key) { List<Ignite> allGrids = Ignition.allGrids(); assertFalse("There are no alive nodes.", F.isEmpty(allGrids)); Affinity<Integer> aff = allGrids.get(0).affinity(DEFAULT_CACHE_NAME); Collection<ClusterNode> nodes = aff.mapKeyToPrimaryAndBackups(key); for (Ignite ignite : allGrids) { if (!nodes.contains(ignite.cluster().localNode())) return ignite.cache(DEFAULT_CACHE_NAME); } fail(); return null; }
/** * @throws Exception If failed. */ @Test public void testInternalKeys() throws Exception { assert gridCount() >= 2; IgniteCache<InternalKey, Object> jcache = grid(0).cache(DEFAULT_CACHE_NAME); final InternalKey key = new InternalKey(0); // Hangs on the first remote put. // Make key belongs to remote node. while (affinity(jcache).mapKeyToPrimaryAndBackups(key).iterator().next().isLocal()) key.key++; // Put bytes array(!), for integer numbers it works correctly. jcache.put(key, new byte[]{1}); assertNotNull(jcache.get(key)); jcache.put(key, new byte[] {2}); assertNotNull(jcache.get(key)); }
/** * @throws Exception If failed. */ @Test public void testSystemCacheOnClientNode() throws Exception { startGrids(2); final IgniteKernal ignite = (IgniteKernal)ignite(1); assertTrue(ignite.configuration().isClientMode()); GridCacheAdapter utilityCache = ((IgniteKernal)ignite(0)).internalCache(CU.UTILITY_CACHE_NAME); Collection<ClusterNode> affNodes = utilityCache.affinity().mapKeyToPrimaryAndBackups(1); assertEquals(1, affNodes.size()); assertTrue(affNodes.contains(ignite(0).cluster().localNode())); } }
/** * @throws Exception If failed. */ @SuppressWarnings("ConstantConditions") private void checkPartitions() throws Exception { AffinityFunction aff = cacheConfiguration(grid(0).configuration(), DEFAULT_CACHE_NAME).getAffinity(); int partCnt = aff.partitions(); IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME); for (int i = 0; i < partCnt; i++) { Collection<ClusterNode> nodes = affinity(cache).mapKeyToPrimaryAndBackups(i); assertEquals(2, nodes.size()); ClusterNode primary = F.first(nodes); ClusterNode backup = F.last(nodes); assertFalse(F.eq(primary.attribute(SPLIT_ATTRIBUTE_NAME), backup.attribute(SPLIT_ATTRIBUTE_NAME))); } }