Skip to content

Commit

Permalink
Merge pull request #2785 from Particular/fix-cp
Browse files Browse the repository at this point in the history
Cherry-pick for #2784 and #2783
  • Loading branch information
WilliamBZA committed Feb 11, 2022
2 parents c0c265c + 7d7d211 commit 1da87cc
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/ServiceControl.Audit/Infrastructure/Bootstrapper.cs
Expand Up @@ -59,6 +59,7 @@ void CreateHost()
builder.ClearProviders();
//HINT: configuration used by NLog comes from LoggingConfigurator.cs
builder.AddNLog();
builder.SetMinimumLevel(loggingSettings.ToHostLogLevel());
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
Expand Down
Expand Up @@ -86,18 +86,27 @@ public static void Clean(int deletionBatchSize, DocumentDatabase database, DateT

var deletedAuditDocuments = Chunker.ExecuteInChunks(items.Count, (itemsForBatch, db, s, e) =>
{
logger.Debug($"Batching deletion of {s}-{e} audit documents.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} audit documents.");
}
var results = db.Batch(itemsForBatch.GetRange(s, e - s + 1), CancellationToken.None);
logger.Debug($"Batching deletion of {s}-{e} audit documents completed.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} audit documents completed.");
}
return results.Count(x => x.Deleted == true);
}, items, database, cancellationToken);

var deletedAttachments = Chunker.ExecuteInChunks(attachments.Count, (att, db, s, e) =>
{
var deleted = 0;
logger.Debug($"Batching deletion of {s}-{e} attachment audit documents.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} attachment audit documents.");
}
db.TransactionalStorage.Batch(accessor =>
{
Expand All @@ -110,18 +119,27 @@ public static void Clean(int deletionBatchSize, DocumentDatabase database, DateT
deleted++;
}
});
logger.Debug($"Batching deletion of {s}-{e} attachment audit documents completed.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} attachment audit documents completed.");
}
return deleted;
}, attachments, database, cancellationToken);

if (deletedAttachments + deletedAuditDocuments == 0)
{
logger.Debug("No expired audit documents found");
if (logger.IsDebugEnabled)
{
logger.Debug("No expired audit documents found");
}
}
else
{
logger.Debug($"Deleted {deletedAuditDocuments} expired audit documents and {deletedAttachments} message body attachments. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
if (logger.IsDebugEnabled)
{
logger.Debug($"Deleted {deletedAuditDocuments} expired audit documents and {deletedAttachments} message body attachments. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
}
}
}

Expand Down
Expand Up @@ -15,7 +15,10 @@ public static Task<TimerJobExecutionResult> RunCleanup(int deletionBatchSize, Do
{
var threshold = SystemTime.UtcNow.Add(-settings.AuditRetentionPeriod);

logger.Debug($"Trying to find expired ProcessedMessage, SagaHistory and KnownEndpoint documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
if (logger.IsDebugEnabled)
{
logger.Debug($"Trying to find expired ProcessedMessage, SagaHistory and KnownEndpoint documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
}
AuditMessageCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);
KnownEndpointsCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);
SagaHistoryCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);
Expand Down
Expand Up @@ -77,22 +77,34 @@ public static void Clean(int deletionBatchSize, DocumentDatabase database, DateT

var deleteKnownEndpointDocuments = Chunker.ExecuteInChunks(items.Count, (itemsForBatch, db, s, e) =>
{
logger.Debug($"Batching deletion of {s}-{e} known endpoint documents.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} known endpoint documents.");
}
var results = db.Batch(itemsForBatch.GetRange(s, e - s + 1), CancellationToken.None);
logger.Debug($"Batching deletion of {s}-{e} known endpoint documents completed.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} known endpoint documents completed.");
}
return results.Count(x => x.Deleted == true);
}, items, database, cancellationToken);


if (deleteKnownEndpointDocuments == 0)
{
logger.Debug("No expired known endpoints documents found");
if (logger.IsDebugEnabled)
{
logger.Debug("No expired known endpoints documents found");
}
}
else
{
logger.Debug($"Deleted {deleteKnownEndpointDocuments} expired known endpoint documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
if (logger.IsDebugEnabled)
{
logger.Debug($"Deleted {deleteKnownEndpointDocuments} expired known endpoint documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
}
}
}

Expand Down
Expand Up @@ -51,5 +51,35 @@ static string DefaultLogPathForInstance(string serviceName)

return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), $"Particular\\{serviceName}\\logs");
}

public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel()
{
if (LoggingLevel == LogLevel.Debug)
{
return Microsoft.Extensions.Logging.LogLevel.Debug;
}
if (LoggingLevel == LogLevel.Error)
{
return Microsoft.Extensions.Logging.LogLevel.Error;
}
if (LoggingLevel == LogLevel.Fatal)
{
return Microsoft.Extensions.Logging.LogLevel.Critical;
}
if (LoggingLevel == LogLevel.Warn)
{
return Microsoft.Extensions.Logging.LogLevel.Warning;
}
if (LoggingLevel == LogLevel.Info)
{
return Microsoft.Extensions.Logging.LogLevel.Information;
}
if (LoggingLevel == LogLevel.Trace)
{
return Microsoft.Extensions.Logging.LogLevel.Trace;
}

return Microsoft.Extensions.Logging.LogLevel.None;
}
}
}
20 changes: 16 additions & 4 deletions src/ServiceControl.SagaAudit/SagaHistoryCleaner.cs
Expand Up @@ -77,21 +77,33 @@ public static void Clean(int deletionBatchSize, DocumentDatabase database, DateT

var deletionCount = Chunker.ExecuteInChunks(items.Count, (itemsForBatch, db, s, e) =>
{
logger.Debug($"Batching deletion of {s}-{e} saga history documents.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} saga history documents.");
}
var results = db.Batch(itemsForBatch.GetRange(s, e - s + 1), CancellationToken.None);
logger.Debug($"Batching deletion of {s}-{e} saga history documents completed.");
if (logger.IsDebugEnabled)
{
logger.Debug($"Batching deletion of {s}-{e} saga history documents completed.");
}
return results.Count(x => x.Deleted == true);
}, items, database, cancellationToken);

if (deletionCount == 0)
{
logger.Debug("No expired saga history documents found");
if (logger.IsDebugEnabled)
{
logger.Debug("No expired saga history documents found");
}
}
else
{
logger.Debug($"Deleted {deletionCount} expired saga history documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
if (logger.IsDebugEnabled)
{
logger.Debug($"Deleted {deletionCount} expired saga history documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ServiceControl/Bootstrapper.cs
Expand Up @@ -100,6 +100,7 @@ void CreateHost()
builder.ClearProviders();
//HINT: configuration used by NLog comes from LoggingConfigurator.cs
builder.AddNLog();
builder.SetMinimumLevel(loggingSettings.ToHostLogLevel());
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
Expand Down
Expand Up @@ -16,19 +16,28 @@ public static Task<TimerJobExecutionResult> RunCleanup(int deletionBatchSize, Do
{
var threshold = SystemTime.UtcNow.Add(-settings.ErrorRetentionPeriod);

logger.Debug($"Trying to find expired FailedMessage documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
if (logger.IsDebugEnabled)
{
logger.Debug($"Trying to find expired FailedMessage documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
}
ErrorMessageCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);

threshold = SystemTime.UtcNow.Add(-settings.EventsRetentionPeriod);

logger.Debug($"Trying to find expired EventLogItem documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
if (logger.IsDebugEnabled)
{
logger.Debug($"Trying to find expired EventLogItem documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
}
EventLogItemsCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);

if (settings.AuditRetentionPeriod.HasValue)
{
threshold = SystemTime.UtcNow.Add(-settings.AuditRetentionPeriod.Value);

logger.Debug($"Trying to find expired ProcessedMessage and SagaHistory documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
if (logger.IsDebugEnabled)
{
logger.Debug($"Trying to find expired ProcessedMessage and SagaHistory documents to delete (with threshold {threshold.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture)})");
}
AuditMessageCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);
SagaHistoryCleaner.Clean(deletionBatchSize, database, threshold, cancellationToken);
}
Expand Down
30 changes: 30 additions & 0 deletions src/ServiceControl/Infrastructure/Settings/LoggingSettings.cs
Expand Up @@ -51,5 +51,35 @@ static string DefaultLogPathForInstance(string serviceName)

return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), $"Particular\\{serviceName}\\logs");
}

public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel()
{
if (LoggingLevel == LogLevel.Debug)
{
return Microsoft.Extensions.Logging.LogLevel.Debug;
}
if (LoggingLevel == LogLevel.Error)
{
return Microsoft.Extensions.Logging.LogLevel.Error;
}
if (LoggingLevel == LogLevel.Fatal)
{
return Microsoft.Extensions.Logging.LogLevel.Critical;
}
if (LoggingLevel == LogLevel.Warn)
{
return Microsoft.Extensions.Logging.LogLevel.Warning;
}
if (LoggingLevel == LogLevel.Info)
{
return Microsoft.Extensions.Logging.LogLevel.Information;
}
if (LoggingLevel == LogLevel.Trace)
{
return Microsoft.Extensions.Logging.LogLevel.Trace;
}

return Microsoft.Extensions.Logging.LogLevel.None;
}
}
}

0 comments on commit 1da87cc

Please sign in to comment.