Search in sources :

Example 1 with CPMembershipEvent

use of com.hazelcast.cp.event.CPMembershipEvent in project hazelcast by hazelcast.

the class RaftService method dispatchEvent.

@Override
public void dispatchEvent(Object e, EventListener l) {
    long now = Clock.currentTimeMillis();
    recentAvailabilityEvents.values().removeIf(expirationTime -> expirationTime < now);
    if (e instanceof CPMembershipEvent) {
        CPMembershipEvent event = (CPMembershipEvent) e;
        CPMembershipListener listener = (CPMembershipListener) l;
        switch(event.getType()) {
            case ADDED:
                listener.memberAdded(event);
                break;
            case REMOVED:
                listener.memberRemoved(event);
                break;
            default:
                throw new IllegalArgumentException("Unhandled event: " + event);
        }
        return;
    }
    if (e instanceof CPGroupAvailabilityEvent) {
        CPGroupAvailabilityEvent event = (CPGroupAvailabilityEvent) e;
        if (recentAvailabilityEvents.putIfAbsent(new CPGroupAvailabilityEventKey(event, l), now + AVAILABILITY_EVENTS_DEDUPLICATION_PERIOD) != null) {
            return;
        }
        CPGroupAvailabilityListener listener = (CPGroupAvailabilityListener) l;
        if (event.isMajorityAvailable()) {
            listener.availabilityDecreased(event);
        } else {
            listener.majorityLost(event);
        }
        return;
    }
    throw new IllegalArgumentException("Unhandled event: " + e);
}
Also used : CPGroupAvailabilityEvent(com.hazelcast.cp.event.CPGroupAvailabilityEvent) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) CPMembershipEvent(com.hazelcast.cp.event.CPMembershipEvent) CPGroupAvailabilityListener(com.hazelcast.cp.event.CPGroupAvailabilityListener)

Example 2 with CPMembershipEvent

use of com.hazelcast.cp.event.CPMembershipEvent in project hazelcast by hazelcast.

the class MetadataRaftGroupManager method sendMembershipEvents.

private void sendMembershipEvents(Collection<CPMemberInfo> currentMembers, Collection<CPMemberInfo> newMembers) {
    if (!isMetadataGroupLeader()) {
        return;
    }
    EventService eventService = nodeEngine.getEventService();
    Collection<CPMemberInfo> addedMembers = new LinkedHashSet<>(newMembers);
    addedMembers.removeAll(currentMembers);
    for (CPMemberInfo member : addedMembers) {
        CPMembershipEvent event = new CPMembershipEventImpl(member, EventType.ADDED);
        eventService.publishEvent(SERVICE_NAME, EVENT_TOPIC_MEMBERSHIP, event, EVENT_TOPIC_MEMBERSHIP.hashCode());
    }
    Collection<CPMemberInfo> removedMembers = new LinkedHashSet<>(currentMembers);
    removedMembers.removeAll(newMembers);
    for (CPMemberInfo member : removedMembers) {
        CPMembershipEvent event = new CPMembershipEventImpl(member, EventType.REMOVED);
        eventService.publishEvent(SERVICE_NAME, EVENT_TOPIC_MEMBERSHIP, event, EVENT_TOPIC_MEMBERSHIP.hashCode());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EventService(com.hazelcast.spi.impl.eventservice.EventService) CPMembershipEvent(com.hazelcast.cp.event.CPMembershipEvent) CPMembershipEventImpl(com.hazelcast.cp.event.impl.CPMembershipEventImpl)

Aggregations

CPMembershipEvent (com.hazelcast.cp.event.CPMembershipEvent)2 CPGroupAvailabilityEvent (com.hazelcast.cp.event.CPGroupAvailabilityEvent)1 CPGroupAvailabilityListener (com.hazelcast.cp.event.CPGroupAvailabilityListener)1 CPMembershipListener (com.hazelcast.cp.event.CPMembershipListener)1 CPMembershipEventImpl (com.hazelcast.cp.event.impl.CPMembershipEventImpl)1 EventService (com.hazelcast.spi.impl.eventservice.EventService)1 LinkedHashSet (java.util.LinkedHashSet)1