001/*
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with this
006 * work for additional information regarding copyright ownership. The ASF
007 * licenses this file to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance with the License.
009 * 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, WITHOUT
015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016 * License for the specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
022import org.apache.hadoop.metrics2.MetricHistogram;
023import org.apache.hadoop.metrics2.lib.MutableFastCounter;
024import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Hadoop2 implementation of MetricsHeapMemoryManagerSource. Implements BaseSource through
029 * BaseSourceImpl, following the pattern
030 */
031@InterfaceAudience.Private
032public class MetricsHeapMemoryManagerSourceImpl extends BaseSourceImpl implements
033    MetricsHeapMemoryManagerSource {
034
035  private final MetricHistogram blockedFlushHistogram;
036  private final MetricHistogram unblockedFlushHistogram;
037  private final MetricHistogram incMemStoreSizeHistogram;
038  private final MetricHistogram decMemStoreSizeHistogram;
039  private final MetricHistogram incBlockCacheSizeHistogram;
040  private final MetricHistogram decBlockCacheSizeHistogram;
041
042  private final MutableGaugeLong blockedFlushGauge;
043  private final MutableGaugeLong unblockedFlushGauge;
044  private final MutableGaugeLong memStoreSizeGauge;
045  private final MutableGaugeLong blockCacheSizeGauge;
046
047  private final MutableFastCounter doNothingCounter;
048  private final MutableFastCounter aboveHeapOccupancyLowWatermarkCounter;
049
050  public MetricsHeapMemoryManagerSourceImpl() {
051    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
052  }
053
054  public MetricsHeapMemoryManagerSourceImpl(String metricsName, String metricsDescription,
055      String metricsContext, String metricsJmxContext) {
056    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
057
058    // Histograms
059    blockedFlushHistogram = getMetricsRegistry()
060        .newSizeHistogram(BLOCKED_FLUSH_NAME, BLOCKED_FLUSH_DESC);
061    unblockedFlushHistogram = getMetricsRegistry()
062        .newSizeHistogram(UNBLOCKED_FLUSH_NAME, UNBLOCKED_FLUSH_DESC);
063    incMemStoreSizeHistogram = getMetricsRegistry()
064        .newSizeHistogram(INC_MEMSTORE_TUNING_NAME, INC_MEMSTORE_TUNING_DESC);
065    decMemStoreSizeHistogram = getMetricsRegistry()
066        .newSizeHistogram(DEC_MEMSTORE_TUNING_NAME, DEC_MEMSTORE_TUNING_DESC);
067    incBlockCacheSizeHistogram = getMetricsRegistry()
068        .newSizeHistogram(INC_BLOCKCACHE_TUNING_NAME, INC_BLOCKCACHE_TUNING_DESC);
069    decBlockCacheSizeHistogram = getMetricsRegistry()
070        .newSizeHistogram(DEC_BLOCKCACHE_TUNING_NAME, DEC_BLOCKCACHE_TUNING_DESC);
071
072    // Gauges
073    blockedFlushGauge = getMetricsRegistry()
074        .newGauge(BLOCKED_FLUSH_GAUGE_NAME, BLOCKED_FLUSH_GAUGE_DESC, 0L);
075    unblockedFlushGauge = getMetricsRegistry()
076        .newGauge(UNBLOCKED_FLUSH_GAUGE_NAME, UNBLOCKED_FLUSH_GAUGE_DESC, 0L);
077    memStoreSizeGauge = getMetricsRegistry()
078        .newGauge(MEMSTORE_SIZE_GAUGE_NAME, MEMSTORE_SIZE_GAUGE_DESC, 0L);
079    blockCacheSizeGauge = getMetricsRegistry()
080        .newGauge(BLOCKCACHE_SIZE_GAUGE_NAME, BLOCKCACHE_SIZE_GAUGE_DESC, 0L);
081
082    // Counters
083    doNothingCounter = getMetricsRegistry()
084        .newCounter(DO_NOTHING_COUNTER_NAME, DO_NOTHING_COUNTER_DESC, 0L);
085    aboveHeapOccupancyLowWatermarkCounter = getMetricsRegistry()
086        .newCounter(ABOVE_HEAP_LOW_WATERMARK_COUNTER_NAME,
087          ABOVE_HEAP_LOW_WATERMARK_COUNTER_DESC, 0L);
088  }
089
090  @Override
091  public void updateBlockedFlushCount(long blockedFlushCount) {
092    if (blockedFlushCount > 0) {
093      blockedFlushHistogram.add(blockedFlushCount);
094      blockedFlushGauge.set(blockedFlushCount);
095    }
096  }
097
098  @Override
099  public void updateUnblockedFlushCount(long unblockedFlushCount) {
100    if (unblockedFlushCount > 0) {
101      unblockedFlushHistogram.add(unblockedFlushCount);
102      unblockedFlushGauge.set(unblockedFlushCount);
103    }
104  }
105
106  @Override
107  public void setCurBlockCacheSizeGauge(long blockcacheSize) {
108    blockCacheSizeGauge.set(blockcacheSize);
109  }
110
111  @Override
112  public void setCurMemStoreSizeGauge(long memstoreSize) {
113    memStoreSizeGauge.set(memstoreSize);
114  }
115
116  @Override
117  public void updateMemStoreDeltaSizeHistogram(int memStoreDeltaSize) {
118    if (memStoreDeltaSize >= 0) {
119      incMemStoreSizeHistogram.add(memStoreDeltaSize);
120    } else if (memStoreDeltaSize < 0) {
121      decMemStoreSizeHistogram.add(-memStoreDeltaSize);
122    }
123  }
124
125  @Override
126  public void updateBlockCacheDeltaSizeHistogram(int blockCacheDeltaSize) {
127    if (blockCacheDeltaSize >= 0) {
128      incBlockCacheSizeHistogram.add(blockCacheDeltaSize);
129    } else if (blockCacheDeltaSize < 0) {
130      decBlockCacheSizeHistogram.add(-blockCacheDeltaSize);
131    }
132  }
133
134  @Override
135  public void increaseTunerDoNothingCounter() {
136    doNothingCounter.incr();
137  }
138
139  @Override
140  public void increaseAboveHeapOccupancyLowWatermarkCounter() {
141    aboveHeapOccupancyLowWatermarkCounter.incr();
142  }
143}