/*
 * 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.server.router;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.metamx.common.Pair;
import com.metamx.common.lifecycle.LifecycleStart;
import com.metamx.common.lifecycle.LifecycleStop;
import com.metamx.emitter.EmittingLogger;
import io.druid.client.selector.HostSelector;
import io.druid.curator.discovery.ServerDiscoveryFactory;
import io.druid.curator.discovery.ServerDiscoverySelector;
import io.druid.query.Query;
import io.druid.server.coordinator.rules.LoadRule;
import io.druid.server.coordinator.rules.Rule;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 */
public class TieredBrokerHostSelector<T> implements HostSelector<T>
{
  private static EmittingLogger log = new EmittingLogger(TieredBrokerHostSelector.class);

  private final CoordinatorRuleManager ruleManager;
  private final TieredBrokerConfig tierConfig;
  private final ServerDiscoveryFactory serverDiscoveryFactory;
  private final ConcurrentHashMap<String, ServerDiscoverySelector> selectorMap = new ConcurrentHashMap<>();
  private final List<TieredBrokerSelectorStrategy> strategies;

  private final Object lock = new Object();

  private volatile boolean started = false;

  @Inject
  public TieredBrokerHostSelector(
      CoordinatorRuleManager ruleManager,
      TieredBrokerConfig tierConfig,
      ServerDiscoveryFactory serverDiscoveryFactory,
      List<TieredBrokerSelectorStrategy> strategies
  )
  {
    this.ruleManager = ruleManager;
    this.tierConfig = tierConfig;
    this.serverDiscoveryFactory = serverDiscoveryFactory;
    this.strategies = strategies;
  }

  @LifecycleStart
  public void start()
  {
    synchronized (lock) {
      if (started) {
        return;
      }

      try {
        for (Map.Entry<String, String> entry : tierConfig.getTierToBrokerMap().entrySet()) {
          ServerDiscoverySelector selector = serverDiscoveryFactory.createSelector(entry.getValue());
          selector.start();
          selectorMap.put(entry.getValue(), selector);
        }
      }
      catch (Exception e) {
        throw Throwables.propagate(e);
      }

      started = true;
    }
  }


  @LifecycleStop
  public void stop()
  {
    synchronized (lock) {
      if (!started) {
        return;
      }

      try {
        for (ServerDiscoverySelector selector : selectorMap.values()) {
          selector.stop();
        }
      }
      catch (Exception e) {
        throw Throwables.propagate(e);
      }

      started = false;
    }
  }

  @Override
  public String getDefaultServiceName()
  {
    return tierConfig.getDefaultBrokerServiceName();
  }

  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);
  }

  public Pair<String, ServerDiscoverySelector> getDefaultLookup()
  {
    final String brokerServiceName = tierConfig.getDefaultBrokerServiceName();
    final ServerDiscoverySelector retVal = selectorMap.get(brokerServiceName);
    return new Pair<>(brokerServiceName, retVal);
  }
}

@@ -1218,32 +1218,87 @@
 r.HostSelector;%0A
+import io.druid.curator.discovery.CuratorServiceUtils;%0A
 import io.druid.
@@ -5805,32 +5805,77 @@
 selectorMap.get(
+CuratorServiceUtils.makeCanonicalServiceName(
 brokerServiceNam
@@ -5876,16 +5876,17 @@
 iceName)
+)
 ;%0A%0A    i
@@ -6138,16 +6138,61 @@
 Map.get(
+CuratorServiceUtils.makeCanonicalServiceName(
 tierConf
@@ -6220,24 +6220,25 @@
 rviceName())
+)
 ;%0A    %7D%0A%0A   
@@ -6497,16 +6497,61 @@
 Map.get(
+CuratorServiceUtils.makeCanonicalServiceName(
 brokerSe
@@ -6560,16 +6560,17 @@
 iceName)
+)
 ;%0A    re

fix tiered selector service name usage