Skip to content

Commit a818066

Browse files
authored
Adjust delete threshold logic in AwsS3SyncPlanStrategy (#1811)
- Update delete ratio calculation to use `TotalRemoteFiles` instead of `TotalSyncRequests`. - Add logging and reset delete threshold to `0.0` when no S3 remote files are discovered. Ensuring our plan only contains additions. Emit `plan-valid` as github actions output.
1 parent ebab77e commit a818066

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

src/tooling/docs-assembler/Cli/DeployCommands.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ internal sealed class DeployCommands(
2525
ICoreService githubActionsService
2626
)
2727
{
28+
private readonly ILogger<Program> _logger = logFactory.CreateLogger<Program>();
29+
2830
[SuppressMessage("Usage", "CA2254:Template should be a static expression")]
2931
private void AssignOutputLogger()
3032
{
31-
var log = logFactory.CreateLogger<Program>();
32-
ConsoleApp.Log = msg => log.LogInformation(msg);
33-
ConsoleApp.LogError = msg => log.LogError(msg);
33+
ConsoleApp.Log = msg => _logger.LogInformation(msg);
34+
ConsoleApp.LogError = msg => _logger.LogError(msg);
3435
}
3536

3637
/// <summary> Creates a sync plan </summary>
@@ -57,20 +58,17 @@ public async Task<int> Plan(
5758
var s3Client = new AmazonS3Client();
5859
IDocsSyncPlanStrategy planner = new AwsS3SyncPlanStrategy(logFactory, s3Client, s3BucketName, assembleContext);
5960
var plan = await planner.Plan(ctx);
60-
ConsoleApp.Log("Total files to sync: " + plan.TotalSyncRequests);
61-
ConsoleApp.Log("Total files to delete: " + plan.DeleteRequests.Count);
62-
ConsoleApp.Log("Total files to add: " + plan.AddRequests.Count);
63-
ConsoleApp.Log("Total files to update: " + plan.UpdateRequests.Count);
64-
ConsoleApp.Log("Total files to skip: " + plan.SkipRequests.Count);
65-
if (plan.TotalSyncRequests == 0)
66-
{
67-
collector.EmitError(@out, $"Plan has no files to sync so no plan will be written.");
68-
await collector.StopAsync(ctx);
69-
return collector.Errors;
70-
}
61+
_logger.LogInformation("Total files to sync: {TotalFiles}", plan.TotalSyncRequests);
62+
_logger.LogInformation("Total files to delete: {DeleteCount}", plan.DeleteRequests.Count);
63+
_logger.LogInformation("Total files to add: {AddCount}", plan.AddRequests.Count);
64+
_logger.LogInformation("Total files to update: {UpdateCount}", plan.UpdateRequests.Count);
65+
_logger.LogInformation("Total files to skip: {SkipCount}", plan.SkipRequests.Count);
66+
_logger.LogInformation("Total local source files: {TotalSourceFiles}", plan.TotalSourceFiles);
67+
_logger.LogInformation("Total remote source files: {TotalSourceFiles}", plan.TotalRemoteFiles);
7168
var validationResult = planner.Validate(plan, deleteThreshold);
7269
if (!validationResult.Valid)
7370
{
71+
await githubActionsService.SetOutputAsync("plan-valid", "false");
7472
collector.EmitError(@out, $"Plan is invalid, delete ratio: {validationResult.DeleteRatio}, threshold: {validationResult.DeleteThreshold} over {plan.TotalSyncRequests:N0} files while plan has {plan.DeleteRequests:N0} deletions");
7573
await collector.StopAsync(ctx);
7674
return collector.Errors;
@@ -85,6 +83,7 @@ public async Task<int> Plan(
8583
ConsoleApp.Log("Plan written to " + @out);
8684
}
8785
await collector.StopAsync(ctx);
86+
await githubActionsService.SetOutputAsync("plan-valid", collector.Errors == 0 ? "true" : "false");
8887
return collector.Errors;
8988
}
9089

@@ -121,6 +120,19 @@ public async Task<int> Apply(
121120
}
122121
var planJson = await File.ReadAllTextAsync(planFile, ctx);
123122
var plan = SyncPlan.Deserialize(planJson);
123+
_logger.LogInformation("Total files to sync: {TotalFiles}", plan.TotalSyncRequests);
124+
_logger.LogInformation("Total files to delete: {DeleteCount}", plan.DeleteRequests.Count);
125+
_logger.LogInformation("Total files to add: {AddCount}", plan.AddRequests.Count);
126+
_logger.LogInformation("Total files to update: {UpdateCount}", plan.UpdateRequests.Count);
127+
_logger.LogInformation("Total files to skip: {SkipCount}", plan.SkipRequests.Count);
128+
_logger.LogInformation("Total local source files: {TotalSourceFiles}", plan.TotalSourceFiles);
129+
_logger.LogInformation("Total remote source files: {TotalSourceFiles}", plan.TotalRemoteFiles);
130+
if (plan.TotalSyncRequests == 0)
131+
{
132+
_logger.LogInformation("Plan has no files to sync, skipping incremental synchronization");
133+
await collector.StopAsync(ctx);
134+
return collector.Errors;
135+
}
124136
await applier.Apply(plan, ctx);
125137
await collector.StopAsync(ctx);
126138
return collector.Errors;

src/tooling/docs-assembler/Deploying/AwsS3SyncPlanStrategy.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ await Parallel.ForEachAsync(localObjects, ctx, async (localFile, token) =>
158158

159159
return new SyncPlan
160160
{
161+
TotalRemoteFiles = remoteObjects.Count,
161162
TotalSourceFiles = localObjects.Length,
162163
DeleteRequests = deleteRequests.ToList(),
163164
AddRequests = addRequests.ToList(),
@@ -176,15 +177,20 @@ public PlanValidationResult Validate(SyncPlan plan, float deleteThreshold)
176177
return new(false, 1.0f, deleteThreshold);
177178
}
178179

179-
var deleteRatio = (float)plan.DeleteRequests.Count / plan.TotalSyncRequests;
180-
// if the total sync requests are less than 100, we enforce a higher ratio of 0.8
180+
var deleteRatio = (float)plan.DeleteRequests.Count / plan.TotalRemoteFiles;
181+
if (plan.TotalRemoteFiles == 0)
182+
{
183+
_logger.LogInformation("No files discovered in S3, assuming a clean bucket resetting delete threshold to `0.0' as our plan should not have ANY deletions");
184+
deleteThreshold = 0.0f;
185+
}
186+
// if the total remote files are less than or equal to 100, we enforce a higher ratio of 0.8
181187
// this allows newer assembled documentation to be in a higher state of flux
182-
if (plan.TotalSyncRequests <= 100)
188+
if (plan.TotalRemoteFiles <= 100)
183189
deleteThreshold = Math.Max(deleteThreshold, 0.8f);
184190

185-
// if the total sync requests are less than 1000, we enforce a higher ratio of 0.5
191+
// if the total remote files are less than or equal to 1000, we enforce a higher ratio of 0.5
186192
// this allows newer assembled documentation to be in a higher state of flux
187-
else if (plan.TotalSyncRequests <= 1000)
193+
else if (plan.TotalRemoteFiles <= 1000)
188194
deleteThreshold = Math.Max(deleteThreshold, 0.5f);
189195

190196
if (deleteRatio > deleteThreshold)

src/tooling/docs-assembler/Deploying/DocsSync.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ public record SkipRequest : SyncRequest
5252

5353
public record SyncPlan
5454
{
55+
/// The total number of source files that were located in the build output
5556
[JsonPropertyName("total_source_files")]
5657
public required int TotalSourceFiles { get; init; }
5758

59+
/// The total number of remote files that were located in the remote location
60+
[JsonPropertyName("total_remote_files")]
61+
public required int TotalRemoteFiles { get; init; }
62+
63+
/// The total number of sync requests that were generated (sum of <see cref="AddRequests"/>, <see cref="UpdateRequests"/>, <see cref="DeleteRequests"/>)
5864
[JsonPropertyName("total_sync_requests")]
5965
public required int TotalSyncRequests { get; init; }
6066

tests/docs-assembler.Tests/src/docs-assembler.Tests/DocsSyncTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public async Task TestPlan()
6666

6767
// Assert
6868

69+
plan.TotalRemoteFiles.Should().Be(3);
70+
6971
plan.TotalSourceFiles.Should().Be(5);
7072
plan.TotalSyncRequests.Should().Be(6); //including skip on server
7173

@@ -231,6 +233,7 @@ public async Task TestApply()
231233
var context = new AssembleContext(config, configurationContext, "dev", collector, fileSystem, fileSystem, null, checkoutDirectory);
232234
var plan = new SyncPlan
233235
{
236+
TotalRemoteFiles = 0,
234237
TotalSourceFiles = 5,
235238
TotalSyncRequests = 6,
236239
AddRequests = [

0 commit comments

Comments
 (0)