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 java.io.IOException;
023
024import org.apache.hadoop.hbase.TableName;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.apache.hadoop.hbase.wal.WALEdit;
029import org.apache.hadoop.hbase.wal.WALKey;
030import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
031import org.apache.hadoop.util.StringUtils;
032
033/**
034 * Class used to push numbers about the WAL into the metrics subsystem.  This will take a
035 * single function call and turn it into multiple manipulations of the hadoop metrics system.
036 */
037@InterfaceAudience.Private
038public class MetricsWAL implements WALActionsListener {
039  private static final Logger LOG = LoggerFactory.getLogger(MetricsWAL.class);
040
041  private final MetricsWALSource source;
042
043  public MetricsWAL() {
044    this(CompatibilitySingletonFactory.getInstance(MetricsWALSource.class));
045  }
046
047  MetricsWAL(MetricsWALSource s) {
048    this.source = s;
049  }
050
051  @Override
052  public void postSync(final long timeInNanos, final int handlerSyncs) {
053    source.incrementSyncTime(timeInNanos/1000000L);
054  }
055
056  @Override
057  public void postAppend(final long size, final long time, final WALKey logkey,
058      final WALEdit logEdit) throws IOException {
059    TableName tableName = logkey.getTableName();
060    source.incrementAppendCount(tableName);
061    source.incrementAppendTime(time);
062    source.incrementAppendSize(tableName, size);
063    source.incrementWrittenBytes(size);
064
065    if (time > 1000) {
066      source.incrementSlowAppendCount();
067      LOG.warn(String.format("%s took %d ms appending an edit to wal; len~=%s",
068          Thread.currentThread().getName(),
069          time,
070          StringUtils.humanReadableInt(size)));
071    }
072  }
073
074  @Override
075  public void logRollRequested(WALActionsListener.RollRequestReason reason) {
076    source.incrementLogRollRequested();
077    switch (reason) {
078      case ERROR:
079        source.incrementErrorLogRoll();
080        break;
081      case LOW_REPLICATION:
082        source.incrementLowReplicationLogRoll();
083        break;
084      case SIZE:
085        source.incrementSizeLogRoll();
086        break;
087      case SLOW_SYNC:
088        source.incrementSlowSyncLogRoll();
089        break;
090      default:
091        break;
092    }
093  }
094}