View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.replication.regionserver;
21  import org.apache.hadoop.hbase.metrics.MetricsRate;
22  import org.apache.hadoop.metrics.MetricsContext;
23  import org.apache.hadoop.metrics.MetricsRecord;
24  import org.apache.hadoop.metrics.MetricsUtil;
25  import org.apache.hadoop.metrics.Updater;
26  import org.apache.hadoop.metrics.jvm.JvmMetrics;
27  import org.apache.hadoop.metrics.util.MetricsIntValue;
28  import org.apache.hadoop.metrics.util.MetricsLongValue;
29  import org.apache.hadoop.metrics.util.MetricsRegistry;
30  
31  /**
32   * This class is for maintaining the various replication statistics
33   * for a sink and publishing them through the metrics interfaces.
34   */
35  public class ReplicationSinkMetrics implements Updater {
36    private final MetricsRecord metricsRecord;
37    private MetricsRegistry registry = new MetricsRegistry();
38    private static ReplicationSinkMetrics instance;
39  
40    /** Rate of operations applied by the sink */
41    public final MetricsRate appliedOpsRate =
42        new MetricsRate("appliedOpsRate", registry);
43  
44    /** Rate of batches (of operations) applied by the sink */
45    public final MetricsRate appliedBatchesRate =
46        new MetricsRate("appliedBatchesRate", registry);
47  
48    /** Age of the last operation that was applied by the sink */
49    private final MetricsLongValue ageOfLastAppliedOp =
50        new MetricsLongValue("ageOfLastAppliedOp", registry);
51  
52    /**
53     * Constructor used to register the metrics
54     */
55    public ReplicationSinkMetrics() {
56      MetricsContext context = MetricsUtil.getContext("hbase");
57      String name = Thread.currentThread().getName();
58      metricsRecord = MetricsUtil.createRecord(context, "replication");
59      metricsRecord.setTag("RegionServer", name);
60      context.registerUpdater(this);
61      // export for JMX
62      new ReplicationStatistics(this.registry, "ReplicationSink");
63    }
64  
65    /**
66     * Set the age of the last edit that was applied
67     * @param timestamp write time of the edit
68     */
69    public void setAgeOfLastAppliedOp(long timestamp) {
70      ageOfLastAppliedOp.set(System.currentTimeMillis() - timestamp);
71    }
72    @Override
73    public void doUpdates(MetricsContext metricsContext) {
74      synchronized (this) {
75        this.appliedOpsRate.pushMetric(this.metricsRecord);
76        this.appliedBatchesRate.pushMetric(this.metricsRecord);
77        this.ageOfLastAppliedOp.pushMetric(this.metricsRecord);
78      }
79      this.metricsRecord.update();
80    }
81  }