Search in sources :

Example 1 with MemberAttributeEvent

use of com.hazelcast.core.MemberAttributeEvent in project hazelcast by hazelcast.

the class ClusterServiceImpl method dispatchEvent.

@SuppressFBWarnings("BC_UNCONFIRMED_CAST")
@Override
public void dispatchEvent(MembershipEvent event, MembershipListener listener) {
    switch(event.getEventType()) {
        case MembershipEvent.MEMBER_ADDED:
            listener.memberAdded(event);
            break;
        case MembershipEvent.MEMBER_REMOVED:
            listener.memberRemoved(event);
            break;
        case MembershipEvent.MEMBER_ATTRIBUTE_CHANGED:
            MemberAttributeEvent memberAttributeEvent = (MemberAttributeEvent) event;
            listener.memberAttributeChanged(memberAttributeEvent);
            break;
        default:
            throw new IllegalArgumentException("Unhandled event: " + event);
    }
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with MemberAttributeEvent

use of com.hazelcast.core.MemberAttributeEvent in project hazelcast by hazelcast.

the class SplitBrainHandlerTest method testMergeAfterSplitBrain.

private void testMergeAfterSplitBrain(boolean multicast) throws InterruptedException {
    String groupName = generateRandomString(10);
    Config config = new Config();
    config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5");
    config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "3");
    config.getGroupConfig().setName(groupName);
    NetworkConfig networkConfig = config.getNetworkConfig();
    JoinConfig join = networkConfig.getJoin();
    join.getMulticastConfig().setEnabled(multicast);
    join.getTcpIpConfig().setEnabled(!multicast);
    join.getTcpIpConfig().addMember("127.0.0.1");
    HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
    final CountDownLatch splitLatch = new CountDownLatch(2);
    h3.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            splitLatch.countDown();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    });
    final CountDownLatch mergeLatch = new CountDownLatch(1);
    h3.getLifecycleService().addLifecycleListener(new MergedEventLifeCycleListener(mergeLatch));
    closeConnectionBetween(h1, h3);
    closeConnectionBetween(h2, h3);
    assertTrue(splitLatch.await(10, TimeUnit.SECONDS));
    assertEquals(2, h1.getCluster().getMembers().size());
    assertEquals(2, h2.getCluster().getMembers().size());
    assertEquals(1, h3.getCluster().getMembers().size());
    assertTrue(mergeLatch.await(30, TimeUnit.SECONDS));
    assertEquals(3, h1.getCluster().getMembers().size());
    assertEquals(3, h2.getCluster().getMembers().size());
    assertEquals(3, h3.getCluster().getMembers().size());
    assertEquals(ACTIVE, h1.getCluster().getClusterState());
    assertEquals(ACTIVE, h2.getCluster().getClusterState());
    assertEquals(ACTIVE, h3.getCluster().getClusterState());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance) MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) MembershipEvent(com.hazelcast.core.MembershipEvent) NetworkConfig(com.hazelcast.config.NetworkConfig) JoinConfig(com.hazelcast.config.JoinConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener)

Example 3 with MemberAttributeEvent

use of com.hazelcast.core.MemberAttributeEvent in project hazelcast by hazelcast.

the class QueueAdvancedTest method testDeadTaker.

/**
     * Test for issue 730 (Google).
     */
@Test
public void testDeadTaker() throws Exception {
    Config config = new Config();
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            shutdownLatch.countDown();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    }));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(config);
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h1, h2);
    final IQueue<String> q1 = h1.getQueue("default");
    final IQueue<String> q2 = h2.getQueue("default");
    final CountDownLatch startLatch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
                Thread.sleep(5000);
                h2.getLifecycleService().terminate();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.countDown();
                String value = q2.take();
                fail("Should not be able to take value from queue, but got: " + value);
            } catch (HazelcastInstanceNotActiveException e) {
                EmptyStatement.ignore(e);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
    q1.offer("item");
    assertEquals(1, q1.size());
    assertEquals("item", q1.poll());
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MembershipEvent(com.hazelcast.core.MembershipEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.hazelcast.test.TestThread) ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with MemberAttributeEvent

use of com.hazelcast.core.MemberAttributeEvent in project hazelcast by hazelcast.

the class AbstractListenersOnReconnectTest method testListenersTerminateRandomNode.

private void testListenersTerminateRandomNode() {
    setupListener();
    terminateRandomNode();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final CountDownLatch memberAddedLatch = new CountDownLatch(1);
    clientInstanceImpl.getClientClusterService().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            memberAddedLatch.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    });
    factory.newHazelcastInstance();
    assertOpenEventually(memberAddedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener)

Example 5 with MemberAttributeEvent

use of com.hazelcast.core.MemberAttributeEvent in project hazelcast by hazelcast.

the class ClusterServiceImpl method sendMemberAttributeEvent.

private void sendMemberAttributeEvent(MemberImpl member, MemberAttributeOperationType operationType, String key, Object value) {
    final MemberAttributeServiceEvent event = new MemberAttributeServiceEvent(this, member, operationType, key, value);
    MemberAttributeEvent attributeEvent = new MemberAttributeEvent(this, member, operationType, key, value);
    Collection<MembershipAwareService> membershipAwareServices = nodeEngine.getServices(MembershipAwareService.class);
    if (membershipAwareServices != null && !membershipAwareServices.isEmpty()) {
        for (final MembershipAwareService service : membershipAwareServices) {
            // service events should not block each other
            nodeEngine.getExecutionService().execute(ExecutionService.SYSTEM_EXECUTOR, new Runnable() {

                public void run() {
                    service.memberAttributeChanged(event);
                }
            });
        }
    }
    EventService eventService = nodeEngine.getEventService();
    Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME);
    for (EventRegistration reg : registrations) {
        eventService.publishEvent(SERVICE_NAME, reg, attributeEvent, reg.getId().hashCode());
    }
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) MembershipAwareService(com.hazelcast.spi.MembershipAwareService) MemberAttributeServiceEvent(com.hazelcast.spi.MemberAttributeServiceEvent) EventService(com.hazelcast.spi.EventService)

Aggregations

MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)6 MembershipEvent (com.hazelcast.core.MembershipEvent)3 MembershipListener (com.hazelcast.core.MembershipListener)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Config (com.hazelcast.config.Config)2 ListenerConfig (com.hazelcast.config.ListenerConfig)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)1 MemberAttributeOperationType (com.hazelcast.cluster.MemberAttributeOperationType)1 JoinConfig (com.hazelcast.config.JoinConfig)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 Member (com.hazelcast.core.Member)1 AbstractMember (com.hazelcast.instance.AbstractMember)1 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)1 EventRegistration (com.hazelcast.spi.EventRegistration)1 EventService (com.hazelcast.spi.EventService)1 MemberAttributeServiceEvent (com.hazelcast.spi.MemberAttributeServiceEvent)1 MembershipAwareService (com.hazelcast.spi.MembershipAwareService)1 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1