Skip to content

Commit

Permalink
Add counts of currently executing get operations
Browse files Browse the repository at this point in the history
  • Loading branch information
imotov authored and kimchy committed Nov 15, 2011
1 parent 72413e7 commit e0f12ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -56,6 +58,7 @@ public void add(GetStats stats) {
existsTimeInMillis += stats.existsTimeInMillis;
missingCount += stats.missingCount;
missingTimeInMillis += stats.missingTimeInMillis;
current += stats.current;
}

public long count() {
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -169,12 +182,14 @@ 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 {
out.writeVLong(existsCount);
out.writeVLong(existsTimeInMillis);
out.writeVLong(missingCount);
out.writeVLong(missingTimeInMillis);
out.writeVLong(current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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...
Expand All @@ -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 {
Expand Down

0 comments on commit e0f12ba

Please sign in to comment.