Skip to content

Commit 91c68fe

Browse files
authored
Update DI for alt stats container (#8472)
1 parent d772bac commit 91c68fe

3 files changed

Lines changed: 71 additions & 25 deletions

File tree

src/NuGetGallery/App_Start/DefaultDependenciesModule.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,27 +707,41 @@ private static void RegisterDeleteAccountService(ContainerBuilder builder, Confi
707707

708708
private static void RegisterStatisticsServices(ContainerBuilder builder, IGalleryConfigurationService configuration, ITelemetryService telemetryService)
709709
{
710+
// when running on Windows Azure, download counts come from the downloads.v1.json blob
711+
builder.Register(c => new SimpleBlobStorageConfiguration(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
712+
.SingleInstance()
713+
.Keyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
714+
715+
builder.Register(c => new SimpleBlobStorageConfiguration(configuration.Current.AzureStorage_Statistics_ConnectionString_Alternate, configuration.Current.AzureStorageReadAccessGeoRedundant))
716+
.SingleInstance()
717+
.Keyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
718+
710719
// when running on Windows Azure, we use a back-end job to calculate stats totals and store in the blobs
711-
builder.RegisterInstance(new JsonAggregateStatsService(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
720+
builder.Register(c =>
721+
{
722+
var primaryConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
723+
var alternateConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
724+
var featureFlagService = c.Resolve<IFeatureFlagService>();
725+
var jsonAggregateStatsService = new JsonAggregateStatsService(featureFlagService, primaryConfiguration, alternateConfiguration);
726+
return jsonAggregateStatsService;
727+
})
712728
.AsSelf()
713729
.As<IAggregateStatsService>()
714730
.SingleInstance();
715731

716732
// when running on Windows Azure, pull the statistics from the warehouse via storage
717-
builder.RegisterInstance(new CloudReportService(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
733+
builder.Register(c =>
734+
{
735+
var primaryConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
736+
var alternateConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
737+
var featureFlagService = c.Resolve<IFeatureFlagService>();
738+
var cloudReportService = new CloudReportService(featureFlagService, primaryConfiguration, alternateConfiguration);
739+
return cloudReportService;
740+
})
718741
.AsSelf()
719742
.As<IReportService>()
720743
.SingleInstance();
721744

722-
// when running on Windows Azure, download counts come from the downloads.v1.json blob
723-
builder.Register(c => new SimpleBlobStorageConfiguration(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
724-
.SingleInstance()
725-
.Keyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
726-
727-
builder.Register(c => new SimpleBlobStorageConfiguration(configuration.Current.AzureStorage_Statistics_ConnectionString_Alternate, configuration.Current.AzureStorageReadAccessGeoRedundant))
728-
.SingleInstance()
729-
.Keyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
730-
731745
builder.Register(c =>
732746
{
733747
var primaryConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);

src/NuGetGallery/Services/CloudReportService.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Threading.Tasks;
56
using Microsoft.WindowsAzure.Storage;
67
using Microsoft.WindowsAzure.Storage.Blob;
78
using Microsoft.WindowsAzure.Storage.RetryPolicies;
9+
using NuGetGallery.Services;
810

911
namespace NuGetGallery
1012
{
1113
public class CloudReportService : IReportService
1214
{
1315
private const string _statsContainerName = "nuget-cdnstats";
14-
private readonly string _connectionString;
15-
private readonly bool _readAccessGeoRedundant;
16+
private readonly IFeatureFlagService _featureFlagService;
17+
private readonly IBlobStorageConfiguration _primaryStorageConfiguration;
18+
private readonly IBlobStorageConfiguration _alternateBlobStorageConfiguration;
1619

17-
public CloudReportService(string connectionString, bool readAccessGeoRedundant)
20+
public CloudReportService(
21+
IFeatureFlagService featureFlagService,
22+
IBlobStorageConfiguration primaryBlobStorageConfiguration,
23+
IBlobStorageConfiguration alternateBlobStorageConfiguration)
1824
{
19-
_connectionString = connectionString;
20-
_readAccessGeoRedundant = readAccessGeoRedundant;
25+
_featureFlagService = featureFlagService ?? throw new ArgumentNullException(nameof(featureFlagService));
26+
_primaryStorageConfiguration = primaryBlobStorageConfiguration ?? throw new ArgumentNullException(nameof(primaryBlobStorageConfiguration));
27+
_alternateBlobStorageConfiguration = alternateBlobStorageConfiguration;
2128
}
2229

2330
public async Task<StatisticsReport> Load(string reportName)
@@ -42,10 +49,19 @@ public async Task<StatisticsReport> Load(string reportName)
4249

4350
private CloudBlobContainer GetCloudBlobContainer()
4451
{
45-
var storageAccount = CloudStorageAccount.Parse(_connectionString);
52+
var connectionString = _primaryStorageConfiguration.ConnectionString;
53+
var readAccessGeoRedundant = _primaryStorageConfiguration.ReadAccessGeoRedundant;
54+
55+
if(_alternateBlobStorageConfiguration != null && _featureFlagService.IsAlternateStatisticsSourceEnabled())
56+
{
57+
connectionString = _alternateBlobStorageConfiguration.ConnectionString;
58+
readAccessGeoRedundant = _alternateBlobStorageConfiguration.ReadAccessGeoRedundant;
59+
}
60+
61+
var storageAccount = CloudStorageAccount.Parse(connectionString);
4662
var blobClient = storageAccount.CreateCloudBlobClient();
4763

48-
if (_readAccessGeoRedundant)
64+
if (readAccessGeoRedundant)
4965
{
5066
blobClient.DefaultRequestOptions.LocationMode = LocationMode.PrimaryThenSecondary;
5167
}

src/NuGetGallery/Services/JsonAggregateStatsService.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Threading.Tasks;
56
using Microsoft.WindowsAzure.Storage;
67
using Microsoft.WindowsAzure.Storage.Blob;
78
using Microsoft.WindowsAzure.Storage.RetryPolicies;
89
using Newtonsoft.Json;
10+
using NuGetGallery.Services;
911

1012
namespace NuGetGallery
1113
{
1214
public class JsonAggregateStatsService : IAggregateStatsService
1315
{
14-
private readonly string _connectionString;
15-
private readonly bool _readAccessGeoRedundant;
16+
private readonly IFeatureFlagService _featureFlagService;
17+
private readonly IBlobStorageConfiguration _primaryStorageConfiguration;
18+
private readonly IBlobStorageConfiguration _alternateBlobStorageConfiguration;
1619

17-
public JsonAggregateStatsService(string connectionString, bool readAccessGeoRedundant)
20+
public JsonAggregateStatsService(
21+
IFeatureFlagService featureFlagService,
22+
IBlobStorageConfiguration primaryBlobStorageConfiguration,
23+
IBlobStorageConfiguration alternateBlobStorageConfiguration)
1824
{
19-
_connectionString = connectionString;
20-
_readAccessGeoRedundant = readAccessGeoRedundant;
25+
_featureFlagService = featureFlagService ?? throw new ArgumentNullException(nameof(featureFlagService));
26+
_primaryStorageConfiguration = primaryBlobStorageConfiguration ?? throw new ArgumentNullException(nameof(primaryBlobStorageConfiguration));
27+
_alternateBlobStorageConfiguration = alternateBlobStorageConfiguration;
2128
}
2229

2330
public async Task<AggregateStats> GetAggregateStats()
2431
{
25-
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);
32+
var connectionString = _primaryStorageConfiguration.ConnectionString;
33+
var readAccessGeoRedundant = _primaryStorageConfiguration.ReadAccessGeoRedundant;
34+
35+
if (_alternateBlobStorageConfiguration != null && _featureFlagService.IsAlternateStatisticsSourceEnabled())
36+
{
37+
connectionString = _alternateBlobStorageConfiguration.ConnectionString;
38+
readAccessGeoRedundant = _alternateBlobStorageConfiguration.ReadAccessGeoRedundant;
39+
}
40+
41+
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
2642
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
2743

28-
if (_readAccessGeoRedundant)
44+
if (readAccessGeoRedundant)
2945
{
3046
blobClient.DefaultRequestOptions.LocationMode = LocationMode.PrimaryThenSecondary;
3147
}

0 commit comments

Comments
 (0)