Skip to content

Commit 52644dc

Browse files
committed
Query Cache: Invalidate the query cache when mappings change, closes elastic#532.
1 parent 9372c77 commit 52644dc

File tree

1 file changed

+19
-8
lines changed
  • modules/elasticsearch/src/main/java/org/elasticsearch/cache

1 file changed

+19
-8
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/cache/NodeCache.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,36 @@
2121

2222
import org.elasticsearch.cache.memory.ByteBufferCache;
2323
import org.elasticsearch.cache.query.parser.QueryParserCache;
24-
import org.elasticsearch.cache.query.parser.none.NoneQueryParserCache;
24+
import org.elasticsearch.cluster.ClusterChangedEvent;
25+
import org.elasticsearch.cluster.ClusterService;
26+
import org.elasticsearch.cluster.ClusterStateListener;
2527
import org.elasticsearch.common.component.AbstractComponent;
2628
import org.elasticsearch.common.inject.Inject;
27-
import org.elasticsearch.common.settings.ImmutableSettings;
2829
import org.elasticsearch.common.settings.Settings;
2930

3031
/**
3132
* @author kimchy (shay.banon)
3233
*/
33-
public class NodeCache extends AbstractComponent {
34+
public class NodeCache extends AbstractComponent implements ClusterStateListener {
35+
36+
private final ClusterService clusterService;
3437

3538
private final ByteBufferCache byteBufferCache;
3639

3740
private final QueryParserCache queryParserCache;
3841

39-
public NodeCache() {
40-
this(ImmutableSettings.Builder.EMPTY_SETTINGS, new ByteBufferCache(ImmutableSettings.Builder.EMPTY_SETTINGS), new NoneQueryParserCache());
41-
}
42-
43-
@Inject public NodeCache(Settings settings, ByteBufferCache byteBufferCache, QueryParserCache queryParserCache) {
42+
@Inject public NodeCache(Settings settings, ByteBufferCache byteBufferCache, QueryParserCache queryParserCache, ClusterService clusterService) {
4443
super(settings);
44+
this.clusterService = clusterService;
4545
this.byteBufferCache = byteBufferCache;
4646
this.queryParserCache = queryParserCache;
47+
clusterService.add(this);
4748
}
4849

4950
public void close() {
51+
clusterService.remove(this);
5052
byteBufferCache.close();
53+
queryParserCache.clear();
5154
}
5255

5356
public ByteBufferCache byteBuffer() {
@@ -57,4 +60,12 @@ public ByteBufferCache byteBuffer() {
5760
public QueryParserCache queryParser() {
5861
return queryParserCache;
5962
}
63+
64+
// listen on cluster change events to invalidate the query parser cache
65+
@Override public void clusterChanged(ClusterChangedEvent event) {
66+
// TODO we can do better by detecting just mappings changes
67+
if (event.metaDataChanged()) {
68+
queryParserCache.clear();
69+
}
70+
}
6071
}

0 commit comments

Comments
 (0)