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

/**
 */
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();
    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) {
        log.makeAlert("Unable to find a matching rule!")
           .addData("dataSource", segment.getDataSource())
           .addData("segment", segment.getIdentifier())
           .emit();
      }
    }

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

@@ -859,16 +859,96 @@
 elper;%0A%0A
+import com.google.api.client.util.Maps;%0Aimport com.google.api.client.util.Sets;%0A
 import c
@@ -1444,16 +1444,60 @@
 il.List;
+%0Aimport java.util.Map;%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -3447,16 +3447,86 @@
 nager();
+%0A%0A    final Map%3CString, Set%3CString%3E%3E missingRules = Maps.newHashMap();
 %0A    for
@@ -4018,91 +4018,194 @@
     
-log.makeAlert(%22Unable to find a matching rule!%22)%0A           .addData(%22dataSource%22, 
+Set%3CString%3E missingSegments = missingRules.get(segment.getDataSource());%0A        if (missingSegments == null) %7B%0A          missingSegments = Sets.newHashSet();%0A          missingRules.put(
 segm
@@ -4223,17 +4223,35 @@
 Source()
-)
+, missingSegments);
 %0A       
@@ -4255,31 +4255,38 @@
     
+%7D%0A
    
-.addData(%22segment%22, 
+     missingSegments.add(
 segm
@@ -4305,27 +4305,183 @@
 ifier())
+;
 %0A      
+%7D%0A
     
+%7D%0A%0A    if (!missingRules.isEmpty()) %7B%0A      log.makeAlert(%22Unable to find a matching rules!%22)%0A         .addData(%22missingSegments%22, missingRules)%0A        
  .emit()
@@ -4481,24 +4481,16 @@
 .emit();
-%0A      %7D
 %0A    %7D%0A%0A

fix crazy amounts of logging when coordinator loses leadership