/** * Constructor. * * @param platformCtx Context. * @param igniteCtx Ignite context. * @param name Cache name. */ public PlatformAffinity(PlatformContext platformCtx, GridKernalContext igniteCtx, @Nullable String name) throws IgniteCheckedException { super(platformCtx); this.aff = igniteCtx.grid().affinity(name); if (aff == null) throw new IgniteCheckedException("Cache with the given name doesn't exist: " + name); discovery = igniteCtx.discovery(); }
/** {@inheritDoc} */ @Override protected UUID run(@Nullable VisorCacheAffinityNodeTaskArg arg) throws IgniteException { assert arg != null; ClusterNode node = ignite.affinity(arg.getCacheName()).mapKeyToNode(arg.getKey()); return node != null ? node.id() : null; }
/** * @param idx Index. * @return Affinity. */ private Affinity<Object> affinity(int idx) { return grid(idx).affinity(DEFAULT_CACHE_NAME); }
/** * Reconstructs object on unmarshalling. * * @return Reconstructed object. * @throws ObjectStreamException Thrown in case of unmarshalling error. */ private Object readResolve() throws ObjectStreamException { return cctx.grid().affinity(cctx.cache().name()); } }
/** * @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 cacheName Cache name. * @param key Key. * @return Partition. */ private int partition(String cacheName, Object key) { return client().affinity(cacheName).partition(key); }
/** * */ @Test public void testMapKeysToNodes() { grid(0).affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(Arrays.asList("1", "2")); }
/** @return Key that is not primary nor backup for grid 0. */ private String nearKey() { String key = ""; for (int i = 0; i < 1000; i++) { key = String.valueOf(i); if (!grid(0).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(0).localNode(), key)) break; } return key; }
/** * */ @Test public void testMapKeyToNode() { assert grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode("1") == null; }
/** * @param ignite Ignite instance. * @param orgId Organization ID. * @return {@code true} if partition for the given organization ID is primary on the given node. */ private static boolean primaryPartition(IgniteEx ignite, int orgId) { int part = ignite.affinity(Organization.class.getSimpleName()).partition(orgId); GridCacheAdapter<?, ?> cacheAdapterPers = ignite.context().cache() .internalCache(Person.class.getSimpleName()); GridDhtLocalPartition pPers = cacheAdapterPers.context().topology() .localPartition(part, AffinityTopologyVersion.NONE, false); return pPers.primary(AffinityTopologyVersion.NONE); }
/** * @param gridIdx Grid index. * @param key Key. * @return {@code True} if grid is primary for given key. */ private boolean primary(int gridIdx, Object key) { Affinity<Object> aff = grid(0).affinity(CACHE_NAME); return aff.isPrimary(grid(gridIdx).cluster().localNode(), key); }
/** * @param gridIdx Grid index. * @param key Key. * @return {@code True} if grid is primary for given key. */ private boolean backup(int gridIdx, Object key) { Affinity<Object> aff = grid(0).affinity(CACHE_NAME); return aff.isBackup(grid(gridIdx).cluster().localNode(), key); }
/** * @param key Key. */ private void checkAffinityProxyNoCache(Object key) { IgniteEx ignite = grid(0); final Affinity<Object> affinity = ignite.affinity("noCache"); assertFalse("Affinity proxy instance expected", affinity instanceof GridCacheAffinityImpl); final ClusterNode n = ignite.cluster().localNode(); assertAffinityMethodsException(affinity, key, n); }
/** * JUnit. * * @throws Exception If failed. */ @Test public void testPartition() throws Exception { String key = "key"; assertEquals(affinity().partition(key), grid(0).affinity(DEFAULT_CACHE_NAME).partition(key)); }
/** * @param key Key. */ private void checkAffinityImplCacheDeleted(Object key) throws InterruptedException { IgniteEx grid = grid(0); final String cacheName = "cacheToBeDeleted"; grid(1).getOrCreateCache(cacheName); awaitPartitionMapExchange(); Affinity<Object> affinity = grid.affinity(cacheName); assertTrue(affinity instanceof GridCacheAffinityImpl); final ClusterNode n = grid.cluster().localNode(); grid.cache(cacheName).destroy(); awaitPartitionMapExchange(); assertAffinityMethodsException(grid.affinity(cacheName), key, n); }
/** * JUnit. * * @throws Exception If failed. */ @Test public void testPartitions() throws Exception { assertEquals(affinity().partitions(), grid(0).affinity(DEFAULT_CACHE_NAME).partitions()); }
/** * Tests timeout on DHT primary node for all tx configurations. * * @throws Exception If failed. */ @Test public void testTimeoutOnPrimaryDHTNode() throws Exception { final ClusterNode n0 = grid(0).affinity(CACHE_NAME).mapKeyToNode(0); final Ignite prim = G.ignite(n0.id()); for (TransactionConcurrency concurrency : TransactionConcurrency.values()) { for (TransactionIsolation isolation : TransactionIsolation.values()) testTimeoutOnPrimaryDhtNode0(prim, concurrency, isolation); } }
/** * @throws Exception If failed. */ @Test public void testPartitionWithAffinityMapper() throws Exception { AffinityKey<Integer> key = new AffinityKey<>(1, 2); int expPart = affinity().partition(affinityMapper().affinityKey(key)); for (int i = 0; i < gridCount(); i++) assertEquals(expPart, grid(i).affinity(DEFAULT_CACHE_NAME).partition(key)); } }
/** * @throws Exception If failed. */ @Test public void testGetFromPrimaryNode() throws Exception { for (int i = 0; i < GRID_CNT; i++) { IgniteCache<String, Integer> c = grid(i).cache(DEFAULT_CACHE_NAME); if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimary(grid(i).localNode(), KEY)) { info("Primary node: " + grid(i).localNode().id()); c.get(KEY); break; } } assert !await(); }