use of io.atomix.primitive.partition.PartitionGroupMembership in project atomix by atomix.
the class DefaultPartitionService method start.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<PartitionService> start() {
groupMembershipService.addListener(groupMembershipEventListener);
return groupMembershipService.start().thenCompose(v -> {
PartitionGroupMembership systemGroupMembership = groupMembershipService.getSystemMembership();
if (systemGroupMembership != null) {
if (systemGroup == null) {
systemGroup = ((PartitionGroup.Type) systemGroupMembership.config().getType()).newPartitionGroup(systemGroupMembership.config());
}
systemElectionService = new DefaultPrimaryElectionService(systemGroup);
systemSessionIdService = new ReplicatedSessionIdService(systemGroup);
electionService = new HashBasedPrimaryElectionService(clusterMembershipService, groupMembershipService, communicationService);
return electionService.start().thenCompose(s -> {
PartitionManagementService managementService = new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, electionService, new DefaultSessionIdService());
if (systemGroupMembership.members().contains(clusterMembershipService.getLocalMember().id())) {
return systemGroup.join(managementService);
} else {
return systemGroup.connect(managementService);
}
});
} else {
return Futures.exceptionalFuture(new ConfigurationException("No system partition group found"));
}
}).thenCompose(v -> systemElectionService.start().thenCompose(v2 -> systemSessionIdService.start()).thenApply(v2 -> new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, systemElectionService, systemSessionIdService))).thenCompose(managementService -> {
this.partitionManagementService = (PartitionManagementService) managementService;
List<CompletableFuture> futures = groupMembershipService.getMemberships().stream().map(membership -> {
ManagedPartitionGroup group;
synchronized (groups) {
group = groups.get(membership.group());
if (group == null) {
group = ((PartitionGroup.Type) membership.config().getType()).newPartitionGroup(membership.config());
groups.put(group.name(), group);
}
}
if (membership.members().contains(clusterMembershipService.getLocalMember().id())) {
return group.join(partitionManagementService);
} else {
return group.connect(partitionManagementService);
}
}).collect(Collectors.toList());
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenApply(v -> {
LOGGER.info("Started");
started.set(true);
return this;
});
});
}
use of io.atomix.primitive.partition.PartitionGroupMembership 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);
}
}
}
}
Aggregations