1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
28 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
29 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30
31
32
33
34
35 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
36 public class MetricsSource {
37
38 private static final Log LOG = LogFactory.getLog(MetricsSource.class);
39
40
41 private Map<String, Long> lastTimeStamps = new HashMap<String, Long>();
42 private int lastQueueSize = 0;
43 private String id;
44
45 private final MetricsReplicationSourceSource singleSourceSource;
46 private final MetricsReplicationSourceSource globalSourceSource;
47
48
49
50
51
52
53 public MetricsSource(String id) {
54 this.id = id;
55 singleSourceSource =
56 CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
57 .getSource(id);
58 globalSourceSource = CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class).getGlobalSource();
59 }
60
61
62
63
64
65
66 public void setAgeOfLastShippedOp(long timestamp, String walGroup) {
67 long age = EnvironmentEdgeManager.currentTime() - timestamp;
68 singleSourceSource.setLastShippedAge(age);
69 globalSourceSource.setLastShippedAge(Math.max(age, globalSourceSource.getLastShippedAge()));
70 this.lastTimeStamps.put(walGroup, timestamp);
71 }
72
73
74
75
76
77
78 public void refreshAgeOfLastShippedOp(String walGroupId) {
79 Long lastTimestamp = this.lastTimeStamps.get(walGroupId);
80 if (lastTimestamp == null) {
81 this.lastTimeStamps.put(walGroupId, 0L);
82 lastTimestamp = 0L;
83 }
84 if (lastTimestamp > 0) {
85 setAgeOfLastShippedOp(lastTimestamp, walGroupId);
86 }
87 }
88
89
90
91
92
93
94 public void setSizeOfLogQueue(int size) {
95 singleSourceSource.setSizeOfLogQueue(size);
96 globalSourceSource.incrSizeOfLogQueue(size - lastQueueSize);
97 lastQueueSize = size;
98 }
99
100
101
102
103
104
105 private void incrLogEditsRead(long delta) {
106 singleSourceSource.incrLogReadInEdits(delta);
107 globalSourceSource.incrLogReadInEdits(delta);
108 }
109
110
111 public void incrLogEditsRead() {
112 incrLogEditsRead(1);
113 }
114
115
116
117
118
119
120 public void incrLogEditsFiltered(long delta) {
121 singleSourceSource.incrLogEditsFiltered(delta);
122 globalSourceSource.incrLogEditsFiltered(delta);
123 }
124
125
126 public void incrLogEditsFiltered() {
127 incrLogEditsFiltered(1);
128 }
129
130
131
132
133
134
135 public void shipBatch(long batchSize, int sizeInKB) {
136 singleSourceSource.incrBatchesShipped(1);
137 globalSourceSource.incrBatchesShipped(1);
138
139 singleSourceSource.incrOpsShipped(batchSize);
140 globalSourceSource.incrOpsShipped(batchSize);
141
142 singleSourceSource.incrShippedKBs(sizeInKB);
143 globalSourceSource.incrShippedKBs(sizeInKB);
144 }
145
146
147 public void incrLogReadInBytes(long readInBytes) {
148 singleSourceSource.incrLogReadInBytes(readInBytes);
149 globalSourceSource.incrLogReadInBytes(readInBytes);
150 }
151
152
153 public void clear() {
154 singleSourceSource.clear();
155 globalSourceSource.decrSizeOfLogQueue(lastQueueSize);
156 lastTimeStamps.clear();
157 lastQueueSize = 0;
158 }
159
160
161
162
163
164 public Long getAgeOfLastShippedOp() {
165 return singleSourceSource.getLastShippedAge();
166 }
167
168
169
170
171
172 public int getSizeOfLogQueue() {
173 return this.lastQueueSize;
174 }
175
176
177
178
179
180 public long getTimeStampOfLastShippedOp() {
181 long lastTimestamp = 0L;
182 for (long ts : lastTimeStamps.values()) {
183 if (ts > lastTimestamp) {
184 lastTimestamp = ts;
185 }
186 }
187 return lastTimestamp;
188 }
189
190
191
192
193
194 public String getPeerID() {
195 return id;
196 }
197
198 public void incrUnknownFileLengthForClosedWAL() {
199 singleSourceSource.incrUnknownFileLengthForClosedWAL();
200 globalSourceSource.incrUnknownFileLengthForClosedWAL();
201 }
202
203 public void incrUncleanlyClosedWALs() {
204 singleSourceSource.incrUncleanlyClosedWALs();
205 globalSourceSource.incrUncleanlyClosedWALs();
206 }
207
208 public void incrBytesSkippedInUncleanlyClosedWALs(final long bytes) {
209 singleSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
210 globalSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
211 }
212
213 public void incrRestartedWALReading() {
214 singleSourceSource.incrRestartedWALReading();
215 globalSourceSource.incrRestartedWALReading();
216 }
217
218 public void incrRepeatedFileBytes(final long bytes) {
219 singleSourceSource.incrRepeatedFileBytes(bytes);
220 globalSourceSource.incrRepeatedFileBytes(bytes);
221 }
222
223 public void incrCompletedWAL() {
224 singleSourceSource.incrCompletedWAL();
225 globalSourceSource.incrCompletedWAL();
226 }
227
228 public void incrCompletedRecoveryQueue() {
229 singleSourceSource.incrCompletedRecoveryQueue();
230 globalSourceSource.incrCompletedRecoveryQueue();
231 }
232 }