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