public ConnectionCache(
     IKuduConnectionFactory connectionFactory,
     ILoggerFactory loggerFactory)
 {
     _connectionFactory = connectionFactory;
     _logger            = loggerFactory.CreateLogger <ConnectionCache>();
     _connections       = new Dictionary <IPEndPoint, Task <KuduConnection> >();
 }
Beispiel #2
0
    public static Task <ServerInfo?> GetServerInfoAsync(
        this IKuduConnectionFactory connectionFactory,
        TSInfoPB tsInfo)
    {
        string uuid      = tsInfo.PermanentUuid.ToStringUtf8();
        string location  = tsInfo.Location;
        var    addresses = tsInfo.RpcAddresses;

        if (addresses.Count == 0)
        {
            // Received a tablet server with no addresses.
            return(Task.FromResult <ServerInfo?>(null));
        }

        // TODO: if the TS advertises multiple host/ports, pick the right one
        // based on some kind of policy. For now just use the first always.
        var hostPort = addresses[0].ToHostAndPort();

        return(connectionFactory.GetTabletServerInfoAsync(hostPort, uuid, location) !);
    }
 public TestConnectionFactory(IKuduConnectionFactory realConnectionFactory)
 {
     _realConnectionFactory = realConnectionFactory;
     _cache = new ConcurrentDictionary <IPEndPoint, Task <KuduConnection> >();
 }
Beispiel #4
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);
    }