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