Search in sources :

Example 6 with ClusterMemberLeadership

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();
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) LoggerFactory(org.slf4j.LoggerFactory) ClusterMembershipSnapshot(com.netflix.titus.api.clustermembership.model.ClusterMembershipSnapshot) HashMap(java.util.HashMap) ReactorExt(com.netflix.titus.common.util.rx.ReactorExt) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) ScheduleReference(com.netflix.titus.common.framework.scheduler.ScheduleReference) ClusterMemberAddress(com.netflix.titus.api.clustermembership.model.ClusterMemberAddress) Duration(java.time.Duration) Map(java.util.Map) ClusterMember(com.netflix.titus.api.clustermembership.model.ClusterMember) ClusterMembershipRevision(com.netflix.titus.api.clustermembership.model.ClusterMembershipRevision) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Retryers(com.netflix.titus.common.util.retry.Retryers) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterMemberLeadership(com.netflix.titus.api.clustermembership.model.ClusterMemberLeadership) Set(java.util.Set) Collectors(java.util.stream.Collectors) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) ScheduleDescriptor(com.netflix.titus.common.framework.scheduler.model.ScheduleDescriptor) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) ClusterMembershipRevision(com.netflix.titus.api.clustermembership.model.ClusterMembershipRevision) ClusterMembershipSnapshot(com.netflix.titus.api.clustermembership.model.ClusterMembershipSnapshot) ClusterMember(com.netflix.titus.api.clustermembership.model.ClusterMember) List(java.util.List)

Example 7 with ClusterMemberLeadership

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;
    }
}
Also used : ClusterMemberLeadership(com.netflix.titus.api.clustermembership.model.ClusterMemberLeadership)

Aggregations

ClusterMemberLeadership (com.netflix.titus.api.clustermembership.model.ClusterMemberLeadership)7 LeaderElectionChangeEvent (com.netflix.titus.api.clustermembership.model.event.LeaderElectionChangeEvent)3 ClusterMembershipRevision (com.netflix.titus.api.clustermembership.model.ClusterMembershipRevision)2 Preconditions (com.google.common.base.Preconditions)1 Stopwatch (com.google.common.base.Stopwatch)1 ClusterMember (com.netflix.titus.api.clustermembership.model.ClusterMember)1 ClusterMemberAddress (com.netflix.titus.api.clustermembership.model.ClusterMemberAddress)1 ClusterMembershipSnapshot (com.netflix.titus.api.clustermembership.model.ClusterMembershipSnapshot)1 ScheduleReference (com.netflix.titus.common.framework.scheduler.ScheduleReference)1 ScheduleDescriptor (com.netflix.titus.common.framework.scheduler.model.ScheduleDescriptor)1 TitusRuntime (com.netflix.titus.common.runtime.TitusRuntime)1 CollectionsExt (com.netflix.titus.common.util.CollectionsExt)1 Retryers (com.netflix.titus.common.util.retry.Retryers)1 ReactorExt (com.netflix.titus.common.util.rx.ReactorExt)1 Duration (java.time.Duration)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1