From 3af763eb8830df15083b97c48a1e8f2c398f8366 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 20 Jul 2015 12:42:13 -0400 Subject: [PATCH] Use time with nanosecond resolution calculated at the executing node to measure the time that contexts are held open Closes #12345 --- .../elasticsearch/index/search/stats/ShardSearchStats.java | 2 +- .../org/elasticsearch/percolator/PercolateContext.java | 6 ++++++ .../search/internal/DefaultSearchContext.java | 6 ++++++ .../search/internal/FilteredSearchContext.java | 5 +++++ .../org/elasticsearch/search/internal/SearchContext.java | 2 ++ .../java/org/elasticsearch/test/TestSearchContext.java | 7 +++++++ 6 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java b/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java index 53b986b96b3..3ef5652db2e 100644 --- a/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java +++ b/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java @@ -175,7 +175,7 @@ public final class ShardSearchStats { public void onFreeScrollContext(SearchContext context) { totalStats.scrollCurrent.dec(); - totalStats.scrollMetric.inc(TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis() - context.nowInMillis())); + totalStats.scrollMetric.inc(System.nanoTime() - context.getOriginNanoTime()); } public void onRefreshSettings(Settings settings) { diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index 9368860a689..e7d5c3fcb07 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -98,6 +98,7 @@ public class PercolateContext extends SearchContext { private final ConcurrentMap percolateQueries; private final int numberOfShards; private final Query aliasFilter; + private final long originNanoTime = System.nanoTime(); private final long startTime; private String[] types; @@ -337,6 +338,11 @@ public class PercolateContext extends SearchContext { throw new UnsupportedOperationException(); } + @Override + public long getOriginNanoTime() { + return originNanoTime; + } + @Override protected long nowInMillisImpl() { return startTime; diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 6023ab3d9e5..d564a6c06c1 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -122,6 +122,7 @@ public class DefaultSearchContext extends SearchContext { private boolean queryRewritten; private volatile long keepAlive; private ScoreDoc lastEmittedDoc; + private final long originNanoTime = System.nanoTime(); private volatile long lastAccessTime = -1; private InnerHitsContext innerHitsContext; @@ -269,6 +270,11 @@ public class DefaultSearchContext extends SearchContext { return this; } + @Override + public long getOriginNanoTime() { + return originNanoTime; + } + @Override protected long nowInMillisImpl() { return request.nowInMillis(); diff --git a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index fa01f46412d..b4308c93215 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -139,6 +139,11 @@ public abstract class FilteredSearchContext extends SearchContext { return in.queryBoost(queryBoost); } + @Override + public long getOriginNanoTime() { + return in.getOriginNanoTime(); + } + @Override protected long nowInMillisImpl() { return in.nowInMillisImpl(); diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index 504a66c3017..b7030954930 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -142,6 +142,8 @@ public abstract class SearchContext implements Releasable, HasContextAndHeaders public abstract SearchContext queryBoost(float queryBoost); + public abstract long getOriginNanoTime(); + public final long nowInMillis() { nowInMillisUsed = true; return nowInMillisImpl(); diff --git a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java index f5719883d68..a186bf43edf 100644 --- a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -82,6 +82,8 @@ public class TestSearchContext extends SearchContext { private String[] types; private SearchContextAggregations aggregations; + private final long originNanoTime = System.nanoTime(); + public TestSearchContext(ThreadPool threadPool,PageCacheRecycler pageCacheRecycler, BigArrays bigArrays, IndexService indexService, QueryCache filterCache, IndexFieldDataService indexFieldDataService) { super(ParseFieldMatcher.STRICT); this.pageCacheRecycler = pageCacheRecycler; @@ -170,6 +172,11 @@ public class TestSearchContext extends SearchContext { return null; } + @Override + public long getOriginNanoTime() { + return originNanoTime; + } + @Override protected long nowInMillisImpl() { return 0;