Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
Stats.Aggregate: Improve mitigation and increase timeout (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenriksson authored Dec 20, 2018
1 parent 2ddda83 commit d1e3faa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Stats.AggregateCdnDownloadsInGallery
{
public class AggregateCdnDownloadsJob : JsonConfigurationJob
{
private const int _defaultCommandTimeoutSeconds = 1800; // 30 minutes
private const int _defaultBatchSize = 5000;
private const int _defaultBatchSleepSeconds = 10;
private const string _tempTableName = "#AggregateCdnDownloadsInGallery";
Expand Down Expand Up @@ -59,12 +60,14 @@ GROUP BY Stats.[PackageRegistrationKey]
private const string _storedProcedureName = "[dbo].[SelectTotalDownloadCountsPerPackageVersion]";

private AggregateCdnDownloadsConfiguration _configuration;
private int _commandTimeoutSeconds;

public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary)
{
base.Init(serviceContainer, jobArgsDictionary);

_configuration = _serviceProvider.GetRequiredService<IOptionsSnapshot<AggregateCdnDownloadsConfiguration>>().Value;
_commandTimeoutSeconds = _configuration.CommandTimeoutSeconds ?? _defaultCommandTimeoutSeconds;
}

public override async Task Run()
Expand All @@ -87,11 +90,12 @@ await connection.QueryWithRetryAsync<DownloadCountData>(
_storedProcedureName,
transaction: transaction,
commandType: CommandType.StoredProcedure,
commandTimeout: TimeSpan.FromMinutes(30),
commandTimeout: TimeSpan.FromSeconds(_commandTimeoutSeconds),
maxRetries: 3))
.ToList();
}

stopwatch.Stop();
Logger.LogInformation(
"Gathered {RecordCount} rows of data (took {DurationSeconds} seconds).",
downloadData.Count,
Expand Down Expand Up @@ -138,6 +142,7 @@ await connection.QueryWithRetryAsync<DownloadCountData>(
}
}

stopwatch.Stop();
Logger.LogInformation(
"It took {DurationSeconds} seconds to update all download counts.",
stopwatch.Elapsed.TotalSeconds);
Expand All @@ -161,7 +166,7 @@ private async Task ProcessBatch(List<IPackageIdGroup> batch, SqlConnection desti
var aggregateCdnDownloadsInGalleryTable = new DataTable();
var command = new SqlCommand("SELECT * FROM " + _tempTableName, destinationDatabase);
command.CommandType = CommandType.Text;
command.CommandTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds;
command.CommandTimeout = _commandTimeoutSeconds;
var reader = await command.ExecuteReaderAsync();
aggregateCdnDownloadsInGalleryTable.Load(reader);
aggregateCdnDownloadsInGalleryTable.Rows.Clear();
Expand All @@ -188,6 +193,7 @@ private async Task ProcessBatch(List<IPackageIdGroup> batch, SqlConnection desti
}
}

stopwatch.Stop();
Logger.LogInformation(
"Populated temporary table in memory with {RecordCount} rows (took {DurationSeconds} seconds).",
aggregateCdnDownloadsInGalleryTable.Rows.Count,
Expand All @@ -205,6 +211,7 @@ private async Task ProcessBatch(List<IPackageIdGroup> batch, SqlConnection desti
bulkcopy.Close();
}

stopwatch.Stop();
Logger.LogInformation(
"Populated temporary table in database (took {DurationSeconds} seconds).",
stopwatch.Elapsed.TotalSeconds);
Expand All @@ -217,11 +224,12 @@ private async Task ProcessBatch(List<IPackageIdGroup> batch, SqlConnection desti
{
cmd.CommandText = _updateFromTempTable;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = (int)TimeSpan.FromMinutes(30).TotalSeconds;
cmd.CommandTimeout = _commandTimeoutSeconds;

await cmd.ExecuteNonQueryAsync();
}

stopwatch.Stop();
Logger.LogInformation(
"Updated destination database Download Counts (took {DurationSeconds} seconds).",
stopwatch.Elapsed.TotalSeconds);
Expand Down Expand Up @@ -275,7 +283,7 @@ private async Task<IDictionary<string, string>> GetPackageRegistrations(SqlConne
// Ensure results are sorted deterministically.
var packageRegistrationData = (await sqlConnection.QueryWithRetryAsync<PackageRegistrationData>(
"SELECT [Key], LOWER([Id]) AS LowercasedId, [Id] AS OriginalId FROM [dbo].[PackageRegistrations] (NOLOCK) ORDER BY [Id] ASC",
commandTimeout: TimeSpan.FromMinutes(10),
commandTimeout: TimeSpan.FromSeconds(_commandTimeoutSeconds),
maxRetries: 5)).ToList();

// We are not using .ToDictionary() and instead explicitly looping through these items to be able to detect
Expand Down Expand Up @@ -308,6 +316,7 @@ private async Task<IDictionary<string, string>> GetPackageRegistrations(SqlConne
}
}

stopwatch.Stop();
Logger.LogInformation(
"Retrieved {Count} package registrations (took {DurationSeconds} seconds).",
packageRegistrationData.Count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public class AggregateCdnDownloadsConfiguration
public int BatchSize { get; set; }

public int BatchSleepSeconds { get; set; }

public int? CommandTimeoutSeconds { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Stats.AggregateCdnDownloadsInGallery/Settings/dev.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"Initialization": {
"BatchSize": 5000,
"BatchSleepSeconds": 10
"BatchSleepSeconds": 10,
"CommandTimeoutSeconds": 1800
},

"GalleryDb": {
Expand Down
3 changes: 2 additions & 1 deletion src/Stats.AggregateCdnDownloadsInGallery/Settings/int.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"Initialization": {
"BatchSize": 5000,
"BatchSleepSeconds": 10
"BatchSleepSeconds": 10,
"CommandTimeoutSeconds": 1800
},

"GalleryDb": {
Expand Down
3 changes: 2 additions & 1 deletion src/Stats.AggregateCdnDownloadsInGallery/Settings/prod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"Initialization": {
"BatchSize": 5000,
"BatchSleepSeconds": 10
"BatchSleepSeconds": 10,
"CommandTimeoutSeconds": 2700
},

"GalleryDb": {
Expand Down

0 comments on commit d1e3faa

Please sign in to comment.