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  
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.metrics.BaseSource;
30  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
31  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
32  
33  /**
34   * This class is for maintaining the various replication statistics for a source and publishing them
35   * through the metrics interfaces.
36   */
37  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
38  public class MetricsSource implements BaseSource {
39  
40    private static final Log LOG = LogFactory.getLog(MetricsSource.class);
41  
42    // tracks last shipped timestamp for each wal group
43    private Map<String, Long> lastTimeStamps = new HashMap<String, Long>();
44    private long lastHFileRefsQueueSize = 0;
45    private String id;
46  
47    private final MetricsReplicationSourceSource singleSourceSource;
48    private final MetricsReplicationSourceSource globalSourceSource;
49    private Map<String, MetricsReplicationSourceSource> singleSourceSourceByTable;
50  
51    /**
52     * Constructor used to register the metrics
53     *
54     * @param id Name of the source this class is monitoring
55     */
56    public MetricsSource(String id) {
57      this.id = id;
58      singleSourceSource =
59          CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
60              .getSource(id);
61      globalSourceSource = CompatibilitySingletonFactory
62          .getInstance(MetricsReplicationSourceFactory.class).getGlobalSource();
63      singleSourceSourceByTable = new HashMap<>();
64    }
65  
66    /**
67     * Constructor for injecting custom (or test) MetricsReplicationSourceSources
68     * @param id Name of the source this class is monitoring
69     * @param singleSourceSource Class to monitor id-scoped metrics
70     * @param globalSourceSource Class to monitor global-scoped metrics
71     */
72    public MetricsSource(String id, MetricsReplicationSourceSource singleSourceSource,
73                         MetricsReplicationSourceSource globalSourceSource,
74                         Map<String, MetricsReplicationSourceSource> singleSourceSourceByTable) {
75      this.id = id;
76      this.singleSourceSource = singleSourceSource;
77      this.globalSourceSource = globalSourceSource;
78      this.singleSourceSourceByTable = singleSourceSourceByTable;
79    }
80  
81    /**
82     * Set the age of the last edit that was shipped
83     * @param timestamp write time of the edit
84     * @param walGroup which group we are setting
85     */
86    public void setAgeOfLastShippedOp(long timestamp, String walGroup) {
87      long age = EnvironmentEdgeManager.currentTime() - timestamp;
88      singleSourceSource.setLastShippedAge(age);
89      globalSourceSource.setLastShippedAge(age);
90      this.lastTimeStamps.put(walGroup, timestamp);
91    }
92  
93    /**
94     * Set the age of the last edit that was shipped group by table
95     * @param timestamp write time of the edit
96     * @param tableName String as group and tableName
97     */
98    public void setAgeOfLastShippedOpByTable(long timestamp, String tableName) {
99      long age = EnvironmentEdgeManager.currentTime() - timestamp;
100     if (!this.getSingleSourceSourceByTable().containsKey(tableName)) {
101       this.getSingleSourceSourceByTable().put(tableName,
102           CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
103           .getSource(tableName));
104     }
105     this.singleSourceSourceByTable.get(tableName).setLastShippedAge(age);
106   }
107   /**
108    * Convenience method to use the last given timestamp to refresh the age of the last edit. Used
109    * when replication fails and need to keep that metric accurate.
110    * @param walGroupId id of the group to update
111    */
112   public void refreshAgeOfLastShippedOp(String walGroupId) {
113     Long lastTimestamp = this.lastTimeStamps.get(walGroupId);
114     if (lastTimestamp == null) {
115       this.lastTimeStamps.put(walGroupId, 0L);
116       lastTimestamp = 0L;
117     }
118     if (lastTimestamp > 0) {
119       setAgeOfLastShippedOp(lastTimestamp, walGroupId);
120     }
121   }
122 
123   /**
124    * Increment size of the log queue.
125    */
126   public void incrSizeOfLogQueue() {
127     singleSourceSource.incrSizeOfLogQueue(1);
128     globalSourceSource.incrSizeOfLogQueue(1);
129   }
130 
131   public void decrSizeOfLogQueue() {
132     singleSourceSource.decrSizeOfLogQueue(1);
133     globalSourceSource.decrSizeOfLogQueue(1);
134   }
135 
136   /**
137    * Add on the the number of log edits read
138    *
139    * @param delta the number of log edits read.
140    */
141   private void incrLogEditsRead(long delta) {
142     singleSourceSource.incrLogReadInEdits(delta);
143     globalSourceSource.incrLogReadInEdits(delta);
144   }
145 
146   /** Increment the number of log edits read by one. */
147   public void incrLogEditsRead() {
148     incrLogEditsRead(1);
149   }
150 
151   /**
152    * Add on the number of log edits filtered
153    *
154    * @param delta the number filtered.
155    */
156   public void incrLogEditsFiltered(long delta) {
157     singleSourceSource.incrLogEditsFiltered(delta);
158     globalSourceSource.incrLogEditsFiltered(delta);
159   }
160 
161   /** The number of log edits filtered out. */
162   public void incrLogEditsFiltered() {
163     incrLogEditsFiltered(1);
164   }
165 
166   /**
167    * Convience method to apply changes to metrics do to shipping a batch of logs.
168    *
169    * @param batchSize the size of the batch that was shipped to sinks.
170    */
171   public void shipBatch(long batchSize, int sizeInBytes) {
172     singleSourceSource.incrBatchesShipped(1);
173     globalSourceSource.incrBatchesShipped(1);
174 
175     singleSourceSource.incrOpsShipped(batchSize);
176     globalSourceSource.incrOpsShipped(batchSize);
177 
178     singleSourceSource.incrShippedBytes(sizeInBytes);
179     globalSourceSource.incrShippedBytes(sizeInBytes);
180   }
181 
182   /**
183    * Convience method to apply changes to metrics do to shipping a batch of logs.
184    *
185    * @param batchSize the size of the batch that was shipped to sinks.
186    * @param hfiles total number of hfiles shipped to sinks.
187    */
188   public void shipBatch(long batchSize, int sizeInBytes, long hfiles) {
189     shipBatch(batchSize, sizeInBytes);
190     singleSourceSource.incrHFilesShipped(hfiles);
191     globalSourceSource.incrHFilesShipped(hfiles);
192   }
193 
194   /** increase the byte number read by source from log file */
195   public void incrLogReadInBytes(long readInBytes) {
196     singleSourceSource.incrLogReadInBytes(readInBytes);
197     globalSourceSource.incrLogReadInBytes(readInBytes);
198   }
199 
200   /** Removes all metrics about this Source. */
201   public void clear() {
202     int lastQueueSize = singleSourceSource.getSizeOfLogQueue();
203     globalSourceSource.decrSizeOfLogQueue(lastQueueSize);
204     singleSourceSource.decrSizeOfLogQueue(lastQueueSize);
205     singleSourceSource.clear();
206     globalSourceSource.decrSizeOfHFileRefsQueue(lastHFileRefsQueueSize);
207     lastTimeStamps.clear();
208     lastHFileRefsQueueSize = 0;
209   }
210 
211   /**
212    * Get AgeOfLastShippedOp
213    * @return AgeOfLastShippedOp
214    */
215   public Long getAgeOfLastShippedOp() {
216     return singleSourceSource.getLastShippedAge();
217   }
218 
219   /**
220    * Get the sizeOfLogQueue
221    * @return sizeOfLogQueue
222    */
223   public int getSizeOfLogQueue() {
224     return singleSourceSource.getSizeOfLogQueue();
225   }
226 
227   /**
228    * Get the timeStampsOfLastShippedOp, if there are multiple groups, return the latest one
229    * @return lastTimestampForAge
230    */
231   public long getTimeStampOfLastShippedOp() {
232     long lastTimestamp = 0L;
233     for (long ts : lastTimeStamps.values()) {
234       if (ts > lastTimestamp) {
235         lastTimestamp = ts;
236       }
237     }
238     return lastTimestamp;
239   }
240 
241   /**
242    * Get the slave peer ID
243    * @return peerID
244    */
245   public String getPeerID() {
246     return id;
247   }
248 
249   public void incrSizeOfHFileRefsQueue(long size) {
250     singleSourceSource.incrSizeOfHFileRefsQueue(size);
251     globalSourceSource.incrSizeOfHFileRefsQueue(size);
252     lastHFileRefsQueueSize = size;
253   }
254 
255   public void decrSizeOfHFileRefsQueue(int size) {
256     singleSourceSource.decrSizeOfHFileRefsQueue(size);
257     globalSourceSource.decrSizeOfHFileRefsQueue(size);
258     lastHFileRefsQueueSize -= size;
259     if (lastHFileRefsQueueSize < 0) {
260       lastHFileRefsQueueSize = 0;
261     }
262   }
263 
264   public void incrUnknownFileLengthForClosedWAL() {
265     singleSourceSource.incrUnknownFileLengthForClosedWAL();
266     globalSourceSource.incrUnknownFileLengthForClosedWAL();
267   }
268 
269   public void incrUncleanlyClosedWALs() {
270     singleSourceSource.incrUncleanlyClosedWALs();
271     globalSourceSource.incrUncleanlyClosedWALs();
272   }
273 
274   public void incrBytesSkippedInUncleanlyClosedWALs(final long bytes) {
275     singleSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
276     globalSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
277   }
278 
279   public void incrRestartedWALReading() {
280     singleSourceSource.incrRestartedWALReading();
281     globalSourceSource.incrRestartedWALReading();
282   }
283 
284   public void incrRepeatedFileBytes(final long bytes) {
285     singleSourceSource.incrRepeatedFileBytes(bytes);
286     globalSourceSource.incrRepeatedFileBytes(bytes);
287   }
288 
289   public void incrCompletedWAL() {
290     singleSourceSource.incrCompletedWAL();
291     globalSourceSource.incrCompletedWAL();
292   }
293 
294   public void incrCompletedRecoveryQueue() {
295     singleSourceSource.incrCompletedRecoveryQueue();
296     globalSourceSource.incrCompletedRecoveryQueue();
297   }
298 
299   public void incrFailedRecoveryQueue() {
300     globalSourceSource.incrFailedRecoveryQueue();
301   }
302 
303 
304   @Override
305   public void init() {
306     singleSourceSource.init();
307     globalSourceSource.init();
308   }
309 
310   @Override
311   public void setGauge(String gaugeName, long value) {
312     singleSourceSource.setGauge(gaugeName, value);
313     globalSourceSource.setGauge(gaugeName, value);
314   }
315 
316   @Override
317   public void incGauge(String gaugeName, long delta) {
318     singleSourceSource.incGauge(gaugeName, delta);
319     globalSourceSource.incGauge(gaugeName, delta);
320   }
321 
322   @Override
323   public void decGauge(String gaugeName, long delta) {
324     singleSourceSource.decGauge(gaugeName, delta);
325     globalSourceSource.decGauge(gaugeName, delta);
326   }
327 
328   @Override
329   public void removeMetric(String key) {
330     singleSourceSource.removeMetric(key);
331     globalSourceSource.removeMetric(key);
332   }
333 
334   @Override
335   public void incCounters(String counterName, long delta) {
336     singleSourceSource.incCounters(counterName, delta);
337     globalSourceSource.incCounters(counterName, delta);
338   }
339 
340   @Override
341   public void updateHistogram(String name, long value) {
342     singleSourceSource.updateHistogram(name, value);
343     globalSourceSource.updateHistogram(name, value);
344   }
345 
346   @Override
347   public String getMetricsContext() {
348     return globalSourceSource.getMetricsContext();
349   }
350 
351   @Override
352   public String getMetricsDescription() {
353     return globalSourceSource.getMetricsDescription();
354   }
355 
356   @Override
357   public String getMetricsJmxContext() {
358     return globalSourceSource.getMetricsJmxContext();
359   }
360 
361   @Override
362   public String getMetricsName() {
363     return globalSourceSource.getMetricsName();
364   }
365 
366   public Map<String, MetricsReplicationSourceSource> getSingleSourceSourceByTable() {
367     return singleSourceSourceByTable;
368   }
369 
370   @Override
371   public MetricRegistryInfo getMetricRegistryInfo() {
372     return new MetricRegistryInfo(getMetricsName(), getMetricsDescription(),
373       getMetricsContext(), getMetricsJmxContext(), true);
374   }
375 }