From f8664641cf270d051aba4d062c17ad2e877721e6 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Mon, 9 Aug 2021 17:24:04 -0700 Subject: [PATCH] Fixing GetServerSystemInfo for Azure DBs (#1231) * Not throwing errors when we try to fetch SQL server properties like server count and physical mem. * Fixed comment * Renaming get server cpu count * Adding more detailed comment over here * Making properties cpu count and physical memory to nullable so they are defaulted to null. * Fixing rest of the properties to nullable. --- .../ReliableConnectionHelper.cs | 17 ++++++++++------- .../Connection/Contracts/ServerInfo.cs | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs index 3b9f0665..33b5f92f 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs @@ -653,8 +653,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection public string OsVersion; public string MachineName; public string ServerName; - public int CpuCount; - public int PhysicalMemoryInMB; + public Nullable CpuCount; + public Nullable PhysicalMemoryInMB; public Dictionary Options { get; set; } } @@ -678,8 +678,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection public class ServerSystemInfo { - public int CpuCount; - public int PhysicalMemoryInMB; + public Nullable CpuCount; + public Nullable PhysicalMemoryInMB; } public static bool TryGetServerVersion(string connectionString, out ServerInfo serverInfo, string azureAccountToken) @@ -763,7 +763,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection /// Gets the server host cpu count and memory from sys.dm_os_sys_info view /// /// The connection - public static ServerSystemInfo GetServerSystemInfo(IDbConnection connection) + public static ServerSystemInfo GetServerCpuAndMemoryInfo(IDbConnection connection) { var sysInfo = new ServerSystemInfo(); try @@ -776,8 +776,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection } catch (Exception ex) { + // We don't want to fail the normal flow if any unexpected thing happens + // since these properties are not available for types of sql servers and users + // and it is not essential to always include them + // just logging the errors here and moving on with the workflow. Logger.Write(TraceEventType.Error, ex.ToString()); - throw ex; } return sysInfo; } @@ -856,7 +859,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection // otherwise - Windows Server 2019 Standard 10.0 serverInfo.OsVersion = hostInfo.Distribution != null ? string.Format("{0} {1}", hostInfo.Distribution, hostInfo.Release) : string.Format("{0} {1}", hostInfo.Platform, hostInfo.Release); - var sysInfo = GetServerSystemInfo(connection); + var sysInfo = GetServerCpuAndMemoryInfo(connection); serverInfo.CpuCount = sysInfo.CpuCount; serverInfo.PhysicalMemoryInMB = sysInfo.PhysicalMemoryInMB; diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ServerInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ServerInfo.cs index c8aa3195..3270e865 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ServerInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ServerInfo.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // +using System; using System.Collections.Generic; namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts @@ -70,12 +71,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts /// /// The CPU count of the host running the server. /// - public int CpuCount; + public Nullable CpuCount; /// /// The physical memory of the host running the server in MBs. /// - public int PhysicalMemoryInMB; + public Nullable PhysicalMemoryInMB; /// /// Server options