This project has retired. For details please refer to its Attic page.
BlockCounters xref
View Javadoc

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.giraph.block_app.framework.internal;
19  
20  import java.lang.reflect.Field;
21  
22  import org.apache.giraph.block_app.framework.api.Counter;
23  import org.apache.giraph.block_app.framework.api.StatusReporter;
24  import org.apache.giraph.block_app.framework.internal.BlockMasterLogic.TimeStatsPerEvent;
25  
26  import org.apache.giraph.counters.CustomCounter;
27  import org.apache.giraph.counters.CustomCounters;
28  import org.apache.hadoop.mapreduce.Mapper;
29  
30  /** Utility class for Blocks Framework related counters */
31  public class BlockCounters {
32    public static final String GROUP = "Blocks Framework";
33  
34    private BlockCounters() { }
35  
36    /**
37     * Takes all fields from stage object, and puts them into counters,
38     * if possible.
39     * Only fields that are convertible to long via widening are set
40     * (i.e. long/int/short/byte)
41     */
42    public static void setStageCounters(
43        String prefix, Object stage, StatusReporter reporter) {
44      if (stage != null && reporter != null) {
45        Class<?> clazz = stage.getClass();
46  
47        while (clazz != null) {
48          Field[] fields = clazz.getDeclaredFields();
49  
50          Field.setAccessible(fields, true);
51          for (Field field : fields) {
52            try {
53              long value = field.getLong(stage);
54              String counterName = prefix + field.getName();
55              CustomCounters.addCustomCounter(GROUP, counterName,
56                      CustomCounter.Aggregation.SUM);
57              reporter.getCounter(
58                  GROUP, prefix + field.getName()).setValue(value);
59  
60            // CHECKSTYLE: stop EmptyBlock - ignore any exceptions
61            } catch (IllegalArgumentException | IllegalAccessException e) {
62            }
63            // CHECKSTYLE: resume EmptyBlock
64          }
65          clazz = clazz.getSuperclass();
66        }
67      }
68    }
69  
70    public static void setMasterTimeCounter(
71        PairedPieceAndStage<?> masterPiece, long superstep,
72        long millis, StatusReporter reporter,
73        TimeStatsPerEvent timeStats) {
74      String name = masterPiece.getPiece().toString();
75      String groupName = GROUP + " Master Timers";
76      String counterName = String.format(
77              "In %6.1f %s (s)", superstep - 0.5, name);
78      CustomCounters.addCustomCounter(groupName, counterName,
79              CustomCounter.Aggregation.SUM);
80      reporter.getCounter(groupName, counterName).setValue(millis / 1000);
81      timeStats.inc(name, millis);
82    }
83  
84    public static void setWorkerTimeCounter(
85        BlockWorkerPieces<?> workerPieces, long superstep,
86        long millis, StatusReporter reporter,
87        TimeStatsPerEvent timeStats) {
88      String name = workerPieces.toStringShort();
89      String groupName = GROUP + " Worker Timers";
90      String counterName = String.format("In %6d %s (s)", superstep, name);
91      CustomCounters.addCustomCounter(groupName, counterName,
92              CustomCounter.Aggregation.SUM);
93      reporter.getCounter(groupName, counterName).setValue(millis / 1000);
94      timeStats.inc(name, millis);
95    }
96  
97    public static Counter getCounter(
98        Mapper.Context context, String group, String name) {
99      final org.apache.hadoop.mapreduce.Counter counter =
100         context.getCounter(group, name);
101     return new Counter() {
102       @Override
103       public void increment(long incr) {
104         counter.increment(incr);
105       }
106 
107       @Override
108       public void setValue(long value) {
109         counter.setValue(value);
110       }
111     };
112   }
113 
114   public static Counter getNoOpCounter() {
115     return new Counter() {
116       @Override
117       public void setValue(long value) { }
118 
119       @Override
120       public void increment(long incr) { }
121     };
122   }
123 }