    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.Set;
    027
    028import cascading.stats.CascadingStats;
    029import org.apache.hadoop.conf.Configuration;
    030import org.apache.hadoop.mapred.Counters;
    031import org.apache.hadoop.mapred.RunningJob;
    032
    033/**
    034 *
    035 */
    036public abstract class HadoopStepCounterCache extends HadoopCounterCache<RunningJob, Counters>
    037  {
    038  protected HadoopStepCounterCache( CascadingStats stats, Configuration configuration )
    039    {
    040    super( stats, configuration );
    041    }
    042
    043  @Override
    044  protected boolean areCountersAvailable( RunningJob runningJob )
    045    {
    046    return true;
    047    }
    048
    049  protected Counters getCounters( RunningJob runningJob ) throws IOException
    050    {
    051    return runningJob.getCounters();
    052    }
    053
    054  protected Collection<String> getGroupNames( Counters groups )
    055    {
    056    return groups.getGroupNames();
    057    }
    058
    059  protected Set<String> getCountersFor( Counters counters, String group )
    060    {
    061    Set<String> results = new HashSet<>();
    062
    063    for( Counters.Counter counter : counters.getGroup( group ) )
    064      results.add( counter.getName() );
    065
    066    return results;
    067    }
    068
    069  protected long getCounterValue( Counters counters, Enum counter )
    070    {
    071    return counters.getCounter( counter );
    072    }
    073
    074  protected long getCounterValue( Counters counters, String groupName, String counterName )
    075    {
    076    Counters.Group counterGroup = counters.getGroup( groupName );
    077
    078    if( counterGroup == null )
    079      return 0;
    080
    081    // getCounter actually searches the display name, wtf
    082    // in theory this is lazily created if does not exist, but don't rely on it
    083    Counters.Counter counterValue = counterGroup.getCounterForName( counterName );
    084
    085    if( counterValue == null )
    086      return 0;
    087
    088    return counterValue.getValue();
    089    }
    090  }