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