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    private String id;
57  
58    private final MetricsReplicationSourceSource singleSourceSource;
59    private final MetricsReplicationSourceSource globalSourceSource;
60  
61    /**
62     * Constructor used to register the metrics
63     *
64     * @param id Name of the source this class is monitoring
65     */
66    public MetricsSource(String id) {
67      this.id = id;
68      singleSourceSource =
69        CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
70          .getSource(id);
71      globalSourceSource =
72        CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
73          .getGlobalSource();
74    }
75  
76    /**
77     * Set the age of the last edit that was shipped
78     *
79     * @param timestamp write time of the edit
80     */
81    public void setAgeOfLastShippedOp(long timestamp) {
82      long age = EnvironmentEdgeManager.currentTimeMillis() - timestamp;
83      singleSourceSource.setLastShippedAge(age);
84      globalSourceSource.setLastShippedAge(age);
85      this.lastTimestamp = timestamp;
86    }
87  
88    /**
89     * Convenience method to use the last given timestamp to refresh the age of the last edit. Used
90     * when replication fails and need to keep that metric accurate.
91     */
92    public void refreshAgeOfLastShippedOp() {
93      if (this.lastTimestamp > 0) {
94        setAgeOfLastShippedOp(this.lastTimestamp);
95      }
96    }
97  
98    /**
99     * Set the size of the log queue
100    *
101    * @param size the size.
102    */
103   public void setSizeOfLogQueue(int size) {
104     singleSourceSource.setSizeOfLogQueue(size);
105     globalSourceSource.incrSizeOfLogQueue(size - lastQueueSize);
106     lastQueueSize = size;
107   }
108 
109   /**
110    * Add on the the number of log edits read
111    *
112    * @param delta the number of log edits read.
113    */
114   private void incrLogEditsRead(long delta) {
115     singleSourceSource.incrLogReadInEdits(delta);
116     globalSourceSource.incrLogReadInEdits(delta);
117   }
118 
119   /** Increment the number of log edits read by one. */
120   public void incrLogEditsRead() {
121     incrLogEditsRead(1);
122   }
123 
124   /**
125    * Add on the number of log edits filtered
126    *
127    * @param delta the number filtered.
128    */
129   private void incrLogEditsFiltered(long delta) {
130     singleSourceSource.incrLogEditsFiltered(delta);
131     globalSourceSource.incrLogEditsFiltered(delta);
132   }
133 
134   /** The number of log edits filtered out. */
135   public void incrLogEditsFiltered() {
136     incrLogEditsFiltered(1);
137   }
138 
139   /**
140    * Convience method to apply changes to metrics do to shipping a batch of logs.
141    *
142    * @param batchSize the size of the batch that was shipped to sinks.
143    */
144   public void shipBatch(long batchSize, int sizeInKB) {
145     singleSourceSource.incrBatchesShipped(1);
146     globalSourceSource.incrBatchesShipped(1);
147 
148     singleSourceSource.incrOpsShipped(batchSize);
149     globalSourceSource.incrOpsShipped(batchSize);
150 
151     singleSourceSource.incrShippedKBs(sizeInKB);
152     globalSourceSource.incrShippedKBs(sizeInKB);
153   }
154 
155   /** increase the byte number read by source from log file */
156   public void incrLogReadInBytes(long readInBytes) {
157     singleSourceSource.incrLogReadInBytes(readInBytes);
158     globalSourceSource.incrLogReadInBytes(readInBytes);
159   }
160 
161   /** Removes all metrics about this Source. */
162   public void clear() {
163     singleSourceSource.clear();
164     globalSourceSource.decrSizeOfLogQueue(lastQueueSize);
165     lastQueueSize = 0;
166   }
167 
168   /**
169    * Get AgeOfLastShippedOp
170    * @return AgeOfLastShippedOp
171    */
172   public Long getAgeOfLastShippedOp() {
173     return singleSourceSource.getLastShippedAge();
174   }
175 
176   /**
177    * Get the sizeOfLogQueue
178    * @return sizeOfLogQueue
179    */
180   public int getSizeOfLogQueue() {
181     return this.lastQueueSize;
182   }
183 
184   /**
185    * Get the timeStampsOfLastShippedOp
186    * @return lastTimestampForAge
187    */
188   public long getTimeStampOfLastShippedOp() {
189     return lastTimestamp;
190   }
191 
192   /**
193    * Get the slave peer ID
194    * @return peerID
195    */
196   public String getPeerID() {
197     return id;
198   }
199 }