fix tests

This commit is contained in:
nishantmonu51 2014-04-08 02:37:57 +05:30
parent db35009acd
commit 4bb36dd453
2 changed files with 131 additions and 108 deletions

View File

@ -64,11 +64,13 @@ import java.util.Map;
public class TopNQueryQueryToolChest extends QueryToolChest<Result<TopNResultValue>, TopNQuery> public class TopNQueryQueryToolChest extends QueryToolChest<Result<TopNResultValue>, TopNQuery>
{ {
private static final byte TOPN_QUERY = 0x1; private static final byte TOPN_QUERY = 0x1;
private static final Joiner COMMA_JOIN = Joiner.on(","); private static final Joiner COMMA_JOIN = Joiner.on(",");
private static final TypeReference<Result<TopNResultValue>> TYPE_REFERENCE = new TypeReference<Result<TopNResultValue>>(){}; private static final TypeReference<Result<TopNResultValue>> TYPE_REFERENCE = new TypeReference<Result<TopNResultValue>>()
{
private static final TypeReference<Object> OBJECT_TYPE_REFERENCE = new TypeReference<Object>(){}; };
private static final TypeReference<Object> OBJECT_TYPE_REFERENCE = new TypeReference<Object>()
{
};
private final TopNQueryConfig config; private final TopNQueryConfig config;
@Inject @Inject
@ -163,7 +165,7 @@ public class TopNQueryQueryToolChest extends QueryToolChest<Result<TopNResultVal
for (PostAggregator postAgg : query.getPostAggregatorSpecs()) { for (PostAggregator postAgg : query.getPostAggregatorSpecs()) {
Object calculatedPostAgg = input.getMetric(postAgg.getName()); Object calculatedPostAgg = input.getMetric(postAgg.getName());
if (calculatedPostAgg != null) { if (calculatedPostAgg != null) {
values.put(postAgg.getName(), input.getMetric(postAgg.getName())); values.put(postAgg.getName(), calculatedPostAgg);
} else { } else {
values.put(postAgg.getName(), postAgg.compute(values)); values.put(postAgg.getName(), postAgg.compute(values));
} }
@ -314,6 +316,11 @@ public class TopNQueryQueryToolChest extends QueryToolChest<Result<TopNResultVal
return new ThresholdAdjustingQueryRunner(runner, config.getMinTopNThreshold()); return new ThresholdAdjustingQueryRunner(runner, config.getMinTopNThreshold());
} }
public Ordering<Result<TopNResultValue>> getOrdering()
{
return Ordering.natural();
}
private static class ThresholdAdjustingQueryRunner implements QueryRunner<Result<TopNResultValue>> private static class ThresholdAdjustingQueryRunner implements QueryRunner<Result<TopNResultValue>>
{ {
private final QueryRunner<Result<TopNResultValue>> runner; private final QueryRunner<Result<TopNResultValue>> runner;
@ -398,9 +405,4 @@ public class TopNQueryQueryToolChest extends QueryToolChest<Result<TopNResultVal
); );
} }
} }
public Ordering<Result<TopNResultValue>> getOrdering()
{
return Ordering.natural();
}
} }

View File

@ -47,6 +47,7 @@ import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.BySegmentResultValueClass; import io.druid.query.BySegmentResultValueClass;
import io.druid.query.DataSource; import io.druid.query.DataSource;
import io.druid.query.Druids; import io.druid.query.Druids;
import io.druid.query.FinalizeResultsQueryRunner;
import io.druid.query.MapQueryToolChestWarehouse; import io.druid.query.MapQueryToolChestWarehouse;
import io.druid.query.Query; import io.druid.query.Query;
import io.druid.query.QueryConfig; import io.druid.query.QueryConfig;
@ -115,17 +116,21 @@ import java.util.concurrent.Executor;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class CachingClusteredClientTest public class CachingClusteredClientTest
{ {
public static final ImmutableMap<String, Object> CONTEXT = ImmutableMap.of();
public static final MultipleIntervalSegmentSpec SEG_SPEC = new MultipleIntervalSegmentSpec(ImmutableList.<Interval>of());
public static final String DATA_SOURCE = "test";
protected static final DefaultObjectMapper jsonMapper = new DefaultObjectMapper(new SmileFactory());
static {
jsonMapper.getFactory().setCodec(jsonMapper);
}
/** /**
* We want a deterministic test, but we'd also like a bit of randomness for the distribution of segments * We want a deterministic test, but we'd also like a bit of randomness for the distribution of segments
* across servers. Thus, we loop multiple times and each time use a deterministically created Random instance. * across servers. Thus, we loop multiple times and each time use a deterministically created Random instance.
* Increase this value to increase exposure to random situations at the expense of test run time. * Increase this value to increase exposure to random situations at the expense of test run time.
*/ */
private static final int RANDOMNESS = 10; private static final int RANDOMNESS = 10;
public static final ImmutableMap<String, Object> CONTEXT = ImmutableMap.of();
public static final MultipleIntervalSegmentSpec SEG_SPEC = new MultipleIntervalSegmentSpec(ImmutableList.<Interval>of());
public static final String DATA_SOURCE = "test";
private static final List<AggregatorFactory> AGGS = Arrays.asList( private static final List<AggregatorFactory> AGGS = Arrays.asList(
new CountAggregatorFactory("rows"), new CountAggregatorFactory("rows"),
new LongSumAggregatorFactory("imps", "imps"), new LongSumAggregatorFactory("imps", "imps"),
@ -152,6 +157,17 @@ public class CachingClusteredClientTest
private static final DateTimeZone TIMEZONE = DateTimeZone.forID("America/Los_Angeles"); private static final DateTimeZone TIMEZONE = DateTimeZone.forID("America/Los_Angeles");
private static final QueryGranularity PT1H_TZ_GRANULARITY = new PeriodGranularity(new Period("PT1H"), null, TIMEZONE); private static final QueryGranularity PT1H_TZ_GRANULARITY = new PeriodGranularity(new Period("PT1H"), null, TIMEZONE);
private static final String TOP_DIM = "a_dim"; private static final String TOP_DIM = "a_dim";
private final Random random;
protected VersionedIntervalTimeline<String, ServerSelector> timeline;
protected TimelineServerView serverView;
protected Cache cache;
public CachingClusteredClient client;
DruidServer[] servers;
public CachingClusteredClientTest(int randomSeed)
{
this.random = new Random(randomSeed);
}
@Parameterized.Parameters @Parameterized.Parameters
public static Collection<?> constructorFeeder() throws IOException public static Collection<?> constructorFeeder() throws IOException
@ -169,28 +185,6 @@ public class CachingClusteredClientTest
); );
} }
protected static final DefaultObjectMapper jsonMapper = new DefaultObjectMapper(new SmileFactory());
static {
jsonMapper.getFactory().setCodec(jsonMapper);
}
private final Random random;
protected VersionedIntervalTimeline<String, ServerSelector> timeline;
protected TimelineServerView serverView;
protected Cache cache;
CachingClusteredClient client;
DruidServer[] servers;
public CachingClusteredClientTest(int randomSeed)
{
this.random = new Random(randomSeed);
}
@Before @Before
public void setUp() throws Exception public void setUp() throws Exception
{ {
@ -223,6 +217,7 @@ public class CachingClusteredClientTest
.context(CONTEXT); .context(CONTEXT);
testQueryCaching( testQueryCaching(
client,
builder.build(), builder.build(),
new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000), new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000),
new Interval("2011-01-02/2011-01-03"), makeTimeResults(new DateTime("2011-01-02"), 30, 6000), new Interval("2011-01-02/2011-01-03"), makeTimeResults(new DateTime("2011-01-02"), 30, 6000),
@ -286,6 +281,7 @@ public class CachingClusteredClientTest
.context(CONTEXT); .context(CONTEXT);
testQueryCaching( testQueryCaching(
client,
builder.build(), builder.build(),
new Interval("2011-11-04/2011-11-08"), new Interval("2011-11-04/2011-11-08"),
makeTimeResults( makeTimeResults(
@ -324,10 +320,15 @@ public class CachingClusteredClientTest
.postAggregators(POST_AGGS); .postAggregators(POST_AGGS);
testQueryCaching( testQueryCaching(
client,
1, 1,
true, true,
builder.context(ImmutableMap.<String, Object>of("useCache", "false", builder.context(
"populateCache", "true")).build(), ImmutableMap.<String, Object>of(
"useCache", "false",
"populateCache", "true"
)
).build(),
new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000) new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000)
); );
@ -338,10 +339,15 @@ public class CachingClusteredClientTest
cache.close("0_0"); cache.close("0_0");
testQueryCaching( testQueryCaching(
client,
1, 1,
false, false,
builder.context(ImmutableMap.<String, Object>of("useCache", "false", builder.context(
"populateCache", "false")).build(), ImmutableMap.<String, Object>of(
"useCache", "false",
"populateCache", "false"
)
).build(),
new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000) new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000)
); );
@ -350,10 +356,15 @@ public class CachingClusteredClientTest
Assert.assertEquals(0, cache.getStats().getNumMisses()); Assert.assertEquals(0, cache.getStats().getNumMisses());
testQueryCaching( testQueryCaching(
client,
1, 1,
false, false,
builder.context(ImmutableMap.<String, Object>of("useCache", "true", builder.context(
"populateCache", "false")).build(), ImmutableMap.<String, Object>of(
"useCache", "true",
"populateCache", "false"
)
).build(),
new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000) new Interval("2011-01-01/2011-01-02"), makeTimeResults(new DateTime("2011-01-01"), 50, 5000)
); );
@ -378,7 +389,10 @@ public class CachingClusteredClientTest
.postAggregators(POST_AGGS) .postAggregators(POST_AGGS)
.context(CONTEXT); .context(CONTEXT);
QueryRunner runner = new FinalizeResultsQueryRunner(client, new TopNQueryQueryToolChest(new TopNQueryConfig()));
testQueryCaching( testQueryCaching(
runner,
builder.build(), builder.build(),
new Interval("2011-01-01/2011-01-02"), new Interval("2011-01-01/2011-01-02"),
makeTopNResults(new DateTime("2011-01-01"), "a", 50, 5000, "b", 50, 4999, "c", 50, 4998), makeTopNResults(new DateTime("2011-01-01"), "a", 50, 5000, "b", 50, 4999, "c", 50, 4998),
@ -420,7 +434,7 @@ public class CachingClusteredClientTest
new DateTime("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983, new DateTime("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983,
new DateTime("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983 new DateTime("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983
), ),
client.run( runner.run(
builder.intervals("2011-01-01/2011-01-10") builder.intervals("2011-01-01/2011-01-10")
.metric("imps") .metric("imps")
.aggregators(RENAMED_AGGS) .aggregators(RENAMED_AGGS)
@ -446,7 +460,10 @@ public class CachingClusteredClientTest
.postAggregators(POST_AGGS) .postAggregators(POST_AGGS)
.context(CONTEXT); .context(CONTEXT);
QueryRunner runner = new FinalizeResultsQueryRunner(client, new TopNQueryQueryToolChest(new TopNQueryConfig()));
testQueryCaching( testQueryCaching(
runner,
builder.build(), builder.build(),
new Interval("2011-11-04/2011-11-08"), new Interval("2011-11-04/2011-11-08"),
makeTopNResults( makeTopNResults(
@ -465,7 +482,7 @@ public class CachingClusteredClientTest
new DateTime("2011-11-06", TIMEZONE), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, new DateTime("2011-11-06", TIMEZONE), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989,
new DateTime("2011-11-07", TIMEZONE), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986 new DateTime("2011-11-07", TIMEZONE), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986
), ),
client.run( runner.run(
builder.intervals("2011-11-04/2011-11-08") builder.intervals("2011-11-04/2011-11-08")
.metric("imps") .metric("imps")
.aggregators(RENAMED_AGGS) .aggregators(RENAMED_AGGS)
@ -491,7 +508,9 @@ public class CachingClusteredClientTest
.postAggregators(POST_AGGS) .postAggregators(POST_AGGS)
.context(CONTEXT); .context(CONTEXT);
QueryRunner runner = new FinalizeResultsQueryRunner(client, new TopNQueryQueryToolChest(new TopNQueryConfig()));
testQueryCaching( testQueryCaching(
runner,
builder.build(), builder.build(),
new Interval("2011-01-01/2011-01-02"), new Interval("2011-01-01/2011-01-02"),
makeTopNResults(), makeTopNResults(),
@ -518,6 +537,7 @@ public class CachingClusteredClientTest
) )
); );
TestHelper.assertExpectedResults( TestHelper.assertExpectedResults(
makeRenamedTopNResults( makeRenamedTopNResults(
new DateTime("2011-01-05"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992, new DateTime("2011-01-05"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992,
@ -531,7 +551,7 @@ public class CachingClusteredClientTest
new DateTime("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983, new DateTime("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983,
new DateTime("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983 new DateTime("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983
), ),
client.run( runner.run(
builder.intervals("2011-01-01/2011-01-10") builder.intervals("2011-01-01/2011-01-10")
.metric("imps") .metric("imps")
.aggregators(RENAMED_AGGS) .aggregators(RENAMED_AGGS)
@ -545,6 +565,7 @@ public class CachingClusteredClientTest
public void testSearchCaching() throws Exception public void testSearchCaching() throws Exception
{ {
testQueryCaching( testQueryCaching(
client,
new SearchQuery( new SearchQuery(
new TableDataSource(DATA_SOURCE), new TableDataSource(DATA_SOURCE),
DIM_FILTER, DIM_FILTER,
@ -582,13 +603,14 @@ public class CachingClusteredClientTest
); );
} }
public void testQueryCaching(final Query query, Object... args) public void testQueryCaching(QueryRunner runner, final Query query, Object... args)
{ {
testQueryCaching(3, true, query, args); testQueryCaching(runner, 3, true, query, args);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testQueryCaching( public void testQueryCaching(
final QueryRunner runner,
final int numTimesToQuery, final int numTimesToQuery,
boolean expectBySegment, boolean expectBySegment,
final Query query, Object... args // does this assume query intervals must be ordered? final Query query, Object... args // does this assume query intervals must be ordered?
@ -742,7 +764,7 @@ public class CachingClusteredClientTest
} }
) )
), ),
client.run( runner.run(
query.withQuerySegmentSpec( query.withQuerySegmentSpec(
new MultipleIntervalSegmentSpec( new MultipleIntervalSegmentSpec(
Arrays.asList( Arrays.asList(
@ -1241,6 +1263,8 @@ public class CachingClusteredClientTest
private class MyDataSegment extends DataSegment private class MyDataSegment extends DataSegment
{ {
private final DataSegment baseSegment = segment;
private MyDataSegment() private MyDataSegment()
{ {
super( super(
@ -1256,8 +1280,6 @@ public class CachingClusteredClientTest
); );
} }
private final DataSegment baseSegment = segment;
@Override @Override
@JsonProperty @JsonProperty
public String getDataSource() public String getDataSource()
@ -1358,7 +1380,6 @@ public class CachingClusteredClientTest
{ {
private final DruidServer server; private final DruidServer server;
private final QueryRunner queryRunner; private final QueryRunner queryRunner;
private final List<ServerExpectation> expectations = Lists.newArrayList(); private final List<ServerExpectation> expectations = Lists.newArrayList();
public ServerExpectations( public ServerExpectations(