Skip to content

Commit e7e2aa4

Browse files
authored
chore(Orleans.Runtime): Use [LoggerMessage] p4 (#9541)
* chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src/Orleans.Runtime/GrainDirectory/LocalGrainDirectoryPartition.cs` * chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src\Orleans.Runtime\GrainDirectory\LocalGrainDirectory.cs` * chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src/Orleans.Runtime/Networking/ConnectionListener.cs` * chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src\Orleans.Runtime\Networking\SiloConnection.cs` * chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src\Orleans.Runtime\Messaging\MessageCenter.cs` * chore(Orleans.Runtime): Use [LoggerMessage] p4 Cleanup `src\Orleans.Runtime\GrainDirectory\AdaptiveDirectoryCacheMaintainer.cs` * chore(Orleans.Runtime): Refactor logging implementation in AdaptiveDirectoryCacheMaintainer
1 parent db24143 commit e7e2aa4

File tree

6 files changed

+431
-145
lines changed

6 files changed

+431
-145
lines changed

src/Orleans.Runtime/GrainDirectory/AdaptiveDirectoryCacheMaintainer.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal sealed partial class AdaptiveDirectoryCacheMaintainer
1414
{
1515
private static readonly TimeSpan SLEEP_TIME_BETWEEN_REFRESHES = Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromMinutes(1); // this should be something like minTTL/4
1616

17+
private readonly ILogger<AdaptiveDirectoryCacheMaintainer> _log;
1718
private readonly AdaptiveGrainDirectoryCache cache;
1819
private readonly LocalGrainDirectory router;
1920
private readonly IInternalGrainFactory grainFactory;
@@ -29,7 +30,7 @@ internal AdaptiveDirectoryCacheMaintainer(
2930
IInternalGrainFactory grainFactory,
3031
ILoggerFactory loggerFactory)
3132
{
32-
Log = loggerFactory.CreateLogger<AdaptiveDirectoryCacheMaintainer>();
33+
_log = loggerFactory.CreateLogger<AdaptiveDirectoryCacheMaintainer>();
3334
this.grainFactory = grainFactory;
3435
this.router = router;
3536
this.cache = cache;
@@ -38,8 +39,6 @@ internal AdaptiveDirectoryCacheMaintainer(
3839
lastNumHits = 0;
3940
}
4041

41-
private ILogger<AdaptiveDirectoryCacheMaintainer> Log { get; }
42-
4342
public void Start()
4443
{
4544
_runTask = Run();
@@ -135,7 +134,7 @@ private async Task Run()
135134
}
136135
}
137136

138-
LogTraceSelfOwnedAndRemoved(Log, router.MyAddress, ownedAndRemovedCount, keptCount, removedCount, refreshedCount);
137+
LogTraceSelfOwnedAndRemoved(_log, router.MyAddress, ownedAndRemovedCount, keptCount, removedCount, refreshedCount);
139138

140139
// Send batch requests
141140
SendBatchCacheRefreshRequests(fetchInBatchList);
@@ -144,7 +143,7 @@ private async Task Run()
144143
}
145144
catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
146145
{
147-
Log.LogError(ex, $"Error in {nameof(AdaptiveDirectoryCacheMaintainer)}.");
146+
LogErrorAdaptiveDirectoryCacheMaintainer(ex);
148147
}
149148
}
150149
}
@@ -167,15 +166,15 @@ private void SendBatchCacheRefreshRequests(Dictionary<SiloAddress, List<GrainId>
167166
ProcessCacheRefreshResponse(silo, response);
168167
}).Ignore();
169168

170-
LogTraceSendingRequest(Log, router.MyAddress, silo, cachedGrainAndETagList.Count);
169+
LogTraceSendingRequest(_log, router.MyAddress, silo, cachedGrainAndETagList.Count);
171170
}
172171
}
173172

174173
private void ProcessCacheRefreshResponse(
175174
SiloAddress silo,
176175
List<AddressAndTag> refreshResponse)
177176
{
178-
LogTraceReceivedProcessCacheRefreshResponse(Log, router.MyAddress, refreshResponse.Count);
177+
LogTraceReceivedProcessCacheRefreshResponse(_log, router.MyAddress, refreshResponse.Count);
179178

180179
int otherSiloCount = 0, updatedCount = 0, unchangedCount = 0;
181180

@@ -211,7 +210,7 @@ private void ProcessCacheRefreshResponse(
211210
}
212211
}
213212

214-
LogTraceProcessedRefreshResponse(Log, router.MyAddress, silo, otherSiloCount, updatedCount, unchangedCount);
213+
LogTraceProcessedRefreshResponse(_log, router.MyAddress, silo, otherSiloCount, updatedCount, unchangedCount);
215214
}
216215

217216
/// <summary>
@@ -237,10 +236,7 @@ private void ProcessCacheRefreshResponse(
237236
{
238237
// this may happen only if the LRU cache is full and decided to drop this grain
239238
// while we try to refresh it
240-
Log.LogWarning(
241-
(int)ErrorCode.Runtime_Error_100199,
242-
"Grain {GrainId} disappeared from the cache during maintenance",
243-
grain);
239+
LogWarningGrainDisappearedFromCache(grain);
244240
}
245241
}
246242

@@ -258,12 +254,25 @@ private void ProduceStats()
258254
long numAccesses = curNumAccesses - lastNumAccesses;
259255
long numHits = curNumHits - lastNumHits;
260256

261-
if (Log.IsEnabled(LogLevel.Trace)) Log.LogTrace("#accesses: {AccessCount}, hit-ratio: {HitRatio}%", numAccesses, (numHits / Math.Max(numAccesses, 0.00001)) * 100);
257+
if (_log.IsEnabled(LogLevel.Trace)) _log.LogTrace("#accesses: {AccessCount}, hit-ratio: {HitRatio}%", numAccesses, (numHits / Math.Max(numAccesses, 0.00001)) * 100);
262258

263259
lastNumAccesses = curNumAccesses;
264260
lastNumHits = curNumHits;
265261
}
266262

263+
[LoggerMessage(
264+
Level = LogLevel.Error,
265+
Message = $"Error in {nameof(AdaptiveDirectoryCacheMaintainer)}."
266+
)]
267+
private partial void LogErrorAdaptiveDirectoryCacheMaintainer(Exception ex);
268+
269+
[LoggerMessage(
270+
EventId = (int)ErrorCode.Runtime_Error_100199,
271+
Level = LogLevel.Warning,
272+
Message = "Grain {GrainId} disappeared from the cache during maintenance"
273+
)]
274+
private partial void LogWarningGrainDisappearedFromCache(GrainId grainId);
275+
267276
[LoggerMessage(
268277
Level = LogLevel.Trace,
269278
Message = "Silo {SiloAddress} self-owned (and removed) {OwnedAndRemovedCount}, kept {KeptCount}, removed {RemovedCount} and tried to refresh {RefreshedCount} grains"

0 commit comments

Comments
 (0)