/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.mapred;

import java.util.List;

import org.apache.hadoop.mapred.Counters.Group;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.TaskType;
import org.apache.hadoop.metrics.MetricsContext;
import org.apache.hadoop.metrics.MetricsRecord;
import org.apache.hadoop.metrics.MetricsUtil;
import org.apache.hadoop.metrics.Updater;
import org.apache.hadoop.metrics.jvm.JvmMetrics;

class JobTrackerMetricsInst extends JobTrackerInstrumentation implements Updater {
  private final MetricsRecord metricsRecord;

  private int numJobsSubmitted = 0;
  private int numJobsCompleted = 0;
  private int numWaitingMaps = 0;
  private int numWaitingReduces = 0;

  /**
   * Helper class that makes it easier to keep track of additional stats for
   * speculated tasks. Needed to evaluate the performance of speculating by
   * processing (bytes/s) vs progress (%/s) rate.
   */
  private static class SpecStats {
    public static enum TaskType {MAP, REDUCE};
    // The type of speculation. Whether it's by progress rate (e.g. %/sec) or
    // processing rate (e.g. bytes/sec)
    public static enum SpecType {PROGRESS, PROCESSING};
    // Wasted time is in ms
    public static enum StatType {WASTED_TASKS, WASTED_TIME, LAUNCHED_TASKS,
      SUCCESFUL_TASKS};

    private final int taskTypeSize = TaskType.values().length;
    private final int specTypeSize = SpecType.values().length;
    private final int statTypeSize = StatType.values().length;

    private final long[][][] values =
      new long[taskTypeSize][specTypeSize][statTypeSize];

    public SpecStats() {}

    public void incStat(TaskType taskType, SpecType specType, StatType statType,
                        long value) {
      values[taskType.ordinal()][specType.ordinal()][statType.ordinal()] +=
        value;
    }

    public long getStat(TaskType taskType, SpecType specType,
                        StatType statType) {
      return values[taskType.ordinal()][specType.ordinal()][statType.ordinal()];
    }
  }

  private final SpecStats specStats = new SpecStats();

  private final JobStats aggregateJobStats = new JobStats();

  private final Counters countersToMetrics = new Counters();

  //Cluster status fields.
  private volatile int numMapSlots = 0;
  private volatile int numReduceSlots = 0;
  private int numBlackListedMapSlots = 0;
  private int numBlackListedReduceSlots = 0;

  private int numReservedMapSlots = 0;
  private int numReservedReduceSlots = 0;
  private int numOccupiedMapSlots = 0;
  private int numOccupiedReduceSlots = 0;

  private int numJobsFailed = 0;
  private int numJobsKilled = 0;

  private int numJobsPreparing = 0;
  private int numJobsRunning = 0;

  private int numRunningMaps = 0;
  private int numRunningReduces = 0;

  private int numTrackers = 0;
  private int numTrackersBlackListed = 0;
  private int numTrackersDecommissioned = 0;
  private int numTrackersExcluded = 0;
  private int numTrackersDead = 0;

  private int numTasksInMemory = 0;

  //Extended JobTracker Metrics
  private long totalSubmitTime = 0;
  private long numJobsLaunched = 0;

  public JobTrackerMetricsInst(JobTracker tracker, JobConf conf) {
    super(tracker, conf);
    String sessionId = conf.getSessionId();
    // Initiate JVM Metrics
    JvmMetrics.init("JobTracker", sessionId);
    // Create a record for map-reduce metrics
    MetricsContext context = MetricsUtil.getContext("mapred");
    metricsRecord = MetricsUtil.createRecord(context, "jobtracker");
    metricsRecord.setTag("sessionId", sessionId);
    context.registerUpdater(this);
  }

  /**
   * Since this object is a registered updater, this method will be called
   * periodically, e.g. every 5 seconds.
   */
  public void doUpdates(MetricsContext unused) {
    // In case of running in LocalMode tracker == null
    if (tracker != null) {
      synchronized (tracker) {
        synchronized (this) {
          numRunningMaps = 0;
          numRunningReduces = 0;

          numWaitingMaps = 0;
          numWaitingReduces = 0;
          numTasksInMemory = 0;

          List<JobInProgress> jobs = tracker.getRunningJobs();
          for (JobInProgress jip : jobs) {
            for (TaskInProgress tip : jip.maps) {
              if (tip.isRunning()) {
                numRunningMaps++;
              } else if (tip.isRunnable()) {
                numWaitingMaps++;
              }
            }
            for (TaskInProgress tip : jip.reduces) {
              if (tip.isRunning()) {
                numRunningReduces++;
              } else if (tip.isRunnable()) {
                numWaitingReduces++;
              }

            }
            numTasksInMemory += jip.getTasks(TaskType.MAP).length;
            numTasksInMemory += jip.getTasks(TaskType.REDUCE).length;
          }

          // Get tracker metrics
          numTrackersDead = tracker.getDeadNodes().size();
          ClusterStatus cs = tracker.getClusterStatus(false);
          numTrackersExcluded = cs.getNumExcludedNodes();
        }
      }
    }
    synchronized (this) {
      metricsRecord.setMetric("map_slots", numMapSlots);
      metricsRecord.setMetric("reduce_slots", numReduceSlots);
      metricsRecord.incrMetric("blacklisted_maps", numBlackListedMapSlots);
      metricsRecor