use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class DistributedStreamIteratorTest method verifyNodeLeavesAfterSendingBackSomeData.
/**
* This test is to verify proper behavior when a node dies after sending a batch to the requestor
*/
@Test
public void verifyNodeLeavesAfterSendingBackSomeData() throws TimeoutException, InterruptedException, ExecutionException {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Map<Object, String> values = new HashMap<>();
int chunkSize = cache0.getCacheConfiguration().clustering().stateTransfer().chunkSize();
// Now insert 2 more values than the chunk size into the node we will kill
for (int i = 0; i < chunkSize + 2; ++i) {
MagicKey key = new MagicKey(cache1);
cache1.put(key, key.toString());
values.put(key, key.toString());
}
CheckPoint checkPoint = new CheckPoint();
// Let the first request come through fine
checkPoint.trigger(Mocks.BEFORE_RELEASE);
waitUntilSendingResponse(cache1, checkPoint);
final BlockingQueue<Map.Entry<Object, String>> returnQueue = new LinkedBlockingQueue<>();
Future<Void> future = fork(() -> {
Iterator<Map.Entry<Object, String>> iter = cache0.entrySet().stream().iterator();
while (iter.hasNext()) {
Map.Entry<Object, String> entry = iter.next();
returnQueue.add(entry);
}
return null;
});
// Now wait for them to send back first results
checkPoint.awaitStrict(Mocks.AFTER_INVOCATION, 10, TimeUnit.SECONDS);
checkPoint.trigger(Mocks.AFTER_RELEASE);
// We should get a value now, note all values are currently residing on cache1 as primary
Map.Entry<Object, String> value = returnQueue.poll(10, TimeUnit.SECONDS);
// Now kill the cache - we should recover
killMember(1, CACHE_NAME);
future.get(10, TimeUnit.SECONDS);
for (Map.Entry<Object, String> entry : values.entrySet()) {
assertTrue("Entry wasn't found:" + entry, returnQueue.contains(entry) || entry.equals(value));
}
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class DistributedStreamIteratorTest method testLocallyForcedStream.
@Test
public void testLocallyForcedStream() {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Cache<Object, String> cache2 = cache(2, CACHE_NAME);
Map<Object, String> values = new HashMap<>();
MagicKey key1 = new MagicKey(cache0);
cache0.put(key1, key1.toString());
values.put(key1, key1.toString());
// Force it so only cache0 has it's primary owned keys
MagicKey key2 = magicKey(cache1, cache2);
// write from backup so that the test works on scattered cache, too
cache2.put(key2, key2.toString());
// Force it so only cache0 has it's primary owned keys
MagicKey key3 = magicKey(cache2, cache1);
// write from backup so that the test works on scattered cache, too
cache1.put(key3, key3.toString());
int count = 0;
Iterator<Map.Entry<Object, String>> iter = cache0.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).entrySet().stream().iterator();
while (iter.hasNext()) {
Map.Entry<Object, String> entry = iter.next();
String cacheValue = cache0.get(entry.getKey());
assertNotNull(cacheValue);
assertEquals(cacheValue, entry.getValue());
count++;
}
assertEquals(values.size(), count);
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class DistributedStreamIteratorTest method waitUntilProcessingResults.
@Test
public void waitUntilProcessingResults() throws TimeoutException, InterruptedException, ExecutionException {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Map<Object, String> values = new HashMap<>();
for (int i = 0; i < 9; ++i) {
MagicKey key = new MagicKey(cache1);
cache1.put(key, key.toString());
values.put(key, key.toString());
}
CheckPoint checkPoint = new CheckPoint();
checkPoint.triggerForever(Mocks.AFTER_RELEASE);
ClusterPublisherManager<Object, String> spy = Mocks.replaceComponentWithSpy(cache0, ClusterPublisherManager.class);
doAnswer(invocation -> {
SegmentPublisherSupplier<?> result = (SegmentPublisherSupplier<?>) invocation.callRealMethod();
return Mocks.blockingPublisher(result, checkPoint);
}).when(spy).entryPublisher(any(), any(), any(), anyLong(), any(), anyInt(), any());
final BlockingQueue<Map.Entry<Object, String>> returnQueue = new LinkedBlockingQueue<>();
Future<Void> future = fork(() -> {
Iterator<Map.Entry<Object, String>> iter = cache0.entrySet().stream().iterator();
while (iter.hasNext()) {
Map.Entry<Object, String> entry = iter.next();
returnQueue.add(entry);
}
return null;
});
// Now wait for them to send back first results but don't let them process
checkPoint.awaitStrict(Mocks.BEFORE_INVOCATION, 10, TimeUnit.SECONDS);
// Now let them process the results
checkPoint.triggerForever(Mocks.BEFORE_RELEASE);
// Now kill the cache - we should recover and get appropriate values
killMember(1, CACHE_NAME);
future.get(10, TimeUnit.SECONDS);
KeyPartitioner keyPartitioner = TestingUtil.extractComponent(cache0, KeyPartitioner.class);
Map<Integer, Set<Map.Entry<Object, String>>> expected = generateEntriesPerSegment(keyPartitioner, values.entrySet());
Map<Integer, Set<Map.Entry<Object, String>>> answer = generateEntriesPerSegment(keyPartitioner, returnQueue);
for (Map.Entry<Integer, Set<Map.Entry<Object, String>>> entry : expected.entrySet()) {
Integer segment = entry.getKey();
Set<Map.Entry<Object, String>> answerForSegment = answer.get(segment);
if (answerForSegment != null) {
for (Map.Entry<Object, String> exp : entry.getValue()) {
if (!answerForSegment.contains(exp)) {
log.errorf("Segment %d, missing %s", segment, exp);
}
}
for (Map.Entry<Object, String> ans : answerForSegment) {
if (!entry.getValue().contains(ans)) {
log.errorf("Segment %d, extra %s", segment, ans);
}
}
assertEquals(entry.getValue().size(), answerForSegment.size());
}
assertEquals("Segment " + segment + " had a mismatch", entry.getValue(), answerForSegment);
}
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class DistributedStreamIteratorWithPassivationTest method testConcurrentActivationWithFilter.
@Test(enabled = false, description = "This requires supporting concurrent activation in cache loader interceptor")
public // TODO: this test needs to be redone to take into account filtering as well, after we support activated entries
void testConcurrentActivationWithFilter() throws InterruptedException, ExecutionException, TimeoutException {
final Cache<MagicKey, String> cache0 = cache(0, CACHE_NAME);
Cache<MagicKey, String> cache1 = cache(1, CACHE_NAME);
Cache<MagicKey, String> cache2 = cache(2, CACHE_NAME);
Map<MagicKey, String> originalValues = new HashMap<>();
originalValues.put(new MagicKey(cache0), "cache0");
originalValues.put(new MagicKey(cache1), "cache1");
originalValues.put(new MagicKey(cache2), "cache2");
final MagicKey loaderKey = new MagicKey(cache0);
final String loaderValue = "loader0";
cache0.putAll(originalValues);
PersistenceManager persistenceManager = TestingUtil.extractComponent(cache0, PersistenceManager.class);
DummyInMemoryStore store = persistenceManager.getStores(DummyInMemoryStore.class).iterator().next();
TestObjectStreamMarshaller sm = new TestObjectStreamMarshaller();
PersistenceManager pm = null;
try {
store.write(MarshalledEntryUtil.create(loaderKey, loaderValue, sm));
final CheckPoint checkPoint = new CheckPoint();
pm = waitUntilAboutToProcessStoreTask(cache0, checkPoint);
Future<Void> future = fork(() -> {
// Wait until loader is invoked
checkPoint.awaitStrict("pre_process_on_all_stores_invoked", 10, TimeUnit.SECONDS);
// Now force the entry to be moved to the in memory
assertEquals(loaderValue, cache0.get(loaderKey));
checkPoint.triggerForever("pre_process_on_all_stores_released");
return null;
});
KeyValueFilter<MagicKey, String> filter = (Serializable & KeyValueFilter<MagicKey, String>) (k, v, m) -> originalValues.containsKey(k);
Iterator<CacheEntry<MagicKey, String>> iterator = cache0.getAdvancedCache().cacheEntrySet().stream().filter(CacheFilters.predicate(filter)).iterator();
// we need this count since the map will replace same key'd value
int count = 0;
Map<MagicKey, String> results = new HashMap<>();
while (iterator.hasNext()) {
Map.Entry<MagicKey, String> entry = iterator.next();
results.put(entry.getKey(), entry.getValue());
count++;
}
// We shouldn't have found the value in the loader
assertEquals(count, 3);
assertEquals(originalValues, results);
future.get(10, TimeUnit.SECONDS);
} finally {
if (pm != null) {
TestingUtil.replaceComponent(cache0, PersistenceManager.class, pm, true, true);
}
sm.stop();
}
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class DistributedStreamIteratorWithStoreAsBinaryTest method testFilterWithStoreAsBinaryPartialKeys.
@Test
public void testFilterWithStoreAsBinaryPartialKeys() {
Cache<MagicKey, String> cache0 = cache(0);
Cache<MagicKey, String> cache1 = cache(1);
Cache<MagicKey, String> cache2 = cache(2);
MagicKey findKey = new MagicKey(cache1);
Map<MagicKey, String> originalValues = new HashMap<>();
originalValues.put(new MagicKey(cache0), "cache0");
originalValues.put(findKey, "cache1");
originalValues.put(new MagicKey(cache2), "cache2");
cache0.putAll(originalValues);
// Try filter for all values
Iterator<CacheEntry<MagicKey, String>> iterator = cache1.getAdvancedCache().cacheEntrySet().stream().filter(CacheFilters.predicate(new MagicKeyStringFilter(Collections.singletonMap(findKey, "cache1")))).iterator();
CacheEntry<MagicKey, String> entry = iterator.next();
AssertJUnit.assertEquals(findKey, entry.getKey());
AssertJUnit.assertEquals("cache1", entry.getValue());
assertFalse(iterator.hasNext());
}
Aggregations