private void VerifyGetReplicaSelectedServerInfoDeterminism(ServerInfoCache cache)
    {
        string init = cache.GetClosestServerInfo(_clientLocation).Uuid;

        for (int i = 0; i < 10; i++)
        {
            string next = cache.GetClosestServerInfo(_clientLocation).Uuid;

            Assert.Equal(init, next);
        }
    }
示例#2
0
 public RemoteTablet(
     string tableId,
     string tabletId,
     Partition partition,
     ServerInfoCache cache)
 {
     TableId   = tableId;
     TabletId  = tabletId;
     Partition = partition;
     _cache    = cache;
 }
示例#3
0
        ServerInfo GetServerInfo(UIConnectionInfo uiConnectionInfo)
        {
            var databases = Properties.Settings.Default.GetDatabases();

            if (databases.Length == 0)
            {
                return(null);
            }

            if (_cache.ContainsKey(uiConnectionInfo.ServerName))
            {
                ServerInfoCache serverInfoCache = _cache[uiConnectionInfo.ServerName];
                if (Enumerable.SequenceEqual(serverInfoCache.databases, databases) &&
                    DateTime.Now.Subtract(serverInfoCache.timestamp).TotalMinutes < 30)
                {
                    return(serverInfoCache.info);
                }
            }

            ServerInfo        res            = new ServerInfo();
            SqlConnectionInfo connectionInfo = Common.Connection.GetConnectionInfo(uiConnectionInfo);
            ServerConnection  connection     = new ServerConnection(connectionInfo);
            Server            server         = new Server(connection);

            foreach (Database database in server.Databases)
            {
                if (databases.Contains(database.Name))
                {
                    DatabaseInfo databaseInfo = new DatabaseInfo();
                    foreach (Table table in database.Tables)
                    {
                        databaseInfo.tables[table.Name] = table.Schema;
                        databaseInfo.tablesLower[table.Name.ToLower()] = table.Name;
                    }
                    foreach (View view in database.Views)
                    {
                        // views are indistinguishable from tables in preloader's context
                        databaseInfo.tables[view.Name] = view.Schema;
                        databaseInfo.tablesLower[view.Name.ToLower()] = view.Name;
                    }
                    foreach (UserDefinedFunction function in database.UserDefinedFunctions)
                    {
                        databaseInfo.functions[function.Name] = function.Schema;
                        databaseInfo.functionsLower[function.Name.ToLower()] = function.Name;
                    }
                    foreach (StoredProcedure proc in database.StoredProcedures)
                    {
                        databaseInfo.procedures[proc.Name] = proc.Schema;
                        databaseInfo.proceduresLower[proc.Name.ToLower()] = proc.Name;
                    }
                    res.databases[database.Name] = databaseInfo;
                }
            }

            {
                ServerInfoCache serverInfoCache = new ServerInfoCache();
                serverInfoCache.info                = res;
                serverInfoCache.databases           = databases;
                serverInfoCache.timestamp           = DateTime.Now;
                _cache[uiConnectionInfo.ServerName] = serverInfoCache;
            }

            return(res);
        }
示例#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);
    }