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

[BUG] Filter path parsing implementation is inefficient #12102

Closed
jainankitk opened this issue Jan 31, 2024 · 3 comments
Closed

[BUG] Filter path parsing implementation is inefficient #12102

jainankitk opened this issue Jan 31, 2024 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers Search:Resiliency

Comments

@jainankitk
Copy link
Collaborator

jainankitk commented Jan 31, 2024

Describe the bug

The current implementation for filter path evaluation is really inefficient, recursively invoking method for every . pattern, when many of those can be skipped altogether. For example, it breaks for simple match all request with 4k . appended together (assuming small size JVM like 512k):

_search?filter_path=hits" + "." * 4000, json={"query": {"match_all" : {}}}
+ private static FilterPath parse(final String filter, final String segment) {
    int end = segment.length();

    for (int i = 0; i < end;) {
        char c = segment.charAt(i);

        if (c == '.') {
            String current = segment.substring(0, i).replaceAll("\\\\.", ".");
+            return new FilterPath(filter, current, parse(filter, segment.substring(i + 1)));
        }
        ++i;
        if ((c == '\\') && (i < end) && (segment.charAt(i) == '.')) {
            ++i;
        }
    }
    return new FilterPath(filter, segment.replaceAll("\\\\.", "."), EMPTY);
}

Extract from FilterPath.java

Solution

Can be written more efficiently by:

  • not using recursion, OR
  • using existing more efficient libraries for matching the pattern against string (cons: additional dependency)

Related component

Search:Resiliency

To Reproduce

opensearch-node1       | java.lang.StackOverflowError
opensearch-node1       |    at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:52)
opensearch-node1       |    at java.base/java.lang.IllegalArgumentException.<init>(IllegalArgumentException.java:40)
opensearch-node1       |    at java.base/java.util.regex.PatternSyntaxException.<init>(PatternSyntaxException.java:58)
opensearch-node1       |    at java.base/java.util.regex.Pattern.error(Pattern.java:2028)
opensearch-node1       |    at java.base/java.util.regex.Pattern.<init>(Pattern.java:1432)
opensearch-node1       |    at java.base/java.util.regex.Pattern.compile(Pattern.java:1069)
opensearch-node1       |    at java.base/java.lang.String.replaceAll(String.java:2944)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:119)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:120)
opensearch-node1       |    at org.opensearch.core.xcontent.filtering.FilterPath.parse(FilterPath.java:12

Expected behavior

There should not be any stack overflow error

Related component

Search:Resiliency

Expected behavior

There should not be any stack overflow error

@robinf95
Copy link
Contributor

Would take this issue.

@peternied
Copy link
Member

[Triage - attendees 1 2 3 4 5 6 7 8]
@jainankitk Thanks for filing this issue. @robinf95 Look forward to seeing a pull request.

@reta
Copy link
Collaborator

reta commented Jan 31, 2024

Closing this one as duplicate of #12067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers Search:Resiliency
Projects
Archived in project
Development

No branches or pull requests

4 participants