@Override public Object call() throws Exception { return affinity.partition(key); } }, IgniteException.class, EXPECTED_MSG);
/** {@inheritDoc} */ @Override public int partition(K key) { CacheOperationContext old = gate.enter(null); try { return delegate.partition(key); } finally { gate.leave(old); } }
/** {@inheritDoc} */ @Override public Integer call() throws Exception { return affinity().partition(key); } }
@Override public int partition(String cacheName, Object key) { return ctx.cache().cache(cacheName).affinity().partition(key); }
/** * Extract expected partitions set from between from/to keys. * * @param keyFrom Key from. * @param keyTo Key to. * @return Expected set of partitions. */ private Set<Integer> extractExpectedPartitions(int keyFrom, int keyTo) { Set<Integer> partitions = new HashSet<>(); for (int i = keyFrom; i <= keyTo; i++) partitions.add(ignite(0).affinity(ORG_CACHE_NAME).partition(i)); return partitions; }
/** * @param cacheName Cache name. * @param key Key. * @return Partition. */ private int partition(String cacheName, Object key) { return client().affinity(cacheName).partition(key); }
/** * @param cache Cache. * @param key Key * @param cntrs Partition counters. */ private void updatePartitionCounter(IgniteCache<Object, Object> cache, Object key, Map<Integer, Long> cntrs) { Affinity<Object> aff = cache.unwrap(Ignite.class).affinity(cache.getName()); int part = aff.partition(key); Long partCntr = cntrs.get(part); if (partCntr == null) partCntr = 0L; cntrs.put(part, ++partCntr); }
/** * @param cache Cache. * @param key Key * @param cntrs Partition counters. * @param skipUpdCntr Skip update counter flag. */ private void updatePartitionCounter(IgniteCache<Object, Object> cache, Object key, Map<Integer, Long> cntrs, boolean skipUpdCntr) { Affinity<Object> aff = cache.unwrap(Ignite.class).affinity(cache.getName()); int part = aff.partition(key); Long partCntr = cntrs.get(part); if (partCntr == null) partCntr = 0L; if (!skipUpdCntr) partCntr++; cntrs.put(part, partCntr); }
/** * * @param ignite Grid * @param key Key. * @return Primary node id. */ @SuppressWarnings("unchecked") private static UUID primaryId(Ignite ignite, Object key) { Affinity aff = ignite.affinity(DEFAULT_CACHE_NAME); Collection<ClusterNode> affNodes = aff.mapPartitionToPrimaryAndBackups(aff.partition(key)); ClusterNode first = F.first(affNodes); assert first != null; return first.id(); }
/** * @param grid Grid. * @param total Total. * @param part Partition. */ private void putData(Ignite grid, int total, int part) { int c = 0, k = 0; while(c < total) { if (grid(0).affinity(DEFAULT_CACHE_NAME).partition(k) == part) { grid.cache(DEFAULT_CACHE_NAME).put(k, k); c++; } k++; } }
/** * @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); }
/** * 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. * @return For the given key pair {primary node, some other node}. */ private IgniteBiTuple<ClusterNode, ClusterNode> getNodes(Integer key) { Affinity<Integer> aff = grid(0).affinity(DEFAULT_CACHE_NAME); int part = aff.partition(key); ClusterNode primary = aff.mapPartitionToNode(part); assert primary != null; Collection<ClusterNode> nodes = new ArrayList<>(grid(0).cluster().nodes()); nodes.remove(primary); ClusterNode other = F.first(nodes); assert other != null; assert !F.eqNodes(primary, other); return F.t(primary, other); } }
/** * @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)); } }
/** * @param g Grid. * @param keyCnt Key count. */ private static synchronized void printAffinity(Ignite g, int keyCnt) { X.println(">>>"); X.println(">>> Printing affinity for node: " + g.name()); Affinity<Object> aff = affinity(g); for (int i = 0; i < keyCnt; i++) { Collection<? extends ClusterNode> affNodes = nodes(aff, i); X.println(">>> Affinity nodes [key=" + i + ", partition=" + aff.partition(i) + ", nodes=" + U.nodes2names(affNodes) + ", ids=" + U.nodeIds(affNodes) + ']'); } partitionMap(g); }
/** */ @Test public void testPreloadPartitionInMemoryLocalMvcc() throws Exception { cfgFactory = () -> cacheConfiguration(TRANSACTIONAL_SNAPSHOT).setDataRegionName(MEM); startGridsMultiThreaded(GRIDS_CNT); int key = 0; Ignite prim = primaryNode(key, DEFAULT_CACHE_NAME); int part = prim.affinity(DEFAULT_CACHE_NAME).partition(key); try { prim.cache(DEFAULT_CACHE_NAME).preloadPartition(part); fail("Exception is expected"); } catch (Exception e) { log.error("Expected", e); } }
/** */ @Test public void testPreloadPartitionInMemoryLocal() throws Exception { cfgFactory = () -> cacheConfiguration(TRANSACTIONAL).setDataRegionName(MEM); startGridsMultiThreaded(GRIDS_CNT); int key = 0; Ignite prim = primaryNode(key, DEFAULT_CACHE_NAME); int part = prim.affinity(DEFAULT_CACHE_NAME).partition(key); try { prim.cache(DEFAULT_CACHE_NAME).preloadPartition(part); fail("Exception is expected"); } catch (Exception e) { log.error("Expected", e); } }
/** * @param c Cache projection. * @param key Key. * @param attempt Attempt. */ private void printFailureDetails(IgniteCache<Integer, String> c, int key, int attempt) { Ignite ignite = c.unwrap(Ignite.class); error("*** Failure details ***"); error("Key: " + key); error("Partition: " + ignite.affinity(c.getName()).partition(key)); error("Attempt: " + attempt); error("Node: " + ignite.cluster().localNode().id()); } }