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

[Issue #2446] Don't open new ResourceResolver to resolve clientlib categories #2528

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ky940819
Copy link
Contributor

Q                       A
Fixed Issues? Fixes #2446
Patch: Bug Fix? N
Minor: New Feature? Y
Major: Breaking Change? N
Tests Added + Pass? Yes
Documentation Provided Yes (code comments)
Any Dependency Changes? No
License Apache License, Version 2.0

This PR is a Proof of Concept on how to resolve issue #2446.
The solution outlined in issue #2446 is to pin an open service-user session to the user request so that the service user session does not need to be re-opened multiple times (which is costly). The problem with this solution is that the Sling API included in AEM 6.5.x does not currently support this.

The PR proposes an alternative solution.

This PR builds on the fact that all resource type -> client library category resolution is performed using a service user session.
Since the same service user is used to perform this resolution for every request, the results of this resolution will not differ for different user sessions - therefore the results are suitable to be cached between multiple requests.

To accomplish this, a new service (ClientLibraryLookupService) is added.
This service performs the client library category resolution, and then uses a simple HashMap as a cache.

Initial requests to a clean cache may still cause multiple service user session logins during a single user request; however, once the cache is populated, there are no service user session logins at all.

This has an advantage over pinning the service user session to the user request because (with the exception of the first few requests) there are 0 service user sessions created (instead of 1), and additionally the entire logic and repository access associated with the resource type -> client library category resolution is also eliminated and replaced with a simple hashmap lookup - reducing repository access in general.

To ensure that the cache is kept in sync with the repository, the cache is cleared every time a com/adobe/granite/ui/librarymanager/INVALIDATED event is issued - which occurs every time a client library is modified, deleted, or added.

When this event is issued, the entire cache is invalidated. This is easier than trying to figure out which cache items are no longer valid and selectively clearing just those entries. Changes to the client libraries on a running AEM instance should be infrequent enough that clearing the entire cache will have a negligible performance impact.

…ceType(String);

Updates `Utils#getSuperType` to use `resourceResolver.getParentResourceType(superType)`
instead of `resourceResolver.getResource(superType).getResourceSuperType`.

This change is made because any `ResourceResolver` can always resolve the resource
super type hierarchy via `ResourceResolver#getParentResourceType(String)`, even if
`ResrouceResolver#getResource(String)` would be unable to resolve the super type
resource due to permissions.

After this change, the only requirement for the `ResourceResolver` passed to
`Utils#getSuperTypes()` is that it be an active `ResourceResolver` - it does
not need to be a special `ResourceResolver` with any elevated permissions.

----
refs issue adobe#2446
Adds a new service `ClientLibrariesLookupService`.
This service provides a means by which to resolve ClientLibraries
by resource type.

The service will internally cache `resourceType` -> `ClientLibrary`
mappings such that future lookups for the same `resourceType` will
not require costly lookup or service-user sessions.

This service listens to the event topic: `com/adobe/granite/ui/librarymanager/INVALIDATED`.
When an event matching this topic occurs, then the cache items are cleared.

----
refs issue adobe#2446
Updates the `ClientLibrariesImpl` model by removing the clientlib lookup
logic and making use of the `ClientLibrariesLookupService` instead.

----
refs issue adobe#2446
@sonarcloud
Copy link

sonarcloud bot commented Jun 11, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@codecov
Copy link

codecov bot commented Jun 11, 2023

Codecov Report

Merging #2528 (eb9c2c5) into main (28efbf8) will decrease coverage by 0.02%.
The diff coverage is 91.17%.

❗ Current head eb9c2c5 differs from pull request most recent head 0eb7ccd. Consider uploading reports for the commit 0eb7ccd to get more accurate results

@@             Coverage Diff              @@
##               main    #2528      +/-   ##
============================================
- Coverage     87.25%   87.23%   -0.02%     
- Complexity     2609     2614       +5     
============================================
  Files           229      230       +1     
  Lines          6926     6950      +24     
  Branches       1049     1047       -2     
============================================
+ Hits           6043     6063      +20     
- Misses          354      357       +3     
- Partials        529      530       +1     
Impacted Files Coverage Δ
...lientLibraries/ClientLibraryLookupServiceImpl.java 90.90% <90.90%> (ø)
...onents/internal/models/v1/ClientLibrariesImpl.java 87.71% <91.66%> (-3.09%) ⬇️
...m/adobe/cq/wcm/core/components/internal/Utils.java 84.91% <100.00%> (-0.09%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClientLibrariesImpl should not always open a new ResourceResolver
1 participant