Search in sources :

Example 1 with CachePartitionLostEvent

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;
}
Also used : CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) EventRegistration(com.hazelcast.spi.EventRegistration) CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent) EventService(com.hazelcast.spi.EventService) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientEndpoint(com.hazelcast.client.ClientEndpoint) CachePartitionLostEventFilter(com.hazelcast.cache.impl.event.CachePartitionLostEventFilter) CachePartitionLostEventFilter(com.hazelcast.cache.impl.event.CachePartitionLostEventFilter) EventFilter(com.hazelcast.spi.EventFilter) InternalCachePartitionLostListenerAdapter(com.hazelcast.cache.impl.event.InternalCachePartitionLostListenerAdapter) CacheService(com.hazelcast.cache.impl.CacheService) ICacheService(com.hazelcast.cache.impl.ICacheService)

Example 2 with CachePartitionLostEvent

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());
        }
    }
}
Also used : CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent)

Example 3 with CachePartitionLostEvent

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();
}
Also used : Address(com.hazelcast.nio.Address) Node(com.hazelcast.instance.Node) IOException(java.io.IOException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) IPartition(com.hazelcast.spi.partition.IPartition) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) AbstractPartitionLostListenerTest(com.hazelcast.partition.AbstractPartitionLostListenerTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with CachePartitionLostEvent

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();
}
Also used : IOException(java.io.IOException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) IPartitionLostEvent(com.hazelcast.spi.partition.IPartitionLostEvent) CacheService(com.hazelcast.cache.impl.CacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) AbstractPartitionLostListenerTest(com.hazelcast.partition.AbstractPartitionLostListenerTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with CachePartitionLostEvent

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);
}
Also used : CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastServerCachingProvider.createCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CachePartitionLostEvent (com.hazelcast.cache.impl.event.CachePartitionLostEvent)5 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)3 CacheConfig (com.hazelcast.config.CacheConfig)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 CacheManager (javax.cache.CacheManager)3 Test (org.junit.Test)3 CacheService (com.hazelcast.cache.impl.CacheService)2 CachePartitionLostListener (com.hazelcast.cache.impl.event.CachePartitionLostListener)2 AbstractPartitionLostListenerTest (com.hazelcast.partition.AbstractPartitionLostListenerTest)2 AssertTask (com.hazelcast.test.AssertTask)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ICache (com.hazelcast.cache.ICache)1 HazelcastServerCachingProvider.createCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider)1 ICacheService (com.hazelcast.cache.impl.ICacheService)1 CachePartitionLostEventFilter (com.hazelcast.cache.impl.event.CachePartitionLostEventFilter)1