Search in sources :

Example 1 with ClusterMemberAddress

use of com.netflix.titus.api.clustermembership.model.ClusterMemberAddress in project titus-control-plane by Netflix.

the class GrpcClusterMembershipLeaderNameResolver method refresh.

private void refresh(ClusterMembershipSnapshot snapshot) {
    try {
        Optional<ClusterMembershipRevision<ClusterMember>> leaderOpt = snapshot.getLeaderRevision().flatMap(l -> Optional.ofNullable(snapshot.getMemberRevisions().get(l.getCurrent().getMemberId())));
        if (leaderOpt.isPresent()) {
            ClusterMembershipRevision<ClusterMember> memberRevision = leaderOpt.get();
            ClusterMemberAddress address = addressSelector.apply(memberRevision.getCurrent());
            if (lastLeader == null || !lastLeader.getCurrent().getMemberId().equals(memberRevision.getCurrent().getMemberId())) {
                logger.info("New leader: {}", memberRevision);
                Evaluators.acceptNotNull(leaderMetrics, LeaderMetrics::close);
                lastLeader = memberRevision;
                leaderMetrics = new LeaderMetrics(lastLeader, titusRuntime);
            } else {
                logger.debug("Refreshing: {}", lastLeader);
            }
            EquivalentAddressGroup server = new EquivalentAddressGroup(new InetSocketAddress(address.getIpAddress(), address.getPortNumber()));
            List<EquivalentAddressGroup> servers = Collections.singletonList(server);
            listener.onAddresses(servers, Attributes.EMPTY);
        } else {
            if (lastLeader != null) {
                leaderMetrics.close();
                lastLeader = null;
                leaderMetrics = null;
                logger.warn("No leader");
            }
            listener.onError(Status.UNAVAILABLE.withDescription("Unable to resolve leader server"));
        }
    } catch (Exception e) {
        logger.error("Unable to create server with error: ", e);
        listener.onError(Status.UNAVAILABLE.withCause(e));
    }
}
Also used : ClusterMember(com.netflix.titus.api.clustermembership.model.ClusterMember) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) ClusterMemberAddress(com.netflix.titus.api.clustermembership.model.ClusterMemberAddress) ClusterMembershipRevision(com.netflix.titus.api.clustermembership.model.ClusterMembershipRevision)

Aggregations

ClusterMember (com.netflix.titus.api.clustermembership.model.ClusterMember)1 ClusterMemberAddress (com.netflix.titus.api.clustermembership.model.ClusterMemberAddress)1 ClusterMembershipRevision (com.netflix.titus.api.clustermembership.model.ClusterMembershipRevision)1 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1 InetSocketAddress (java.net.InetSocketAddress)1