Search in sources :

Example 6 with ServerDiscoverySelector

use of io.druid.curator.discovery.ServerDiscoverySelector in project druid by druid-io.

the class TieredBrokerHostSelector method select.

public Pair<String, ServerDiscoverySelector> select(final Query<T> query) {
    synchronized (lock) {
        if (!ruleManager.isStarted() || !started) {
            return getDefaultLookup();
        }
    }
    String brokerServiceName = null;
    for (TieredBrokerSelectorStrategy strategy : strategies) {
        final Optional<String> optionalName = strategy.getBrokerServiceName(tierConfig, query);
        if (optionalName.isPresent()) {
            brokerServiceName = optionalName.get();
            break;
        }
    }
    if (brokerServiceName == null) {
        // For Union Queries tier will be selected on the rules for first dataSource.
        List<Rule> rules = ruleManager.getRulesWithDefault(Iterables.getFirst(query.getDataSource().getNames(), null));
        // find the rule that can apply to the entire set of intervals
        DateTime now = new DateTime();
        int lastRulePosition = -1;
        LoadRule baseRule = null;
        for (Interval interval : query.getIntervals()) {
            int currRulePosition = 0;
            for (Rule rule : rules) {
                if (rule instanceof LoadRule && currRulePosition > lastRulePosition && rule.appliesTo(interval, now)) {
                    lastRulePosition = currRulePosition;
                    baseRule = (LoadRule) rule;
                    break;
                }
                currRulePosition++;
            }
        }
        if (baseRule == null) {
            return getDefaultLookup();
        }
        // in the baseRule, find the broker of highest priority
        for (Map.Entry<String, String> entry : tierConfig.getTierToBrokerMap().entrySet()) {
            if (baseRule.getTieredReplicants().containsKey(entry.getKey())) {
                brokerServiceName = entry.getValue();
                break;
            }
        }
    }
    if (brokerServiceName == null) {
        log.error("WTF?! No brokerServiceName found for datasource[%s], intervals[%s]. Using default[%s].", query.getDataSource(), query.getIntervals(), tierConfig.getDefaultBrokerServiceName());
        brokerServiceName = tierConfig.getDefaultBrokerServiceName();
    }
    ServerDiscoverySelector retVal = selectorMap.get(brokerServiceName);
    if (retVal == null) {
        log.error("WTF?! No selector found for brokerServiceName[%s]. Using default selector for[%s]", brokerServiceName, tierConfig.getDefaultBrokerServiceName());
        retVal = selectorMap.get(tierConfig.getDefaultBrokerServiceName());
    }
    return new Pair<>(brokerServiceName, retVal);
}
Also used : DateTime(org.joda.time.DateTime) ServerDiscoverySelector(io.druid.curator.discovery.ServerDiscoverySelector) LoadRule(io.druid.server.coordinator.rules.LoadRule) LoadRule(io.druid.server.coordinator.rules.LoadRule) Rule(io.druid.server.coordinator.rules.Rule) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) Interval(org.joda.time.Interval) Pair(io.druid.java.util.common.Pair)

Example 7 with ServerDiscoverySelector

use of io.druid.curator.discovery.ServerDiscoverySelector in project druid by druid-io.

the class OverlordProxyServletTest method testRewriteURI.

@Test
public void testRewriteURI() {
    ServerDiscoverySelector selector = EasyMock.createMock(ServerDiscoverySelector.class);
    Server server = EasyMock.createMock(Server.class);
    EasyMock.expect(server.getHost()).andReturn("overlord:port");
    EasyMock.expect(selector.pick()).andReturn(server).anyTimes();
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getScheme()).andReturn("https").anyTimes();
    EasyMock.expect(request.getQueryString()).andReturn("param1=test&param2=test2").anyTimes();
    EasyMock.expect(request.getRequestURI()).andReturn("/druid/overlord/worker").anyTimes();
    EasyMock.replay(server, selector, request);
    URI uri = URI.create(new OverlordProxyServlet(selector).rewriteTarget(request));
    Assert.assertEquals("https://overlord:port/druid/overlord/worker?param1=test&param2=test2", uri.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerDiscoverySelector(io.druid.curator.discovery.ServerDiscoverySelector) Server(io.druid.client.selector.Server) URI(java.net.URI) Test(org.junit.Test)

Aggregations

ServerDiscoverySelector (io.druid.curator.discovery.ServerDiscoverySelector)7 Map (java.util.Map)3 Server (io.druid.client.selector.Server)2 Pair (io.druid.java.util.common.Pair)2 EventReceiverFirehoseTestClient (io.druid.testing.clients.EventReceiverFirehoseTestClient)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DateTime (org.joda.time.DateTime)2 ISE (io.druid.java.util.common.ISE)1 LifecycleStart (io.druid.java.util.common.lifecycle.LifecycleStart)1 LoadRule (io.druid.server.coordinator.rules.LoadRule)1 Rule (io.druid.server.coordinator.rules.Rule)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DateTimeZone (org.joda.time.DateTimeZone)1 Interval (org.joda.time.Interval)1 Test (org.junit.Test)1