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!"); }
public void testStressNodesLeavingWhileMultipleIteratorsLocalSegments() throws Throwable { testStressNodesLeavingWhilePerformingCallable((cache, masterValues, iteration) -> { Map<Integer, Integer> seenValues = new HashMap<>(); KeyPartitioner keyPartitioner = extractComponent(cache, KeyPartitioner.class); AdvancedCache<Integer, Integer> advancedCache = cache.getAdvancedCache(); LocalizedCacheTopology cacheTopology = advancedCache.getDistributionManager().getCacheTopology(); Set<Integer> targetSegments = cacheTopology.getWriteConsistentHash().getSegmentsForOwner(cacheTopology.getLocalAddress()); masterValues = masterValues.entrySet().stream() .filter(e -> targetSegments.contains(keyPartitioner.getSegment(e.getKey()))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); Iterator<Map.Entry<Integer, Integer>> iterator = cache.entrySet().stream() .distributedBatchSize(50000) .filterKeySegments(targetSegments) .iterator(); while (iterator.hasNext()) { Map.Entry<Integer, Integer> entry = iterator.next(); if (seenValues.containsKey(entry.getKey())) { log.tracef("Seen values were: %s", seenValues); throw new IllegalArgumentException(Thread.currentThread() + "-Found duplicate value: " + entry.getKey() + " on iteration " + iteration); } else if (!masterValues.get(entry.getKey()).equals(entry.getValue())) { log.tracef("Seen values were: %s", seenValues); throw new IllegalArgumentException(Thread.currentThread() + "-Found incorrect value: " + entry.getKey() + " with value " + entry.getValue() + " on iteration " + iteration); } seenValues.put(entry.getKey(), entry.getValue()); } if (seenValues.size() != masterValues.size()) { findMismatchedSegments(keyPartitioner, masterValues, seenValues, iteration); } }); }