use of com.hazelcast.cache.impl.HazelcastServerCachingProvider in project hazelcast by hazelcast.
the class CacheQuorumListenerTest method testQuorumEventProvidesCorrectMemberListSize.
@Test
public void testQuorumEventProvidesCorrectMemberListSize() {
final CountDownLatch belowLatch = new CountDownLatch(1);
Config config = new Config();
QuorumListenerConfig listenerConfig = new QuorumListenerConfig();
listenerConfig.setImplementation(new QuorumListener() {
public void onChange(QuorumEvent quorumEvent) {
if (!quorumEvent.isPresent()) {
Collection<Member> currentMembers = quorumEvent.getCurrentMembers();
assertEquals(3, quorumEvent.getThreshold());
assertTrue(currentMembers.size() < quorumEvent.getThreshold());
belowLatch.countDown();
}
}
});
String cacheName = randomString();
String quorumName = randomString();
QuorumConfig quorumConfig = new QuorumConfig(quorumName, true, 3);
quorumConfig.addListenerConfig(listenerConfig);
config.getCacheConfig(cacheName).setQuorumName(quorumName);
config.addQuorumConfig(quorumConfig);
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
factory.newHazelcastInstance(config);
HazelcastServerCachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(instance1);
Cache<Object, Object> cache = cachingProvider.getCacheManager().getCache(cacheName);
try {
cache.put(generateKeyOwnedBy(instance1), 1);
fail("Expected a QuorumException");
} catch (QuorumException expected) {
EmptyStatement.ignore(expected);
}
assertOpenEventually(belowLatch, 15);
}
use of com.hazelcast.cache.impl.HazelcastServerCachingProvider in project hazelcast by hazelcast.
the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_invoked_fromOtherNode.
@Test
public void test_cachePartitionLostListener_invoked_fromOtherNode() {
final String cacheName = randomName();
HazelcastInstance instance1 = hazelcastFactory.newHazelcastInstance();
HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(instance1);
final CacheManager cacheManager = cachingProvider.getCacheManager();
final CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
config.setBackupCount(0);
cacheManager.createCache(cacheName, config);
final CachingProvider clientCachingProvider = createClientCachingProvider(client);
final CacheManager clientCacheManager = clientCachingProvider.getCacheManager();
final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName);
final ICache iCache = cache.unwrap(ICache.class);
final EventCollectingCachePartitionLostListener listener = new EventCollectingCachePartitionLostListener();
iCache.addPartitionLostListener(listener);
assertRegistrationsSizeEventually(instance1, cacheName, 1);
assertRegistrationsSizeEventually(instance2, cacheName, 1);
final CacheService cacheService1 = getNode(instance1).getNodeEngine().getService(CacheService.SERVICE_NAME);
final CacheService cacheService2 = getNode(instance2).getNodeEngine().getService(CacheService.SERVICE_NAME);
final int partitionId = 5;
cacheService1.onPartitionLost(new PartitionLostEventImpl(partitionId, 0, null));
cacheService2.onPartitionLost(new PartitionLostEventImpl(partitionId, 0, null));
assertCachePartitionLostEventEventually(listener, partitionId);
}
use of com.hazelcast.cache.impl.HazelcastServerCachingProvider in project hazelcast by hazelcast.
the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_removed.
@Test
public void test_cachePartitionLostListener_removed() {
final String cacheName = randomName();
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(instance);
final CacheManager cacheManager = cachingProvider.getCacheManager();
final CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
config.setBackupCount(0);
cacheManager.createCache(cacheName, config);
final CachingProvider clientCachingProvider = createClientCachingProvider(client);
final CacheManager clientCacheManager = clientCachingProvider.getCacheManager();
final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName);
final ICache iCache = cache.unwrap(ICache.class);
final UUID registrationId = iCache.addPartitionLostListener(mock(CachePartitionLostListener.class));
assertRegistrationsSizeEventually(instance, cacheName, 1);
assertTrue(iCache.removePartitionLostListener(registrationId));
assertRegistrationsSizeEventually(instance, cacheName, 0);
}
use of com.hazelcast.cache.impl.HazelcastServerCachingProvider in project hazelcast by hazelcast.
the class CachePartitionLostListenerTest method test_partitionLostListenerInvoked.
@Test
public void test_partitionLostListenerInvoked() {
List<HazelcastInstance> instances = getCreatedInstancesShuffledAfterWarmedUp(1);
final HazelcastInstance instance = instances.get(0);
HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(instance);
CacheManager cacheManager = cachingProvider.getCacheManager();
CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
Cache<Integer, String> cache = cacheManager.createCache(getIthCacheName(0), config);
ICache iCache = cache.unwrap(ICache.class);
final EventCollectingCachePartitionLostListener listener = new EventCollectingCachePartitionLostListener(0);
iCache.addPartitionLostListener(listener);
final IPartitionLostEvent internalEvent = new PartitionLostEventImpl(1, 1, null);
CacheService cacheService = getNode(instance).getNodeEngine().getService(CacheService.SERVICE_NAME);
cacheService.onPartitionLost(internalEvent);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
List<CachePartitionLostEvent> events = listener.getEvents();
assertEquals(1, events.size());
CachePartitionLostEvent event = events.get(0);
assertEquals(internalEvent.getPartitionId(), event.getPartitionId());
assertEquals(getIthCacheName(0), event.getSource());
assertEquals(getIthCacheName(0), event.getName());
assertEquals(instance.getCluster().getLocalMember(), event.getMember());
assertEquals(CacheEventType.PARTITION_LOST, event.getEventType());
}
});
cacheManager.destroyCache(getIthCacheName(0));
cacheManager.close();
cachingProvider.close();
}
use of com.hazelcast.cache.impl.HazelcastServerCachingProvider in project hazelcast by hazelcast.
the class CachePartitionLostListenerTest method test_partitionLostListenerInvoked_whenNodeCrashed.
@Test
public void test_partitionLostListenerInvoked_whenNodeCrashed() {
List<HazelcastInstance> instances = getCreatedInstancesShuffledAfterWarmedUp(2);
HazelcastInstance survivingInstance = instances.get(0);
HazelcastInstance terminatingInstance = instances.get(1);
HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(survivingInstance);
CacheManager cacheManager = cachingProvider.getCacheManager();
CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
config.setBackupCount(0);
Cache<Integer, String> cache = cacheManager.createCache(getIthCacheName(0), config);
ICache iCache = cache.unwrap(ICache.class);
final EventCollectingCachePartitionLostListener listener = new EventCollectingCachePartitionLostListener(0);
iCache.addPartitionLostListener(listener);
final Set<Integer> survivingPartitionIds = new HashSet<Integer>();
Node survivingNode = getNode(survivingInstance);
Address survivingAddress = survivingNode.getThisAddress();
for (IPartition partition : survivingNode.getPartitionService().getPartitions()) {
if (survivingAddress.equals(partition.getReplicaAddress(0))) {
survivingPartitionIds.add(partition.getPartitionId());
}
}
terminatingInstance.getLifecycleService().terminate();
waitAllForSafeState(survivingInstance);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
final List<CachePartitionLostEvent> events = listener.getEvents();
assertFalse(events.isEmpty());
for (CachePartitionLostEvent event : events) {
assertFalse(survivingPartitionIds.contains(event.getPartitionId()));
}
}
});
cacheManager.destroyCache(getIthCacheName(0));
cacheManager.close();
cachingProvider.close();
}
Aggregations