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 newCounter(T key) { 053 return new FrameworkCounter(key); 054 } 055 } 056 057 // Mix generic group implementation into CounterGroup interface 058 // and provide some mandatory group factory methods. 059 private static class GenericGroup extends AbstractCounterGroup<Counter> 060 implements CounterGroup { 061 062 GenericGroup(String name, String displayName, Limits limits) { 063 super(name, displayName, limits); 064 } 065 066 @Override 067 protected Counter newCounter(String name, String displayName, long value) { 068 return new GenericCounter(name, displayName, value); 069 } 070 071 @Override 072 protected Counter newCounter() { 073 return new GenericCounter(); 074 } 075 } 076 077 // Mix file system group implementation into the CounterGroup interface 078 private static class FileSystemGroup extends FileSystemCounterGroup<Counter> 079 implements CounterGroup { 080 081 @Override 082 protected Counter newCounter(String scheme, FileSystemCounter key) { 083 return new FSCounter(scheme, key); 084 } 085 } 086 087 /** 088 * Provide factory methods for counter group factory implementation. 089 * See also the GroupFactory in 090 * {@link org.apache.hadoop.mapred.Counters mapred.Counters} 091 */ 092 private static class GroupFactory 093 extends CounterGroupFactory<Counter, CounterGroup> { 094 095 @Override 096 protected <T extends Enum<T>> 097 FrameworkGroupFactory<CounterGroup> 098 newFrameworkGroupFactory(final Class<T> cls) { 099 return new FrameworkGroupFactory<CounterGroup>() { 100 @Override public CounterGroup newGroup(String name) { 101 return new FrameworkGroupImpl<T>(cls); // impl in this package 102 } 103 }; 104 } 105 106 @Override 107 protected CounterGroup newGenericGroup(String name, String displayName, 108 Limits limits) { 109 return new GenericGroup(name, displayName, limits); 110 } 111 112 @Override 113 protected CounterGroup newFileSystemGroup() { 114 return new FileSystemGroup(); 115 } 116 } 117 118 private static final GroupFactory groupFactory = new GroupFactory(); 119 120 /** 121 * Default constructor 122 */ 123 public Counters() { 124 super(groupFactory); 125 } 126 127 /** 128 * Construct the Counters object from the another counters object 129 * @param <C> the type of counter 130 * @param <G> the type of counter group 131 * @param counters the old counters object 132 */ 133 public <C extends Counter, G extends CounterGroupBase<C>> 134 Counters(AbstractCounters<C, G> counters) { 135 super(counters, groupFactory); 136 } 137 }