001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.wal;
019
020import java.util.concurrent.ConcurrentHashMap;
021import java.util.concurrent.ConcurrentMap;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
024import org.apache.hadoop.metrics2.MetricHistogram;
025import org.apache.hadoop.metrics2.lib.MutableFastCounter;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Class that transitions metrics from MetricsWAL into the metrics subsystem. Implements BaseSource
030 * through BaseSourceImpl, following the pattern.
031 * @see org.apache.hadoop.hbase.regionserver.wal.MetricsWALSource
032 */
033@InterfaceAudience.Private
034public class MetricsWALSourceImpl extends BaseSourceImpl implements MetricsWALSource {
035
036  private final MetricHistogram appendSizeHisto;
037  private final MetricHistogram appendTimeHisto;
038  private final MetricHistogram syncTimeHisto;
039  private final MutableFastCounter appendCount;
040  private final MutableFastCounter slowAppendCount;
041  private final MutableFastCounter logRollRequested;
042  private final MutableFastCounter errorRollRequested;
043  private final MutableFastCounter lowReplicationRollRequested;
044  private final MutableFastCounter slowSyncRollRequested;
045  private final MutableFastCounter sizeRollRequested;
046  private final MutableFastCounter writtenBytes;
047  private final MutableFastCounter successfulLogRolls;
048  // Per table metrics.
049  private final ConcurrentMap<TableName, MutableFastCounter> perTableAppendCount;
050  private final ConcurrentMap<TableName, MutableFastCounter> perTableAppendSize;
051
052  public MetricsWALSourceImpl() {
053    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
054  }
055
056  public MetricsWALSourceImpl(String metricsName, String metricsDescription, String metricsContext,
057    String metricsJmxContext) {
058    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
059
060    // Create and store the metrics that will be used.
061    appendTimeHisto = this.getMetricsRegistry().newTimeHistogram(APPEND_TIME, APPEND_TIME_DESC);
062    appendSizeHisto = this.getMetricsRegistry().newSizeHistogram(APPEND_SIZE, APPEND_SIZE_DESC);
063    appendCount = this.getMetricsRegistry().newCounter(APPEND_COUNT, APPEND_COUNT_DESC, 0L);
064    slowAppendCount =
065      this.getMetricsRegistry().newCounter(SLOW_APPEND_COUNT, SLOW_APPEND_COUNT_DESC, 0L);
066    syncTimeHisto = this.getMetricsRegistry().newTimeHistogram(SYNC_TIME, SYNC_TIME_DESC);
067    logRollRequested =
068      this.getMetricsRegistry().newCounter(ROLL_REQUESTED, ROLL_REQUESTED_DESC, 0L);
069    errorRollRequested =
070      this.getMetricsRegistry().newCounter(ERROR_ROLL_REQUESTED, ERROR_ROLL_REQUESTED_DESC, 0L);
071    lowReplicationRollRequested = this.getMetricsRegistry().newCounter(LOW_REPLICA_ROLL_REQUESTED,
072      LOW_REPLICA_ROLL_REQUESTED_DESC, 0L);
073    slowSyncRollRequested = this.getMetricsRegistry().newCounter(SLOW_SYNC_ROLL_REQUESTED,
074      SLOW_SYNC_ROLL_REQUESTED_DESC, 0L);
075    sizeRollRequested =
076      this.getMetricsRegistry().newCounter(SIZE_ROLL_REQUESTED, SIZE_ROLL_REQUESTED_DESC, 0L);
077    writtenBytes = this.getMetricsRegistry().newCounter(WRITTEN_BYTES, WRITTEN_BYTES_DESC, 0L);
078    successfulLogRolls =
079      this.getMetricsRegistry().newCounter(SUCCESSFUL_LOG_ROLLS, SUCCESSFUL_LOG_ROLLS_DESC, 0L);
080    perTableAppendCount = new ConcurrentHashMap<>();
081    perTableAppendSize = new ConcurrentHashMap<>();
082  }
083
084  @Override
085  public void incrementAppendSize(TableName tableName, long size) {
086    appendSizeHisto.add(size);
087    MutableFastCounter tableAppendSizeCounter = perTableAppendSize.get(tableName);
088    if (tableAppendSizeCounter == null) {
089      // Ideally putIfAbsent is atomic and we don't need a branch check but we still do it to avoid
090      // expensive string construction for every append.
091      String metricsKey = String.format("%s.%s", tableName, APPEND_SIZE);
092      perTableAppendSize.putIfAbsent(tableName,
093        getMetricsRegistry().newCounter(metricsKey, APPEND_SIZE_DESC, 0L));
094      tableAppendSizeCounter = perTableAppendSize.get(tableName);
095    }
096    tableAppendSizeCounter.incr(size);
097  }
098
099  @Override
100  public void incrementAppendTime(long time) {
101    appendTimeHisto.add(time);
102  }
103
104  @Override
105  public void incrementAppendCount(TableName tableName) {
106    appendCount.incr();
107    MutableFastCounter tableAppendCounter = perTableAppendCount.get(tableName);
108    if (tableAppendCounter == null) {
109      String metricsKey = String.format("%s.%s", tableName, APPEND_COUNT);
110      perTableAppendCount.putIfAbsent(tableName,
111        getMetricsRegistry().newCounter(metricsKey, APPEND_COUNT_DESC, 0L));
112      tableAppendCounter = perTableAppendCount.get(tableName);
113    }
114    tableAppendCounter.incr();
115  }
116
117  @Override
118  public void incrementSlowAppendCount() {
119    slowAppendCount.incr();
120  }
121
122  @Override
123  public void incrementSyncTime(long time) {
124    syncTimeHisto.add(time);
125  }
126
127  @Override
128  public void incrementLogRollRequested() {
129    logRollRequested.incr();
130  }
131
132  @Override
133  public void incrementErrorLogRoll() {
134    errorRollRequested.incr();
135  }
136
137  @Override
138  public void incrementLowReplicationLogRoll() {
139    lowReplicationRollRequested.incr();
140  }
141
142  @Override
143  public void incrementSlowSyncLogRoll() {
144    slowSyncRollRequested.incr();
145  }
146
147  @Override
148  public void incrementSizeLogRoll() {
149    sizeRollRequested.incr();
150  }
151
152  @Override
153  public long getSlowAppendCount() {
154    return slowAppendCount.value();
155  }
156
157  @Override
158  public void incrementWrittenBytes(long val) {
159    writtenBytes.incr(val);
160  }
161
162  @Override
163  public void incrementSuccessfulLogRolls() {
164    successfulLogRolls.incr();
165  }
166
167  @Override
168  public long getSuccessfulLogRolls() {
169    return successfulLogRolls.value();
170  }
171}