Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to net7.0 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<UserSecretsId>f21c130e-d067-4ec4-a6a5-1b7b81af9ef1</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Src/AzureTablePurger/AzureTablePurger.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static async Task Main(string[] args)
BuildConfig(args);
var serviceCollection = RegisterServices();
ConfigureLogging(serviceCollection);

_serviceProvider = serviceCollection.BuildServiceProvider();

await using (_serviceProvider)
Expand Down Expand Up @@ -77,7 +78,7 @@ private static ServiceCollection RegisterServices()
// Core logic
serviceCollection.AddScoped<ITablePurger, SimpleTablePurger>();
serviceCollection.AddScoped<IAzureStorageClientFactory, AzureStorageClientFactory>();
serviceCollection.AddScoped<IPartitionKeyHandler, TicksAscendingWithLeadingZeroPartitionKeyHandler>();
serviceCollection.AddScoped<IEntityQueryHandler, TicksAscendingWithLeadingZeroPartitionKeyHandler>();

return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void ConvertPartitionKeyToDateTime_FailsWithGuid()
// Arrange

// Act
_target.ConvertPartitionKeyToDateTime(Guid.NewGuid().ToString());
_target.ConvertKeyToDateTime(Guid.NewGuid().ToString());

// Assert - handled by method attribute
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

using Microsoft.Azure.Cosmos.Table;

namespace AzureTablePurger.Services
{
public interface IEntityQueryHandler
{
TableQuery GetTableQuery(int purgeEntitiesOlderThanDays);

TableQuery GetTableQuery(string lowerBoundKey, string upperBoundKey);

DateTime ConvertKeyToDateTime(DynamicTableEntity entry);


}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class SimpleTablePurger : ITablePurger

private readonly IAzureStorageClientFactory _storageClientFactory;
private readonly ILogger<SimpleTablePurger> _logger;
private readonly IPartitionKeyHandler _partitionKeyHandler;
private readonly IEntityQueryHandler _rowHandler;

public SimpleTablePurger(IAzureStorageClientFactory storageClientFactory, IPartitionKeyHandler partitionKeyHandler, ILogger<SimpleTablePurger> logger)
public SimpleTablePurger(IAzureStorageClientFactory storageClientFactory, IEntityQueryHandler rowHandler, ILogger<SimpleTablePurger> logger)
{
_storageClientFactory = storageClientFactory;
_partitionKeyHandler = partitionKeyHandler;
_rowHandler = rowHandler;
_logger = logger;

ServicePointManager.DefaultConnectionLimit = ConnectionLimit;
Expand All @@ -43,7 +43,7 @@ public async Task<Tuple<int, int>> PurgeEntitiesAsync(PurgeEntitiesOptions optio

_logger.LogInformation($"TargetAccount={tableClient.StorageUri.PrimaryUri}, Table={table.Name}, PurgeRecordsOlderThanDays={options.PurgeRecordsOlderThanDays}");

var query = _partitionKeyHandler.GetTableQuery(options.PurgeRecordsOlderThanDays);
var query = _rowHandler.GetTableQuery(options.PurgeRecordsOlderThanDays);
var continuationToken = new TableContinuationToken();

int numPagesProcessed = 0;
Expand All @@ -64,7 +64,7 @@ public async Task<Tuple<int, int>> PurgeEntitiesAsync(PurgeEntitiesOptions optio
break;
}

var firstResultTimestamp = _partitionKeyHandler.ConvertPartitionKeyToDateTime(page.Results.First().PartitionKey);
var firstResultTimestamp = _rowHandler.ConvertKeyToDateTime(page.Results.First());
_logger.LogInformation($"Page {pageNumber}: processing {page.Count()} results starting at timestamp {firstResultTimestamp}");

var partitionsFromPage = GetPartitionsFromPage(page.Results);
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<Tuple<int, int>> PurgeEntitiesAsync(PurgeEntitiesOptions optio

var entitiesPerSecond = numEntitiesDeleted > 0 ? (int)(numEntitiesDeleted / sw.Elapsed.TotalSeconds) : 0;
var msPerEntity = numEntitiesDeleted > 0 ? (int)(sw.Elapsed.TotalMilliseconds / numEntitiesDeleted) : 0;

_logger.LogInformation($"Finished PurgeEntitiesAsync, processed {numPagesProcessed} pages and deleted {numEntitiesDeleted} entities in {sw.Elapsed} ({entitiesPerSecond} entities per second, or {msPerEntity} ms per entity)");

return new Tuple<int, int>(numPagesProcessed, numEntitiesDeleted);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Globalization;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;

namespace AzureTablePurger.Services
{
public class StringDateRowKeyHandler : IEntityQueryHandler
{
private readonly ILogger _logger;

public StringDateRowKeyHandler(ILogger<StringDateRowKeyHandler> logger)
{
_logger = logger;
}

public TableQuery GetTableQuery(int purgeEntitiesOlderThanDays)
{

var maximumPartitionKeyToDelete = GetMaximumToDelete(purgeEntitiesOlderThanDays);

_logger.LogDebug($"{nameof(DynamicTableEntity.RowKey)}: {purgeEntitiesOlderThanDays}");

return GetTableQuery(null, maximumPartitionKeyToDelete);
}

public TableQuery GetTableQuery(string lowerBoundPartitionKey, string upperBoundRowKey)
{

var upperBound = TableQuery.GenerateFilterCondition(nameof(DynamicTableEntity.RowKey), QueryComparisons.LessThan, upperBoundRowKey);

var query = new TableQuery()
.Where(upperBound)
.Select(new[] { nameof(DynamicTableEntity.PartitionKey), nameof(DynamicTableEntity.RowKey) });


_logger.LogInformation($"Query : {query.FilterString}");

return query;
}

public string GetKeyForDate(DateTime date)
{
return date.ToString("yyyy_MM_dd_HH_mm");
}

public DateTime ConvertKeyToDateTime(DynamicTableEntity entry)
{

var result = DateTime.ParseExact(entry.RowKey, "yyyy_MM_dd_HH_mm", CultureInfo.InvariantCulture);

return result;

}

private string GetMaximumToDelete(int purgeRecordsOlderThanDays)
{
return GetKeyForDate(DateTime.UtcNow.AddDays(-1 * purgeRecordsOlderThanDays));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace AzureTablePurger.Services
{
public class TicksAscendingWithLeadingZeroPartitionKeyHandler : IPartitionKeyHandler
public class TicksAscendingWithLeadingZeroPartitionKeyHandler : IEntityQueryHandler
{
private readonly ILogger<TicksAscendingWithLeadingZeroPartitionKeyHandler> _logger;

Expand All @@ -18,6 +18,8 @@ public TableQuery GetTableQuery(int purgeEntitiesOlderThanDays)
{
var maximumPartitionKeyToDelete = GetMaximumPartitionKeyToDelete(purgeEntitiesOlderThanDays);

_logger.LogDebug($"{nameof(DynamicTableEntity.PartitionKey)}: {purgeEntitiesOlderThanDays}");

return GetTableQuery(null, maximumPartitionKeyToDelete);
}

Expand All @@ -28,22 +30,30 @@ public TableQuery GetTableQuery(string lowerBoundPartitionKey, string upperBound
lowerBoundPartitionKey = "0";
}

var lowerBoundDateTime = ConvertPartitionKeyToDateTime(lowerBoundPartitionKey);
var upperBoundDateTime = ConvertPartitionKeyToDateTime(upperBoundPartitionKey);
_logger.LogDebug($"Generating table query: lowerBound partitionKey={lowerBoundPartitionKey} ({lowerBoundDateTime}), upperBound partitionKey={upperBoundPartitionKey} ({upperBoundDateTime})");
var lowerBoundDateTime = ConvertKeyToDateTime(lowerBoundPartitionKey);
var upperBoundDateTime = ConvertKeyToDateTime(upperBoundPartitionKey);
_logger.LogDebug($"Generating table query: lowerBound {nameof(DynamicTableEntity.PartitionKey)}={lowerBoundPartitionKey} ({lowerBoundDateTime}), upperBound {nameof(DynamicTableEntity.PartitionKey)}={upperBoundPartitionKey} ({upperBoundDateTime})");

var lowerBound = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.GreaterThanOrEqual, lowerBoundPartitionKey);
var upperBound = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThan, upperBoundPartitionKey);
var lowerBound = TableQuery.GenerateFilterCondition(nameof(DynamicTableEntity.PartitionKey), QueryComparisons.GreaterThanOrEqual, lowerBoundPartitionKey);
var upperBound = TableQuery.GenerateFilterCondition(nameof(DynamicTableEntity.PartitionKey), QueryComparisons.LessThan, upperBoundPartitionKey);
var combinedFilter = TableQuery.CombineFilters(lowerBound, TableOperators.And, upperBound);

var query = new TableQuery()
.Where(combinedFilter)
.Select(new[] { "PartitionKey", "RowKey" });
.Select(new[] { nameof(DynamicTableEntity.PartitionKey), nameof(DynamicTableEntity.RowKey) });

_logger.LogInformation($"Query : {query}");

return query;
}

public DateTime ConvertPartitionKeyToDateTime(string partitionKey)

public DateTime ConvertKeyToDateTime(DynamicTableEntity entry)
{
return ConvertKeyToDateTime(entry.PartitionKey);
}

public DateTime ConvertKeyToDateTime(string partitionKey)
{
var result = long.TryParse(partitionKey, out long ticks);

Expand Down