From 2b617dc114c10b24b52687c0fc5a02ee93989807 Mon Sep 17 00:00:00 2001 From: Kevin Cunnane Date: Wed, 14 Aug 2019 10:54:34 -0700 Subject: [PATCH] Use new DMV for big data cluster check (#849) * Use new DMV for big data cluster check * Include fallback logic to use older CTP cluster table if new DMV does not exist --- .../ReliableConnectionHelper.cs | 72 +++++++++++++------ .../SqlConnectionHelperScripts.cs | 7 +- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs index e490a406..bf6cf3ee 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs @@ -708,6 +708,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection public class ClusterEndpoint { public string ServiceName; + public string Description; + public string Endpoint; + public string Protocol; public string IpAddress; public int Port; } @@ -782,12 +785,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection serverInfo.IsSelectiveXmlIndexMetadataPresent = reader.GetInt32(5) == 1; } - if (reader.FieldCount > 6) - { - serverInfo.Options = new Dictionary(); - serverInfo.Options.Add(ServerInfo.OptionIsBigDataCluster, reader.GetInt32(6)); - } - // The 'ProductVersion' server property is of the form ##.#[#].####.#, Version serverVersion = new Version(serverInfo.ServerVersion); @@ -824,29 +821,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection serverInfo.OsVersion = reader[0].ToString(); }); + serverInfo.Options = new Dictionary(); + // Get BDC endpoints if (!serverInfo.IsCloud) { - serverInfo.Options = new Dictionary(); List clusterEndpoints = new List(); + serverInfo.Options.Add(ServerInfo.OptionClusterEndpoints, clusterEndpoints); + try { - ExecuteReader( - connection, - SqlConnectionHelperScripts.GetClusterEndpoints, - delegate (IDataReader reader) - { - while (reader.Read()) - { - clusterEndpoints.Add(new ClusterEndpoint { ServiceName = reader.GetString(0), IpAddress = reader.GetString(1), Port = reader.GetInt32(2) }); - } - serverInfo.Options.Add(ServerInfo.OptionClusterEndpoints, clusterEndpoints); - serverInfo.Options.Add(ServerInfo.OptionIsBigDataCluster, clusterEndpoints.Count > 0); - }); + LookupClusterEndpoints(connection, serverInfo, clusterEndpoints); } catch (SqlException) { - serverInfo.Options.Add(ServerInfo.OptionClusterEndpoints, clusterEndpoints); + // Failed to find cluster endpoints DMV / table, this must not be a cluster + // or user does not have permissions to see cluster info serverInfo.Options.Add(ServerInfo.OptionIsBigDataCluster, false); } } @@ -869,6 +859,48 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection return result; } + private static void LookupClusterEndpoints(IDbConnection connection, ServerInfo serverInfo, List clusterEndpoints) + { + try + { + ExecuteReader( + connection, + SqlConnectionHelperScripts.GetClusterEndpoints, + delegate (IDataReader reader) + { + while (reader.Read()) + { + clusterEndpoints.Add(new ClusterEndpoint { + ServiceName = reader.GetString(0), + Description = reader.GetString(1), + Endpoint = reader.GetString(2), + Protocol = reader.GetString(3) + }); + } + serverInfo.Options.Add(ServerInfo.OptionIsBigDataCluster, clusterEndpoints.Count > 0); + } + ); + } + catch (Exception) + { + // Fallback to using previous CTP logic. + // TODO #847 remove this logic once we've stopped supporting CTPs + ExecuteReader( + connection, + SqlConnectionHelperScripts.GetClusterEndpoints_CTP, + delegate (IDataReader reader) + { + while (reader.Read()) + { + clusterEndpoints.Add(new ClusterEndpoint { ServiceName = reader.GetString(0), IpAddress = reader.GetString(1), Port = reader.GetInt32(2) }); + } + serverInfo.Options.Add(ServerInfo.OptionIsBigDataCluster, clusterEndpoints.Count > 0); + } + ); + + } + } + public static string GetServerName(IDbConnection connection) { return new DbConnectionWrapper(connection).DataSource; diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs index eab17d1a..c7f7ebc4 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs @@ -47,7 +47,12 @@ SELECT @filepath AS FilePath "; public const string GetOsVersion = @"SELECT OSVersion = RIGHT(@@version, LEN(@@version)- 3 -charindex (' on ', LOWER(@@version)))"; - public const string GetClusterEndpoints = @"IF OBJECT_ID (N'master.dbo.cluster_endpoint_info') IS NOT NULL + // TODO: #847 remove the Throw condition from endpoints query when removing the GetClusterEndpoints_CTP code. + // This is needed for 1 monthly release, to support back compatibility with CTP 3.2 + public const string GetClusterEndpoints = @"IF OBJECT_ID (N'sys.dm_cluster_endpoints') IS NOT NULL +SELECT [name], [description], [endpoint], [protocol_desc] FROM .[sys].[dm_cluster_endpoints]; +ELSE THROW 50000, 'Use older DMV', 1"; + public const string GetClusterEndpoints_CTP = @"IF OBJECT_ID (N'master.dbo.cluster_endpoint_info') IS NOT NULL SELECT [service_name], [ip_address], [port] FROM [master].[dbo].[cluster_endpoint_info];"; } }