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 */
018
019package org.apache.hadoop.hbase.replication.regionserver;
020
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
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 = System.currentTimeMillis();
032  private final MetricsReplicationSinkSource mss;
033
034  public MetricsSink() {
035    mss =
036        CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class).getSink();
037  }
038
039  /**
040   * Set the age of the last applied operation
041   *
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 = System.currentTimeMillis() - 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
057   * not the one set a 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   * @param batchSize
068   */
069  public void applyBatch(long batchSize) {
070    mss.incrAppliedBatches(1);
071    mss.incrAppliedOps(batchSize);
072  }
073
074  /**
075   * Convience method to change metrics when a batch of operations are applied.
076   *
077   * @param batchSize total number of mutations that are applied/replicated
078   * @param hfileSize total number of hfiles that are applied/replicated
079   */
080  public void applyBatch(long batchSize, long hfileSize) {
081    applyBatch(batchSize);
082    mss.incrAppliedHFiles(hfileSize);
083  }
084
085  /**
086   * Get the Age of Last Applied Op
087   * @return ageOfLastAppliedOp
088   */
089  public long getAgeOfLastAppliedOp() {
090    return mss.getLastAppliedOpAge();
091  }
092
093  /**
094   * Get the TimeStampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
095   * at which hbase instance starts
096   * @return timeStampsOfLastAppliedOp;
097   * @deprecated Since 2.0.0. Will be removed in 3.0.0.
098   * @see #getTimestampOfLastAppliedOp()
099   */
100  @Deprecated
101  public long getTimeStampOfLastAppliedOp() {
102    return getTimestampOfLastAppliedOp();
103  }
104
105  /**
106   * Get the TimestampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
107   * at which hbase instance starts
108   * @return timeStampsOfLastAppliedOp;
109   */
110  public long getTimestampOfLastAppliedOp() {
111    return this.lastTimestampForAge;
112  }
113}