Merge pull request #813 from metamx/better-example-and-test

provide a little more involved example
This commit is contained in:
Fangjin Yang 2014-10-30 11:53:43 -06:00
commit 8af3c45657
2 changed files with 29 additions and 8 deletions

View File

@ -119,15 +119,15 @@ Including this strategy means all timeBoundary queries are always routed to the
Queries with a priority set to less than minPriority are routed to the lowest priority broker. Queries with priority set to greater than maxPriority are routed to the highest priority broker. By default, minPriority is 0 and maxPriority is 1. Using these default values, if a query with priority 0 (the default query priority is 0) is sent, the query skips the priority selection logic. Queries with a priority set to less than minPriority are routed to the lowest priority broker. Queries with priority set to greater than maxPriority are routed to the highest priority broker. By default, minPriority is 0 and maxPriority is 1. Using these default values, if a query with priority 0 (the default query priority is 0) is sent, the query skips the priority selection logic.
### javascript ### JavaScript
Allows defining arbitrary routing rules using a JavaScript function. The function is passed the configuration and the query to be executed, and returns the tier it should be routed to, or null for the default tier. Allows defining arbitrary routing rules using a JavaScript function. The function is passed the configuration and the query to be executed, and returns the tier it should be routed to, or null for the default tier.
*Example*: a function that return the highest priority broker unless the given query has more than two aggregators. *Example*: a function that sends queries containing more than three aggregators to the lowest priority broker.
```json ```json
{ {
"type" : "javascript", "type" : "javascript",
"function" : "function (config, query) { if (config.getTierToBrokerMap().values().size() > 0 && query.getAggregatorSpecs && query.getAggregatorSpecs().size() <= 2) { return config.getTierToBrokerMap().values().toArray()[0] } else { return config.getDefaultBrokerServiceName() } }" "function" : "function (config, query) { if (query.getAggregatorSpecs && query.getAggregatorSpecs().size() >= 3) { var size = config.getTierToBrokerMap().values().size(); if (size > 0) { return config.getTierToBrokerMap().values().toArray()[size-1] } else { return config.getDefaultBrokerServiceName() } } else { return null } }"
} }
``` ```

View File

@ -28,6 +28,7 @@ import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.CountAggregatorFactory; import io.druid.query.aggregation.CountAggregatorFactory;
import io.druid.query.aggregation.DoubleSumAggregatorFactory; import io.druid.query.aggregation.DoubleSumAggregatorFactory;
import io.druid.query.aggregation.LongSumAggregatorFactory; import io.druid.query.aggregation.LongSumAggregatorFactory;
import io.druid.query.topn.TopNQueryBuilder;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -36,7 +37,7 @@ import java.util.LinkedHashMap;
public class JavaScriptTieredBrokerSelectorStrategyTest public class JavaScriptTieredBrokerSelectorStrategyTest
{ {
final TieredBrokerSelectorStrategy jsStrategy = new JavaScriptTieredBrokerSelectorStrategy( final TieredBrokerSelectorStrategy jsStrategy = new JavaScriptTieredBrokerSelectorStrategy(
"function (config, query) { if (config.getTierToBrokerMap().values().size() > 0 && query.getAggregatorSpecs && query.getAggregatorSpecs().size() <= 2) { return config.getTierToBrokerMap().values().toArray()[0] } else { return config.getDefaultBrokerServiceName() } }" "function (config, query) { if (query.getAggregatorSpecs && query.getDimensionSpec && query.getDimensionSpec().getDimension() == 'bigdim' && query.getAggregatorSpecs().size() >= 3) { var size = config.getTierToBrokerMap().values().size(); if (size > 0) { return config.getTierToBrokerMap().values().toArray()[size-1] } else { return config.getDefaultBrokerServiceName() } } else { return null } }"
); );
@Test @Test
@ -57,7 +58,8 @@ public class JavaScriptTieredBrokerSelectorStrategyTest
{ {
final LinkedHashMap<String, String> tierBrokerMap = new LinkedHashMap<>(); final LinkedHashMap<String, String> tierBrokerMap = new LinkedHashMap<>();
tierBrokerMap.put("fast", "druid/fastBroker"); tierBrokerMap.put("fast", "druid/fastBroker");
tierBrokerMap.put("slow", "druid/broker"); tierBrokerMap.put("fast", "druid/broker");
tierBrokerMap.put("slow", "druid/slowBroker");
final TieredBrokerConfig tieredBrokerConfig = new TieredBrokerConfig() final TieredBrokerConfig tieredBrokerConfig = new TieredBrokerConfig()
{ {
@ -74,8 +76,11 @@ public class JavaScriptTieredBrokerSelectorStrategyTest
} }
}; };
final Druids.TimeseriesQueryBuilder queryBuilder = Druids.newTimeseriesQueryBuilder().dataSource("test") final TopNQueryBuilder queryBuilder = new TopNQueryBuilder().dataSource("test")
.intervals("2014/2015") .intervals("2014/2015")
.dimension("bigdim")
.metric("count")
.threshold(1)
.aggregators( .aggregators(
ImmutableList.<AggregatorFactory>of( ImmutableList.<AggregatorFactory>of(
new CountAggregatorFactory("count") new CountAggregatorFactory("count")
@ -83,7 +88,7 @@ public class JavaScriptTieredBrokerSelectorStrategyTest
); );
Assert.assertEquals( Assert.assertEquals(
Optional.of("druid/fastBroker"), Optional.absent(),
jsStrategy.getBrokerServiceName( jsStrategy.getBrokerServiceName(
tieredBrokerConfig, tieredBrokerConfig,
queryBuilder.build() queryBuilder.build()
@ -92,13 +97,29 @@ public class JavaScriptTieredBrokerSelectorStrategyTest
Assert.assertEquals( Assert.assertEquals(
Optional.of("druid/broker"), Optional.absent(),
jsStrategy.getBrokerServiceName( jsStrategy.getBrokerServiceName(
tieredBrokerConfig, tieredBrokerConfig,
Druids.newTimeBoundaryQueryBuilder().dataSource("test").bound("maxTime").build() Druids.newTimeBoundaryQueryBuilder().dataSource("test").bound("maxTime").build()
) )
); );
Assert.assertEquals(
Optional.of("druid/slowBroker"),
jsStrategy.getBrokerServiceName(
tieredBrokerConfig,
queryBuilder.aggregators(
ImmutableList.of(
new CountAggregatorFactory("count"),
new LongSumAggregatorFactory("longSum", "a"),
new DoubleSumAggregatorFactory("doubleSum", "b")
)
).build()
)
);
// in absence of tiers, expect the default
tierBrokerMap.clear();
Assert.assertEquals( Assert.assertEquals(
Optional.of("druid/broker"), Optional.of("druid/broker"),
jsStrategy.getBrokerServiceName( jsStrategy.getBrokerServiceName(