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    private long lastTimestamp = 0;
38    private int lastQueueSize = 0;
39    private long lastHFileRefsQueueSize = 0;
40    private String id;
41  
42    private final MetricsReplicationSourceSource singleSourceSource;
43    private final MetricsReplicationSourceSource globalSourceSource;
44  
45  
46    /**
47     * Constructor used to register the metrics
48     *
49     * @param id Name of the source this class is monitoring
50     */
51    public MetricsSource(String id) {
52      this.id = id;
53      singleSourceSource =
54          CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class)
55              .getSource(id);
56      globalSourceSource = CompatibilitySingletonFactory.getInstance(MetricsReplicationSourceFactory.class).getGlobalSource();
57    }
58  
59    /**
60     * Set the age of the last edit that was shipped
61     *
62     * @param timestamp write time of the edit
63     */
64    public void setAgeOfLastShippedOp(long timestamp) {
65      long age = EnvironmentEdgeManager.currentTime() - timestamp;
66      singleSourceSource.setLastShippedAge(age);
67      globalSourceSource.setLastShippedAge(age);
68      this.lastTimestamp = timestamp;
69    }
70  
71    /**
72     * Convenience method to use the last given timestamp to refresh the age of the last edit. Used
73     * when replication fails and need to keep that metric accurate.
74     */
75    public void refreshAgeOfLastShippedOp() {
76      if (this.lastTimestamp > 0) {
77        setAgeOfLastShippedOp(this.lastTimestamp);
78      }
79    }
80  
81    /**
82     * Set the size of the log queue
83     *
84     * @param size the size.
85     */
86    public void setSizeOfLogQueue(int size) {
87      singleSourceSource.setSizeOfLogQueue(size);
88      globalSourceSource.incrSizeOfLogQueue(size - lastQueueSize);
89      lastQueueSize = size;
90    }
91  
92    /**
93     * Add on the the number of log edits read
94     *
95     * @param delta the number of log edits read.
96     */
97    private void incrLogEditsRead(long delta) {
98      singleSourceSource.incrLogReadInEdits(delta);
99      globalSourceSource.incrLogReadInEdits(delta);
100   }
101 
102   /** Increment the number of log edits read by one. */
103   public void incrLogEditsRead() {
104     incrLogEditsRead(1);
105   }
106 
107   /**
108    * Add on the number of log edits filtered
109    *
110    * @param delta the number filtered.
111    */
112   public void incrLogEditsFiltered(long delta) {
113     singleSourceSource.incrLogEditsFiltered(delta);
114     globalSourceSource.incrLogEditsFiltered(delta);
115   }
116 
117   /** The number of log edits filtered out. */
118   public void incrLogEditsFiltered() {
119     incrLogEditsFiltered(1);
120   }
121 
122   /**
123    * Convience method to apply changes to metrics do to shipping a batch of logs.
124    *
125    * @param batchSize the size of the batch that was shipped to sinks.
126    */
127   public void shipBatch(long batchSize, int sizeInKB) {
128     singleSourceSource.incrBatchesShipped(1);
129     globalSourceSource.incrBatchesShipped(1);
130 
131     singleSourceSource.incrOpsShipped(batchSize);
132     globalSourceSource.incrOpsShipped(batchSize);
133 
134     singleSourceSource.incrShippedKBs(sizeInKB);
135     globalSourceSource.incrShippedKBs(sizeInKB);
136   }
137 
138   /**
139    * Convience method to apply changes to metrics do to shipping a batch of logs.
140    *
141    * @param batchSize the size of the batch that was shipped to sinks.
142    * @param hfiles total number of hfiles shipped to sinks.
143    */
144   public void shipBatch(long batchSize, int sizeInKB, long hfiles) {
145     shipBatch(batchSize, sizeInKB);
146     singleSourceSource.incrHFilesShipped(hfiles);
147     globalSourceSource.incrHFilesShipped(hfiles);
148   }
149 
150   /** increase the byte number read by source from log file */
151   public void incrLogReadInBytes(long readInBytes) {
152     singleSourceSource.incrLogReadInBytes(readInBytes);
153     globalSourceSource.incrLogReadInBytes(readInBytes);
154   }
155 
156   /** Removes all metrics about this Source. */
157   public void clear() {
158     singleSourceSource.clear();
159     globalSourceSource.decrSizeOfLogQueue(lastQueueSize);
160     globalSourceSource.decrSizeOfHFileRefsQueue(lastHFileRefsQueueSize);
161     lastQueueSize = 0;
162     lastHFileRefsQueueSize = 0;
163   }
164 
165   /**
166    * Get AgeOfLastShippedOp
167    * @return AgeOfLastShippedOp
168    */
169   public Long getAgeOfLastShippedOp() {
170     return singleSourceSource.getLastShippedAge();
171   }
172 
173   /**
174    * Get the sizeOfLogQueue
175    * @return sizeOfLogQueue
176    */
177   public int getSizeOfLogQueue() {
178     return this.lastQueueSize;
179   }
180 
181   /**
182    * Get the timeStampsOfLastShippedOp
183    * @return lastTimestampForAge
184    */
185   public long getTimeStampOfLastShippedOp() {
186     return lastTimestamp;
187   }
188 
189   /**
190    * Get the slave peer ID
191    * @return peerID
192    */
193   public String getPeerID() {
194     return id;
195   }
196 
197   public void incrSizeOfHFileRefsQueue(long size) {
198     singleSourceSource.incrSizeOfHFileRefsQueue(size);
199     globalSourceSource.incrSizeOfHFileRefsQueue(size);
200     lastHFileRefsQueueSize = size;
201   }
202 
203   public void decrSizeOfHFileRefsQueue(int size) {
204     singleSourceSource.decrSizeOfHFileRefsQueue(size);
205     globalSourceSource.decrSizeOfHFileRefsQueue(size);
206     lastHFileRefsQueueSize -= size;
207     if (lastHFileRefsQueueSize < 0) {
208       lastHFileRefsQueueSize = 0;
209     }
210   }
211 
212   public void incrUnknownFileLengthForClosedWAL() {
213     singleSourceSource.incrUnknownFileLengthForClosedWAL();
214     globalSourceSource.incrUnknownFileLengthForClosedWAL();
215   }
216 
217   public void incrUncleanlyClosedWALs() {
218     singleSourceSource.incrUncleanlyClosedWALs();
219     globalSourceSource.incrUncleanlyClosedWALs();
220   }
221 
222   public void incrBytesSkippedInUncleanlyClosedWALs(final long bytes) {
223     singleSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
224     globalSourceSource.incrBytesSkippedInUncleanlyClosedWALs(bytes);
225   }
226 
227   public void incrRestartedWALReading() {
228     singleSourceSource.incrRestartedWALReading();
229     globalSourceSource.incrRestartedWALReading();
230   }
231 
232   public void incrRepeatedFileBytes(final long bytes) {
233     singleSourceSource.incrRepeatedFileBytes(bytes);
234     globalSourceSource.incrRepeatedFileBytes(bytes);
235   }
236 
237   public void incrCompletedWAL() {
238     singleSourceSource.incrCompletedWAL();
239     globalSourceSource.incrCompletedWAL();
240   }
241 
242   public void incrCompletedRecoveryQueue() {
243     singleSourceSource.incrCompletedRecoveryQueue();
244     globalSourceSource.incrCompletedRecoveryQueue();
245   }
246 }