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.
026 * We can easy monitor some useful table level replication metrics such as
027 * ageOfLastShippedOp and shippedBytes
028 */
029@InterfaceAudience.Private
030public class MetricsReplicationTableSourceImpl implements MetricsReplicationTableSource {
031
032  private final MetricsReplicationSourceImpl rms;
033  private final String tableName;
034  private final String ageOfLastShippedOpKey;
035  private String keyPrefix;
036
037  private final String shippedBytesKey;
038
039  private final MutableHistogram ageOfLastShippedOpHist;
040  private final MutableFastCounter shippedBytesCounter;
041
042  public MetricsReplicationTableSourceImpl(MetricsReplicationSourceImpl rms, String tableName) {
043    this.rms = rms;
044    this.tableName = tableName;
045    this.keyPrefix = "source." + this.tableName + ".";
046
047    ageOfLastShippedOpKey = this.keyPrefix + "ageOfLastShippedOp";
048    ageOfLastShippedOpHist = rms.getMetricsRegistry().getHistogram(ageOfLastShippedOpKey);
049
050    shippedBytesKey = this.keyPrefix + "shippedBytes";
051    shippedBytesCounter = rms.getMetricsRegistry().getCounter(shippedBytesKey, 0L);
052  }
053
054  @Override
055  public void setLastShippedAge(long age) {
056    ageOfLastShippedOpHist.add(age);
057  }
058
059  @Override
060  public void incrShippedBytes(long size) {
061    shippedBytesCounter.incr(size);
062  }
063
064  @Override
065  public void clear() {
066    rms.removeMetric(ageOfLastShippedOpKey);
067    rms.removeMetric(shippedBytesKey);
068  }
069
070  @Override
071  public long getLastShippedAge() {
072    return ageOfLastShippedOpHist.getMax();
073  }
074
075  @Override
076  public long getShippedBytes() {
077    return shippedBytesCounter.value();
078  }
079
080  @Override
081  public void init() {
082    rms.init();
083  }
084
085  @Override
086  public void setGauge(String gaugeName, long value) {
087    rms.setGauge(this.keyPrefix + gaugeName, value);
088  }
089
090  @Override
091  public void incGauge(String gaugeName, long delta) {
092    rms.incGauge(this.keyPrefix + gaugeName, delta);
093  }
094
095  @Override
096  public void decGauge(String gaugeName, long delta) {
097    rms.decGauge(this.keyPrefix + gaugeName, delta);
098  }
099
100  @Override
101  public void removeMetric(String key) {
102    rms.removeMetric(this.keyPrefix + key);
103  }
104
105  @Override
106  public void incCounters(String counterName, long delta) {
107    rms.incCounters(this.keyPrefix + counterName, delta);
108  }
109
110  @Override
111  public void updateHistogram(String name, long value) {
112    rms.updateHistogram(this.keyPrefix + name, value);
113  }
114
115  @Override
116  public String getMetricsContext() {
117    return rms.getMetricsContext();
118  }
119
120  @Override
121  public String getMetricsDescription() {
122    return rms.getMetricsDescription();
123  }
124
125  @Override
126  public String getMetricsJmxContext() {
127    return rms.getMetricsJmxContext();
128  }
129
130  @Override
131  public String getMetricsName() {
132    return rms.getMetricsName();
133  }
134}