/** * @param c Cache projection. */ private void affinityAfterStart(IgniteCache<Integer, String> c) { if (DEBUG) { for (int key = 0; key < keyCnt; key++) { int part = affinity(c).partition(key); info("Affinity nodes after start [key=" + key + ", partition" + part + ", nodes=" + U.nodeIds(affinity(c).mapPartitionToPrimaryAndBackups(part)) + ']'); } } }
/** * @param c Cache projection. */ private void affinityBeforeStop(IgniteCache<Integer, String> c) { if (DEBUG) { for (int key = 0; key < keyCnt; key++) { int part = affinity(c).partition(key); info("Affinity nodes before stop [key=" + key + ", partition" + part + ", nodes=" + U.nodeIds(affinity(c).mapPartitionToPrimaryAndBackups(part)) + ']'); } } }
/** {@inheritDoc} */ @Override public int partition(Object key) { return delegate.partition(key); }
/** * Gets affinity nodes for partition. * * @param key Affinity key. * @return Affinity nodes. */ private Iterable<ClusterNode> nodes(List<List<ClusterNode>> assignment, AffinityFunction aff, Object key) { return assignment.get(aff.partition(key)); }
/** * Get primary node for cached key. * * @param aff Affinity function. * @param key Key to check. * @return Primary node for given key. * @throws IgniteCheckedException In case of error. */ private <K> ClusterNode primary(AffinityInfo aff, K key) throws IgniteCheckedException { int part = aff.affFunc.partition(aff.affinityKey(key)); Collection<ClusterNode> nodes = aff.assignment.get(part); if (F.isEmpty(nodes)) throw new IgniteCheckedException("Failed to get affinity nodes [aff=" + aff + ", key=" + key + ']'); return nodes.iterator().next(); }
/** {@inheritDoc} */ @Override public long processInStreamOutLong(int type, BinaryRawReaderEx reader) throws IgniteCheckedException { if (type == OP_PARTITION) return baseFunc.partition(reader.readObjectDetached()); else if (type == OP_REMOVE_NODE) { baseFunc.removeNode(reader.readUuid()); return 0; } return super.processInStreamOutLong(type, reader); }
/** * @param cacheName Cache name (needed only if {@code aff} is not provided. * @param key Key. * @param aff Affinity. * @return Key partition. * @throws IgniteCheckedException If failed. */ public int partition0(String cacheName, Object key, @Nullable AffinityInfo aff) throws IgniteCheckedException { assert cacheName != null; if (aff == null) { aff = affinityCache(cacheName, ctx.cache().context().exchange().readyAffinityVersion()); if (aff == null) throw new IgniteCheckedException("Failed to get cache affinity (cache was not started " + "yet or cache was already stopped): " + cacheName); } return aff.affFunc.partition(aff.affinityKey(key)); }
/** * NOTE: Use this method always when you need to calculate partition id for * a key provided by user. It's required since we should apply affinity mapper * logic in order to find a key that will eventually be passed to affinity function. * * @param key Key. * @param useKeyPart If {@code true} can use pre-calculated partition stored in KeyCacheObject. * @return Partition. */ public int partition(Object key, boolean useKeyPart) { GridAffinityAssignmentCache aff0 = aff; if (aff0 == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); if (useKeyPart && (key instanceof KeyCacheObject)) { int part = ((KeyCacheObject)key).partition(); if (part != -1) return part; } return affFunction.partition(affinityKey(key)); }
/** * @param backups Number of backups. * @throws Exception If failed. */ @Test public void testNullKeyForPartitionCalculation() throws Exception { AffinityFunction aff = affinityFunction(); try { aff.partition(null); fail("Should throw IllegalArgumentException due to NULL affinity key."); } catch (IllegalArgumentException e) { e.getMessage().contains("Null key is passed for a partition calculation. " + "Make sure that an affinity key that is used is initialized properly."); } }
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { map = newMap(); if (keys == null) { keys = new T3[LOAD_CNT]; wrappers = new GridByteArrayWrapper[LOAD_CNT]; AffinityFunction aff = new RendezvousAffinityFunction(); Random rnd = new Random(); for (int i = 0; i < LOAD_CNT; i++) { byte[] key = new byte[rnd.nextInt(511) + 1]; rnd.nextBytes(key); GridByteArrayWrapper wrap = new GridByteArrayWrapper(key); keys[i] = new T3<>(aff.partition(wrap), wrap.hashCode(), key); wrappers[i] = wrap; } } }
/** * @throws Exception If failed. */ @Test public void testPutRandomKeys() throws Exception { map = newMap(); AffinityFunction aff = new RendezvousAffinityFunction(parts, null); getTestResources().inject(aff); GridByteArrayWrapper[] keys = new GridByteArrayWrapper[512]; Random rnd = new Random(); for (int i = 0; i < keys.length; i++) { byte[] key = new byte[rnd.nextInt(64) + 1]; rnd.nextBytes(key); keys[i] = new GridByteArrayWrapper(key); } for (int i = 0; i < 10000; i++) { int idx = rnd.nextInt(keys.length); GridByteArrayWrapper key = keys[idx]; map.put(aff.partition(key), key.hashCode(), key.array(), new byte[64]); } }
/** {@inheritDoc} */ @Override public int partition(Object key) { if ((overrideFlags & FLAG_PARTITION) == 0) { assert baseFunc != null; return baseFunc.partition(key); } assert ctx != null; assert ptr != 0; try (PlatformMemory mem = ctx.memory().allocate()) { PlatformOutputStream out = mem.output(); BinaryRawWriterEx writer = ctx.writer(out); writer.writeLong(ptr); writer.writeObject(key); out.synchronize(); return ctx.gateway().affinityFunctionPartition(mem.pointer()); } }
/** * 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)); }
/** * @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 c Cache projection. * @param key Key. * @param attempt Attempt. */ private void printFailureDetails(IgniteCache<Integer, String> c, int key, int attempt) { error("*** Failure details ***"); error("Key: " + key); error("Partition: " + c.getConfiguration(CacheConfiguration.class).getAffinity().partition(key)); error("Attempt: " + attempt); error("Node: " + c.unwrap(Ignite.class).cluster().localNode().id()); } }
AffinityFunction aff = cache.getConfiguration(CacheConfiguration.class).getAffinity(); int part = aff.partition(key);
AffinityFunction aff = cache.getConfiguration(CacheConfiguration.class).getAffinity(); int part = aff.partition(key);
/** * Get primary node for cached key. * * @param aff Affinity function. * @param key Key to check. * @return Primary node for given key. * @throws IgniteCheckedException In case of error. */ private <K> ClusterNode primary(AffinityInfo aff, K key) throws IgniteCheckedException { int part = aff.affFunc.partition(aff.affinityKey(key)); Collection<ClusterNode> nodes = aff.assignment.get(part); if (F.isEmpty(nodes)) throw new IgniteCheckedException("Failed to get affinity nodes [aff=" + aff + ", key=" + key + ']'); return nodes.iterator().next(); }
/** {@inheritDoc} */ @Override public long processInStreamOutLong(int type, BinaryRawReaderEx reader) throws IgniteCheckedException { if (type == OP_PARTITION) return baseFunc.partition(reader.readObjectDetached()); else if (type == OP_REMOVE_NODE) { baseFunc.removeNode(reader.readUuid()); return 0; } return super.processInStreamOutLong(type, reader); }
/** {@inheritDoc} */ @Override public int partition(Object key) { if ((overrideFlags & FLAG_PARTITION) == 0) { assert baseFunc != null; return baseFunc.partition(key); } assert ctx != null; assert ptr != 0; try (PlatformMemory mem = ctx.memory().allocate()) { PlatformOutputStream out = mem.output(); BinaryRawWriterEx writer = ctx.writer(out); writer.writeLong(ptr); writer.writeObject(key); out.synchronize(); return ctx.gateway().affinityFunctionPartition(mem.pointer()); } }