Shared session usage in AEM 6.3 (oh no, not again!)

etugarev

01-03-2018

For our client we are developing a specific health check which analyses a page output traversing the tree of child pages.

I am aware that a shared session usage is a well known anti-pattern (https://cqdump.wordpress.com/2015/03/02/aem-scaling-patterns-avoid-shared-sessions/ and https://github.com/joerghoh/cqdump/blob/master/examples/bundle/src/main/java/de/joerghoh/cq5/example...) in AEM and keeping this in mind I created this code:

Assuming that I have a list of nodes to check:

for (Node node : nodes) {

  Future f = completionService.submit(testResultFunction(node.getPath()));
  futures.add(f);
}

...

private Callable<String> testResultFunction(final String nodePath) {

   return () -> Templates.serviceResourceResolverFunction(Constants.ServiceUsers.READ_SERVICE,
   resourceResolverFactory, resourceResolver -> {

     String result = requestService.doGet(checkedPageUrl, resourceResolver).getContent();
     ...

     return result;
   });
}

Templates.serviceResourceResolverFunction() looks like this:

public static <R> R serviceResourceResolverFunction(String serviceName, @Nonnull ResourceResolverFactory resourceResolverFactory,
   @Nonnull Function<ResourceResolver, R> function) {

  Map<String, Object> map = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, serviceName);
  ResourceResolver resolver = null;
 

  try {

   resolver = resourceResolverFactory.getServiceResourceResolver(map);
     return function.apply(resolver);
  } catch (LoginException e) {

    LOG.error("Access denied", e);
    return null;
  } finally {

    if (resolver != null && resolver.isLive()) {

     resolver.close();
    }

  }

}

I am receiving an output which forces me to scratch my head over and over again:

01.03.2018 15:51:00.880 *WARN* [ForkJoinPool-2-worker-1] org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate Attempted to perform hasProperty while thread ForkJoinPool-2-worker-2 was concurrently writing to this session. Blocked until the other thread finished using this session. Please review your code to avoid concurrent use of a session. ...and so on...

In debugger i see that I receive  a new resourceResolver per every request made, and I am not writing anything to the session (?!), so I can only wonder what's the problem here.

Any help will be much appreciated!

Edited 09.03.2018 by Evgeny Tugarev

Replies

Highlighted
Highlighted

Jörg_Hoh

Employee

11-04-2018

Can you extract that code into a custom bundle, so I can try that on my own system? In the meanwhile I will look at the logs your provided.

Jörg

Highlighted

smacdonald2008

11-04-2018

I have never attempted to loop through many nodes and opening a session for each node. Therefore i am not sure if these log messages are normal. I recommend following Joerg's suggestion of placing a bundle on Google drive so community can test.

Highlighted

etugarev

11-04-2018

Thank you both for a quick feedback. I will start preparing a bundle and will update the thread as soon as I am done (in a next few days).

Highlighted

etugarev

23-04-2018

Indeed, this would be interesting to know.

UPD: I created a sample project and put it in git: GitHub - etugarev/acme-hc: Sling health check which produces blocking threads

Running project should be pretty straightforward, otherwise, if you experience any problems, please let me know.

Regards,

Evgeny

Highlighted

Jörg_Hoh

Employee

24-04-2018

Hi Evgeny,

I can reproduce it here locally. Thanks for the work you put into this!

My best assumption is [SLING-7542] Avoid shared session/resource resolver usage - ASF JIRA  , which is not yet released. But you can try to build GitHub - apache/sling-org-apache-sling-i18n: Apache Sling I18N Support  and deploy it locally on your instance (my gut feeling says that it this version should work on AEM 6.3.2.0 as well). Can you try that?

regards,

Jörg

Highlighted

etugarev

24-04-2018

Hi Jörg,

I am happy the demo project worked out for you   I will try the approach you suggested tomorrow and update the thread with results.

Thanks again for a prompt feedback!

UPD: I grabbed apache-sling-i18n version 2.5.15-SNAPSHOT from sling repository and installed compiled bundle via console.

But it looks that the problem still there..

Regards,

Evgeny

Highlighted

etugarev

08-05-2018

2 Forum Admins: Please do not mark answer as correct or "assumed answered" before having a confirmation from the post author (me)

Highlighted

Jörg_Hoh

Employee

08-05-2018

Thanks for the feedback. In that case please raise a Daycare ticket for help and attach your sample code. Being able to reproduce it with a test will definitly help to speedup the problem resolution.

Thanks,

Jörg