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 long startTimestamp = System.currentTimeMillis(); 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 * 043 * @param timestamp The timestamp of the last operation applied. 044 * @return the age that was set 045 */ 046 public long setAgeOfLastAppliedOp(long timestamp) { 047 long age = 0; 048 if (lastTimestampForAge != timestamp) { 049 lastTimestampForAge = timestamp; 050 age = System.currentTimeMillis() - lastTimestampForAge; 051 } 052 mss.setLastAppliedOpAge(age); 053 return age; 054 } 055 056 /** 057 * Refreshing the age makes sure the value returned is the actual one and 058 * not the one set a replication time 059 * @return refreshed age 060 */ 061 public long refreshAgeOfLastAppliedOp() { 062 return setAgeOfLastAppliedOp(lastTimestampForAge); 063 } 064 065 /** 066 * Convience method to change metrics when a batch of operations are applied. 067 * 068 * @param batchSize 069 */ 070 public void applyBatch(long batchSize) { 071 mss.incrAppliedBatches(1); 072 mss.incrAppliedOps(batchSize); 073 } 074 075 /** 076 * Convience method to change metrics when a batch of operations are applied. 077 * 078 * @param batchSize total number of mutations that are applied/replicated 079 * @param hfileSize total number of hfiles that are applied/replicated 080 */ 081 public void applyBatch(long batchSize, long hfileSize) { 082 applyBatch(batchSize); 083 mss.incrAppliedHFiles(hfileSize); 084 } 085 086 /** 087 * Get the Age of Last Applied Op 088 * @return ageOfLastAppliedOp 089 */ 090 public long getAgeOfLastAppliedOp() { 091 return mss.getLastAppliedOpAge(); 092 } 093 094 /** 095 * Get the TimeStampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp 096 * at which hbase instance starts 097 * @return timeStampsOfLastAppliedOp; 098 * @deprecated Since 2.0.0. Will be removed in 3.0.0. 099 * @see #getTimestampOfLastAppliedOp() 100 */ 101 @Deprecated 102 public long getTimeStampOfLastAppliedOp() { 103 return getTimestampOfLastAppliedOp(); 104 } 105 106 /** 107 * Get the TimestampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp 108 * at which hbase instance starts 109 * @return timeStampsOfLastAppliedOp; 110 */ 111 public long getTimestampOfLastAppliedOp() { 112 return this.lastTimestampForAge; 113 } 114 115 /** 116 * Gets the time stamp from when the Sink was initialized. 117 * @return startTimestamp 118 */ 119 public long getStartTimestamp() { 120 return this.startTimestamp; 121 } 122 123 /** 124 * Gets the total number of OPs delivered to this sink. 125 * @return totalAplliedOps 126 */ 127 public long getAppliedOps() { 128 return this.mss.getSinkAppliedOps(); 129 } 130 131}