001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.regionserver.wal;
021
022import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
023
024import java.io.IOException;
025
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.apache.hadoop.hbase.wal.WALEdit;
030import org.apache.hadoop.hbase.wal.WALKey;
031import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
032import org.apache.hadoop.util.StringUtils;
033
034/**
035 * Class used to push numbers about the WAL into the metrics subsystem.  This will take a
036 * single function call and turn it into multiple manipulations of the hadoop metrics system.
037 */
038@InterfaceAudience.Private
039public class MetricsWAL implements WALActionsListener {
040  private static final Logger LOG = LoggerFactory.getLogger(MetricsWAL.class);
041
042  private final MetricsWALSource source;
043
044  public MetricsWAL() {
045    this(CompatibilitySingletonFactory.getInstance(MetricsWALSource.class));
046  }
047
048  @VisibleForTesting
049  MetricsWAL(MetricsWALSource s) {
050    this.source = s;
051  }
052
053  @Override
054  public void postSync(final long timeInNanos, final int handlerSyncs) {
055    source.incrementSyncTime(timeInNanos/1000000L);
056  }
057
058  @Override
059  public void postAppend(final long size, final long time, final WALKey logkey,
060      final WALEdit logEdit) throws IOException {
061    source.incrementAppendCount();
062    source.incrementAppendTime(time);
063    source.incrementAppendSize(size);
064    source.incrementWrittenBytes(size);
065
066    if (time > 1000) {
067      source.incrementSlowAppendCount();
068      LOG.warn(String.format("%s took %d ms appending an edit to wal; len~=%s",
069          Thread.currentThread().getName(),
070          time,
071          StringUtils.humanReadableInt(size)));
072    }
073  }
074
075  @Override
076  public void logRollRequested(boolean underReplicated) {
077    source.incrementLogRollRequested();
078    if (underReplicated) {
079      source.incrementLowReplicationLogRoll();
080    }
081  }
082}