Skip to content

Commit

Permalink
properly cleanup searchcontexts after creation and after each test me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
msbt committed Feb 25, 2015
1 parent 6d7f65b commit a38c526
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,33 @@ public SearchContext apply(@Nullable IndexReader indexReader) {
new String[] { Constants.DEFAULT_MAPPING_TYPE },
System.currentTimeMillis()
);
SearchContext localContext = new DefaultSearchContext(
searchContextId,
searchRequest,
searchShardTarget,
EngineSearcher.getSearcherWithRetry(indexShard, null), // TODO: use same searcher/reader for same jobId and searchContextId
indexService,
indexShard,
scriptService,
cacheRecycler,
pageCacheRecycler,
bigArrays,
threadPool.estimatedTimeInMillisCounter()
);
LuceneQueryBuilder builder = new LuceneQueryBuilder(functions, localContext, indexService.cache());
LuceneQueryBuilder.Context ctx = builder.convert(whereClause);
localContext.parsedQuery(new ParsedQuery(ctx.query(), ImmutableMap.<String, Filter>of()));
Float minScore = ctx.minScore();
if (minScore != null) {
localContext.minimumScore(minScore);
SearchContext localContext = null;
try {
localContext = new DefaultSearchContext(
searchContextId,
searchRequest,
searchShardTarget,
EngineSearcher.getSearcherWithRetry(indexShard, null), // TODO: use same searcher/reader for same jobId and searchContextId
indexService,
indexShard,
scriptService,
cacheRecycler,
pageCacheRecycler,
bigArrays,
threadPool.estimatedTimeInMillisCounter()
);
LuceneQueryBuilder builder = new LuceneQueryBuilder(functions, localContext, indexService.cache());
LuceneQueryBuilder.Context ctx = builder.convert(whereClause);
localContext.parsedQuery(new ParsedQuery(ctx.query(), ImmutableMap.<String, Filter>of()));
Float minScore = ctx.minScore();
if (minScore != null) {
localContext.minimumScore(minScore);
}
} catch (Throwable t) {
if (localContext != null) {
localContext.close();
}
throw t;
}
return localContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.crate.action.sql.*;
import io.crate.action.sql.parser.SQLXContentSourceContext;
import io.crate.action.sql.parser.SQLXContentSourceParser;
import io.crate.executor.transport.CollectContextService;
import io.crate.test.integration.CrateIntegrationTest;
import io.crate.testing.SQLTransportExecutor;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
Expand All @@ -35,12 +36,14 @@
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +72,12 @@ public Settings indexSettings() {
return ImmutableSettings.builder().put("number_of_replicas", 0).build();
}

@After
public void cleanUp() {
// release all cached SearchContexts, so no open Searchers are leaked
Releasables.close(cluster().getInstances(CollectContextService.class));
}

/**
* Execute an SQL Statement on a random node of the cluster
*
Expand Down

0 comments on commit a38c526

Please sign in to comment.