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 com.google.common.annotations.VisibleForTesting;
23  
24  import java.io.IOException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
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    @VisibleForTesting
48    MetricsWAL(MetricsWALSource s) {
49      this.source = s;
50    }
51  
52    @Override
53    public void postSync(final long timeInNanos, final int handlerSyncs) {
54      source.incrementSyncTime(timeInNanos/1000000L);
55    }
56  
57    @Override
58    public void postAppend(final long size, final long time, final WALKey logkey,
59        final WALEdit logEdit) throws IOException {
60      source.incrementAppendCount();
61      source.incrementAppendTime(time);
62      source.incrementAppendSize(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(boolean underReplicated) {
76      source.incrementLogRollRequested();
77      if (underReplicated) {
78        source.incrementLowReplicationLogRoll();
79      }
80    }
81  }