View Javadoc

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    public static final String SINK_AGE_OF_LAST_APPLIED_OP =
32      MetricsReplicationSinkSource.SINK_AGE_OF_LAST_APPLIED_OP;
33    public static final String SINK_APPLIED_BATCHES =
34      MetricsReplicationSinkSource.SINK_APPLIED_BATCHES;
35    public static final String SINK_APPLIED_OPS =
36      MetricsReplicationSinkSource.SINK_APPLIED_OPS;
37  
38    private long lastTimestampForAge = System.currentTimeMillis();
39    private final MetricsReplicationSinkSource mss;
40  
41    public MetricsSink() {
42      mss = CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
43        .getSink();
44    }
45  
46    /**
47     * Set the age of the last applied operation
48     *
49     * @param timestamp The timestamp of the last operation applied.
50     * @return the age that was set
51     */
52    public long setAgeOfLastAppliedOp(long timestamp) {
53      long age = 0;
54      if (lastTimestampForAge != timestamp) {
55        lastTimestampForAge = timestamp;
56        age = System.currentTimeMillis() - lastTimestampForAge;
57      } 
58      mss.setLastAppliedOpAge(age);
59      return age;
60    }
61  
62    /**
63     * Refreshing the age makes sure the value returned is the actual one and
64     * not the one set a replication time
65     * @return refreshed age
66     */
67    public long refreshAgeOfLastAppliedOp() {
68      return setAgeOfLastAppliedOp(lastTimestampForAge);
69    }
70  
71    /**
72     * Convience method to change metrics when a batch of operations are applied.
73     *
74     * @param batchSize
75     */
76    public void applyBatch(long batchSize) {
77      mss.incrAppliedBatches(1);
78      mss.incrAppliedOps(batchSize);
79    }
80  
81    /**
82     * Get the Age of Last Applied Op
83     * @return ageOfLastAppliedOp
84     */
85    public long getAgeOfLastAppliedOp() {
86      return mss.getLastAppliedOpAge();
87    }
88  
89    /**
90     * Get the TimeStampOfLastAppliedOp. If no replication Op applied yet, the value is the timestamp
91     * at which hbase instance starts
92     * @return timeStampsOfLastAppliedOp;
93     */
94    public long getTimeStampOfLastAppliedOp() {
95      return this.lastTimestampForAge;
96    }
97  
98  }