From f09ad507a44367ed6ca29c6f3dae7659a2da1994 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sat, 27 Apr 2013 15:09:38 +0200 Subject: [PATCH] open context stats - rename to open_contexts from open, we might have other open stats in the future related to search (lucene index searchers?) - add a test to verify it works --- .../index/search/stats/SearchStats.java | 4 +-- .../elasticsearch/search/SearchService.java | 3 +- .../search/stats/SearchStatsTests.java | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java b/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java index 1691f0e2e22..77a3cb4bf48 100644 --- a/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java +++ b/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java @@ -203,7 +203,7 @@ public class SearchStats implements Streamable, ToXContent { @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(Fields.SEARCH); - builder.field(Fields.OPEN, openContexts); + builder.field(Fields.OPEN_CONTEXTS, openContexts); totalStats.toXContent(builder, params); if (groupStats != null && !groupStats.isEmpty()) { builder.startObject(Fields.GROUPS); @@ -220,7 +220,7 @@ public class SearchStats implements Streamable, ToXContent { static final class Fields { static final XContentBuilderString SEARCH = new XContentBuilderString("search"); - static final XContentBuilderString OPEN = new XContentBuilderString("open"); + static final XContentBuilderString OPEN_CONTEXTS = new XContentBuilderString("open_contexts"); static final XContentBuilderString GROUPS = new XContentBuilderString("groups"); static final XContentBuilderString QUERY_TOTAL = new XContentBuilderString("query_total"); static final XContentBuilderString QUERY_TIME = new XContentBuilderString("query_time"); diff --git a/src/main/java/org/elasticsearch/search/SearchService.java b/src/main/java/org/elasticsearch/search/SearchService.java index bdc6e68d090..5c4354a79bb 100644 --- a/src/main/java/org/elasticsearch/search/SearchService.java +++ b/src/main/java/org/elasticsearch/search/SearchService.java @@ -516,7 +516,8 @@ public class SearchService extends AbstractLifecycleComponent { if (context == null) { return; } - freeContext(context); + context.indexShard().searchService().onFreeContext(context); + context.release(); } private void freeContext(SearchContext context) { diff --git a/src/test/java/org/elasticsearch/test/integration/search/stats/SearchStatsTests.java b/src/test/java/org/elasticsearch/test/integration/search/stats/SearchStatsTests.java index 9c0e96dead2..ba7fdc041bc 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/stats/SearchStatsTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/stats/SearchStatsTests.java @@ -21,8 +21,11 @@ package org.elasticsearch.test.integration.search.stats; import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.integration.AbstractNodesTests; import org.testng.annotations.AfterClass; @@ -30,6 +33,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -95,4 +99,30 @@ public class SearchStatsTests extends AbstractNodesTests { assertThat(nodeStats.getNodes()[0].getIndices().getSearch().getTotal().getQueryCount(), greaterThan(0l)); assertThat(nodeStats.getNodes()[0].getIndices().getSearch().getTotal().getQueryTimeInMillis(), greaterThan(0l)); } + + @Test + public void testOpenContexts() { + client.admin().indices().prepareDelete().execute().actionGet(); + for (int i = 0; i < 50; i++) { + client.prepareIndex("test1", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet(); + } + IndicesStatsResponse indicesStats = client.admin().indices().prepareStats().execute().actionGet(); + assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0l)); + + SearchResponse searchResponse = client.prepareSearch() + .setSearchType(SearchType.SCAN) + .setQuery(matchAllQuery()) + .setSize(5) + .setScroll(TimeValue.timeValueMinutes(2)) + .execute().actionGet(); + + indicesStats = client.admin().indices().prepareStats().execute().actionGet(); + assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(3l)); // 3 shards + + // scroll, but with no timeout (so no context) + searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).execute().actionGet(); + + indicesStats = client.admin().indices().prepareStats().execute().actionGet(); + assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0l)); + } }