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

import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.tez.dag.api.TezConfiguration;

@InterfaceAudience.Private
public class Limits {
  
  private static final Logger LOG = LoggerFactory.getLogger(Limits.class);

  private static Configuration conf = null;
  private static int GROUP_NAME_MAX;
  private static int COUNTER_NAME_MAX;
  private static int GROUPS_MAX;
  private static int COUNTERS_MAX;
  private static boolean initialized = false;

  private static synchronized void ensureInitialized() {
    if (initialized) {
      return;
    }
    if (conf == null) {
      conf = new TezConfiguration();
    }
    GROUP_NAME_MAX =
        conf.getInt(TezConfiguration.TEZ_COUNTERS_GROUP_NAME_MAX_LENGTH,
            TezConfiguration.TEZ_COUNTERS_GROUP_NAME_MAX_LENGTH_DEFAULT);
    COUNTER_NAME_MAX =
        conf.getInt(TezConfiguration.TEZ_COUNTERS_COUNTER_NAME_MAX_LENGTH,
            TezConfiguration.TEZ_COUNTERS_COUNTER_NAME_MAX_LENGTH_DEFAULT);
    GROUPS_MAX =
        conf.getInt(TezConfiguration.TEZ_COUNTERS_MAX_GROUPS,
            TezConfiguration.TEZ_COUNTERS_MAX_GROUPS_DEFAULT);
    COUNTERS_MAX =
        conf.getInt(TezConfiguration.TEZ_COUNTERS_MAX,
            TezConfiguration.TEZ_COUNTERS_MAX_DEFAULT);
    initialized = true;
    LOG.info("Counter limits initialized with parameters: " + " GROUP_NAME_MAX=" + GROUP_NAME_MAX
        + ", MAX_GROUPS=" + GROUPS_MAX + ", COUNTER_NAME_MAX=" + COUNTER_NAME_MAX
        + ", MAX_COUNTERS=" + COUNTERS_MAX);
  }

  private int totalCounters;
  private LimitExceededException firstViolation;

  public static String filterName(String name, int maxLen) {
    return name.length() > maxLen ? name.substring(0, maxLen - 1) : name;
  }

  public static String filterCounterName(String name) {
    ensureInitialized();
    return filterName(name, COUNTER_NAME_MAX);
  }

  public static String filterGroupName(String name) {
    ensureInitialized();
    return filterName(name, GROUP_NAME_MAX);
  }

  public synchronized void checkCounters(int size) {
    ensureInitialized();
    if (firstViolation != null) {
      throw new LimitExceededException(firstViolation);
    }
    if (size > COUNTERS_MAX) {
      firstViolation = new LimitExceededException("Too many counters: "+ size +
                                                  " max="+ COUNTERS_MAX);
      throw firstViolation;
    }
  }

  public synchronized void incrCounters() {
    checkCounters(totalCounters + 1);
    ++totalCounters;
  }

  public synchronized void checkGroups(int size) {
    ensureInitialized();
    if (firstViolation != null) {
      throw new LimitExceededException(firstViolation);
    }
    if (size > GROUPS_MAX) {
      firstViolation = new LimitExceededException("Too many counter groups: "+
                                                  size +" max="+ GROUPS_MAX);
    }
  }

  public synchronized LimitExceededException violation() {
    return firstViolation;
  }

  public synchronized static void setConfiguration(Configuration conf) {
    if (Limits.conf == null && conf != null) {
      Limits.conf = conf;
    }
  }

  @VisibleForTesting
  @InterfaceAudience.Private
  public synchronized static void reset() {
    conf = null;
    initialized = false;
  }

}

@@ -1427,169 +1427,169 @@
 AX;%0A
+%0A
   
-private static boolean initialized = false;%0A%0A  private static synchronized void ensureInitialized() %7B%0A    if (initialized) %7B%0A      return;%0A    %7D%0A    if (
+static %7B%0A    init(new TezConfiguration());%0A  %7D%0A%0A  public synchronized static void setConfiguration(Configuration conf) %7B%0A    if (Limits.conf == null && 
 conf 
-=
+!
 = nu
@@ -1604,44 +1604,103 @@
     
-conf = new TezConfiguration();%0A    %7D
+init(conf);%0A    %7D%0A  %7D%0A%0A  private static void init(Configuration conf) %7B%0A    Limits.conf = conf;
 %0A   
@@ -1712,32 +1712,24 @@
 P_NAME_MAX =
-%0A       
  conf.getInt
@@ -1774,36 +1774,32 @@
 AME_MAX_LENGTH,%0A
-    
         TezConfi
@@ -1870,32 +1870,24 @@
 R_NAME_MAX =
-%0A       
  conf.getInt
@@ -1938,28 +1938,24 @@
 MAX_LENGTH,%0A
-    
         TezC
@@ -2026,32 +2026,24 @@
 GROUPS_MAX =
-%0A       
  conf.getInt
@@ -2085,36 +2085,32 @@
 GROUPS,%0A        
- 
-   
 TezConfiguration
@@ -2217,28 +2217,16 @@
 ERS_MAX,
-%0A           
  TezConf
@@ -2266,32 +2266,8 @@
 T);%0A
-    initialized = true;%0A
     
@@ -2771,33 +2771,8 @@
 ) %7B%0A
-    ensureInitialized();%0A
     
@@ -2877,33 +2877,8 @@
 ) %7B%0A
-    ensureInitialized();%0A
     
@@ -2980,33 +2980,8 @@
 ) %7B%0A
-    ensureInitialized();%0A
     
@@ -3459,33 +3459,8 @@
 ) %7B%0A
-    ensureInitialized();%0A
     
@@ -3752,256 +3752,8 @@
  %7D%0A%0A
-  public synchronized LimitExceededException violation() %7B%0A    return firstViolation;%0A  %7D%0A%0A  public synchronized static void setConfiguration(Configuration conf) %7B%0A    if (Limits.conf == null && conf != null) %7B%0A      Limits.conf = conf;%0A    %7D%0A  %7D%0A%0A
   @V
@@ -3863,33 +3863,8 @@
 ll;%0A
-    initialized = false;%0A
   %7D%0A

Remove sync bottleneck in counters (László Bodor reviewed by Rajesh Balamohan)