Search in sources :

Example 1 with PartitionGroupMembershipEvent

use of io.atomix.primitive.partition.PartitionGroupMembershipEvent in project atomix by atomix.

the class DefaultPartitionGroupMembershipService method updatePartitionGroups.

private void updatePartitionGroups(PartitionGroupInfo info) {
    if (systemGroup == null && info.systemGroup != null) {
        systemGroup = info.systemGroup;
        post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, systemGroup));
        LOGGER.info("{} - Bootstrapped management group {} from {}", membershipService.getLocalMember().id(), systemGroup, info.memberId);
    } else if (systemGroup != null && info.systemGroup != null) {
        if (!systemGroup.group().equals(info.systemGroup.group()) || !systemGroup.config().getType().name().equals(info.systemGroup.config().getType().name())) {
            throw new ConfigurationException("Duplicate system group detected");
        } else {
            Set<MemberId> newMembers = Stream.concat(systemGroup.members().stream(), info.systemGroup.members().stream()).filter(memberId -> membershipService.getMember(memberId) != null).collect(Collectors.toSet());
            if (!Sets.difference(newMembers, systemGroup.members()).isEmpty()) {
                systemGroup = new PartitionGroupMembership(systemGroup.group(), systemGroup.config(), ImmutableSet.copyOf(newMembers), true);
                post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, systemGroup));
                LOGGER.debug("{} - Updated management group {} from {}", membershipService.getLocalMember().id(), systemGroup, info.memberId);
            }
        }
    }
    for (PartitionGroupMembership newMembership : info.groups) {
        PartitionGroupMembership oldMembership = groups.get(newMembership.group());
        if (oldMembership == null) {
            groups.put(newMembership.group(), newMembership);
            post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, newMembership));
            LOGGER.info("{} - Bootstrapped partition group {} from {}", membershipService.getLocalMember().id(), newMembership, info.memberId);
        } else if (!oldMembership.group().equals(newMembership.group()) || !oldMembership.config().getType().name().equals(newMembership.config().getType().name())) {
            throw new ConfigurationException("Duplicate partition group " + newMembership.group() + " detected");
        } else {
            Set<MemberId> newMembers = Stream.concat(oldMembership.members().stream(), newMembership.members().stream()).filter(memberId -> membershipService.getMember(memberId) != null).collect(Collectors.toSet());
            if (!Sets.difference(newMembers, oldMembership.members()).isEmpty()) {
                PartitionGroupMembership newGroup = new PartitionGroupMembership(oldMembership.group(), oldMembership.config(), ImmutableSet.copyOf(newMembers), false);
                groups.put(oldMembership.group(), newGroup);
                post(new PartitionGroupMembershipEvent(MEMBERS_CHANGED, newGroup));
                LOGGER.debug("{} - Updated partition group {} from {}", membershipService.getLocalMember().id(), newGroup, info.memberId);
            }
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ConfigurationException(io.atomix.utils.config.ConfigurationException) PartitionGroupMembershipEvent(io.atomix.primitive.partition.PartitionGroupMembershipEvent)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 PartitionGroupMembership (io.atomix.primitive.partition.PartitionGroupMembership)1 PartitionGroupMembershipEvent (io.atomix.primitive.partition.PartitionGroupMembershipEvent)1 ConfigurationException (io.atomix.utils.config.ConfigurationException)1 Set (java.util.Set)1