Search in sources :

Example 1 with CPGroupAvailabilityEvent

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

the class RaftService method publishGroupAvailabilityEvents.

private void publishGroupAvailabilityEvents(MemberImpl removedMember) {
    ClusterService clusterService = nodeEngine.getClusterService();
    if (clusterService.getClusterVersion().isUnknownOrLessThan(Versions.V4_1)) {
        return;
    }
    // they will be the ones that keep track of unreachable CP members.
    for (CPGroupId groupId : metadataGroupManager.getActiveGroupIds()) {
        CPGroupSummary group = metadataGroupManager.getGroup(groupId);
        Collection<CPMember> missing = new ArrayList<>();
        boolean availabilityDecreased = false;
        for (CPMember member : group.members()) {
            if (member.getAddress().equals(removedMember.getAddress())) {
                // Group's availability decreased because of this removed member
                availabilityDecreased = true;
                missing.add(member);
            } else if (clusterService.getMember(member.getAddress()) == null) {
                missing.add(member);
            }
        }
        if (availabilityDecreased) {
            CPGroupAvailabilityEvent e = new CPGroupAvailabilityEventImpl(group.id(), group.members(), missing);
            nodeEngine.getEventService().publishEvent(SERVICE_NAME, EVENT_TOPIC_AVAILABILITY, e, EVENT_TOPIC_AVAILABILITY.hashCode());
        }
    }
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) CPGroupAvailabilityEventImpl(com.hazelcast.cp.event.impl.CPGroupAvailabilityEventImpl) CPGroupAvailabilityEvent(com.hazelcast.cp.event.CPGroupAvailabilityEvent) ClusterService(com.hazelcast.internal.cluster.ClusterService) ArrayList(java.util.ArrayList) CPMember(com.hazelcast.cp.CPMember)

Example 2 with CPGroupAvailabilityEvent

use of com.hazelcast.cp.event.CPGroupAvailabilityEvent 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)

Aggregations

CPGroupAvailabilityEvent (com.hazelcast.cp.event.CPGroupAvailabilityEvent)2 CPGroupId (com.hazelcast.cp.CPGroupId)1 CPMember (com.hazelcast.cp.CPMember)1 CPGroupAvailabilityListener (com.hazelcast.cp.event.CPGroupAvailabilityListener)1 CPMembershipEvent (com.hazelcast.cp.event.CPMembershipEvent)1 CPMembershipListener (com.hazelcast.cp.event.CPMembershipListener)1 CPGroupAvailabilityEventImpl (com.hazelcast.cp.event.impl.CPGroupAvailabilityEventImpl)1 ClusterService (com.hazelcast.internal.cluster.ClusterService)1 ArrayList (java.util.ArrayList)1