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    // It's a little dirty to preset the age to now since if we fail
70    // to replicate the very first time then it will show that age instead
71    // of nothing (although that might not be good either).
72    private long lastTimestampForAge = System.currentTimeMillis();
73  
74    /**
75     * Constructor used to register the metrics
76     * @param id Name of the source this class is monitoring
77     */
78    public ReplicationSourceMetrics(String id) {
79      MetricsContext context = MetricsUtil.getContext("hbase");
80      String name = Thread.currentThread().getName();
81      metricsRecord = MetricsUtil.createRecord(context, "replication");
82      metricsRecord.setTag("RegionServer", name);
83      context.registerUpdater(this);
84      try {
85        id = URLEncoder.encode(id, "UTF8");
86      } catch (UnsupportedEncodingException e) {
87        id = "CAN'T ENCODE UTF8";
88      }
89      // export for JMX
90      new ReplicationStatistics(this.registry, "ReplicationSource for " + id);
91    }
92  
93    /**
94     * Set the age of the last edit that was shipped
95     * @param timestamp write time of the edit
96     */
97    public void setAgeOfLastShippedOp(long timestamp) {
98      lastTimestampForAge = timestamp;
99      ageOfLastShippedOp.set(System.currentTimeMillis() - lastTimestampForAge);
100   }
101 
102   /**
103    * Convenience method to use the last given timestamp to refresh the age
104    * of the last edit. Used when replication fails and need to keep that
105    * metric accurate.
106    */
107   public void refreshAgeOfLastShippedOp() {
108     setAgeOfLastShippedOp(lastTimestampForAge);
109   }
110 
111   @Override
112   public void doUpdates(MetricsContext metricsContext) {
113     synchronized (this) {
114       this.shippedOpsRate.pushMetric(this.metricsRecord);
115       this.shippedBatchesRate.pushMetric(this.metricsRecord);
116       this.logEditsReadRate.pushMetric(this.metricsRecord);
117       this.logEditsFilteredRate.pushMetric(this.metricsRecord);
118       this.ageOfLastShippedOp.pushMetric(this.metricsRecord);
119       this.sizeOfLogQueue.pushMetric(this.metricsRecord);
120     }
121     this.metricsRecord.update();
122   }
123 }