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.hbase.CompatibilitySingletonFactory;
021import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * This class is for maintaining the various replication statistics for a sink and publishing them
026 * through the metrics interfaces.
027 */
028@InterfaceAudience.Private
029public class MetricsSink {
030
031  private long lastTimestampForAge = EnvironmentEdgeManager.currentTime();
032  private long startTimestamp = EnvironmentEdgeManager.currentTime();
033  private final MetricsReplicationSinkSource mss;
034
035  public MetricsSink() {
036    mss =
037      CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class).getSink();
038  }
039
040  /**
041   * Set the age of the last applied operation
042   * @param timestamp The timestamp of the last operation applied.
043   * @return the age that was set
044   */
045  public long setAgeOfLastAppliedOp(long timestamp) {
046    long age = 0;
047    if (lastTimestampForAge != timestamp) {
048      lastTimestampForAge = timestamp;
049      age = EnvironmentEdgeManager.currentTime() - lastTimestampForAge;
050    }
051    mss.setLastAppliedOpAge(age);
052    return age;
053  }
054
055  /**
056   * Refreshing the age makes sure the value returned is the actual one and not the one set a
057   * replication time
058   * @return refreshed age
059   */
060  public long refreshAgeOfLastAppliedOp() {
061    return setAgeOfLastAppliedOp(lastTimestampForAge);
062  }
063
064  /**
065   * Convience method to change metrics when a batch of operations are applied.
066   */
067  public void applyBatch(long batchSize) {
068    mss.incrAppliedBatches(1);
069    mss.incrAppliedOps(batchSize);
070  }
071
072  /**
073   * Convience method to change metrics when a batch of operations are applied.
074   * @param batchSize total number of mutations that are applied/replicated
075   * @param hfileSize total number of hfiles that are applied/replicated
076   */
077  public void applyBatch(long batchSize, long hfileSize) {
078    applyBatch(batchSize);
079    mss.incrAppliedHFiles(hfileSize);
080  }
081
082  /**
083   * Convenience method to update metrics when batch of operations has failed.
084   */
085  public void incrementFailedBatches() {
086    mss.incrFailedBatches();
087  }
088
089  /**
090   * Get the count of the failed bathes
091   */
092  protected long getFailedBatches() {
093    return mss.getFailedBatches();
094  }
095
096  /**
097   * Get the Age of Last Applied Op
098   */
099  public long getAgeOfLastAppliedOp() {
100    return mss.getLastAppliedOpAge();
101  }
102
103  /**
104   * Get the TimeStampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
105   * at which hbase instance starts
106   * @return timeStampsOfLastAppliedOp;
107   * @deprecated Since 2.0.0. Will be removed in 3.0.0.
108   * @see #getTimestampOfLastAppliedOp()
109   */
110  @Deprecated
111  public long getTimeStampOfLastAppliedOp() {
112    return getTimestampOfLastAppliedOp();
113  }
114
115  /**
116   * Get the TimestampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
117   * at which hbase instance starts
118   * @return timeStampsOfLastAppliedOp;
119   */
120  public long getTimestampOfLastAppliedOp() {
121    return this.lastTimestampForAge;
122  }
123
124  /**
125   * Gets the time stamp from when the Sink was initialized.
126   */
127  public long getStartTimestamp() {
128    return this.startTimestamp;
129  }
130
131  /**
132   * Gets the total number of OPs delivered to this sink.
133   */
134  public long getAppliedOps() {
135    return this.mss.getSinkAppliedOps();
136  }
137
138}