001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication.regionserver;
019
020import org.apache.hadoop.metrics2.lib.MutableFastCounter;
021import org.apache.hadoop.metrics2.lib.MutableHistogram;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * This is the metric source for table level replication metrics. We can easy monitor some useful
026 * table level replication metrics such as ageOfLastShippedOp and shippedBytes
027 */
028@InterfaceAudience.Private
029public class MetricsReplicationTableSourceImpl implements MetricsReplicationTableSource {
030
031  private final MetricsReplicationSourceImpl rms;
032  private final String tableName;
033  private final String ageOfLastShippedOpKey;
034  private String keyPrefix;
035
036  private final String shippedBytesKey;
037
038  private final MutableHistogram ageOfLastShippedOpHist;
039  private final MutableFastCounter shippedBytesCounter;
040
041  public MetricsReplicationTableSourceImpl(MetricsReplicationSourceImpl rms, String tableName) {
042    this.rms = rms;
043    this.tableName = tableName;
044    this.keyPrefix = "source." + this.tableName + ".";
045
046    ageOfLastShippedOpKey = this.keyPrefix + "ageOfLastShippedOp";
047    ageOfLastShippedOpHist = rms.getMetricsRegistry().newTimeHistogram(ageOfLastShippedOpKey);
048
049    shippedBytesKey = this.keyPrefix + "shippedBytes";
050    shippedBytesCounter = rms.getMetricsRegistry().getCounter(shippedBytesKey, 0L);
051  }
052
053  @Override
054  public void setLastShippedAge(long age) {
055    ageOfLastShippedOpHist.add(age);
056  }
057
058  @Override
059  public void incrShippedBytes(long size) {
060    shippedBytesCounter.incr(size);
061  }
062
063  @Override
064  public void clear() {
065    rms.removeMetric(ageOfLastShippedOpKey);
066    rms.removeMetric(shippedBytesKey);
067  }
068
069  @Override
070  public long getLastShippedAge() {
071    return ageOfLastShippedOpHist.getMax();
072  }
073
074  @Override
075  public long getShippedBytes() {
076    return shippedBytesCounter.value();
077  }
078
079  @Override
080  public void init() {
081    rms.init();
082  }
083
084  @Override
085  public void setGauge(String gaugeName, long value) {
086    rms.setGauge(this.keyPrefix + gaugeName, value);
087  }
088
089  @Override
090  public void incGauge(String gaugeName, long delta) {
091    rms.incGauge(this.keyPrefix + gaugeName, delta);
092  }
093
094  @Override
095  public void decGauge(String gaugeName, long delta) {
096    rms.decGauge(this.keyPrefix + gaugeName, delta);
097  }
098
099  @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}