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 | |
---|
19 | package org.apache.hadoop.mapreduce; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.io.DataInput; |
---|
23 | import java.io.DataOutput; |
---|
24 | |
---|
25 | import org.apache.hadoop.io.Text; |
---|
26 | import org.apache.hadoop.io.Writable; |
---|
27 | import org.apache.hadoop.io.WritableUtils; |
---|
28 | |
---|
29 | /** |
---|
30 | * A named counter that tracks the progress of a map/reduce job. |
---|
31 | * |
---|
32 | * <p><code>Counters</code> represent global counters, defined either by the |
---|
33 | * Map-Reduce framework or applications. Each <code>Counter</code> is named by |
---|
34 | * an {@link Enum} and has a long for the value.</p> |
---|
35 | * |
---|
36 | * <p><code>Counters</code> are bunched into Groups, each comprising of |
---|
37 | * counters from a particular <code>Enum</code> class. |
---|
38 | */ |
---|
39 | public class Counter implements Writable { |
---|
40 | |
---|
41 | private String name; |
---|
42 | private String displayName; |
---|
43 | private long value = 0; |
---|
44 | |
---|
45 | protected Counter() { |
---|
46 | } |
---|
47 | |
---|
48 | protected Counter(String name, String displayName) { |
---|
49 | this.name = name; |
---|
50 | this.displayName = displayName; |
---|
51 | } |
---|
52 | |
---|
53 | @Deprecated |
---|
54 | protected synchronized void setDisplayName(String displayName) { |
---|
55 | this.displayName = displayName; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Read the binary representation of the counter |
---|
60 | */ |
---|
61 | @Override |
---|
62 | public synchronized void readFields(DataInput in) throws IOException { |
---|
63 | name = Text.readString(in); |
---|
64 | if (in.readBoolean()) { |
---|
65 | displayName = Text.readString(in); |
---|
66 | } else { |
---|
67 | displayName = name; |
---|
68 | } |
---|
69 | value = WritableUtils.readVLong(in); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Write the binary representation of the counter |
---|
74 | */ |
---|
75 | @Override |
---|
76 | public synchronized void write(DataOutput out) throws IOException { |
---|
77 | Text.writeString(out, name); |
---|
78 | boolean distinctDisplayName = ! name.equals(displayName); |
---|
79 | out.writeBoolean(distinctDisplayName); |
---|
80 | if (distinctDisplayName) { |
---|
81 | Text.writeString(out, displayName); |
---|
82 | } |
---|
83 | WritableUtils.writeVLong(out, value); |
---|
84 | } |
---|
85 | |
---|
86 | public synchronized String getName() { |
---|
87 | return name; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Get the name of the counter. |
---|
92 | * @return the user facing name of the counter |
---|
93 | */ |
---|
94 | public synchronized String getDisplayName() { |
---|
95 | return displayName; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * What is the current value of this counter? |
---|
100 | * @return the current value |
---|
101 | */ |
---|
102 | public synchronized long getValue() { |
---|
103 | return value; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Increment this counter by the given value |
---|
108 | * @param incr the value to increase this counter by |
---|
109 | */ |
---|
110 | public synchronized void increment(long incr) { |
---|
111 | value += incr; |
---|
112 | } |
---|
113 | |
---|
114 | @Override |
---|
115 | public synchronized boolean equals(Object genericRight) { |
---|
116 | if (genericRight instanceof Counter) { |
---|
117 | synchronized (genericRight) { |
---|
118 | Counter right = (Counter) genericRight; |
---|
119 | return name.equals(right.name) && |
---|
120 | displayName.equals(right.displayName) && |
---|
121 | value == right.value; |
---|
122 | } |
---|
123 | } |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | @Override |
---|
128 | public synchronized int hashCode() { |
---|
129 | return name.hashCode() + displayName.hashCode(); |
---|
130 | } |
---|
131 | } |
---|