This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Auxiliary2AzureSearch to split steps into classes (#747)
Progress on NuGet/Engineering#2978
- Loading branch information
1 parent
42bd053
commit 587d6e9
Showing
10 changed files
with
580 additions
and
400 deletions.
There are no files selected for viewing
349 changes: 16 additions & 333 deletions
349
src/NuGet.Services.AzureSearch/Auxiliary2AzureSearch/Auxiliary2AzureSearchCommand.cs
Large diffs are not rendered by default.
Oops, something went wrong.
337 changes: 337 additions & 0 deletions
337
src/NuGet.Services.AzureSearch/Auxiliary2AzureSearch/UpdateDownloadsCommand.cs
Large diffs are not rendered by default.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
src/NuGet.Services.AzureSearch/Auxiliary2AzureSearch/UpdateVerifiedPackagesCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using NuGet.Services.AzureSearch.AuxiliaryFiles; | ||
using NuGetGallery; | ||
|
||
namespace NuGet.Services.AzureSearch.Auxiliary2AzureSearch | ||
{ | ||
public class UpdateVerifiedPackagesCommand : IAzureSearchCommand | ||
{ | ||
private readonly IDatabaseAuxiliaryDataFetcher _databaseFetcher; | ||
private readonly IVerifiedPackagesDataClient _verifiedPackagesDataClient; | ||
private readonly IAzureSearchTelemetryService _telemetryService; | ||
private readonly ILogger<Auxiliary2AzureSearchCommand> _logger; | ||
private readonly StringCache _stringCache; | ||
|
||
public UpdateVerifiedPackagesCommand( | ||
IDatabaseAuxiliaryDataFetcher databaseFetcher, | ||
IVerifiedPackagesDataClient verifiedPackagesDataClient, | ||
IAzureSearchTelemetryService telemetryService, | ||
ILogger<Auxiliary2AzureSearchCommand> logger) | ||
{ | ||
_databaseFetcher = databaseFetcher ?? throw new ArgumentNullException(nameof(databaseFetcher)); | ||
_verifiedPackagesDataClient = verifiedPackagesDataClient ?? throw new ArgumentNullException(nameof(verifiedPackagesDataClient)); | ||
_telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_stringCache = new StringCache(); | ||
} | ||
|
||
public async Task ExecuteAsync() | ||
{ | ||
var stopwatch = Stopwatch.StartNew(); | ||
var outcome = JobOutcome.Failure; | ||
try | ||
{ | ||
outcome = await UpdateVerifiedPackagesAsync() ? JobOutcome.Success : JobOutcome.NoOp; | ||
} | ||
finally | ||
{ | ||
stopwatch.Stop(); | ||
_telemetryService.TrackUpdateVerifiedPackagesCompleted(outcome, stopwatch.Elapsed); | ||
} | ||
} | ||
|
||
private async Task<bool> UpdateVerifiedPackagesAsync() | ||
{ | ||
// The "old" data in this case is the latest file that was copied to the region's storage container by this | ||
// job (or initialized by Db2AzureSearch). | ||
var oldResult = await _verifiedPackagesDataClient.ReadLatestAsync( | ||
AccessConditionWrapper.GenerateEmptyCondition(), | ||
_stringCache); | ||
|
||
// The "new" data in this case is from the database. | ||
var newData = await _databaseFetcher.GetVerifiedPackagesAsync(); | ||
|
||
var changes = new HashSet<string>(oldResult.Data, oldResult.Data.Comparer); | ||
changes.SymmetricExceptWith(newData); | ||
_logger.LogInformation("{Count} package IDs have verified status changes.", changes.Count); | ||
|
||
if (changes.Count == 0) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
await _verifiedPackagesDataClient.ReplaceLatestAsync(newData, oldResult.Metadata.GetIfMatchCondition()); | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...et.Services.AzureSearch.Tests/Auxiliary2AzureSearch/UpdateVerifiedPackagesCommandFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
using NuGet.Services.AzureSearch.AuxiliaryFiles; | ||
using NuGet.Services.AzureSearch.Support; | ||
using NuGetGallery; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace NuGet.Services.AzureSearch.Auxiliary2AzureSearch | ||
{ | ||
public class UpdateVerifiedPackagesCommandFacts | ||
{ | ||
public class ExecuteAsync : Facts | ||
{ | ||
public ExecuteAsync(ITestOutputHelper output) : base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task PushesAddedVerifiedPackage() | ||
{ | ||
NewVerifiedPackagesData.Add("NuGet.Versioning"); | ||
|
||
await Target.ExecuteAsync(); | ||
|
||
VerifyCompletedTelemetry(JobOutcome.Success); | ||
VerifiedPackagesDataClient.Verify( | ||
x => x.ReplaceLatestAsync( | ||
NewVerifiedPackagesData, | ||
It.Is<IAccessCondition>(a => a.IfMatchETag == OldVerifiedPackagesResult.Metadata.ETag)), | ||
Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task PushesRemovedVerifiedPackage() | ||
{ | ||
OldVerifiedPackagesData.Add("NuGet.Versioning"); | ||
|
||
await Target.ExecuteAsync(); | ||
|
||
VerifyCompletedTelemetry(JobOutcome.Success); | ||
VerifiedPackagesDataClient.Verify( | ||
x => x.ReplaceLatestAsync( | ||
NewVerifiedPackagesData, | ||
It.Is<IAccessCondition>(a => a.IfMatchETag == OldVerifiedPackagesResult.Metadata.ETag)), | ||
Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task DoesNotPushUnchangedVerifiedPackages() | ||
{ | ||
await Target.ExecuteAsync(); | ||
|
||
VerifyCompletedTelemetry(JobOutcome.NoOp); | ||
VerifiedPackagesDataClient.Verify( | ||
x => x.ReplaceLatestAsync(It.IsAny<HashSet<string>>(), It.IsAny<IAccessCondition>()), | ||
Times.Never); | ||
} | ||
} | ||
|
||
public abstract class Facts | ||
{ | ||
public Facts(ITestOutputHelper output) | ||
{ | ||
DatabaseAuxiliaryDataFetcher = new Mock<IDatabaseAuxiliaryDataFetcher>(); | ||
VerifiedPackagesDataClient = new Mock<IVerifiedPackagesDataClient>(); | ||
TelemetryService = new Mock<IAzureSearchTelemetryService>(); | ||
Logger = output.GetLogger<Auxiliary2AzureSearchCommand>(); | ||
|
||
OldVerifiedPackagesData = new HashSet<string>(); | ||
OldVerifiedPackagesResult = Data.GetAuxiliaryFileResult(OldVerifiedPackagesData, "verified-packages-etag"); | ||
VerifiedPackagesDataClient | ||
.Setup(x => x.ReadLatestAsync(It.IsAny<IAccessCondition>(), It.IsAny<StringCache>())) | ||
.ReturnsAsync(() => OldVerifiedPackagesResult); | ||
NewVerifiedPackagesData = new HashSet<string>(); | ||
DatabaseAuxiliaryDataFetcher.Setup(x => x.GetVerifiedPackagesAsync()).ReturnsAsync(() => NewVerifiedPackagesData); | ||
|
||
Target = new UpdateVerifiedPackagesCommand( | ||
DatabaseAuxiliaryDataFetcher.Object, | ||
VerifiedPackagesDataClient.Object, | ||
TelemetryService.Object, | ||
Logger); | ||
} | ||
|
||
public Mock<IDatabaseAuxiliaryDataFetcher> DatabaseAuxiliaryDataFetcher { get; } | ||
public Mock<IVerifiedPackagesDataClient> VerifiedPackagesDataClient { get; } | ||
public Mock<IAzureSearchTelemetryService> TelemetryService { get; } | ||
public RecordingLogger<Auxiliary2AzureSearchCommand> Logger { get; } | ||
public UpdateVerifiedPackagesCommand Target { get; } | ||
public HashSet<string> OldVerifiedPackagesData { get; } | ||
public AuxiliaryFileResult<HashSet<string>> OldVerifiedPackagesResult { get; } | ||
public HashSet<string> NewVerifiedPackagesData { get; } | ||
|
||
public void VerifyCompletedTelemetry(JobOutcome outcome) | ||
{ | ||
TelemetryService.Verify( | ||
x => x.TrackUpdateVerifiedPackagesCompleted(It.IsAny<JobOutcome>(), It.IsAny<TimeSpan>()), | ||
Times.Once); | ||
TelemetryService.Verify( | ||
x => x.TrackUpdateVerifiedPackagesCompleted(outcome, It.IsAny<TimeSpan>()), | ||
Times.Once); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.