001/*
002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.stats.hadoop;
022
023import java.io.IOException;
024import java.util.Collection;
025import java.util.HashSet;
026import java.util.LinkedHashSet;
027import java.util.Set;
028
029import cascading.stats.CascadingStats;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.mapred.RunningJob;
032import org.apache.hadoop.mapreduce.Counter;
033import org.apache.hadoop.mapreduce.CounterGroup;
034import org.apache.hadoop.mapreduce.Counters;
035
036/**
037 *
038 */
039public abstract class HadoopStepCounterCache extends HadoopCounterCache<RunningJob, Counters>
040  {
041  protected HadoopStepCounterCache( CascadingStats stats, Configuration configuration )
042    {
043    super( stats, configuration );
044    }
045
046  @Override
047  protected boolean areCountersAvailable( RunningJob runningJob )
048    {
049    return true;
050    }
051
052  @Override
053  protected Counters getCounters( RunningJob runningJob ) throws IOException
054    {
055    return HadoopStepStats.getJob( runningJob ).getCounters();
056    }
057
058  @Override
059  protected Collection<String> getGroupNames( Counters groups )
060    {
061    LinkedHashSet<String> result = new LinkedHashSet<>();
062
063    for( String value : groups.getGroupNames() )
064      result.add( value );
065
066    return result;
067    }
068
069  @Override
070  protected Set<String> getCountersFor( Counters counters, String group )
071    {
072    Set<String> results = new HashSet<>();
073
074    for( Counter counter : counters.getGroup( group ) )
075      results.add( counter.getName() );
076
077    return results;
078    }
079
080  @Override
081  protected long getCounterValue( Counters counters, Enum counter )
082    {
083    Counter result = counters.findCounter( counter );
084
085    if( result == null )
086      return 0;
087
088    return result.getValue();
089    }
090
091  @Override
092  protected long getCounterValue( Counters counters, String groupName, String counterName )
093    {
094    CounterGroup counterGroup = counters.getGroup( groupName );
095
096    if( counterGroup == null )
097      return 0;
098
099    // getCounter actually searches the display name, wtf
100    // in theory this is lazily created if does not exist, but don't rely on it
101    Counter counterValue = counterGroup.findCounter( counterName );
102
103    if( counterValue == null )
104      return 0;
105
106    return counterValue.getValue();
107    }
108  }