Allow FieldStatsRequest to disable cache

This commit is contained in:
Nik Everett 2016-06-15 14:15:14 -04:00
parent 2fc328e10a
commit 8cc848f31c
10 changed files with 40 additions and 9 deletions

View File

@ -141,7 +141,7 @@ public class SnapshotIndexShardStatus extends BroadcastShardResponse implements
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Integer.toString(getShardId())); builder.startObject(Integer.toString(getShardId().getId()));
builder.field(Fields.STAGE, getStage()); builder.field(Fields.STAGE, getStage());
stats.toXContent(builder, params); stats.toXContent(builder, params);
if (getNodeId() != null) { if (getNodeId() != null) {

View File

@ -49,7 +49,7 @@ public class SnapshotIndexStatus implements Iterable<SnapshotIndexShardStatus>,
Map<Integer, SnapshotIndexShardStatus> indexShards = new HashMap<>(); Map<Integer, SnapshotIndexShardStatus> indexShards = new HashMap<>();
stats = new SnapshotStats(); stats = new SnapshotStats();
for (SnapshotIndexShardStatus shard : shards) { for (SnapshotIndexShardStatus shard : shards) {
indexShards.put(shard.getShardId(), shard); indexShards.put(shard.getShardId().getId(), shard);
stats.add(shard.getStats()); stats.add(shard.getStats());
} }
shardsStats = new SnapshotShardsStats(shards); shardsStats = new SnapshotShardsStats(shards);

View File

@ -44,6 +44,7 @@ public class FieldStatsRequest extends BroadcastRequest<FieldStatsRequest> {
private String[] fields = Strings.EMPTY_ARRAY; private String[] fields = Strings.EMPTY_ARRAY;
private String level = DEFAULT_LEVEL; private String level = DEFAULT_LEVEL;
private IndexConstraint[] indexConstraints = new IndexConstraint[0]; private IndexConstraint[] indexConstraints = new IndexConstraint[0];
private boolean useCache = true;
public String[] getFields() { public String[] getFields() {
return fields; return fields;
@ -56,6 +57,14 @@ public class FieldStatsRequest extends BroadcastRequest<FieldStatsRequest> {
this.fields = fields; this.fields = fields;
} }
public void setUseCache(boolean useCache) {
this.useCache = useCache;
}
public boolean shouldUseCache() {
return useCache;
}
public IndexConstraint[] getIndexConstraints() { public IndexConstraint[] getIndexConstraints() {
return indexConstraints; return indexConstraints;
} }
@ -184,6 +193,7 @@ public class FieldStatsRequest extends BroadcastRequest<FieldStatsRequest> {
indexConstraints[i] = new IndexConstraint(in); indexConstraints[i] = new IndexConstraint(in);
} }
level = in.readString(); level = in.readString();
useCache = in.readBoolean();
} }
@Override @Override
@ -201,6 +211,7 @@ public class FieldStatsRequest extends BroadcastRequest<FieldStatsRequest> {
} }
} }
out.writeString(level); out.writeString(level);
out.writeBoolean(useCache);
} }
} }

View File

@ -45,4 +45,9 @@ public class FieldStatsRequestBuilder extends
request().level(level); request().level(level);
return this; return this;
} }
public FieldStatsRequestBuilder setUseCache(boolean useCache) {
request().setUseCache(useCache);
return this;
}
} }

View File

@ -34,6 +34,7 @@ import java.util.Set;
public class FieldStatsShardRequest extends BroadcastShardRequest { public class FieldStatsShardRequest extends BroadcastShardRequest {
private String[] fields; private String[] fields;
private boolean useCache;
public FieldStatsShardRequest() { public FieldStatsShardRequest() {
} }
@ -46,22 +47,29 @@ public class FieldStatsShardRequest extends BroadcastShardRequest {
fields.add(indexConstraint.getField()); fields.add(indexConstraint.getField());
} }
this.fields = fields.toArray(new String[fields.size()]); this.fields = fields.toArray(new String[fields.size()]);
useCache = request.shouldUseCache();
} }
public String[] getFields() { public String[] getFields() {
return fields; return fields;
} }
public boolean shouldUseCache() {
return useCache;
}
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
fields = in.readStringArray(); fields = in.readStringArray();
useCache = in.readBoolean();
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeStringArrayNullable(fields); out.writeStringArrayNullable(fields);
out.writeBoolean(useCache);
} }
} }

View File

@ -46,7 +46,6 @@ public class FieldStatsShardResponse extends BroadcastShardResponse {
return fieldStats; return fieldStats;
} }
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);

View File

@ -187,16 +187,15 @@ public class TransportFieldStatsAction extends
ShardId shardId = request.shardId(); ShardId shardId = request.shardId();
Map<String, FieldStats<?>> fieldStats = new HashMap<>(); Map<String, FieldStats<?>> fieldStats = new HashMap<>();
IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex()); IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex());
MapperService mapperService = indexServices.mapperService();
IndexShard shard = indexServices.getShard(shardId.id()); IndexShard shard = indexServices.getShard(shardId.id());
try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) { try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
// Resolve patterns and deduplicate // Resolve patterns and deduplicate
Set<String> fieldNames = new HashSet<>(); Set<String> fieldNames = new HashSet<>();
for (String field : request.getFields()) { for (String field : request.getFields()) {
fieldNames.addAll(mapperService.simpleMatchToIndexNames(field)); fieldNames.addAll(shard.mapperService().simpleMatchToIndexNames(field));
} }
for (String field : fieldNames) { for (String field : fieldNames) {
FieldStats<?> stats = indicesService.getFieldStats(shard, searcher, field); FieldStats<?> stats = indicesService.getFieldStats(shard, searcher, field, request.shouldUseCache());
if (stats != null) { if (stats != null) {
fieldStats.put(field, stats); fieldStats.put(field, stats);
} }

View File

@ -45,8 +45,8 @@ public abstract class BroadcastShardResponse extends TransportResponse {
return this.shardId.getIndexName(); return this.shardId.getIndexName();
} }
public int getShardId() { public ShardId getShardId() {
return this.shardId.id(); return this.shardId;
} }
@Override @Override

View File

@ -1121,12 +1121,16 @@ public class IndicesService extends AbstractLifecycleComponent<IndicesService>
* @param shard the shard to use with the cache key * @param shard the shard to use with the cache key
* @param searcher searcher to use to lookup the field stats * @param searcher searcher to use to lookup the field stats
* @param field the actual field * @param field the actual field
* @param useCache should this request use the cache?
*/ */
public FieldStats<?> getFieldStats(IndexShard shard, Engine.Searcher searcher, String field) throws Exception { public FieldStats<?> getFieldStats(IndexShard shard, Engine.Searcher searcher, String field, boolean useCache) throws Exception {
MappedFieldType fieldType = shard.mapperService().fullName(field); MappedFieldType fieldType = shard.mapperService().fullName(field);
if (fieldType == null) { if (fieldType == null) {
return null; return null;
} }
if (useCache == false) {
return fieldType.stats(searcher.reader());
}
BytesReference cacheKey = new BytesArray("fieldstats:" + field); BytesReference cacheKey = new BytesArray("fieldstats:" + field);
BytesReference statsRef = cacheShardLevelResult(shard, searcher.getDirectoryReader(), cacheKey, out -> { BytesReference statsRef = cacheShardLevelResult(shard, searcher.getDirectoryReader(), cacheKey, out -> {
out.writeOptionalWriteable(fieldType.stats(searcher.reader())); out.writeOptionalWriteable(fieldType.stats(searcher.reader()));

View File

@ -487,6 +487,11 @@ public class FieldStatsIntegrationIT extends ESIntegTestCase {
assertEquals(200, fieldStats.getAllFieldStats().get("value").getDocCount()); assertEquals(200, fieldStats.getAllFieldStats().get("value").getDocCount());
// Because we refreshed the index we don't have any more hits in the cache. This is read from the index. // Because we refreshed the index we don't have any more hits in the cache. This is read from the index.
assertEquals(oldHitCount, indexStats.getHitCount()); assertEquals(oldHitCount, indexStats.getHitCount());
// We can also turn off the cache entirely
fieldStats = client().prepareFieldStats().setFields("value").get();
assertEquals(200, fieldStats.getAllFieldStats().get("value").getDocCount());
assertEquals(oldHitCount, indexStats.getHitCount());
} }
private void indexRange(String index, long from, long to) throws Exception { private void indexRange(String index, long from, long to) throws Exception {