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.tez;
022
023import java.util.Collection;
024import java.util.Collections;
025import java.util.HashSet;
026import java.util.Set;
027
028import cascading.stats.CascadingStats;
029import cascading.stats.CounterCache;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.tez.common.counters.CounterGroup;
032import org.apache.tez.common.counters.TezCounter;
033import org.apache.tez.common.counters.TezCounters;
034
035/**
036 *
037 */
038public abstract class TezCounterCache<JobStatus> extends CounterCache<Configuration, JobStatus, TezCounters>
039  {
040  protected TezCounterCache( CascadingStats stats, Configuration configuration )
041    {
042    super( stats, configuration );
043    }
044
045  @Override
046  protected boolean areCountersAvailable( JobStatus runningJob )
047    {
048    return true;
049    }
050
051  @Override
052  protected Collection<String> getGroupNames( TezCounters counterGroups )
053    {
054    Iterable<String> iterable = counterGroups.getGroupNames();
055
056    if( iterable == null ) // it's hadoop, be defensive
057      return Collections.emptySet();
058
059    Set<String> groupNames = new HashSet<>();
060
061    for( String groupName : iterable )
062      groupNames.add( groupName );
063
064    return groupNames;
065    }
066
067  @Override
068  protected Set<String> getCountersFor( TezCounters counterGroups, String group )
069    {
070    Set<String> results = new HashSet<>();
071
072    for( TezCounter counter : counterGroups.getGroup( group ) )
073      results.add( counter.getName() );
074
075    return results;
076    }
077
078  @Override
079  protected long getCounterValue( TezCounters counterGroups, Enum counter )
080    {
081    TezCounter tezCounter = counterGroups.findCounter( counter );
082
083    if( tezCounter == null )
084      return 0;
085
086    return tezCounter.getValue();
087    }
088
089  @Override
090  protected long getCounterValue( TezCounters counterGroups, String groupName, String counterName )
091    {
092    CounterGroup counterGroup = counterGroups.getGroup( groupName );
093
094    if( counterGroup == null )
095      return 0;
096
097    TezCounter counterValue = counterGroup.findCounter( counterName );
098
099    if( counterValue == null )
100      return 0;
101
102    return counterValue.getValue();
103    }
104
105  protected int getIntProperty( String property, int defaultValue )
106    {
107    return configuration.getInt( property, defaultValue );
108    }
109  }