Automatically close releasables after test (#24687)

This moves the releasing logic to the base test, so that individual test cases don't need
to worry about releasing the aggregators.  It's not a big deal for individual aggs,
but once tests start using sub-aggs, it can become tricky to free (without double-freeing)
all the aggregators.
This commit is contained in:
Zachary Tong 2017-05-16 09:01:38 -04:00 committed by GitHub
parent f6e19dcedc
commit 1e97184519
12 changed files with 152 additions and 152 deletions

View File

@ -78,17 +78,14 @@ public class GlobalAggregatorTests extends AggregatorTestCase {
aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number")); aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number"));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
try { GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection(); aggregator.postCollection();
InternalGlobal result = (InternalGlobal) aggregator.buildAggregation(0L); InternalGlobal result = (InternalGlobal) aggregator.buildAggregation(0L);
verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global")); verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global"));
} finally {
IOUtils.close(aggregator.subAggregators());
}
}
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -112,12 +112,13 @@ public class GeoHashGridAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
try (Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(query, aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(query, aggregator);
verify.accept((InternalGeoHashGrid) aggregator.buildAggregation(0L)); aggregator.postCollection();
} verify.accept((InternalGeoHashGrid) aggregator.buildAggregation(0L));
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -75,21 +75,22 @@ public class TermsAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType();
fieldType.setName("string"); fieldType.setName("string");
fieldType.setHasDocValues(true ); fieldType.setHasDocValues(true );
try (TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(new MatchAllDocsQuery(), aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
Terms result = (Terms) aggregator.buildAggregation(0L); aggregator.postCollection();
assertEquals(4, result.getBuckets().size()); Terms result = (Terms) aggregator.buildAggregation(0L);
assertEquals("a", result.getBuckets().get(0).getKeyAsString()); assertEquals(4, result.getBuckets().size());
assertEquals(2L, result.getBuckets().get(0).getDocCount()); assertEquals("a", result.getBuckets().get(0).getKeyAsString());
assertEquals("b", result.getBuckets().get(1).getKeyAsString()); assertEquals(2L, result.getBuckets().get(0).getDocCount());
assertEquals(2L, result.getBuckets().get(1).getDocCount()); assertEquals("b", result.getBuckets().get(1).getKeyAsString());
assertEquals("c", result.getBuckets().get(2).getKeyAsString()); assertEquals(2L, result.getBuckets().get(1).getDocCount());
assertEquals(1L, result.getBuckets().get(2).getDocCount()); assertEquals("c", result.getBuckets().get(2).getKeyAsString());
assertEquals("d", result.getBuckets().get(3).getKeyAsString()); assertEquals(1L, result.getBuckets().get(2).getDocCount());
assertEquals(1L, result.getBuckets().get(3).getDocCount()); assertEquals("d", result.getBuckets().get(3).getKeyAsString());
} assertEquals(1L, result.getBuckets().get(3).getDocCount());
} }
indexReader.close(); indexReader.close();
directory.close(); directory.close();
@ -191,12 +192,11 @@ public class TermsAggregatorTests extends AggregatorTestCase {
private InternalAggregation buildInternalAggregation(TermsAggregationBuilder builder, MappedFieldType fieldType, private InternalAggregation buildInternalAggregation(TermsAggregationBuilder builder, MappedFieldType fieldType,
IndexSearcher searcher) throws IOException { IndexSearcher searcher) throws IOException {
try (TermsAggregator aggregator = createAggregator(builder, searcher, fieldType)) { TermsAggregator aggregator = createAggregator(builder, searcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
searcher.search(new MatchAllDocsQuery(), aggregator); searcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection(); aggregator.postCollection();
return aggregator.buildAggregation(0L); return aggregator.buildAggregation(0L);
}
} }
} }

View File

@ -118,13 +118,13 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(
NumberFieldMapper.NumberType.LONG); NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (CardinalityAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, CardinalityAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher,
fieldType)) { fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(query, aggregator); indexSearcher.search(query, aggregator);
aggregator.postCollection(); aggregator.postCollection();
verify.accept((InternalCardinality) aggregator.buildAggregation(0L)); verify.accept((InternalCardinality) aggregator.buildAggregation(0L));
}
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -112,12 +112,13 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number"); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (MaxAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); MaxAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(query, aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(query, aggregator);
verify.accept((InternalMax) aggregator.buildAggregation(0L)); aggregator.postCollection();
} verify.accept((InternalMax) aggregator.buildAggregation(0L));
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -113,12 +113,13 @@ public class AvgAggregatorTests extends AggregatorTestCase {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(query, aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(query, aggregator);
verify.accept((InternalAvg) aggregator.buildAggregation(0L)); aggregator.postCollection();
} verify.accept((InternalAvg) aggregator.buildAggregation(0L));
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -62,13 +62,14 @@ public class MinAggregatorTests extends AggregatorTestCase {
MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number"); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(new MatchAllDocsQuery(), aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
InternalMin result = (InternalMin) aggregator.buildAggregation(0L); aggregator.postCollection();
assertEquals(-1.0, result.getValue(), 0); InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
} assertEquals(-1.0, result.getValue(), 0);
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }
@ -96,13 +97,14 @@ public class MinAggregatorTests extends AggregatorTestCase {
MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number"); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(new MatchAllDocsQuery(), aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
InternalMin result = (InternalMin) aggregator.buildAggregation(0L); aggregator.postCollection();
assertEquals(-1.0, result.getValue(), 0); InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
} assertEquals(-1.0, result.getValue(), 0);
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }
@ -127,13 +129,14 @@ public class MinAggregatorTests extends AggregatorTestCase {
MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number2"); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number2");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number2"); fieldType.setName("number2");
try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(new MatchAllDocsQuery(), aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
InternalMin result = (InternalMin) aggregator.buildAggregation(0L); aggregator.postCollection();
assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0); InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
} assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0);
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }
@ -149,13 +152,14 @@ public class MinAggregatorTests extends AggregatorTestCase {
MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number"); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection(); MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
indexSearcher.search(new MatchAllDocsQuery(), aggregator); aggregator.preCollection();
aggregator.postCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator);
InternalMin result = (InternalMin) aggregator.buildAggregation(0L); aggregator.postCollection();
assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0); InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
} assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0);
indexReader.close(); indexReader.close();
directory.close(); directory.close();
} }

View File

@ -127,12 +127,12 @@ public class HDRPercentilesAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (HDRPercentilesAggregator aggregator = createAggregator(builder, indexSearcher, fieldType)) { HDRPercentilesAggregator aggregator = createAggregator(builder, indexSearcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(query, aggregator); indexSearcher.search(query, aggregator);
aggregator.postCollection(); aggregator.postCollection();
verify.accept((InternalHDRPercentiles) aggregator.buildAggregation(0L)); verify.accept((InternalHDRPercentiles) aggregator.buildAggregation(0L));
}
} }
} }
} }

View File

@ -148,12 +148,11 @@ public class TDigestPercentilesAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
try (TDigestPercentilesAggregator aggregator = createAggregator(builder, indexSearcher, fieldType)) { TDigestPercentilesAggregator aggregator = createAggregator(builder, indexSearcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(query, aggregator); indexSearcher.search(query, aggregator);
aggregator.postCollection(); aggregator.postCollection();
verify.accept((InternalTDigestPercentiles) aggregator.buildAggregation(0L)); verify.accept((InternalTDigestPercentiles) aggregator.buildAggregation(0L));
}
} }
} }
} }

View File

@ -132,13 +132,12 @@ public class SumAggregatorTests extends AggregatorTestCase {
SumAggregationBuilder aggregationBuilder = new SumAggregationBuilder("_name"); SumAggregationBuilder aggregationBuilder = new SumAggregationBuilder("_name");
aggregationBuilder.field(FIELD_NAME); aggregationBuilder.field(FIELD_NAME);
try (SumAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) { SumAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(query, aggregator); indexSearcher.search(query, aggregator);
aggregator.postCollection(); aggregator.postCollection();
verify.accept((Sum) aggregator.buildAggregation(0L)); verify.accept((Sum) aggregator.buildAggregation(0L));
}
} }
} }
} }

View File

@ -120,13 +120,11 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name", valueType); ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name", valueType);
aggregationBuilder.field(FIELD_NAME); aggregationBuilder.field(FIELD_NAME);
try (ValueCountAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) { ValueCountAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection(); aggregator.preCollection();
indexSearcher.search(query, aggregator); indexSearcher.search(query, aggregator);
aggregator.postCollection(); aggregator.postCollection();
verify.accept((ValueCount) aggregator.buildAggregation(0L));
verify.accept((ValueCount) aggregator.buildAggregation(0L));
}
} }
} }
} }

View File

@ -63,6 +63,7 @@ import org.elasticsearch.search.internal.ContextIndexSearcher;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.After;
import org.mockito.Matchers; import org.mockito.Matchers;
import java.io.IOException; import java.io.IOException;
@ -203,17 +204,13 @@ public abstract class AggregatorTestCase extends ESTestCase {
AggregationBuilder builder, AggregationBuilder builder,
MappedFieldType... fieldTypes) throws IOException { MappedFieldType... fieldTypes) throws IOException {
C a = createAggregator(builder, searcher, fieldTypes); C a = createAggregator(builder, searcher, fieldTypes);
try { a.preCollection();
a.preCollection(); searcher.search(query, a);
searcher.search(query, a); a.postCollection();
a.postCollection(); @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") A internalAgg = (A) a.buildAggregation(0L);
A internalAgg = (A) a.buildAggregation(0L); return internalAgg;
return internalAgg;
} finally {
Releasables.close(releasables);
releasables.clear();
}
} }
/** /**
@ -245,38 +242,35 @@ public abstract class AggregatorTestCase extends ESTestCase {
Query rewritten = searcher.rewrite(query); Query rewritten = searcher.rewrite(query);
Weight weight = searcher.createWeight(rewritten, true, 1f); Weight weight = searcher.createWeight(rewritten, true, 1f);
C root = createAggregator(builder, searcher, fieldTypes); C root = createAggregator(builder, searcher, fieldTypes);
try {
for (ShardSearcher subSearcher : subSearchers) { for (ShardSearcher subSearcher : subSearchers) {
C a = createAggregator(builder, subSearcher, fieldTypes); C a = createAggregator(builder, subSearcher, fieldTypes);
a.preCollection(); a.preCollection();
subSearcher.search(weight, a); subSearcher.search(weight, a);
a.postCollection(); a.postCollection();
aggs.add(a.buildAggregation(0L)); aggs.add(a.buildAggregation(0L));
}
if (aggs.isEmpty()) {
return null;
} else {
if (randomBoolean() && aggs.size() > 1) {
// sometimes do an incremental reduce
int toReduceSize = aggs.size();
Collections.shuffle(aggs, random());
int r = randomIntBetween(1, toReduceSize);
List<InternalAggregation> toReduce = aggs.subList(0, r);
A reduced = (A) aggs.get(0).doReduce(toReduce,
new InternalAggregation.ReduceContext(root.context().bigArrays(), null, false));
aggs = new ArrayList<>(aggs.subList(r, toReduceSize));
aggs.add(reduced);
}
// now do the final reduce
@SuppressWarnings("unchecked")
A internalAgg = (A) aggs.get(0).doReduce(aggs, new InternalAggregation.ReduceContext(root.context().bigArrays(), null,
true));
return internalAgg;
}
} finally {
Releasables.close(releasables);
releasables.clear();
} }
if (aggs.isEmpty()) {
return null;
} else {
if (randomBoolean() && aggs.size() > 1) {
// sometimes do an incremental reduce
int toReduceSize = aggs.size();
Collections.shuffle(aggs, random());
int r = randomIntBetween(1, toReduceSize);
List<InternalAggregation> toReduce = aggs.subList(0, r);
A reduced = (A) aggs.get(0).doReduce(toReduce,
new InternalAggregation.ReduceContext(root.context().bigArrays(), null, false));
aggs = new ArrayList<>(aggs.subList(r, toReduceSize));
aggs.add(reduced);
}
// now do the final reduce
@SuppressWarnings("unchecked")
A internalAgg = (A) aggs.get(0).doReduce(aggs, new InternalAggregation.ReduceContext(root.context().bigArrays(), null,
true));
return internalAgg;
}
} }
private static class ShardSearcher extends IndexSearcher { private static class ShardSearcher extends IndexSearcher {
@ -300,4 +294,10 @@ public abstract class AggregatorTestCase extends ESTestCase {
protected static DirectoryReader wrap(DirectoryReader directoryReader) throws IOException { protected static DirectoryReader wrap(DirectoryReader directoryReader) throws IOException {
return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("_index", "_na_"), 0)); return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("_index", "_na_"), 0));
} }
@After
private void cleanupReleasables() {
Releasables.close(releasables);
releasables.clear();
}
} }