From b0fd20f82b330d5fd1c27598a9c91e14af8acbaa Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Thu, 28 Mar 2019 08:39:12 -0700 Subject: [PATCH] Allow the SQL command timeout to be configured in Search.GenerateAuxiliaryData (#736) Progress on https://github.com/NuGet/Engineering/issues/2327 --- .../Configuration/InitializationConfiguration.cs | 4 ++++ src/Search.GenerateAuxiliaryData/Job.cs | 9 ++++++--- .../NestedJArrayExporter.cs | 8 ++++++-- src/Search.GenerateAuxiliaryData/RankingsExporter.cs | 6 +++--- src/Search.GenerateAuxiliaryData/SqlExporter.cs | 11 ++++++++--- .../VerifiedPackagesExporter.cs | 6 +++--- .../RankingsExporterTests.cs | 3 ++- .../VerifiedPackagesExporterTests.cs | 3 ++- 8 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/Search.GenerateAuxiliaryData/Configuration/InitializationConfiguration.cs b/src/Search.GenerateAuxiliaryData/Configuration/InitializationConfiguration.cs index 1ff142b10..34dacc9e4 100644 --- a/src/Search.GenerateAuxiliaryData/Configuration/InitializationConfiguration.cs +++ b/src/Search.GenerateAuxiliaryData/Configuration/InitializationConfiguration.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; + namespace Search.GenerateAuxiliaryData { public class InitializationConfiguration @@ -12,5 +14,7 @@ public class InitializationConfiguration public string PrimaryDestination { get; set; } public string DestinationContainerName { get; set; } + + public TimeSpan SqlCommandTimeout { get; set; } = TimeSpan.FromMinutes(1); } } diff --git a/src/Search.GenerateAuxiliaryData/Job.cs b/src/Search.GenerateAuxiliaryData/Job.cs index a78fea2a1..036a5fb9e 100644 --- a/src/Search.GenerateAuxiliaryData/Job.cs +++ b/src/Search.GenerateAuxiliaryData/Job.cs @@ -58,7 +58,8 @@ public override void Init(IServiceContainer serviceContainer, IDictionary, destinationContainer, ScriptVerifiedPackages, - OutputNameVerifiedPackages), + OutputNameVerifiedPackages, + Configuration.SqlCommandTimeout), new NestedJArrayExporter( LoggerFactory.CreateLogger(), @@ -67,14 +68,16 @@ public override void Init(IServiceContainer serviceContainer, IDictionary(), OpenSqlConnectionAsync, destinationContainer, ScriptRankingsTotal, - OutputNameRankings), + OutputNameRankings, + Configuration.SqlCommandTimeout), new BlobStorageExporter( LoggerFactory.CreateLogger(), diff --git a/src/Search.GenerateAuxiliaryData/NestedJArrayExporter.cs b/src/Search.GenerateAuxiliaryData/NestedJArrayExporter.cs index 8d2b8232b..9acc88d51 100644 --- a/src/Search.GenerateAuxiliaryData/NestedJArrayExporter.cs +++ b/src/Search.GenerateAuxiliaryData/NestedJArrayExporter.cs @@ -23,8 +23,12 @@ public NestedJArrayExporter( ILogger logger, Func> openSqlConnectionAsync, CloudBlobContainer defaultDestinationContainer, - string defaultSqlScript, string defaultName, string defaultCol0, string defaultCol1) - : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName) + string defaultSqlScript, + string defaultName, + string defaultCol0, + string defaultCol1, + TimeSpan commandTimeout) + : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName, commandTimeout) { Col0 = defaultCol0; Col1 = defaultCol1; diff --git a/src/Search.GenerateAuxiliaryData/RankingsExporter.cs b/src/Search.GenerateAuxiliaryData/RankingsExporter.cs index eb418c944..9aa89fa7d 100644 --- a/src/Search.GenerateAuxiliaryData/RankingsExporter.cs +++ b/src/Search.GenerateAuxiliaryData/RankingsExporter.cs @@ -24,8 +24,9 @@ public RankingsExporter( Func> openSqlConnectionAsync, CloudBlobContainer defaultDestinationContainer, string defaultRankingsScript, - string defaultName) - : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName) + string defaultName, + TimeSpan commandTimeout) + : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName, commandTimeout) { _rankingsTotalScript = defaultRankingsScript; } @@ -34,7 +35,6 @@ protected override JContainer GetResultOfQuery(SqlConnection connection) { var rankingsTotalCommand = GetEmbeddedSqlCommand(connection, _rankingsTotalScript); rankingsTotalCommand.Parameters.AddWithValue(_rankingCountParameterName, _rankingCount); - rankingsTotalCommand.CommandTimeout = 60; return GetRankings(rankingsTotalCommand.ExecuteReader()); } diff --git a/src/Search.GenerateAuxiliaryData/SqlExporter.cs b/src/Search.GenerateAuxiliaryData/SqlExporter.cs index 1a0b77b56..9ec40c83e 100644 --- a/src/Search.GenerateAuxiliaryData/SqlExporter.cs +++ b/src/Search.GenerateAuxiliaryData/SqlExporter.cs @@ -24,19 +24,23 @@ public abstract class SqlExporter : Exporter private Func> OpenSqlConnectionAsync { get; } + private readonly TimeSpan _commandTimeout; + public SqlExporter( ILogger logger, Func> openSqlConnectionAsync, CloudBlobContainer defaultDestinationContainer, - string defaultName) + string defaultName, + TimeSpan commandTimeout) : base(logger, defaultDestinationContainer, defaultName) { _logger = logger; OpenSqlConnectionAsync = openSqlConnectionAsync; + _commandTimeout = commandTimeout; } [SuppressMessage("Microsoft.Security", "CA2100", Justification = "Query string comes from embedded resource, not user input.")] - protected static SqlCommand GetEmbeddedSqlCommand(SqlConnection connection, string resourceName) + protected SqlCommand GetEmbeddedSqlCommand(SqlConnection connection, string resourceName) { using (var reader = new StreamReader(_executingAssembly.GetManifestResourceStream(_assemblyName + "." + resourceName))) { @@ -44,7 +48,8 @@ protected static SqlCommand GetEmbeddedSqlCommand(SqlConnection connection, stri return new SqlCommand(commandText, connection) { - CommandType = CommandType.Text + CommandType = CommandType.Text, + CommandTimeout = (int)_commandTimeout.TotalSeconds, }; } } diff --git a/src/Search.GenerateAuxiliaryData/VerifiedPackagesExporter.cs b/src/Search.GenerateAuxiliaryData/VerifiedPackagesExporter.cs index 95c4091bd..8a9287824 100644 --- a/src/Search.GenerateAuxiliaryData/VerifiedPackagesExporter.cs +++ b/src/Search.GenerateAuxiliaryData/VerifiedPackagesExporter.cs @@ -21,8 +21,9 @@ public VerifiedPackagesExporter( Func> openSqlConnectionAsync, CloudBlobContainer defaultDestinationContainer, string defaultVerifiedPackagesScript, - string defaultName) - : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName) + string defaultName, + TimeSpan commandTimeout) + : base(logger, openSqlConnectionAsync, defaultDestinationContainer, defaultName, commandTimeout) { _verifiedPackagesScript = defaultVerifiedPackagesScript; } @@ -30,7 +31,6 @@ public VerifiedPackagesExporter( protected override JContainer GetResultOfQuery(SqlConnection connection) { var verifiedPackagesCommand = GetEmbeddedSqlCommand(connection, _verifiedPackagesScript); - verifiedPackagesCommand.CommandTimeout = 60; SqlDataReader reader = null; diff --git a/tests/Tests.Search.GenerateAuxiliaryData/RankingsExporterTests.cs b/tests/Tests.Search.GenerateAuxiliaryData/RankingsExporterTests.cs index b3386ccad..5af9712ab 100644 --- a/tests/Tests.Search.GenerateAuxiliaryData/RankingsExporterTests.cs +++ b/tests/Tests.Search.GenerateAuxiliaryData/RankingsExporterTests.cs @@ -47,7 +47,8 @@ private static RankingsExporter CreateExporter() openSqlConnectionAsync: () => null, defaultDestinationContainer: new CloudBlobContainer(new Uri("https://nuget.org")), defaultRankingsScript: "b", - defaultName: "c"); + defaultName: "c", + commandTimeout: TimeSpan.FromSeconds(10)); } } } \ No newline at end of file diff --git a/tests/Tests.Search.GenerateAuxiliaryData/VerifiedPackagesExporterTests.cs b/tests/Tests.Search.GenerateAuxiliaryData/VerifiedPackagesExporterTests.cs index a16001b10..4b884844e 100644 --- a/tests/Tests.Search.GenerateAuxiliaryData/VerifiedPackagesExporterTests.cs +++ b/tests/Tests.Search.GenerateAuxiliaryData/VerifiedPackagesExporterTests.cs @@ -46,7 +46,8 @@ private static VerifiedPackagesExporter CreateExporter() openSqlConnectionAsync: () => null, defaultDestinationContainer: new CloudBlobContainer(new Uri("https://nuget.org")), defaultVerifiedPackagesScript: "b", - defaultName: "c"); + defaultName: "c", + commandTimeout: TimeSpan.FromSeconds(10)); } } } \ No newline at end of file