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.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
25  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
26  
27  /**
28   * This class is for maintaining the various replication statistics for a source and publishing them
29   * through the metrics interfaces.
30   */
31  @InterfaceAudience.Private
32  public class MetricsSource {
33  
34    public static final String SOURCE_SIZE_OF_LOG_QUEUE = "source.sizeOfLogQueue";
35    public static final String SOURCE_AGE_OF_LAST_SHIPPED_OP = "source.ageOfLastShippedOp";
36    public static final String SOURCE_LOG_EDITS_READ = "source.logEditsRead";
37    public static final String SOURCE_LOG_EDITS_FILTERED = "source.logEditsFiltered";
38    public static final String SOURCE_SHIPPED_BATCHES = "source.shippedBatches";
39    public static final String SOURCE_SHIPPED_OPS = "source.shippedOps";
40  
41    public static final Log LOG = LogFactory.getLog(MetricsSource.class);
42    private String id;
43  
44    private long lastTimestamp = 0;
45    private int lastQueueSize = 0;
46  
47    private String sizeOfLogQueKey;
48    private String ageOfLastShippedOpKey;
49    private String logEditsReadKey;
50    private String logEditsFilteredKey;
51    private final String shippedBatchesKey;
52    private final String shippedOpsKey;
53  
54    private MetricsReplicationSource rms;
55  
56    /**
57     * Constructor used to register the metrics
58     *
59     * @param id Name of the source this class is monitoring
60     */
61    public MetricsSource(String id) {
62      this.id = id;
63  
64      sizeOfLogQueKey = "source." + id + ".sizeOfLogQueue";
65      ageOfLastShippedOpKey = "source." + id + ".ageOfLastShippedOp";
66      logEditsReadKey = "source." + id + ".logEditsRead";
67      logEditsFilteredKey = "source." + id + ".logEditsFiltered";
68      shippedBatchesKey = "source." + this.id + ".shippedBatches";
69      shippedOpsKey = "source." + this.id + ".shippedOps";
70      rms = CompatibilitySingletonFactory.getInstance(MetricsReplicationSource.class);
71    }
72  
73    /**
74     * Set the age of the last edit that was shipped
75     *
76     * @param timestamp write time of the edit
77     */
78    public void setAgeOfLastShippedOp(long timestamp) {
79      long age = EnvironmentEdgeManager.currentTimeMillis() - timestamp;
80      rms.setGauge(ageOfLastShippedOpKey, age);
81      rms.setGauge(SOURCE_AGE_OF_LAST_SHIPPED_OP, age);
82      this.lastTimestamp = timestamp;
83    }
84  
85    /**
86     * Convenience method to use the last given timestamp to refresh the age of the last edit. Used
87     * when replication fails and need to keep that metric accurate.
88     */
89    public void refreshAgeOfLastShippedOp() {
90      if (this.lastTimestamp > 0) {
91        setAgeOfLastShippedOp(this.lastTimestamp);
92      }
93    }
94  
95    /**
96     * Set the size of the log queue
97     *
98     * @param size the size.
99     */
100   public void setSizeOfLogQueue(int size) {
101     rms.setGauge(sizeOfLogQueKey, size);
102     rms.incGauge(SOURCE_SIZE_OF_LOG_QUEUE, size - lastQueueSize);
103     lastQueueSize = size;
104   }
105 
106   /**
107    * Add on the the number of log edits read
108    *
109    * @param delta the number of log edits read.
110    */
111   private void incrLogEditsRead(long delta) {
112     rms.incCounters(logEditsReadKey, delta);
113     rms.incCounters(SOURCE_LOG_EDITS_READ, delta);
114   }
115 
116   /** Increment the number of log edits read by one. */
117   public void incrLogEditsRead() {
118     incrLogEditsRead(1);
119   }
120 
121   /**
122    * Add on the number of log edits filtered
123    *
124    * @param delta the number filtered.
125    */
126   private void incrLogEditsFiltered(long delta) {
127     rms.incCounters(logEditsFilteredKey, delta);
128     rms.incCounters(SOURCE_LOG_EDITS_FILTERED, delta);
129   }
130 
131   /** The number of log edits filtered out. */
132   public void incrLogEditsFiltered() {
133     incrLogEditsFiltered(1);
134   }
135 
136   /**
137    * Convience method to apply changes to metrics do to shipping a batch of logs.
138    *
139    * @param batchSize the size of the batch that was shipped to sinks.
140    */
141   public void shipBatch(long batchSize) {
142     rms.incCounters(shippedBatchesKey, 1);
143     rms.incCounters(SOURCE_SHIPPED_BATCHES, 1);
144     rms.incCounters(shippedOpsKey, batchSize);
145     rms.incCounters(SOURCE_SHIPPED_OPS, batchSize);
146   }
147 
148   /** Removes all metrics about this Source. */
149   public void clear() {
150     rms.removeMetric(sizeOfLogQueKey);
151     rms.decGauge(SOURCE_SIZE_OF_LOG_QUEUE, lastQueueSize);
152     lastQueueSize = 0;
153     rms.removeMetric(ageOfLastShippedOpKey);
154 
155     rms.removeMetric(logEditsFilteredKey);
156     rms.removeMetric(logEditsReadKey);
157 
158   }
159 }