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] Handle canMatchSearchAfter for frozen context scenario #11248

Closed
gashutos opened this issue Nov 17, 2023 · 0 comments · Fixed by #11249
Closed

[BUG] Handle canMatchSearchAfter for frozen context scenario #11248

gashutos opened this issue Nov 17, 2023 · 0 comments · Fixed by #11249
Labels
bug Something isn't working v2.12.0 Issues and PRs related to version 2.12.0 v3.0.0 Issues and PRs related to version 3.0.0

Comments

@gashutos
Copy link
Contributor

gashutos commented Nov 17, 2023

Description

There is a corner case where sort is failing on nested field after this PR #7453. When nested field is date and have filter in nest field has now() function call.
OpenSearch prevents SortFields' initialization in such cases more then one time.
This is very corner scenario but I added integ tests for that as we did have any integ test.
As a mitigation, I am pssing already initialized SortAndFormats in ContextIndexSearcher in this PR. In fact I also made change to invoke that if clause only if SearchContext contains searchAfter,

How to repro the issue

PUT test_sort_nested_date2
{
  "settings" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 0
  },
  "mappings": {
    "properties": {
      "nested_field": {
        "type": "nested",
        "properties": {
          "date_field": {
            "type": "date",
            "format": "date_optional_time"
          }
        }
      }
    }
  }
}
POST test_sort_nested_date2/_bulk
{"index": {"_id": "1"}}
{"nested_field": [{"date_field": "2023-10-26T12:00:00+09:00"}, {"date_field": "2025-10-01T12:00:00+09:00"}]}
GET test_sort_nested_date2/_search
{
  "sort": [
    {
      "nested_field.date_field": {
      "mode": "max",
        "order": "desc",
        "nested": {
          "path": "nested_field",
          "filter": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "nested_field.date_field": {
                      "gte": "now/h",
                      "time_zone": "+09:00"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  ]
}

This search query failes with below Exception,

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "could not read the current timestamp"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test_sort_nested_date2",
        "node": "WT1VH4gyTVGVZyi_tNMO-Q",
        "reason": {
          "type": "parse_exception",
          "reason": "could not read the current timestamp",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "features that prevent cachability are disabled on this context"
          }
        }
      }
    ]
  },
  "status": 400
}

Server side traces are corelated with canMatchSearchAfter where we are trying to initialize SortAndFormats again to get MinMax values.

[2023-10-21T12:21:59,877][DEBUG][o.o.a.s.TransportSearchAction] [bc908df47a785e9192f108cf1282c4e4] All shards failed for phase: [query]
OpenSearchParseException[could not read the current timestamp]; nested: IllegalArgumentException[features that prevent cachability are disabled on this context];
    at org.opensearch.common.time.JavaDateMathParser.parse(JavaDateMathParser.java:83)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.parseToLong(DateFieldMapper.java:510)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.lambda$dateRangeQuery$1(DateFieldMapper.java:464)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.handleNow(DateFieldMapper.java:493)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.dateRangeQuery(DateFieldMapper.java:459)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.rangeQuery(DateFieldMapper.java:434)
    at org.opensearch.index.query.RangeQueryBuilder.doToQuery(RangeQueryBuilder.java:527)
    at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
    at org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)
    at org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:329)
    at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
    at org.opensearch.search.sort.SortBuilder.resolveNestedQuery(SortBuilder.java:245)
    at org.opensearch.search.sort.SortBuilder.resolveNested(SortBuilder.java:203)
    at org.opensearch.search.sort.FieldSortBuilder.nested(FieldSortBuilder.java:588)
    at org.opensearch.search.sort.FieldSortBuilder.build(FieldSortBuilder.java:411)
    at org.opensearch.search.sort.SortBuilder.buildSort(SortBuilder.java:166)
    at org.opensearch.search.sort.FieldSortBuilder.getMinMaxOrNullInternal(FieldSortBuilder.java:631)
    at org.opensearch.search.sort.FieldSortBuilder.getMinMaxOrNullForSegment(FieldSortBuilder.java:626)
    at org.opensearch.search.internal.ContextIndexSearcher.canMatchSearchAfter(ContextIndexSearcher.java:499)
    at org.opensearch.search.internal.ContextIndexSearcher.canMatch(ContextIndexSearcher.java:491)
    at org.opensearch.search.internal.ContextIndexSearcher.searchLeaf(ContextIndexSearcher.java:309)
    at org.opensearch.search.internal.ContextIndexSearcher.search(ContextIndexSearcher.java:295)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:551)
    at org.opensearch.search.query.QueryPhase.searchWithCollector(QueryPhase.java:341)
    at org.opensearch.search.query.QueryPhase$DefaultQueryPhaseSearcher.searchWithCollector(QueryPhase.java:419)
    at org.opensearch.search.query.QueryPhase$DefaultQueryPhaseSearcher.searchWith(QueryPhase.java:408)
    at org.opensearch.search.query.QueryPhase.executeInternal(QueryPhase.java:263)
    at org.opensearch.search.query.QueryPhase.execute(QueryPhase.java:150)
    at org.opensearch.search.SearchService.loadOrExecuteQueryPhase(SearchService.java:527)
    at org.opensearch.search.SearchService.executeQueryPhase(SearchService.java:591)
    at org.opensearch.search.SearchService$2.lambda$onResponse$0(SearchService.java:560)
    at org.opensearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:73)
    at org.opensearch.action.ActionRunnable$2.doRun(ActionRunnable.java:88)
    at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
    at org.opensearch.threadpool.TaskAwareRunnable.doRun(TaskAwareRunnable.java:78)
    at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
    at org.opensearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:59)
    at org.opensearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:815)
    at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalArgumentException: features that prevent cachability are disabled on this context
    at org.opensearch.index.query.QueryShardContext.failIfFrozen(QueryShardContext.java:521)
    at org.opensearch.index.query.QueryShardContext.nowInMillis(QueryShardContext.java:555)
    at org.opensearch.index.mapper.DateFieldMapper$DateFieldType.lambda$handleNow$2(DateFieldMapper.java:491)
    at org.opensearch.common.time.JavaDateMathParser.parse(JavaDateMathParser.java:81)
    ... 41 more

Host/Environment (please complete the following information):

  • OS: [All]
  • Version [>=2.8]

Additional context
Add any other context about the problem here.

@gashutos gashutos added bug Something isn't working untriaged labels Nov 17, 2023
@reta reta added v3.0.0 Issues and PRs related to version 3.0.0 v2.12.0 Issues and PRs related to version 2.12.0 and removed untriaged labels Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v2.12.0 Issues and PRs related to version 2.12.0 v3.0.0 Issues and PRs related to version 3.0.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants