Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,359,878
Community Members
 
Community Events
168
Community Groups

Incorrect usage of JIRA/lucene search API

Hi Everybody!

In our Jira 8.3.2. we have a lot of calculated fields that, based on an issue field, perform a JQL on the rest of the projects and gather a list of issues. From that list of issues we then get some custom field values and calculate them in order to get a total value (for ex. total estimated effort for a project). The scripts are written and running using Scriptrunner v6.6.0.

Lately, we've noticed some errors in the logs when the fields are calculated:

2020-10-10 10:13:47,174 g2gcrm-69914:Cron Service ERROR anonymous [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: XXXX-2127, field: Custom Field21 java.lang.IllegalStateException: Incorrect usage of JIRA/lucene search API. You can only create/use: ManagedIndexSearcher inside a context (request or Jira-Thread-Local). Check: JiraThreadLocalUtils for details.

The errors don’t appear on one of our staging environments but are present on the live instance which I assume is because the live environment is much busier and there are a lot of queries in the Lucene index.
I did some research and on the JiraThreadLocalUtil class, but unfortunately there is not much documentation to rely on.
The bit of code that I assume generates this error looks like:

com.atlassian.jira.issue.search.SearchResults searchResults = null;
try {
    searchResults = searchService.search(user, query, pagerFilter);
} catch (SearchException e) {
    e.printStackTrace();
    return (Double) 0 ;
}

I saw one idea on a post about using “ThreadLocalUtil” in the loop, transforming to:

jiraThreadLocalUtil.preCall()
try {
try {
searchResults = searchService.search(user, query, pagerFilter);
} catch (SearchException e) {
e.printStackTrace();
return (Double) 0 ;
}
} finally
{
jiraThreadLocalUtil.postCall(log, null)
}

However, this approach seems to break other things in Jira. I noticed some strange behavior when running filters that include the scripted fields: page formatting is off, results are not showing, "Activity" stream section is not showing, etc. 

Has anybody noticed this behavior and have any ideas on how to overcome this?

 

2 answers

1 accepted

2 votes
Answer accepted

I'm going to post the answer here, as with the help of Adaptavist support I resolved the issue by refactoring the code to:

try {

     ThreadLocalSearcherCache.startSearcherContext()

     searchResults = searchService.search(user, query, pagerFilter);

} catch (SearchException e) {

    e.printStackTrace();

    return (Double) total;

} finally {

    ThreadLocalSearcherCache.stopAndCloseSearcherContext()
}

by using methods from a different class:

import com.atlassian.jira.issue.index.ThreadLocalSearcherCache

although the documentation for this class states:

Low level lucene searcher context. You almost never want to use this. Most of the time you want: JiraThreadLocalUtils.wrap(Runnable) or JiraThreadLocalUtils.wrap(java.util.concurrent.Callable)

Thank you for this

Hey.

I'm still curious as to why this happens. I have encountered the same behavior.

Shouldn't each instance of a scripted field, that exists in an issue, have a context i.e. project + issuetype?

Or is there something I'm not understanding correctly.

Would really appreciate it if somebody could shed some light on this whole thing, like what is the problem or why there is no context, when there should be one.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Apps for Confluence you won't want to miss: RSVP for September's Appy Hours

Calling all collaborators and Confluence users! Our Appy Hours event on September 29th features 4 presenters demoing functionality to superpower Confluence. Don't miss learning about these apps i...

112 views 0 9
Read article

Atlassian Community Events