View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver.wal;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.wal.WALKey;
30  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
31  import org.apache.hadoop.util.StringUtils;
32  
33  /**
34   * Class used to push numbers about the WAL into the metrics subsystem.  This will take a
35   * single function call and turn it into multiple manipulations of the hadoop metrics system.
36   */
37  @InterfaceAudience.Private
38  public class MetricsWAL extends WALActionsListener.Base {
39    private static final Log LOG = LogFactory.getLog(MetricsWAL.class);
40  
41    private final MetricsWALSource source;
42  
43    public MetricsWAL() {
44      this(CompatibilitySingletonFactory.getInstance(MetricsWALSource.class));
45    }
46  
47    MetricsWAL(MetricsWALSource s) {
48      this.source = s;
49    }
50  
51    @Override
52    public void postSync(final long timeInNanos, final int handlerSyncs) {
53      source.incrementSyncTime(timeInNanos/1000000L);
54    }
55  
56    @Override
57    public void postAppend(final long size, final long time, final WALKey logkey,
58        final WALEdit logEdit) throws IOException {
59      TableName tableName = logkey.getTablename();
60      source.incrementAppendCount(tableName);
61      source.incrementAppendTime(time);
62      source.incrementAppendSize(tableName, size);
63      source.incrementWrittenBytes(size);
64  
65      if (time > 1000) {
66        source.incrementSlowAppendCount();
67        LOG.warn(String.format("%s took %d ms appending an edit to wal; len~=%s",
68            Thread.currentThread().getName(),
69            time,
70            StringUtils.humanReadableInt(size)));
71      }
72    }
73  
74    @Override
75    public void logRollRequested(WALActionsListener.RollRequestReason reason) {
76      source.incrementLogRollRequested();
77      switch (reason) {
78        case ERROR:
79          source.incrementErrorLogRoll();
80          break;
81        case LOW_REPLICATION:
82          source.incrementLowReplicationLogRoll();
83          break;
84        case SIZE:
85          source.incrementSizeLogRoll();
86          break;
87        case SLOW_SYNC:
88          source.incrementSlowSyncLogRoll();
89          break;
90        default:
91          break;
92      }
93    }
94  
95    @Override
96    public void postLogRoll(Path oldPath, Path newPath) {
97      // oldPath can be null if this is the first time we created a wal
98      // Also newPath can be equal to oldPath if AbstractFSWAL#replaceWriter fails
99      if (newPath != oldPath) {
100       source.incrementSuccessfulLogRolls();
101     }
102   }
103 }