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.hadoop.hbase.replication.regionserver;
19  
20  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
21  import org.apache.hadoop.metrics2.lib.MutableFastCounter;
22  import org.apache.hadoop.metrics2.lib.MutableHistogram;
23  
24  /**
25   * This is the metric source for table level replication metrics.
26   * We can easy monitor some useful table level replication metrics such as
27   * ageOfLastShippedOp and shippedBytes
28   */
29  public class MetricsReplicationTableSourceImpl implements MetricsReplicationTableSource {
30  
31    private final MetricsReplicationSourceImpl rms;
32    private final String tableName;
33    private final String ageOfLastShippedOpKey;
34    private String keyPrefix;
35  
36    private final String shippedBytesKey;
37  
38    private final MutableHistogram ageOfLastShippedOpHist;
39    private final MutableFastCounter shippedBytesCounter;
40  
41    public MetricsReplicationTableSourceImpl(MetricsReplicationSourceImpl rms, String tableName) {
42      this.rms = rms;
43      this.tableName = tableName;
44      this.keyPrefix = "source." + this.tableName + ".";
45  
46      ageOfLastShippedOpKey = this.keyPrefix + "ageOfLastShippedOp";
47      ageOfLastShippedOpHist = rms.getMetricsRegistry().getHistogram(ageOfLastShippedOpKey);
48  
49      shippedBytesKey = this.keyPrefix + "shippedBytes";
50      shippedBytesCounter = rms.getMetricsRegistry().getCounter(shippedBytesKey, 0L);
51    }
52  
53    @Override
54    public void setLastShippedAge(long age) {
55      ageOfLastShippedOpHist.add(age);
56    }
57  
58    @Override
59    public void incrShippedBytes(long size) {
60      shippedBytesCounter.incr(size);
61    }
62  
63    @Override
64    public void clear() {
65      rms.removeMetric(ageOfLastShippedOpKey);
66      rms.removeMetric(shippedBytesKey);
67    }
68  
69    @Override
70    public long getLastShippedAge() {
71      return ageOfLastShippedOpHist.getMax();
72    }
73  
74    @Override
75    public long getShippedBytes() {
76      return shippedBytesCounter.value();
77    }
78  
79    @Override
80    public void init() {
81      rms.init();
82    }
83  
84    @Override
85    public void setGauge(String gaugeName, long value) {
86      rms.setGauge(this.keyPrefix + gaugeName, value);
87    }
88  
89    @Override
90    public void incGauge(String gaugeName, long delta) {
91      rms.incGauge(this.keyPrefix + gaugeName, delta);
92    }
93  
94    @Override
95    public void decGauge(String gaugeName, long delta) {
96      rms.decGauge(this.keyPrefix + gaugeName, delta);
97    }
98  
99    @Override
100   public void removeMetric(String key) {
101     rms.removeMetric(this.keyPrefix + key);
102   }
103 
104   @Override
105   public void incCounters(String counterName, long delta) {
106     rms.incCounters(this.keyPrefix + counterName, delta);
107   }
108 
109   @Override
110   public void updateHistogram(String name, long value) {
111     rms.updateHistogram(this.keyPrefix + name, value);
112   }
113 
114   @Override
115   public String getMetricsContext() {
116     return rms.getMetricsContext();
117   }
118 
119   @Override
120   public String getMetricsDescription() {
121     return rms.getMetricsDescription();
122   }
123 
124   @Override
125   public String getMetricsJmxContext() {
126     return rms.getMetricsJmxContext();
127   }
128 
129   @Override
130   public String getMetricsName() {
131     return rms.getMetricsName();
132   }
133 
134   @Override
135   public MetricRegistryInfo getMetricRegistryInfo() {
136     return rms.getMetricRegistryInfo();
137   }
138 }