-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Extract references tracking from SearchLookup #68202
base: main
Are you sure you want to change the base?
Changes from 14 commits
553860a
7986ecf
0d7b6c3
f8f0d5a
0b9abb3
c10a8b3
5a34a41
4b9dc70
01d8945
17d4c3b
5289cd6
810ac89
680b7e2
1c2cd01
af997da
fce385e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.lookup; | ||
|
||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Used for detecting loops caused by fields referring to other fields. The chain is empty | ||
* for the "top level" lookup created for the entire search. When a lookup is used to load | ||
* fielddata for a field, we fork it and make sure the field name name isn't in the chain, | ||
* then add it to the end. So the lookup for the a field named {@code a} will be {@code ["a"]}. If | ||
* that field looks up the values of a field named {@code b} then | ||
* {@code b}'s chain will contain {@code ["a", "b"]}. | ||
*/ | ||
public final class TrackingMappedFieldsLookup { | ||
|
||
/** | ||
* The maximum depth of field dependencies. | ||
* When a runtime field's doc values depends on another runtime field's doc values, | ||
* which depends on another runtime field's doc values and so on, it can | ||
* make a very deep stack, which we want to limit. | ||
*/ | ||
public static final int MAX_FIELD_CHAIN_DEPTH = 5; | ||
|
||
final Function<String, MappedFieldType> lookup; | ||
final Set<String> references; | ||
|
||
public TrackingMappedFieldsLookup(Function<String, MappedFieldType> lookup) { | ||
this.lookup = lookup; | ||
this.references = Collections.emptySet(); | ||
} | ||
|
||
private TrackingMappedFieldsLookup(Function<String, MappedFieldType> lookup, Set<String> references, String field) { | ||
this.lookup = lookup; | ||
this.references = new LinkedHashSet<>(references); | ||
if (this.references.add(field) == false) { | ||
String message = String.join(" -> ", this.references) + " -> " + field; | ||
throw new IllegalArgumentException("Cyclic dependency detected while resolving runtime fields: " + message); | ||
} | ||
if (this.references.size() > MAX_FIELD_CHAIN_DEPTH) { | ||
throw new IllegalArgumentException("Field requires resolving too many dependent fields: " | ||
+ String.join(" -> ", this.references)); | ||
} | ||
} | ||
|
||
public TrackingMappedFieldsLookup excludingField(String field) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really exclude the field, right? It tracks it? |
||
return new TrackingMappedFieldsLookup(lookup, this.references, field); | ||
} | ||
|
||
public MappedFieldType get(String field) { | ||
return lookup.apply(field); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
package org.elasticsearch.xpack.runtimefields.mapper; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.memory.MemoryIndex; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.Strings; | ||
|
@@ -37,6 +39,7 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiFunction; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.Matchers.anyString; | ||
|
@@ -213,7 +216,7 @@ public void testExistsQueryIsExpensive() { | |
checkExpensiveQuery(MappedFieldType::existsQuery); | ||
} | ||
|
||
public void testExistsQueryInLoop() { | ||
public void testExistsQueryInLoop() throws IOException { | ||
checkLoop(MappedFieldType::existsQuery); | ||
} | ||
|
||
|
@@ -230,7 +233,7 @@ public void testRangeQueryIsExpensive() { | |
checkExpensiveQuery(this::randomRangeQuery); | ||
} | ||
|
||
public void testRangeQueryInLoop() { | ||
public void testRangeQueryInLoop() throws IOException { | ||
assumeTrue("Impl does not support range queries", supportsRangeQueries()); | ||
checkLoop(this::randomRangeQuery); | ||
} | ||
|
@@ -240,7 +243,7 @@ public void testTermQueryIsExpensive() { | |
checkExpensiveQuery(this::randomTermQuery); | ||
} | ||
|
||
public void testTermQueryInLoop() { | ||
public void testTermQueryInLoop() throws IOException { | ||
assumeTrue("Impl does not support term queries", supportsTermQueries()); | ||
checkLoop(this::randomTermQuery); | ||
} | ||
|
@@ -250,7 +253,7 @@ public void testTermsQueryIsExpensive() { | |
checkExpensiveQuery(this::randomTermsQuery); | ||
} | ||
|
||
public void testTermsQueryInLoop() { | ||
public void testTermsQueryInLoop() throws IOException { | ||
assumeTrue("Impl does not support term queries", supportsTermQueries()); | ||
checkLoop(this::randomTermsQuery); | ||
} | ||
|
@@ -301,8 +304,11 @@ protected final void checkExpensiveQuery(BiConsumer<MappedFieldType, SearchExecu | |
); | ||
} | ||
|
||
protected final void checkLoop(BiConsumer<MappedFieldType, SearchExecutionContext> queryBuilder) { | ||
Exception e = expectThrows(IllegalArgumentException.class, () -> queryBuilder.accept(loopFieldType(), mockContext())); | ||
protected final void checkLoop(BiFunction<MappedFieldType, SearchExecutionContext, Query> queryBuilder) throws IOException { | ||
Query query = queryBuilder.apply(loopFieldType(), mockContext(true, loopFieldType())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't really checking an actual production code path before. We now run a search, which forces the script to look up values triggering the loop handling. |
||
MemoryIndex mindex = new MemoryIndex(); | ||
IndexSearcher searcher = mindex.createSearcher(); | ||
Exception e = expectThrows(IllegalArgumentException.class, () -> searcher.search(query, 1)); | ||
assertThat(e.getMessage(), equalTo("Cyclic dependency detected while resolving runtime fields: test -> test")); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,7 +183,9 @@ public void testRangeQuery() throws IOException { | |
|
||
@Override | ||
protected Query randomRangeQuery(MappedFieldType ft, SearchExecutionContext ctx) { | ||
return ft.rangeQuery(randomLong(), randomLong(), randomBoolean(), randomBoolean(), null, null, null, ctx); | ||
long lower = randomLongBetween(0, 1000); | ||
long upper = lower + randomLongBetween(5, 5000); | ||
return ft.rangeQuery(lower, upper, randomBoolean(), randomBoolean(), null, null, null, ctx); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we're actually executing the random query now, we need to ensure its parameter constraints are met. |
||
|
||
@Override | ||
|
@@ -294,10 +296,11 @@ public void execute() { | |
} | ||
}; | ||
case "loop": | ||
return (fieldName, params, lookup) -> { | ||
// Indicate that this script wants the field call "test", which *is* the name of this field | ||
lookup.forkAndTrackFieldReferences("test"); | ||
throw new IllegalStateException("shoud have thrown on the line above"); | ||
return (fieldName, params, lookup) -> (ctx) -> new DoubleFieldScript(fieldName, params, lookup, ctx) { | ||
@Override | ||
public void execute() { | ||
leafSearchLookup.doc().get("test"); | ||
} | ||
}; | ||
default: | ||
throw new IllegalArgumentException("unsupported script [" + code + "]"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had hoped to be able to remove this method entirely, but we need it in runtime field queries if we want the root of the field cycle to be reported accurately. In other words, without this method we still get errors thrown on cycles, but instead of being "field -> a -> b -> field" the cycle is reported as "a -> b -> field -> a". I'm not sure if this is a real problem or not - it's a perfectly good cycle detection wherever it starts from. I lean towards removing it anyway, as I think the additional API surface isn't worth the different error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of like the error message being accurate. Could we drop the API and have the queries make
TrackingMappedFieldsLookup
and immediate call the method to add themselves to its list?