diff --git a/server/src/main/java/io/druid/client/selector/AbstractTierSelectorStrategy.java b/server/src/main/java/io/druid/client/selector/AbstractTierSelectorStrategy.java index fd0a3de280f..ad04d3a7fa2 100644 --- a/server/src/main/java/io/druid/client/selector/AbstractTierSelectorStrategy.java +++ b/server/src/main/java/io/druid/client/selector/AbstractTierSelectorStrategy.java @@ -22,6 +22,7 @@ package io.druid.client.selector; import com.metamx.common.ISE; import io.druid.timeline.DataSegment; +import java.util.Comparator; import java.util.Map; import java.util.Set; import java.util.TreeMap; @@ -37,14 +38,12 @@ public abstract class AbstractTierSelectorStrategy implements TierSelectorStrate this.serverSelectorStrategy = serverSelectorStrategy; } - public abstract Map.Entry> getServers(TreeMap> prioritizedServers); - @Override public QueryableDruidServer pick( TreeMap> prioritizedServers, DataSegment segment ) { - final Map.Entry> priorityServers = getServers(prioritizedServers); + final Map.Entry> priorityServers = prioritizedServers.pollFirstEntry(); if (priorityServers == null) { return null; diff --git a/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategy.java b/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategy.java new file mode 100644 index 00000000000..1ed750a0dc9 --- /dev/null +++ b/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategy.java @@ -0,0 +1,48 @@ +package io.druid.client.selector; + +import com.google.api.client.util.Maps; +import com.google.common.primitives.Ints; +import com.google.inject.Inject; + +import java.util.Comparator; +import java.util.Map; + +/** + */ +public class CustomTierSelectorStrategy extends AbstractTierSelectorStrategy +{ + private final Comparator comparator; + + @Inject + public CustomTierSelectorStrategy( + ServerSelectorStrategy serverSelectorStrategy, + CustomTierSelectorStrategyConfig config + ) + { + super(serverSelectorStrategy); + + final Map lookup = Maps.newHashMap(); + int pos = 0; + for (Integer integer : config.getPriorities()) { + lookup.put(integer, pos); + pos++; + } + + this.comparator = new Comparator() + { + @Override + public int compare(Integer o1, Integer o2) + { + int pos1 = lookup.get(o1); + int pos2 = lookup.get(o2); + return Ints.compare(pos1, pos2); + } + }; + } + + @Override + public Comparator getComparator() + { + return comparator; + } +} diff --git a/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategyConfig.java b/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategyConfig.java new file mode 100644 index 00000000000..d5bc0201c70 --- /dev/null +++ b/server/src/main/java/io/druid/client/selector/CustomTierSelectorStrategyConfig.java @@ -0,0 +1,38 @@ +/* + * Druid - a distributed column store. + * Copyright (C) 2012, 2013 Metamarkets Group Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package io.druid.client.selector; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.api.client.util.Lists; + +import java.util.List; + +/** + */ +public class CustomTierSelectorStrategyConfig +{ + @JsonProperty + private List priorities = Lists.newArrayList(); + + public List getPriorities() + { + return priorities; + } +} diff --git a/server/src/main/java/io/druid/client/selector/HighestPriorityTierSelectorStrategy.java b/server/src/main/java/io/druid/client/selector/HighestPriorityTierSelectorStrategy.java index e23f1a978af..a2dd22ec792 100644 --- a/server/src/main/java/io/druid/client/selector/HighestPriorityTierSelectorStrategy.java +++ b/server/src/main/java/io/druid/client/selector/HighestPriorityTierSelectorStrategy.java @@ -19,16 +19,24 @@ package io.druid.client.selector; +import com.google.common.primitives.Ints; import com.google.inject.Inject; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; +import java.util.Comparator; /** */ public class HighestPriorityTierSelectorStrategy extends AbstractTierSelectorStrategy { + private static final Comparator comparator = new Comparator() + { + @Override + public int compare(Integer o1, Integer o2) + { + return -Ints.compare(o1, o2); + } + }; + @Inject public HighestPriorityTierSelectorStrategy(ServerSelectorStrategy serverSelectorStrategy) { @@ -36,8 +44,8 @@ public class HighestPriorityTierSelectorStrategy extends AbstractTierSelectorStr } @Override - public Map.Entry> getServers(TreeMap> prioritizedServers) + public Comparator getComparator() { - return prioritizedServers.pollLastEntry(); + return comparator; } } diff --git a/server/src/main/java/io/druid/client/selector/LowestPriorityTierSelectorStrategy.java b/server/src/main/java/io/druid/client/selector/LowestPriorityTierSelectorStrategy.java index 09c2913cfb9..00e6988a50e 100644 --- a/server/src/main/java/io/druid/client/selector/LowestPriorityTierSelectorStrategy.java +++ b/server/src/main/java/io/druid/client/selector/LowestPriorityTierSelectorStrategy.java @@ -19,16 +19,24 @@ package io.druid.client.selector; +import com.google.common.primitives.Ints; import com.google.inject.Inject; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; +import java.util.Comparator; /** */ public class LowestPriorityTierSelectorStrategy extends AbstractTierSelectorStrategy { + private static final Comparator comparator = new Comparator() + { + @Override + public int compare(Integer o1, Integer o2) + { + return Ints.compare(o1, o2); + } + }; + @Inject public LowestPriorityTierSelectorStrategy(ServerSelectorStrategy serverSelectorStrategy) { @@ -36,8 +44,8 @@ public class LowestPriorityTierSelectorStrategy extends AbstractTierSelectorStra } @Override - public Map.Entry> getServers(TreeMap> prioritizedServers) + public Comparator getComparator() { - return prioritizedServers.pollFirstEntry(); + return comparator; } } diff --git a/server/src/main/java/io/druid/client/selector/ServerSelector.java b/server/src/main/java/io/druid/client/selector/ServerSelector.java index 82aaa9ac4e0..71c776d8ac4 100644 --- a/server/src/main/java/io/druid/client/selector/ServerSelector.java +++ b/server/src/main/java/io/druid/client/selector/ServerSelector.java @@ -79,7 +79,7 @@ public class ServerSelector implements DiscoverySelector public QueryableDruidServer pick() { synchronized (this) { - TreeMap> prioritizedServers = Maps.newTreeMap(); + final TreeMap> prioritizedServers = new TreeMap<>(strategy.getComparator()); for (QueryableDruidServer server : servers) { Set theServers = prioritizedServers.get(server.getServer().getPriority()); if (theServers == null) { diff --git a/server/src/main/java/io/druid/client/selector/TierSelectorStrategy.java b/server/src/main/java/io/druid/client/selector/TierSelectorStrategy.java index a9cc86b794a..cadbd435478 100644 --- a/server/src/main/java/io/druid/client/selector/TierSelectorStrategy.java +++ b/server/src/main/java/io/druid/client/selector/TierSelectorStrategy.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import io.druid.timeline.DataSegment; +import java.util.Comparator; import java.util.Set; import java.util.TreeMap; @@ -31,9 +32,12 @@ import java.util.TreeMap; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = HighestPriorityTierSelectorStrategy.class) @JsonSubTypes(value = { @JsonSubTypes.Type(name = "highestPriority", value = HighestPriorityTierSelectorStrategy.class), - @JsonSubTypes.Type(name = "lowestPriority", value = LowestPriorityTierSelectorStrategy.class) + @JsonSubTypes.Type(name = "lowestPriority", value = LowestPriorityTierSelectorStrategy.class), + @JsonSubTypes.Type(name = "custom", value = CustomTierSelectorStrategy.class) }) public interface TierSelectorStrategy { + public Comparator getComparator(); + public QueryableDruidServer pick(TreeMap> prioritizedServers, DataSegment segment); } diff --git a/services/src/main/java/io/druid/cli/CliBroker.java b/services/src/main/java/io/druid/cli/CliBroker.java index f92ae410146..c10a3b05eb2 100644 --- a/services/src/main/java/io/druid/cli/CliBroker.java +++ b/services/src/main/java/io/druid/cli/CliBroker.java @@ -31,6 +31,7 @@ import io.druid.client.cache.Cache; import io.druid.client.cache.CacheConfig; import io.druid.client.cache.CacheMonitor; import io.druid.client.cache.CacheProvider; +import io.druid.client.selector.CustomTierSelectorStrategyConfig; import io.druid.client.selector.ServerSelectorStrategy; import io.druid.client.selector.TierSelectorStrategy; import io.druid.curator.discovery.DiscoveryModule; @@ -85,6 +86,7 @@ public class CliBroker extends ServerRunnable JsonConfigProvider.bind(binder, "druid.broker.cache", CacheProvider.class); JsonConfigProvider.bind(binder, "druid.broker.cache", CacheConfig.class); JsonConfigProvider.bind(binder, "druid.broker.select.tier", TierSelectorStrategy.class); + JsonConfigProvider.bind(binder, "druid.broker.select.tier.custom", CustomTierSelectorStrategyConfig.class); JsonConfigProvider.bind(binder, "druid.broker.select.server", ServerSelectorStrategy.class); binder.bind(QuerySegmentWalker.class).to(ClientQuerySegmentWalker.class).in(LazySingleton.class);