From 575919ec6c889a5429386f238f0957246773efd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Mon, 27 Oct 2014 19:14:23 -0700 Subject: [PATCH 1/2] provide a little more involved example --- docs/content/Router.md | 6 ++-- ...criptTieredBrokerSelectorStrategyTest.java | 31 ++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/docs/content/Router.md b/docs/content/Router.md index 064bf1d646d..d23af149fec 100644 --- a/docs/content/Router.md +++ b/docs/content/Router.md @@ -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. -### 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. -*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 { "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 } }" } ``` diff --git a/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java b/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java index 2b2162148ed..2e71e9937dd 100644 --- a/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java +++ b/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java @@ -28,6 +28,7 @@ import io.druid.query.aggregation.AggregatorFactory; import io.druid.query.aggregation.CountAggregatorFactory; import io.druid.query.aggregation.DoubleSumAggregatorFactory; import io.druid.query.aggregation.LongSumAggregatorFactory; +import io.druid.query.topn.TopNQueryBuilder; import org.junit.Assert; import org.junit.Test; @@ -36,7 +37,7 @@ import java.util.LinkedHashMap; public class JavaScriptTieredBrokerSelectorStrategyTest { 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 @@ -57,7 +58,8 @@ public class JavaScriptTieredBrokerSelectorStrategyTest { final LinkedHashMap tierBrokerMap = new LinkedHashMap<>(); 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() { @@ -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") + .dimension("bigdim") + .metric("count") + .threshold(1) .aggregators( ImmutableList.of( new CountAggregatorFactory("count") @@ -83,7 +88,7 @@ public class JavaScriptTieredBrokerSelectorStrategyTest ); Assert.assertEquals( - Optional.of("druid/fastBroker"), + Optional.absent(), jsStrategy.getBrokerServiceName( tieredBrokerConfig, queryBuilder.build() @@ -92,13 +97,29 @@ public class JavaScriptTieredBrokerSelectorStrategyTest Assert.assertEquals( - Optional.of("druid/broker"), + Optional.absent(), jsStrategy.getBrokerServiceName( tieredBrokerConfig, 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( Optional.of("druid/broker"), jsStrategy.getBrokerServiceName( From ec70ecc5fd10e30ad0521383d7bcc2a1e5451c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Tue, 28 Oct 2014 23:17:30 -0700 Subject: [PATCH 2/2] make tests pass with Java 7 as well --- .../router/JavaScriptTieredBrokerSelectorStrategyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java b/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java index 2e71e9937dd..d020a615c46 100644 --- a/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java +++ b/server/src/test/java/io/druid/server/router/JavaScriptTieredBrokerSelectorStrategyTest.java @@ -37,7 +37,7 @@ import java.util.LinkedHashMap; public class JavaScriptTieredBrokerSelectorStrategyTest { final TieredBrokerSelectorStrategy jsStrategy = new JavaScriptTieredBrokerSelectorStrategy( - "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 } }" + "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