use of com.hazelcast.cache.impl.event.CachePartitionLostEvent in project hazelcast by hazelcast.
the class CacheAddPartitionLostListenerMessageTask method call.
@Override
protected Object call() {
final ClientEndpoint endpoint = getEndpoint();
CachePartitionLostListener listener = new CachePartitionLostListener() {
@Override
public void partitionLost(CachePartitionLostEvent event) {
if (endpoint.isAlive()) {
ClientMessage eventMessage = CacheAddPartitionLostListenerCodec.encodeCachePartitionLostEvent(event.getPartitionId(), event.getMember().getUuid());
sendClientMessage(null, eventMessage);
}
}
};
InternalCachePartitionLostListenerAdapter listenerAdapter = new InternalCachePartitionLostListenerAdapter(listener);
EventFilter filter = new CachePartitionLostEventFilter();
CacheService service = getService(CacheService.SERVICE_NAME);
EventService eventService = service.getNodeEngine().getEventService();
EventRegistration registration;
if (parameters.localOnly) {
registration = eventService.registerLocalListener(ICacheService.SERVICE_NAME, parameters.name, filter, listenerAdapter);
} else {
registration = eventService.registerListener(ICacheService.SERVICE_NAME, parameters.name, filter, listenerAdapter);
}
String registrationId = registration.getId();
endpoint.addListenerDestroyAction(CacheService.SERVICE_NAME, parameters.name, registrationId);
return registrationId;
}
use of com.hazelcast.cache.impl.event.CachePartitionLostEvent in project hazelcast by hazelcast.
the class CachePartitionLostListenerStressTest method assertLostPartitions.
private void assertLostPartitions(String log, EventCollectingCachePartitionLostListener listener, Map<Integer, Integer> survivingPartitions) {
List<CachePartitionLostEvent> events = listener.getEvents();
assertFalse(survivingPartitions.isEmpty());
for (CachePartitionLostEvent event : events) {
int failedPartitionId = event.getPartitionId();
Integer survivingReplicaIndex = survivingPartitions.get(failedPartitionId);
if (survivingReplicaIndex != null) {
String message = log + ", PartitionId: " + failedPartitionId + " SurvivingReplicaIndex: " + survivingReplicaIndex + " Event: " + event.toString();
assertTrue(message, survivingReplicaIndex > listener.getBackupCount());
}
}
}
use of com.hazelcast.cache.impl.event.CachePartitionLostEvent 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 = createCachingProvider(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();
}
use of com.hazelcast.cache.impl.event.CachePartitionLostEvent 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 = createCachingProvider(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 IPartitionLostEvent(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.event.CachePartitionLostEvent in project hazelcast by hazelcast.
the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_registered.
@Test
public void test_cachePartitionLostListener_registered() {
final String cacheName = randomName();
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final CachingProvider cachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
final CacheManager cacheManager = cachingProvider.getCacheManager();
final CacheConfig<Integer, String> cacheConfig = new CacheConfig<Integer, String>();
final Cache<Integer, String> cache = cacheManager.createCache(cacheName, cacheConfig);
final ICache iCache = cache.unwrap(ICache.class);
iCache.addPartitionLostListener(new CachePartitionLostListener() {
@Override
public void partitionLost(CachePartitionLostEvent event) {
}
});
assertRegistrationsSizeEventually(instance, cacheName, 1);
}
Aggregations