private int getSegment(Object key) { DistributionManager distributionManager = this.getDistributionManager(); if (distributionManager == null) { return 0; } return distributionManager.getCacheTopology().getSegment(key); }
private int getSegment(Object key) { DistributionManager distributionManager = this.getDistributionManager(); if (distributionManager == null) { return 0; } return distributionManager.getCacheTopology().getSegment(key); }
@Override protected void assertOwnershipAndNonOwnership(Object key, boolean allowL1) { if (lostSegments.contains(getCacheTopology(c1).getSegment(key))) return; super.assertOwnershipAndNonOwnership(key, allowL1); }
@Override protected void assertOnAllCaches(Object key, String value) { if (lostSegments.contains(getCacheTopology(c1).getSegment(key))) return; super.assertOnAllCaches(key, value); }
@Override public String getShardFromKey(Object key) { int segment = distributionManager.getCacheTopology().getSegment(key); logger.debugf("Segment for key %s: %d", key, segment); return this.getShardDistribution().getShardFromSegment(segment); }
@Override public String getShardFromKey(Object key) { int segment = distributionManager.getCacheTopology().getSegment(key); logger.debugf("Segment for key %s: %d", key, segment); return this.getShardDistribution().getShardFromSegment(segment); }
private static synchronized int getHashCodeForSegment(LocalizedCacheTopology cacheTopology, int segment) { int numSegments = cacheTopology.getReadConsistentHash().getNumSegments(); // Caching the hash codes prevents random failures in tests where we create many magic keys int[] hcs = hashCodes.computeIfAbsent(numSegments, k -> new int[numSegments]); int hc = hcs[segment]; if (hc != 0) { return hc; } Random r = new Random(); int attemptsLeft = 100 * numSegments; int dummy; do { dummy = r.nextInt(); attemptsLeft--; if (attemptsLeft < 0) { throw new IllegalStateException("Could not find any key in segment " + segment); } } while (cacheTopology.getSegment(dummy) != segment); return hcs[segment] = dummy; }
private String findKeyBasedOnOwnership(String keyPrefix, LocalizedCacheTopology cacheTopology, boolean shouldBePrimaryOwner) { for (int i = 0; i < 1000; i++) { String key = keyPrefix + i; boolean isPrimaryOwner = cacheTopology.getDistribution(key).isPrimary(); if (isPrimaryOwner == shouldBePrimaryOwner) { if (shouldBePrimaryOwner) { log.debugf("Found key %s with primary owner %s, segment %d", key, cacheTopology.getLocalAddress(), cacheTopology.getSegment(key)); } else { log.debugf("Found key %s with primary owner != %s, segment %d", key, cacheTopology.getLocalAddress(), cacheTopology.getSegment(key)); } return key; } } throw new RuntimeException("No key could be found for owner, this may be a bug in test or really bad luck!"); }
@Test public void testAffinityPartitioner() throws Exception { Cache<AffinityKey, String> cache = cacheManagers.get(0).getCache(); IntStream.range(0, 10).boxed().forEach(num -> cache.put(new AffinityKey(num), "value")); addClusterEnabledCacheManager(getConfigurationBuilder()); waitForClusterToForm(); cacheManagers.stream().map(cm -> cm.getCache().getAdvancedCache()).forEach(advancedCache -> { LocalizedCacheTopology cacheTopology = advancedCache.getDistributionManager().getCacheTopology(); advancedCache.getDataContainer().keySet().forEach(key -> { int keySegmentId = ((AffinityKey) key).segmentId; assertEquals(cacheTopology.getSegment(key), keySegmentId); }); }); }
key, cacheTopology.getSegment(key), owners, cacheTopology); throw th;
int segmentToUse = cache.getAdvancedCache().getDistributionManager().getCacheTopology().getSegment(keyToChange);