001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.mapreduce;
019    
020    import org.apache.hadoop.classification.InterfaceAudience;
021    import org.apache.hadoop.classification.InterfaceStability;
022    import org.apache.hadoop.mapreduce.counters.Limits;
023    import org.apache.hadoop.mapreduce.counters.GenericCounter;
024    import org.apache.hadoop.mapreduce.counters.AbstractCounterGroup;
025    import org.apache.hadoop.mapreduce.counters.CounterGroupBase;
026    import org.apache.hadoop.mapreduce.counters.FileSystemCounterGroup;
027    import org.apache.hadoop.mapreduce.counters.AbstractCounters;
028    import org.apache.hadoop.mapreduce.counters.CounterGroupFactory;
029    import org.apache.hadoop.mapreduce.counters.FrameworkCounterGroup;
030    
031    /**
032     * <p><code>Counters</code> holds per job/task counters, defined either by the
033     * Map-Reduce framework or applications. Each <code>Counter</code> can be of
034     * any {@link Enum} type.</p>
035     *
036     * <p><code>Counters</code> are bunched into {@link CounterGroup}s, each
037     * comprising of counters from a particular <code>Enum</code> class.
038     */
039    @InterfaceAudience.Public
040    @InterfaceStability.Stable
041    public class Counters extends AbstractCounters<Counter, CounterGroup> {
042    
043      // Mix framework group implementation into CounterGroup interface
044      private static class FrameworkGroupImpl<T extends Enum<T>>
045          extends FrameworkCounterGroup<T, Counter> implements CounterGroup {
046    
047        FrameworkGroupImpl(Class<T> cls) {
048          super(cls);
049        }
050    
051        @Override
052        protected FrameworkCounter<T> newCounter(T key) {
053          return new FrameworkCounter<T>(key, getName());
054        }
055    
056        @Override
057        public CounterGroupBase<Counter> getUnderlyingGroup() {
058          return this;
059        }
060      }
061    
062      // Mix generic group implementation into CounterGroup interface
063      // and provide some mandatory group factory methods.
064      private static class GenericGroup extends AbstractCounterGroup<Counter>
065          implements CounterGroup {
066    
067        GenericGroup(String name, String displayName, Limits limits) {
068          super(name, displayName, limits);
069        }
070    
071        @Override
072        protected Counter newCounter(String name, String displayName, long value) {
073          return new GenericCounter(name, displayName, value);
074        }
075    
076        @Override
077        protected Counter newCounter() {
078          return new GenericCounter();
079        }
080    
081        @Override
082        public CounterGroupBase<Counter> getUnderlyingGroup() {
083          return this;
084        }
085      }
086    
087      // Mix file system group implementation into the CounterGroup interface
088      private static class FileSystemGroup extends FileSystemCounterGroup<Counter>
089          implements CounterGroup {
090    
091        @Override
092        protected Counter newCounter(String scheme, FileSystemCounter key) {
093          return new FSCounter(scheme, key);
094        }
095    
096        @Override
097        public CounterGroupBase<Counter> getUnderlyingGroup() {
098          return this;
099        }
100      }
101    
102      /**
103       * Provide factory methods for counter group factory implementation.
104       * See also the GroupFactory in
105       *  {@link org.apache.hadoop.mapred.Counters mapred.Counters}
106       */
107      private static class GroupFactory
108          extends CounterGroupFactory<Counter, CounterGroup> {
109    
110        @Override
111        protected <T extends Enum<T>>
112        FrameworkGroupFactory<CounterGroup>
113            newFrameworkGroupFactory(final Class<T> cls) {
114          return new FrameworkGroupFactory<CounterGroup>() {
115            @Override public CounterGroup newGroup(String name) {
116              return new FrameworkGroupImpl<T>(cls); // impl in this package
117            }
118          };
119        }
120    
121        @Override
122        protected CounterGroup newGenericGroup(String name, String displayName,
123                                               Limits limits) {
124          return new GenericGroup(name, displayName, limits);
125        }
126    
127        @Override
128        protected CounterGroup newFileSystemGroup() {
129          return new FileSystemGroup();
130        }
131      }
132    
133      private static final GroupFactory groupFactory = new GroupFactory();
134    
135      /**
136       * Default constructor
137       */
138      public Counters() {
139        super(groupFactory);
140      }
141    
142      /**
143       * Construct the Counters object from the another counters object
144       * @param <C> the type of counter
145       * @param <G> the type of counter group
146       * @param counters the old counters object
147       */
148      public <C extends Counter, G extends CounterGroupBase<C>>
149      Counters(AbstractCounters<C, G> counters) {
150        super(counters, groupFactory);
151      }
152    }