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

chore: Log fusion cache in memory entry #1527

Merged
merged 11 commits into from
Nov 25, 2024
Merged
Changes from 9 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
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.Authorization.ABAC.Xacml.JsonProfile;
Expand Down Expand Up @@ -90,9 +91,22 @@
var authorizedParties = await _partiesCache.GetOrSetAsync(cacheKey, async token
=> await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);

<<<<<<< HEAD

Check failure on line 94 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered

Check failure on line 94 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered
var mcaField = typeof(FusionCache).GetField("_mca", BindingFlags.NonPublic | BindingFlags.Instance);
var mcaValue = mcaField?.GetValue(_partiesCache);
var mcField = mcaValue!.GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);
var mcValue = mcField?.GetValue(mcaValue) as IMemoryCache;

var inMemoryCacheValue = mcValue!.TryGetValue(cacheKey, out var inMemoryCacheEntry);
var inMemoryCacheEntryValue = inMemoryCacheEntry?.GetType().GetProperty("Value")?.GetValue(inMemoryCacheEntry);

_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@inMemoryCacheEntryValue}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntryValue);
=======

Check failure on line 105 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered

Check failure on line 105 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered
var inMemoryCacheValue = _inMemoryCache.TryGetValue<AuthorizedPartiesResult>(cacheKey, out var inMemoryCacheEntry);
_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@InMemoryCacheEntry}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntry);
>>>>>>> main

Check failure on line 109 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered

Check failure on line 109 in src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

View workflow job for this annotation

GitHub Actions / build / build-and-test

Merge conflict marker encountered
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

⚠️ Resolve merge conflict and reconsider the reflection-based approach

The current implementation has several issues:

  1. There's an unresolved merge conflict between two different approaches to cache logging
  2. The HEAD version uses reflection to access private fields (_mca, _cache), which is problematic because:
    • It breaks encapsulation
    • It's fragile and may break with FusionCache updates
    • It has performance overhead
    • It makes the code harder to maintain

Consider these alternatives:

  1. Use the existing IMemoryCache approach from the main branch (simpler and safer)
  2. If additional cache diagnostics are needed, consider:
    • Using FusionCache's public API for diagnostics
    • Adding proper logging/metrics through FusionCache's events/callbacks
    • Contacting FusionCache maintainers to request a proper diagnostics API

Here's the recommended approach (using main's version):

-        var mcaField = typeof(FusionCache).GetField("_mca", BindingFlags.NonPublic | BindingFlags.Instance);
-        var mcaValue = mcaField?.GetValue(_partiesCache);
-        var mcField = mcaValue!.GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);
-        var mcValue = mcField?.GetValue(mcaValue) as IMemoryCache;
-
-        var inMemoryCacheValue = mcValue!.TryGetValue(cacheKey, out var inMemoryCacheEntry);
-        var inMemoryCacheEntryValue = inMemoryCacheEntry?.GetType().GetProperty("Value")?.GetValue(inMemoryCacheEntry);
-
-        _logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@inMemoryCacheEntryValue}",
-        cacheKey, inMemoryCacheValue, inMemoryCacheEntryValue);
+        var inMemoryCacheValue = _inMemoryCache.TryGetValue<AuthorizedPartiesResult>(cacheKey, out var inMemoryCacheEntry);
+        _logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@InMemoryCacheEntry}",
+            cacheKey, inMemoryCacheValue, inMemoryCacheEntry);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<<<<<<< HEAD
var mcaField = typeof(FusionCache).GetField("_mca", BindingFlags.NonPublic | BindingFlags.Instance);
var mcaValue = mcaField?.GetValue(_partiesCache);
var mcField = mcaValue!.GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);
var mcValue = mcField?.GetValue(mcaValue) as IMemoryCache;
var inMemoryCacheValue = mcValue!.TryGetValue(cacheKey, out var inMemoryCacheEntry);
var inMemoryCacheEntryValue = inMemoryCacheEntry?.GetType().GetProperty("Value")?.GetValue(inMemoryCacheEntry);
_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@inMemoryCacheEntryValue}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntryValue);
=======
var inMemoryCacheValue = _inMemoryCache.TryGetValue<AuthorizedPartiesResult>(cacheKey, out var inMemoryCacheEntry);
_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@InMemoryCacheEntry}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntry);
>>>>>>> main
var inMemoryCacheValue = _inMemoryCache.TryGetValue<AuthorizedPartiesResult>(cacheKey, out var inMemoryCacheEntry);
_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@InMemoryCacheEntry}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntry);
🧰 Tools
🪛 GitHub Check: build / build-and-test

[failure] 94-94:
Merge conflict marker encountered


[failure] 105-105:
Merge conflict marker encountered


[failure] 109-109:
Merge conflict marker encountered


[failure] 94-94:
Merge conflict marker encountered


[failure] 105-105:
Merge conflict marker encountered


[failure] 109-109:
Merge conflict marker encountered


// Temporary logging to debug missing authorized sub parties
_logger.LogInformation("Authorized parties for {Party}: {@AuthorizedParties}", authenticatedParty, authorizedParties);
Expand Down
Loading