/*
 * 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.coordinator.helper;

import com.google.api.client.util.Maps;
import com.google.api.client.util.Sets;
import com.metamx.emitter.EmittingLogger;
import io.druid.metadata.MetadataRuleManager;
import io.druid.server.coordinator.CoordinatorStats;
import io.druid.server.coordinator.DruidCluster;
import io.druid.server.coordinator.DruidCoordinator;
import io.druid.server.coordinator.DruidCoordinatorRuntimeParams;
import io.druid.server.coordinator.ReplicationThrottler;
import io.druid.server.coordinator.rules.Rule;
import io.druid.timeline.DataSegment;
import org.joda.time.DateTime;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 */
public class DruidCoordinatorRuleRunner implements DruidCoordinatorHelper
{
  private static final EmittingLogger log = new EmittingLogger(DruidCoordinatorRuleRunner.class);

  private final ReplicationThrottler replicatorThrottler;

  private final DruidCoordinator coordinator;

  public DruidCoordinatorRuleRunner(DruidCoordinator coordinator)
  {
    this(
        new ReplicationThrottler(
            coordinator.getDynamicConfigs().getReplicationThrottleLimit(),
            coordinator.getDynamicConfigs().getReplicantLifetime()
        ),
        coordinator
    );
  }

  public DruidCoordinatorRuleRunner(ReplicationThrottler replicatorThrottler, DruidCoordinator coordinator)
  {
    this.replicatorThrottler = replicatorThrottler;
    this.coordinator = coordinator;
  }

  @Override
  public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params)
  {
    replicatorThrottler.updateParams(
        coordinator.getDynamicConfigs().getReplicationThrottleLimit(),
        coordinator.getDynamicConfigs().getReplicantLifetime()
    );

    CoordinatorStats stats = new CoordinatorStats();
    DruidCluster cluster = params.getDruidCluster();

    if (cluster.isEmpty()) {
      log.warn("Uh... I have no servers. Not assigning anything...");
      return params;
    }

    for (String tier : cluster.getTierNames()) {
      replicatorThrottler.updateReplicationState(tier);
      replicatorThrottler.updateTerminationState(tier);
    }

    DruidCoordinatorRuntimeParams paramsWithReplicationManager = params.buildFromExisting()
                                                                       .withReplicationManager(replicatorThrottler)
                                                                       .build();

    // Run through all matched rules for available segments
    DateTime now = new DateTime();
    MetadataRuleManager databaseRuleManager = paramsWithReplicationManager.getDatabaseRuleManager();

    final Map<String, Set<String>> missingRules = Maps.newHashMap();
    for (DataSegment segment : paramsWithReplicationManager.getAvailableSegments()) {
      List<Rule> rules = databaseRuleManager.getRulesWithDefault(segment.getDataSource());
      boolean foundMatchingRule = false;
      for (Rule rule : rules) {
        if (rule.appliesTo(segment, now)) {
          stats.accumulate(rule.run(coordinator, paramsWithReplicationManager, segment));
          foundMatchingRule = true;
          break;
        }
      }

      if (!foundMatchingRule) {
        Set<String> missingSegments = missingRules.get(segment.getDataSource());
        if (missingSegments == null) {
          missingSegments = Sets.newHashSet();
          missingRules.put(segment.getDataSource(), missingSegments);
        }
        missingSegments.add(segment.getIdentifier());
      }
    }

    if (!missingRules.isEmpty()) {
      log.makeAlert("Unable to find a matching rules!")
         .addData("missingSegments", missingRules)
         .emit();
    }

    return paramsWithReplicationManager.buildFromExisting()
                                       .withCoordinatorStats(stats)
                                       .build();
  }
}

@@ -881,66 +881,26 @@
 gle.
-api.client.util.Maps;%0Aimport com.google.api.client.util.Se
+common.collect.Lis
 ts;%0A
@@ -1408,52 +1408,8 @@
 ist;
-%0Aimport java.util.Map;%0Aimport java.util.Set;
 %0A%0A/*
@@ -1587,16 +1587,61 @@
 .class);
+%0A  private static int MAX_MISSING_RULES = 10;
 %0A%0A  priv
@@ -3424,34 +3424,34 @@
 nal 
-Map%3CString, Set%3CString%3E%3E m
+List%3CString%3E segmentsWithM
 issi
@@ -3464,25 +3464,68 @@
 s = 
-Map
+List
 s.new
-HashMap()
+ArrayListWithCapacity(10);%0A    int missingRules = 0
 ;%0A  
@@ -4022,39 +4022,25 @@
     
-Set%3CString%3E missingS
+if (s
 egments
- = m
+WithM
 issi
@@ -4051,135 +4051,61 @@
 les.
-get(segment.getDataSource());%0A        if (missingSegments == null) %7B%0A          missingSegments = Sets.newHashSet();%0A          m
+size() %3C MAX_MISSING_RULES) %7B%0A          segmentsWithM
 issi
@@ -4116,11 +4116,11 @@
 les.
-put
+add
 (seg
@@ -4131,102 +4131,55 @@
 .get
-DataSource(), missingSegments);%0A        %7D%0A        missingSegments.add(segment.getIdentifier())
+Identifier());%0A        %7D%0A        missingRules++
 ;%0A  
@@ -4200,17 +4200,29 @@
    if (!
-m
+segmentsWithM
 issingRu
@@ -4274,18 +4274,16 @@
 to find 
-a 
 matching
@@ -4315,27 +4315,113 @@
 ta(%22
-missingSegments%22, m
+segmentsWithMissingRulesCount%22, missingRules)%0A         .addData(%22segmentsWithMissingRules%22, segmentsWithM
 issi

address code review