/** * @param key Key. * @return Affinity key. */ private Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cacheObjCtx, false); return mapper.affinityKey(key); }
@Override public AffinityInfo applyx(IgniteInternalFuture<GridTuple3<GridAffinityMessage, GridAffinityMessage, GridAffinityAssignment>> fut) throws IgniteCheckedException { GridTuple3<GridAffinityMessage, GridAffinityMessage, GridAffinityAssignment> t = fut.get(); AffinityFunction f = (AffinityFunction)unmarshall(ctx, n.id(), t.get1()); AffinityKeyMapper m = (AffinityKeyMapper)unmarshall(ctx, n.id(), t.get2()); assert m != null; // Bring to initial state. f.reset(); m.reset(); CacheConfiguration ccfg = ctx.cache().cacheConfiguration(cacheName); return new AffinityInfo(f, m, t.get3(), ctx.cacheObjects().contextForCache(ccfg)); } });
/** * Requests {@link AffinityFunction} and {@link AffinityKeyMapper} from remote node. * * @param cacheName Name of cache on which affinity is requested. * @param topVer Topology version. * @param n Node from which affinity is requested. * @return Affinity cached function. * @throws IgniteCheckedException If either local or remote node cannot get deployment for affinity objects. */ private AffinityInfo affinityInfoFromNode(String cacheName, AffinityTopologyVersion topVer, ClusterNode n) throws IgniteCheckedException { GridTuple3<GridAffinityMessage, GridAffinityMessage, GridAffinityAssignment> t = ctx.closure() .callAsyncNoFailover(BROADCAST, affinityJob(cacheName, topVer), F.asList(n), true/*system pool*/, 0, false).get(); AffinityFunction f = (AffinityFunction)unmarshall(ctx, n.id(), t.get1()); AffinityKeyMapper m = (AffinityKeyMapper)unmarshall(ctx, n.id(), t.get2()); assert m != null; // Bring to initial state. f.reset(); m.reset(); CacheConfiguration ccfg = ctx.cache().cacheConfiguration(cacheName); return new AffinityInfo(f, m, t.get3(), ctx.cacheObjects().contextForCache(ccfg)); }
/** * If Key is {@link GridCacheInternal GridCacheInternal} entry when won't passed into user's mapper and * will use {@link GridCacheDefaultAffinityKeyMapper default}. * * @param key Key. * @return Affinity key. */ public Object affinityKey(Object key) { CacheObjectContext coCtx = cctx.cacheObjectContext(); if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(coCtx, false); return (key instanceof GridCacheInternal ? coCtx.defaultAffMapper() : affMapper).affinityKey(key); }
/** {@inheritDoc} */ @Override public Object affinityKey(K key) { A.notNull(key, "key"); if (key instanceof CacheObject && !(key instanceof BinaryObject)) { CacheObjectContext ctx = cctx.cacheObjectContext(); if (ctx == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); key = ((CacheObject)key).value(ctx, false); } CacheConfiguration ccfg = cctx.config(); if (ccfg == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); return ccfg.getAffinityMapper().affinityKey(key); }
/** * */ @Test public void testFieldAffinityMapper() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); List<FieldAffinityKey<Integer>> keys = new ArrayList<>(); for (int i = 1; i <= 10; i++) keys.add(new FieldAffinityKey<>(i, Integer.toString(i))); for (int i = 1; i <= 10; i++) { FieldAffinityKey<Integer> key = keys.get(i - 1); Object mapped = mapper.affinityKey(key); info("Mapped key: " + mapped); assertNotNull(mapped); assertSame(key.affinityKey(), mapped); } }
/** * */ @Test public void testMethodAffinityMapper() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); List<AffinityKey<Integer>> keys = new ArrayList<>(); for (int i = 1; i <= 10; i++) keys.add(new AffinityKey<>(i, Integer.toString(i))); for (int i = 1; i <= 10; i++) { AffinityKey<Integer> key = keys.get(i - 1); Object mapped = mapper.affinityKey(key); info("Mapped key: " + mapped); assertNotNull(mapped); assertSame(key.affinityKey(), mapped); } }
/** * */ @Test public void testFieldAffinityMapperWithWrongClass() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); FieldNoAffinityKey key = new FieldNoAffinityKey(); Object mapped = mapper.affinityKey(key); assertEquals(key, mapped); }
/** * @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 key Key. * @return Affinity key. */ private Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cacheObjCtx, false); return mapper.affinityKey(key); }
/** * If Key is {@link GridCacheInternal GridCacheInternal} entry when won't passed into user's mapper and * will use {@link GridCacheDefaultAffinityKeyMapper default}. * * @param key Key. * @return Affinity key. */ public Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cctx.cacheObjectContext(), false); return (key instanceof GridCacheInternal ? cctx.defaultAffMapper() : affMapper).affinityKey(key); }
/** {@inheritDoc} */ @Override public Object affinityKey(K key) { A.notNull(key, "key"); if (key instanceof CacheObject && !(key instanceof BinaryObject)) { CacheObjectContext ctx = cctx.cacheObjectContext(); if (ctx == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); key = ((CacheObject)key).value(ctx, false); } CacheConfiguration ccfg = cctx.config(); if (ccfg == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); return ccfg.getAffinityMapper().affinityKey(key); }