use of com.netflix.titus.api.clustermembership.model.ClusterMemberLeadership in project titus-control-plane by Netflix.
the class MultiNodeClusterMemberResolver method buildSnapshot.
private ClusterMembershipSnapshot buildSnapshot() {
if (memberResolversByIpAddress.isEmpty()) {
return ClusterMembershipSnapshot.empty();
}
List<ClusterMembershipSnapshot> healthySnapshots = findHealthySnapshots();
ClusterMembershipSnapshot.Builder builder = ClusterMembershipSnapshot.newBuilder();
Map<String, List<ClusterMembershipRevision<ClusterMember>>> grouped = healthySnapshots.stream().flatMap(snapshot -> snapshot.getMemberRevisions().values().stream()).collect(Collectors.groupingBy(m -> m.getCurrent().getMemberId()));
List<ClusterMembershipRevision<ClusterMember>> recentRevisions = grouped.values().stream().map(this::findBestMemberRevision).collect(Collectors.toList());
builder.withMemberRevisions(recentRevisions);
// Find leader
Optional<ClusterMembershipRevision<ClusterMemberLeadership>> recentLeader = Optional.empty();
for (ClusterMembershipSnapshot snapshot : healthySnapshots) {
if (snapshot.getLeaderRevision().isPresent()) {
if (recentLeader.isPresent()) {
if (recentLeader.get().getRevision() < snapshot.getLeaderRevision().get().getRevision()) {
recentLeader = snapshot.getLeaderRevision();
}
} else {
recentLeader = snapshot.getLeaderRevision();
}
}
}
recentLeader.ifPresent(builder::withLeaderRevision);
// Choose latest version of each
long minStaleness = healthySnapshots.stream().mapToLong(ClusterMembershipSnapshot::getStalenessMs).min().orElse(0);
builder.withStalenessMs(minStaleness);
return builder.build();
}
use of com.netflix.titus.api.clustermembership.model.ClusterMemberLeadership in project titus-control-plane by Netflix.
the class MultiNodeClusterMemberResolverMetrics method updateSnapshot.
void updateSnapshot(ClusterMembershipSnapshot snapshot) {
// Members
snapshot.getMemberRevisions().forEach((memberId, revision) -> {
if (!knownMembers.containsKey(memberId)) {
knownMembers.put(memberId, new KnownMember(memberId));
}
});
Set<String> toRemove = CollectionsExt.copyAndRemove(knownMembers.keySet(), snapshot.getMemberRevisions().keySet());
toRemove.forEach(memberIp -> knownMembers.remove(memberIp).close());
// Leader
ClusterMemberLeadership currentLeader = snapshot.getLeaderRevision().map(ClusterMembershipRevision::getCurrent).orElse(null);
if (currentLeader != null) {
if (leader == null) {
this.leader = new Leader(currentLeader);
} else {
if (!leader.getMemberId().equals(currentLeader.getMemberId())) {
leader.close();
this.leader = new Leader(currentLeader);
}
}
} else if (leader != null) {
leader.close();
leader = null;
}
}
Aggregations