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

Bust the request cache when the mapping changes #66295

Merged
merged 62 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
528bfc6
Bust the request cache when the mapping changes
nik9000 Nov 20, 2020
8c808e7
Refactor
nik9000 Nov 24, 2020
3fc61e8
Refactor again
nik9000 Nov 24, 2020
1dedc0a
Javadoc
nik9000 Nov 24, 2020
fddfdb0
Moar javadoc
nik9000 Nov 24, 2020
b42f493
Merge branch 'master' into cache_bust
nik9000 Nov 24, 2020
58af6fe
Add example in core
nik9000 Nov 24, 2020
5f54e5b
Merge branch 'master' into cache_bust
nik9000 Nov 30, 2020
177e4d8
WIP
nik9000 Dec 1, 2020
e597164
WIP
nik9000 Dec 1, 2020
72887cd
Merge branch 'master' into cache_bust
nik9000 Dec 9, 2020
3587385
ooops:
nik9000 Dec 9, 2020
82b2a53
Compile plz
nik9000 Dec 9, 2020
d30da9a
Fix test
nik9000 Dec 9, 2020
bcd8e52
Checkstyle
nik9000 Dec 9, 2020
c3ace97
Merge branch 'master' into cache_bust
nik9000 Dec 9, 2020
81b9824
WIP
nik9000 Dec 14, 2020
a36ea7f
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 14, 2020
dde4996
Checkstyle
nik9000 Dec 14, 2020
7519d77
Implement sourceEnabled
nik9000 Dec 14, 2020
56cc647
Fixup test
nik9000 Dec 14, 2020
a075886
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 15, 2020
76a512c
Fix test
nik9000 Dec 15, 2020
5769100
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 15, 2020
d4fe787
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 15, 2020
c1cf01b
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 15, 2020
6273512
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 16, 2020
a359630
Fixup
nik9000 Dec 16, 2020
1335aab
Remove spacing change
nik9000 Dec 16, 2020
9900fcb
Javadoc
nik9000 Dec 16, 2020
501aea8
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 16, 2020
ed582f4
Switch cache around
nik9000 Dec 16, 2020
eed96df
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 16, 2020
2b6fbce
We use up to field 10!
nik9000 Dec 16, 2020
4621ff8
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 21, 2020
d9c19af
Fixup
nik9000 Dec 21, 2020
000ea68
Iter
nik9000 Dec 21, 2020
e151e5d
link to public
nik9000 Dec 21, 2020
de00a13
Revert cleanup of nested
nik9000 Dec 21, 2020
fafa838
Fix test
nik9000 Dec 21, 2020
4ecd781
Finish reverting nested change
nik9000 Dec 21, 2020
6ef65a7
Checkstyle, you loveable scamp
nik9000 Dec 21, 2020
4fc3ed9
Probably not neeeded
nik9000 Dec 21, 2020
07c74b3
Revert moar
nik9000 Dec 21, 2020
dcfdf44
Document not fixed issue
nik9000 Dec 21, 2020
6736482
words are hard
nik9000 Dec 22, 2020
65962e2
Switch to MockFieldMapper
nik9000 Dec 22, 2020
0f4adcc
Speeling
nik9000 Dec 22, 2020
068e67d
Add link in todo
nik9000 Dec 22, 2020
7b7cc27
Finish rename
nik9000 Dec 22, 2020
83f9f30
Special handling of runtime fields
nik9000 Dec 22, 2020
fc89ecb
Extra casting
nik9000 Dec 22, 2020
78d5b33
equals and hashcode for the key
nik9000 Dec 22, 2020
d479591
Speeling
nik9000 Dec 22, 2020
8657573
Start your paragraph with a capital letter
nik9000 Dec 22, 2020
50ca0ef
Not null
nik9000 Dec 22, 2020
551ce49
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 22, 2020
465e6a6
Try this
nik9000 Dec 22, 2020
caba149
Merge branch 'master' into cache_bust_take_two
nik9000 Dec 23, 2020
27eef8b
Update cache invalidation
nik9000 Dec 23, 2020
629f510
Speeling
nik9000 Dec 23, 2020
4f4efd6
Typo
nik9000 Dec 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/reference/indices/apis/reload-analyzers.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ stream's backing indices.
[source,console]
--------------------------------------------------
POST /my-index-000001/_reload_search_analyzers
POST /my-index-000001/_cache/clear?request=true
--------------------------------------------------
// TEST[setup:my_index]

IMPORTANT: After reloading the search analyzers you should clear the request
nik9000 marked this conversation as resolved.
Show resolved Hide resolved
cache to make sure it doesn't contain responses derived from the
previous versions of the analyzer.
// the need for this is tracked in https://github.com/elastic/elasticsearch/issues/66722



[discrete]
[[indices-reload-analyzers-api-request]]
=== {api-request-title}
Expand Down
12 changes: 6 additions & 6 deletions docs/reference/modules/indices/request_cache.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ Scripted queries that use the API calls which are non-deterministic, such as
The cache is smart -- it keeps the same _near real-time_ promise as uncached
search.

Cached results are invalidated automatically whenever the shard refreshes, but
only if the data in the shard has actually changed. In other words, you will
always get the same results from the cache as you would for an uncached search
request.
Cached results are invalidated automatically whenever the shard refreshes to
pick up changes to the documents or when you update the mapping. In other
words you will always get the same results from the cache as you would for an
uncached search request.

The longer the refresh interval, the longer that cached entries will remain
valid. If the cache is full, the least recently used cache keys will be
evicted.
valid even if there are changes to the documents. If the cache is full, the
least recently used cache keys will be evicted.

The cache can be expired manually with the <<indices-clearcache,`clear-cache` API>>:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,72 @@ setup:
the_filter:
filters:
filters: []


---
"cache":
- skip:
version: " - 7.99.99"
reason: cache fixed in 8.0.0 to be backported to 7.11.0

- do:
bulk:
refresh: true
body:
- index:
_index: test_1
_id: 100
- int_field: 1
double_field: 1.0
string_field: foo bar

- do:
search:
index: test_1
body:
size: 0
aggs:
f:
filters:
filters:
foo:
match:
string_field: foo
foo_bar:
match:
string_field: foo bar
- match: { hits.total.value: 5 }
- length: { aggregations.f.buckets: 2 }
- match: { aggregations.f.buckets.foo.doc_count: 4 }
- match: { aggregations.f.buckets.foo_bar.doc_count: 1 }

# Modify the mapping configuration that generates queries. This should bust the cache.
- do:
indices.put_mapping:
index: test_1
body:
properties:
string_field:
type: keyword
split_queries_on_whitespace: true

# This should be entirely fresh because updating the mapping busted the cache.
- do:
search:
index: test_1
body:
size: 0
aggs:
f:
filters:
filters:
foo:
match:
string_field: foo
foo_bar:
match:
string_field: foo bar
- match: { hits.total.value: 5 }
- length: { aggregations.f.buckets: 2 }
- match: { aggregations.f.buckets.foo.doc_count: 4 }
- match: { aggregations.f.buckets.foo_bar.doc_count: 4 }
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ public QueryShardContext newQueryShardContext(
indexCache.bitsetFilterCache(),
indexFieldData::getForField,
mapperService(),
javanna marked this conversation as resolved.
Show resolved Hide resolved
mapperService().mappingLookup(),
similarityService(),
scriptService,
xContentRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private DocumentMapper(IndexSettings indexSettings,
this.documentParser = documentParser;
this.indexSettings = indexSettings;
this.indexAnalyzers = indexAnalyzers;
this.fieldMappers = MappingLookup.fromMapping(this.mapping);
this.fieldMappers = MappingLookup.fromMapping(mapping, this::parse);

try {
mappingSource = new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.common.regex.Regex;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -43,11 +44,16 @@ final class FieldTypeLookup {
* For convenience, the set of copied fields includes the field itself.
*/
private final Map<String, Set<String>> fieldToCopiedFields = new HashMap<>();
private final String type;
private final DynamicKeyFieldTypeLookup dynamicKeyLookup;

FieldTypeLookup(Collection<FieldMapper> fieldMappers,
Collection<FieldAliasMapper> fieldAliasMappers,
Collection<RuntimeFieldType> runtimeFieldTypes) {
FieldTypeLookup(
javanna marked this conversation as resolved.
Show resolved Hide resolved
String type,
Collection<FieldMapper> fieldMappers,
Collection<FieldAliasMapper> fieldAliasMappers,
Collection<RuntimeFieldType> runtimeFieldTypes
) {
this.type = type;
Map<String, DynamicKeyFieldMapper> dynamicKeyMappers = new HashMap<>();

for (FieldMapper fieldMapper : fieldMappers) {
Expand Down Expand Up @@ -89,6 +95,10 @@ final class FieldTypeLookup {
* Returns the mapped field type for the given field name.
*/
MappedFieldType get(String field) {
if (field.equals(TypeFieldType.NAME)) {
return new TypeFieldType(type);
}

MappedFieldType fieldType = fullNameToFieldType.get(field);
if (fieldType != null) {
return fieldType;
Expand All @@ -103,6 +113,10 @@ MappedFieldType get(String field) {
* Returns a list of the full names of a simple match regex like pattern against full name and index name.
*/
Set<String> simpleMatchToFullName(String pattern) {
if (Regex.isSimpleMatchPattern(pattern) == false) {
// no wildcards
return Collections.singleton(pattern);
}
Set<String> fields = new HashSet<>();
for (String field : fullNameToFieldType.keySet()) {
if (Regex.simpleMatch(pattern, field)) {
Expand All @@ -125,6 +139,9 @@ Set<String> simpleMatchToFullName(String pattern) {
* @return A set of paths in the _source that contain the field's values.
*/
Set<String> sourcePaths(String field) {
if (fullNameToFieldType.isEmpty()) {
return Set.of();
}
String resolvedField = field;
int lastDotIndex = field.lastIndexOf('.');
if (lastDotIndex > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -132,7 +131,7 @@ public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers,
}

public boolean hasNested() {
jtibshirani marked this conversation as resolved.
Show resolved Hide resolved
return this.mapper != null && this.mapper.hasNestedObjects();
return mappingLookup().hasNested();
}

public IndexAnalyzers getIndexAnalyzers() {
Expand Down Expand Up @@ -399,30 +398,23 @@ public DocumentMapperForType documentMapperWithAutoCreate() {
* Given the full name of a field, returns its {@link MappedFieldType}.
*/
public MappedFieldType fieldType(String fullName) {
if (fullName.equals(TypeFieldType.NAME)) {
return new TypeFieldType(this.mapper == null ? "_doc" : this.mapper.type());
}
return this.mapper == null ? null : this.mapper.mappers().fieldTypes().get(fullName);
jtibshirani marked this conversation as resolved.
Show resolved Hide resolved
return mappingLookup().fieldTypes().get(fullName);
}

/**
* Returns all the fields that match the given pattern. If the pattern is prefixed with a type
* then the fields will be returned with a type prefix.
*/
public Set<String> simpleMatchToFullName(String pattern) {
if (Regex.isSimpleMatchPattern(pattern) == false) {
// no wildcards
return Collections.singleton(pattern);
}
return this.mapper == null ? Collections.emptySet() : this.mapper.mappers().fieldTypes().simpleMatchToFullName(pattern);
return mappingLookup().simpleMatchToFullName(pattern);
}

/**
* Given a field name, returns its possible paths in the _source. For example,
* the 'source path' for a multi-field is the path to its parent field.
* {@code volatile} read a (mostly) immutable snapshot current mapping.
*/
public Set<String> sourcePath(String fullName) {
return this.mapper == null ? Collections.emptySet() : this.mapper.mappers().fieldTypes().sourcePaths(fullName);
public MappingLookup mappingLookup() {
DocumentMapper mapper = this.mapper;
return mapper == null ? MappingLookup.EMPTY : mapper.mappers();
}

/**
Expand All @@ -444,18 +436,7 @@ public ObjectMapper getObjectMapper(String name) {
* directly associated index-time analyzer
*/
public NamedAnalyzer indexAnalyzer(String field, Function<String, NamedAnalyzer> unindexedFieldAnalyzer) {
jtibshirani marked this conversation as resolved.
Show resolved Hide resolved
if (this.mapper == null) {
return unindexedFieldAnalyzer.apply(field);
}
return this.mapper.mappers().indexAnalyzer(field, unindexedFieldAnalyzer);
}

public boolean containsBrokenAnalysis(String field) {
NamedAnalyzer a = indexAnalyzer(field, f -> null);
if (a == null) {
return false;
}
return a.containsBrokenAnalysis();
return mappingLookup().indexAnalyzer(field, unindexedFieldAnalyzer);
}

@Override
Expand Down Expand Up @@ -504,6 +485,7 @@ public synchronized List<String> reloadSearchAnalyzers(AnalysisRegistry registry
reloadedAnalyzers.add(analyzerName);
}
}
// TODO this should bust the cache somehow. Tracked in https://github.com/elastic/elasticsearch/issues/66722
return reloadedAnalyzers;
}
}
Loading