1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
23
24 /**
25 * This class is for maintaining the various replication statistics for a sink and publishing them
26 * through the metrics interfaces.
27 */
28 @InterfaceAudience.Private
29 public class MetricsSink {
30
31 private long lastTimestampForAge = System.currentTimeMillis();
32 private final MetricsReplicationSinkSource mss;
33
34 public MetricsSink() {
35 mss =
36 CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class).getSink();
37 }
38
39 /**
40 * Set the age of the last applied operation
41 *
42 * @param timestamp The timestamp of the last operation applied.
43 * @return the age that was set
44 */
45 public long setAgeOfLastAppliedOp(long timestamp) {
46 long age = 0;
47 if (lastTimestampForAge != timestamp) {
48 lastTimestampForAge = timestamp;
49 age = System.currentTimeMillis() - lastTimestampForAge;
50 }
51 mss.setLastAppliedOpAge(age);
52 return age;
53 }
54
55 /**
56 * Refreshing the age makes sure the value returned is the actual one and
57 * not the one set a replication time
58 * @return refreshed age
59 */
60 public long refreshAgeOfLastAppliedOp() {
61 return setAgeOfLastAppliedOp(lastTimestampForAge);
62 }
63
64 /**
65 * Convience method to change metrics when a batch of operations are applied.
66 *
67 * @param batchSize
68 */
69 public void applyBatch(long batchSize) {
70 mss.incrAppliedBatches(1);
71 mss.incrAppliedOps(batchSize);
72 }
73
74 /**
75 * Convience method to change metrics when a batch of operations are applied.
76 *
77 * @param batchSize total number of mutations that are applied/replicated
78 * @param hfileSize total number of hfiles that are applied/replicated
79 */
80 public void applyBatch(long batchSize, long hfileSize) {
81 applyBatch(batchSize);
82 mss.incrAppliedHFiles(hfileSize);
83 }
84
85 /**
86 * Get the Age of Last Applied Op
87 * @return ageOfLastAppliedOp
88 */
89 public long getAgeOfLastAppliedOp() {
90 return mss.getLastAppliedOpAge();
91 }
92
93 /**
94 * Get the TimeStampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
95 * at which hbase instance starts
96 * @return timeStampsOfLastAppliedOp;
97 */
98 public long getTimeStampOfLastAppliedOp() {
99 return this.lastTimestampForAge;
100 }
101
102 }