From e0f12ba54586fbc5620b4d0ecb1f02fac97b6b99 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Mon, 14 Nov 2011 21:35:51 -0500 Subject: [PATCH] Add counts of currently executing get operations --- .../org/elasticsearch/index/get/GetStats.java | 17 +++++++++++++- .../index/get/ShardGetService.java | 23 ++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/GetStats.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/GetStats.java index f792c121d51a6..49fdf90b54952 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/GetStats.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/GetStats.java @@ -37,15 +37,17 @@ public class GetStats implements Streamable, ToXContent { private long existsTimeInMillis; private long missingCount; private long missingTimeInMillis; + private long current; public GetStats() { } - public GetStats(long existsCount, long existsTimeInMillis, long missingCount, long missingTimeInMillis) { + public GetStats(long existsCount, long existsTimeInMillis, long missingCount, long missingTimeInMillis, long current) { this.existsCount = existsCount; this.existsTimeInMillis = existsTimeInMillis; this.missingCount = missingCount; this.missingTimeInMillis = missingTimeInMillis; + this.current = current; } public void add(GetStats stats) { @@ -56,6 +58,7 @@ public void add(GetStats stats) { existsTimeInMillis += stats.existsTimeInMillis; missingCount += stats.missingCount; missingTimeInMillis += stats.missingTimeInMillis; + current += stats.current; } public long count() { @@ -130,6 +133,14 @@ public TimeValue getMissingTime() { return missingTime(); } + public long current() { + return this.current; + } + + public long getCurrent() { + return this.current; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(Fields.GET); builder.field(Fields.TOTAL, count()); @@ -141,6 +152,7 @@ public TimeValue getMissingTime() { builder.field(Fields.MISSING_TOTAL, missingCount); builder.field(Fields.MISSING_TIME, missingTime().toString()); builder.field(Fields.MISSING_TIME_IN_MILLIS, missingTimeInMillis); + builder.field(Fields.CURRENT, current); builder.endObject(); return builder; } @@ -156,6 +168,7 @@ static final class Fields { static final XContentBuilderString MISSING_TOTAL = new XContentBuilderString("missing_total"); static final XContentBuilderString MISSING_TIME = new XContentBuilderString("missing_time"); static final XContentBuilderString MISSING_TIME_IN_MILLIS = new XContentBuilderString("missing_time_in_millis"); + static final XContentBuilderString CURRENT = new XContentBuilderString("current"); } public static GetStats readGetStats(StreamInput in) throws IOException { @@ -169,6 +182,7 @@ public static GetStats readGetStats(StreamInput in) throws IOException { existsTimeInMillis = in.readVLong(); missingCount = in.readVLong(); missingTimeInMillis = in.readVLong(); + current = in.readVLong(); } @Override public void writeTo(StreamOutput out) throws IOException { @@ -176,5 +190,6 @@ public static GetStats readGetStats(StreamInput in) throws IOException { out.writeVLong(existsTimeInMillis); out.writeVLong(missingCount); out.writeVLong(missingTimeInMillis); + out.writeVLong(current); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/ShardGetService.java index e457a27935868..a2bcc6ca0a700 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.document.ResetFieldSelector; import org.elasticsearch.common.lucene.uid.UidField; +import org.elasticsearch.common.metrics.CounterMetric; import org.elasticsearch.common.metrics.MeanMetric; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.cache.IndexCache; @@ -72,6 +73,7 @@ public class ShardGetService extends AbstractIndexShardComponent { private final MeanMetric existsMetric = new MeanMetric(); private final MeanMetric missingMetric = new MeanMetric(); + private final CounterMetric currentMetric = new CounterMetric(); @Inject public ShardGetService(ShardId shardId, @IndexSettings Settings indexSettings, ScriptService scriptService, MapperService mapperService, IndexCache indexCache) { @@ -82,7 +84,7 @@ public class ShardGetService extends AbstractIndexShardComponent { } public GetStats stats() { - return new GetStats(existsMetric.count(), TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()), missingMetric.count(), TimeUnit.NANOSECONDS.toMillis(missingMetric.sum())); + return new GetStats(existsMetric.count(), TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()), missingMetric.count(), TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()), currentMetric.count()); } // sadly, to overcome cyclic dep, we need to do this and inject it ourselves... @@ -92,14 +94,19 @@ public ShardGetService setIndexShard(IndexShard indexShard) { } public GetResult get(String type, String id, String[] gFields, boolean realtime) throws ElasticSearchException { - long now = System.nanoTime(); - GetResult getResult = innerGet(type, id, gFields, realtime); - if (getResult.exists()) { - existsMetric.inc(System.nanoTime() - now); - } else { - missingMetric.inc(System.nanoTime() - now); + currentMetric.inc(); + try { + long now = System.nanoTime(); + GetResult getResult = innerGet(type, id, gFields, realtime); + if (getResult.exists()) { + existsMetric.inc(System.nanoTime() - now); + } else { + missingMetric.inc(System.nanoTime() - now); + } + return getResult; + } finally { + currentMetric.dec(); } - return getResult; } public GetResult innerGet(String type, String id, String[] gFields, boolean realtime) throws ElasticSearchException {