/// <summary>
    /// Clears the leader UUID if the passed tablet server is the current leader.
    /// If it is the current leader, then the next call to this tablet will have
    /// to query the master to find the new leader.
    /// </summary>
    /// <param name="uuid">
    /// A tablet server that gave a sign that it isn't this tablet's leader.
    /// </param>
    public ServerInfoCache DemoteLeader(string uuid)
    {
        var leaderIndex = _leaderIndex;

        if (leaderIndex != -1)
        {
            var serverInfo = _servers[leaderIndex];
            if (serverInfo.Uuid == uuid)
            {
                var replicas           = _replicas;
                var newReplicas        = new List <KuduReplica>(replicas);
                var priorLeaderReplica = replicas[leaderIndex];
                var newLeaderReplica   = new KuduReplica(
                    priorLeaderReplica.HostPort,
                    ReplicaRole.Follower,
                    priorLeaderReplica.DimensionLabel);

                newReplicas[leaderIndex] = newLeaderReplica;

                return(new ServerInfoCache(_servers, newReplicas, -1));
            }
        }

        return(this);
    }
Beispiel #2
0
    public static async Task <List <RemoteTablet> > GetTabletsAsync(
        this IKuduConnectionFactory connectionFactory,
        string tableId, GetTableLocationsResponsePB locations)
    {
        // TODO: Need error handling here.
        var tsInfos         = locations.TsInfos;
        var internedServers = new List <ServerInfo>(tsInfos.Count);
        var results         = new List <RemoteTablet>(locations.TabletLocations.Count);

        foreach (var tsInfo in tsInfos)
        {
            var serverInfo = await GetServerInfoAsync(connectionFactory, tsInfo)
                             .ConfigureAwait(false);

            if (serverInfo is not null)
            {
                internedServers.Add(serverInfo);
            }
        }

        foreach (var tabletInfo in locations.TabletLocations)
        {
            var tabletId  = tabletInfo.TabletId.ToStringUtf8();
            var partition = ProtobufHelper.ToPartition(tabletInfo.Partition);

            var numReplicas = Math.Max(
                tabletInfo.DEPRECATEDReplicas.Count,
                tabletInfo.InternedReplicas.Count);

            var servers     = new List <ServerInfo>(numReplicas);
            var replicas    = new List <KuduReplica>(numReplicas);
            int leaderIndex = -1;

            // Handle interned replicas.
            foreach (var replicaPb in tabletInfo.InternedReplicas)
            {
                var tsInfoIdx  = (int)replicaPb.TsInfoIdx;
                var serverInfo = internedServers[tsInfoIdx];

                var replica = new KuduReplica(
                    serverInfo.HostPort,
                    (ReplicaRole)replicaPb.Role,
                    replicaPb.DimensionLabel);

                if (replica.Role == ReplicaRole.Leader)
                {
                    leaderIndex = servers.Count;
                }

                servers.Add(serverInfo);
                replicas.Add(replica);
            }

            // Handle "old-style" non-interned replicas.
            // It's used for backward compatibility.
            foreach (var replicaPb in tabletInfo.DEPRECATEDReplicas)
            {
                var serverInfo = await connectionFactory.GetServerInfoAsync(
                    replicaPb.TsInfo).ConfigureAwait(false);

                if (serverInfo != null)
                {
                    var replica = new KuduReplica(
                        serverInfo.HostPort,
                        (ReplicaRole)replicaPb.Role,
                        replicaPb.DimensionLabel);

                    if (replica.Role == ReplicaRole.Leader)
                    {
                        leaderIndex = servers.Count;
                    }

                    servers.Add(serverInfo);
                    replicas.Add(replica);
                }
            }

            var serverCache = new ServerInfoCache(servers, replicas, leaderIndex);

            var tablet = new RemoteTablet(
                tableId,
                tabletId,
                partition,
                serverCache);

            results.Add(tablet);
        }

        return(results);
    }