Incorrect usage of JIRA/lucene search API

Gabriel Udvar
Contributor
October 11, 2020

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

8 votes
Answer accepted
Gabriel Udvar
Contributor
October 26, 2020

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)

Ricardo Martinez
Contributor
October 26, 2021

Thank you for this

0 votes
Joerg
Contributor
January 5, 2022

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
AUG Leaders

Atlassian Community Events