1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.mapreduce; 19 20 import org.apache.hadoop.classification.InterfaceAudience; 21 import org.apache.hadoop.classification.InterfaceStability; 22 import org.apache.hadoop.mapreduce.counters.Limits; 23 import org.apache.hadoop.mapreduce.counters.GenericCounter; 24 import org.apache.hadoop.mapreduce.counters.AbstractCounterGroup; 25 import org.apache.hadoop.mapreduce.counters.CounterGroupBase; 26 import org.apache.hadoop.mapreduce.counters.FileSystemCounterGroup; 27 import org.apache.hadoop.mapreduce.counters.AbstractCounters; 28 import org.apache.hadoop.mapreduce.counters.CounterGroupFactory; 29 import org.apache.hadoop.mapreduce.counters.FrameworkCounterGroup; 30 31 /** 32 * <p><code>Counters</code> holds per job/task counters, defined either by the 33 * Map-Reduce framework or applications. Each <code>Counter</code> can be of 34 * any {@link Enum} type.</p> 35 * 36 * <p><code>Counters</code> are bunched into {@link CounterGroup}s, each 37 * comprising of counters from a particular <code>Enum</code> class. 38 */ 39 @InterfaceAudience.Public 40 @InterfaceStability.Stable 41 public class Counters extends AbstractCounters<Counter, CounterGroup> { 42 43 // Mix framework group implementation into CounterGroup interface 44 private static class FrameworkGroupImpl<T extends Enum<T>> 45 extends FrameworkCounterGroup<T, Counter> implements CounterGroup { 46 FrameworkGroupImpl(Class<T> cls)47 FrameworkGroupImpl(Class<T> cls) { 48 super(cls); 49 } 50 51 @Override newCounter(T key)52 protected FrameworkCounter<T> newCounter(T key) { 53 return new FrameworkCounter<T>(key, getName()); 54 } 55 56 @Override getUnderlyingGroup()57 public CounterGroupBase<Counter> getUnderlyingGroup() { 58 return this; 59 } 60 } 61 62 // Mix generic group implementation into CounterGroup interface 63 // and provide some mandatory group factory methods. 64 private static class GenericGroup extends AbstractCounterGroup<Counter> 65 implements CounterGroup { 66 GenericGroup(String name, String displayName, Limits limits)67 GenericGroup(String name, String displayName, Limits limits) { 68 super(name, displayName, limits); 69 } 70 71 @Override newCounter(String name, String displayName, long value)72 protected Counter newCounter(String name, String displayName, long value) { 73 return new GenericCounter(name, displayName, value); 74 } 75 76 @Override newCounter()77 protected Counter newCounter() { 78 return new GenericCounter(); 79 } 80 81 @Override getUnderlyingGroup()82 public CounterGroupBase<Counter> getUnderlyingGroup() { 83 return this; 84 } 85 } 86 87 // Mix file system group implementation into the CounterGroup interface 88 private static class FileSystemGroup extends FileSystemCounterGroup<Counter> 89 implements CounterGroup { 90 91 @Override newCounter(String scheme, FileSystemCounter key)92 protected Counter newCounter(String scheme, FileSystemCounter key) { 93 return new FSCounter(scheme, key); 94 } 95 96 @Override getUnderlyingGroup()97 public CounterGroupBase<Counter> getUnderlyingGroup() { 98 return this; 99 } 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> newFrameworkGroupFactory(final Class<T> cls)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 newGenericGroup(String name, String displayName, Limits limits)122 protected CounterGroup newGenericGroup(String name, String displayName, 123 Limits limits) { 124 return new GenericGroup(name, displayName, limits); 125 } 126 127 @Override newFileSystemGroup()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 } 153