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 source and publishing them through the metrics interfaces.
34   */
35  public class ReplicationSourceMetrics implements Updater {
36    private final MetricsRecord metricsRecord;
37    private MetricsRegistry registry = new MetricsRegistry();
38  
39    /** Rate of shipped operations by the source */
40    public final MetricsRate shippedOpsRate =
41        new MetricsRate("shippedOpsRate", registry);
42  
43    /** Rate of shipped batches by the source */
44    public final MetricsRate shippedBatchesRate =
45        new MetricsRate("shippedBatchesRate", registry);
46  
47    /** Rate of log entries (can be multiple Puts) read from the logs */
48    public final MetricsRate logEditsReadRate =
49        new MetricsRate("logEditsReadRate", registry);
50  
51    /** Rate of log entries filtered by the source */
52    public final MetricsRate logEditsFilteredRate =
53        new MetricsRate("logEditsFilteredRate", registry);
54  
55    /** Age of the last operation that was shipped by the source */
56    private final MetricsLongValue ageOfLastShippedOp =
57        new MetricsLongValue("ageOfLastShippedOp", registry);
58  
59    /**
60     * Current size of the queue of logs to replicate,
61     * excluding the one being processed at the moment
62     */
63    public final MetricsIntValue sizeOfLogQueue =
64        new MetricsIntValue("sizeOfLogQueue", registry);
65  
66    /**
67     * Constructor used to register the metrics
68     * @param id Name of the source this class is monitoring
69     */
70    public ReplicationSourceMetrics(String id) {
71      MetricsContext context = MetricsUtil.getContext("hbase");
72      String name = Thread.currentThread().getName();
73      metricsRecord = MetricsUtil.createRecord(context, "replication");
74      metricsRecord.setTag("RegionServer", name);
75      context.registerUpdater(this);
76      // Add jvmmetrics.
77      JvmMetrics.init("RegionServer", name);
78      // export for JMX
79      new ReplicationStatistics(this.registry, "ReplicationSource for " + id);
80    }
81  
82    /**
83     * Set the age of the last edit that was shipped
84     * @param timestamp write time of the edit
85     */
86    public void setAgeOfLastShippedOp(long timestamp) {
87      ageOfLastShippedOp.set(System.currentTimeMillis() - timestamp);
88    }
89  
90    @Override
91    public void doUpdates(MetricsContext metricsContext) {
92      synchronized (this) {
93        this.shippedOpsRate.pushMetric(this.metricsRecord);
94        this.shippedBatchesRate.pushMetric(this.metricsRecord);
95        this.logEditsReadRate.pushMetric(this.metricsRecord);
96        this.logEditsFilteredRate.pushMetric(this.metricsRecord);
97        this.ageOfLastShippedOp.pushMetric(this.metricsRecord);
98        this.sizeOfLogQueue.pushMetric(this.metricsRecord);
99      }
100   }
101 }