Skip to content

Commit 4714a6a

Browse files
committed
Clear cache: allow to invalidate specific filter cache keys
closes elastic#2653
1 parent c12c456 commit 4714a6a

File tree

9 files changed

+130
-30
lines changed

9 files changed

+130
-30
lines changed

src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
3535
private boolean fieldDataCache = false;
3636
private boolean idCache = false;
3737
private String[] fields = null;
38+
private String[] filterKeys = null;
3839

3940
ClearIndicesCacheRequest() {
4041
}
@@ -72,6 +73,15 @@ public String[] fields() {
7273
return this.fields;
7374
}
7475

76+
public ClearIndicesCacheRequest filterKeys(String... filterKeys) {
77+
this.filterKeys = filterKeys;
78+
return this;
79+
}
80+
81+
public String[] filterKeys() {
82+
return this.filterKeys;
83+
}
84+
7585
public boolean idCache() {
7686
return this.idCache;
7787
}
@@ -86,27 +96,16 @@ public void readFrom(StreamInput in) throws IOException {
8696
filterCache = in.readBoolean();
8797
fieldDataCache = in.readBoolean();
8898
idCache = in.readBoolean();
89-
int size = in.readVInt();
90-
if (size > 0) {
91-
fields = new String[size];
92-
for (int i = 0; i < size; i++) {
93-
fields[i] = in.readString();
94-
}
95-
}
99+
fields = in.readStringArray();
100+
filterKeys = in.readStringArray();
96101
}
97102

98103
public void writeTo(StreamOutput out) throws IOException {
99104
super.writeTo(out);
100105
out.writeBoolean(filterCache);
101106
out.writeBoolean(fieldDataCache);
102107
out.writeBoolean(idCache);
103-
if (fields == null) {
104-
out.writeVInt(0);
105-
} else {
106-
out.writeVInt(fields.length);
107-
for (String field : fields) {
108-
out.writeString(field);
109-
}
110-
}
108+
out.writeStringArrayNullable(fields);
109+
out.writeStringArrayNullable(filterKeys);
111110
}
112111
}

src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequestBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public ClearIndicesCacheRequestBuilder setFields(String... fields) {
4848
return this;
4949
}
5050

51+
public ClearIndicesCacheRequestBuilder setFilterKeys(String... filterKeys) {
52+
request.filterKeys(filterKeys);
53+
return this;
54+
}
55+
5156
public ClearIndicesCacheRequestBuilder setIdCache(boolean idCache) {
5257
request.idCache(idCache);
5358
return this;

src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ShardClearIndicesCacheRequest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
3434
private boolean fieldDataCache = false;
3535
private boolean idCache = false;
3636
private String[] fields = null;
37+
private String[] filterKeys = null;
3738

3839
ShardClearIndicesCacheRequest() {
3940
}
@@ -44,6 +45,7 @@ public ShardClearIndicesCacheRequest(String index, int shardId, ClearIndicesCach
4445
fieldDataCache = request.fieldDataCache();
4546
idCache = request.idCache();
4647
fields = request.fields();
48+
filterKeys = request.filterKeys();
4749
}
4850

4951
public boolean filterCache() {
@@ -62,6 +64,10 @@ public String[] fields() {
6264
return this.fields;
6365
}
6466

67+
public String[] filterKeys() {
68+
return this.filterKeys;
69+
}
70+
6571
public ShardClearIndicesCacheRequest waitForOperations(boolean waitForOperations) {
6672
this.filterCache = waitForOperations;
6773
return this;
@@ -73,13 +79,8 @@ public void readFrom(StreamInput in) throws IOException {
7379
filterCache = in.readBoolean();
7480
fieldDataCache = in.readBoolean();
7581
idCache = in.readBoolean();
76-
int size = in.readVInt();
77-
if (size > 0) {
78-
fields = new String[size];
79-
for (int i = 0; i < size; i++) {
80-
fields[i] = in.readString();
81-
}
82-
}
82+
fields = in.readStringArray();
83+
filterKeys = in.readStringArray();
8384
}
8485

8586
@Override
@@ -88,13 +89,7 @@ public void writeTo(StreamOutput out) throws IOException {
8889
out.writeBoolean(filterCache);
8990
out.writeBoolean(fieldDataCache);
9091
out.writeBoolean(idCache);
91-
if (fields == null) {
92-
out.writeVInt(0);
93-
} else {
94-
out.writeVInt(fields.length);
95-
for (String field : fields) {
96-
out.writeString(field);
97-
}
98-
}
92+
out.writeStringArrayNullable(fields);
93+
out.writeStringArrayNullable(filterKeys);
9994
}
10095
}

src/main/java/org/elasticsearch/action/admin/indices/cache/clear/TransportClearIndicesCacheAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ protected ShardClearIndicesCacheResponse shardOperation(ShardClearIndicesCacheRe
124124
clearedAtLeastOne = true;
125125
service.cache().filter().clear("api");
126126
}
127+
if (request.filterKeys() != null && request.filterKeys().length > 0) {
128+
clearedAtLeastOne = true;
129+
service.cache().filter().clear("api", request.filterKeys());
130+
}
127131
if (request.fieldDataCache()) {
128132
clearedAtLeastOne = true;
129133
if (request.fields() == null || request.fields().length == 0) {

src/main/java/org/elasticsearch/index/cache/filter/FilterCache.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public EntriesStats(long sizeInBytes, long count) {
4747

4848
void clear(String reason);
4949

50+
void clear(String reason, String[] keys);
51+
5052
EntriesStats entriesStats();
5153

5254
long evictions();

src/main/java/org/elasticsearch/index/cache/filter/none/NoneFilterCache.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public void clear(String reason) {
5959
// nothing to do here
6060
}
6161

62+
@Override
63+
public void clear(String reason, String[] keys) {
64+
// nothing to do there
65+
}
66+
6267
@Override
6368
public void clear(IndexReader reader) {
6469
// nothing to do here

src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ public void clear(String reason) {
8888
}
8989
}
9090

91+
@Override
92+
public void clear(String reason, String[] keys) {
93+
logger.debug("clear keys [], reason [{}]", reason, keys);
94+
for (String key : keys) {
95+
for (Object readerKey : seenReaders.keySet()) {
96+
indicesFilterCache.cache().invalidate(new FilterCacheKey(this, readerKey, new CacheKeyFilter.Key(key)));
97+
}
98+
}
99+
}
100+
91101
@Override
92102
public void onClose(SegmentReader owner) {
93103
clear(owner);

src/main/java/org/elasticsearch/rest/action/admin/indices/cache/clear/RestClearIndicesCacheAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
6767
clearIndicesCacheRequest.fieldDataCache(request.paramAsBoolean("field_data", clearIndicesCacheRequest.fieldDataCache()));
6868
clearIndicesCacheRequest.idCache(request.paramAsBoolean("id", clearIndicesCacheRequest.idCache()));
6969
clearIndicesCacheRequest.fields(request.paramAsStringArray("fields", clearIndicesCacheRequest.fields()));
70+
clearIndicesCacheRequest.filterKeys(request.paramAsStringArray("filter_keys", clearIndicesCacheRequest.filterKeys()));
7071

7172
BroadcastOperationThreading operationThreading = BroadcastOperationThreading.fromString(request.param("operationThreading"), BroadcastOperationThreading.SINGLE_THREAD);
7273
if (operationThreading == BroadcastOperationThreading.NO_THREADS) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test.integration.indices.cache;
21+
22+
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
23+
import org.elasticsearch.action.search.SearchResponse;
24+
import org.elasticsearch.client.Client;
25+
import org.elasticsearch.common.settings.ImmutableSettings;
26+
import org.elasticsearch.index.query.FilterBuilders;
27+
import org.elasticsearch.test.integration.AbstractNodesTests;
28+
import org.testng.annotations.AfterClass;
29+
import org.testng.annotations.BeforeClass;
30+
import org.testng.annotations.Test;
31+
32+
import static org.elasticsearch.index.query.QueryBuilders.filteredQuery;
33+
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
34+
import static org.hamcrest.MatcherAssert.assertThat;
35+
import static org.hamcrest.Matchers.equalTo;
36+
import static org.hamcrest.Matchers.greaterThan;
37+
38+
/**
39+
*/
40+
@Test
41+
public class ClearCacheTests extends AbstractNodesTests {
42+
43+
private Client client;
44+
45+
@BeforeClass
46+
public void createNodes() throws Exception {
47+
startNode("node1", ImmutableSettings.settingsBuilder().put("index.cache.stats.refresh_interval", 0));
48+
client = getClient();
49+
}
50+
51+
@AfterClass
52+
public void closeNodes() {
53+
client.close();
54+
closeAllNodes();
55+
}
56+
57+
protected Client getClient() {
58+
return client("node1");
59+
}
60+
61+
@Test
62+
public void testClearCacheFilterKeys() {
63+
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
64+
client.prepareIndex("test", "type", "1").setSource("field", "value").execute().actionGet();
65+
client.admin().indices().prepareRefresh().execute().actionGet();
66+
67+
NodesStatsResponse nodesStats = client.admin().cluster().prepareNodesStats().setIndices(true).execute().actionGet();
68+
assertThat(nodesStats.nodes()[0].indices().getCache().filterSizeInBytes(), equalTo(0l));
69+
70+
SearchResponse searchResponse = client.prepareSearch().setQuery(filteredQuery(matchAllQuery(), FilterBuilders.termFilter("field", "value").cacheKey("test_key"))).execute().actionGet();
71+
assertThat(searchResponse.hits().getHits().length, equalTo(1));
72+
nodesStats = client.admin().cluster().prepareNodesStats().setIndices(true).execute().actionGet();
73+
assertThat(nodesStats.nodes()[0].indices().getCache().filterSizeInBytes(), greaterThan(0l));
74+
75+
client.admin().indices().prepareClearCache().setFilterKeys("test_key").execute().actionGet();
76+
nodesStats = client.admin().cluster().prepareNodesStats().setIndices(true).execute().actionGet();
77+
assertThat(nodesStats.nodes()[0].indices().getCache().filterSizeInBytes(), equalTo(0l));
78+
}
79+
}

0 commit comments

Comments
 (0)