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 */
019package org.apache.hadoop.hbase.regionserver;
020
021import java.lang.management.MemoryType;
022import java.util.concurrent.ConcurrentMap;
023import java.util.concurrent.ConcurrentSkipListMap;
024import java.util.concurrent.atomic.LongAdder;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.io.util.MemorySizeUtil;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.apache.hadoop.hbase.util.Pair;
031
032/**
033 * RegionServerAccounting keeps record of some basic real time information about
034 * the Region Server. Currently, it keeps record the global memstore size and global memstore
035 * on-heap and off-heap overhead. It also tracks the replay edits per region.
036 */
037@InterfaceAudience.Private
038public class RegionServerAccounting {
039  // memstore data size
040  private final LongAdder globalMemStoreDataSize = new LongAdder();
041  // memstore heap size.
042  private final LongAdder globalMemStoreHeapSize = new LongAdder();
043  // memstore off-heap size.
044  private final LongAdder globalMemStoreOffHeapSize = new LongAdder();
045
046  // Store the edits size during replaying WAL. Use this to roll back the
047  // global memstore size once a region opening failed.
048  private final ConcurrentMap<byte[], MemStoreSizing> replayEditsPerRegion =
049    new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR);
050
051  private long globalMemStoreLimit;
052  private final float globalMemStoreLimitLowMarkPercent;
053  private long globalMemStoreLimitLowMark;
054  private final MemoryType memType;
055  private long globalOnHeapMemstoreLimit;
056  private long globalOnHeapMemstoreLimitLowMark;
057
058  public RegionServerAccounting(Configuration conf) {
059    Pair<Long, MemoryType> globalMemstoreSizePair = MemorySizeUtil.getGlobalMemStoreSize(conf);
060    this.globalMemStoreLimit = globalMemstoreSizePair.getFirst();
061    this.memType = globalMemstoreSizePair.getSecond();
062    this.globalMemStoreLimitLowMarkPercent =
063        MemorySizeUtil.getGlobalMemStoreHeapLowerMark(conf, this.memType == MemoryType.HEAP);
064    // When off heap memstore in use we configure the global off heap space for memstore as bytes
065    // not as % of max memory size. In such case, the lower water mark should be specified using the
066    // key "hbase.regionserver.global.memstore.size.lower.limit" which says % of the global upper
067    // bound and defaults to 95%. In on heap case also specifying this way is ideal. But in the past
068    // we used to take lower bound also as the % of xmx (38% as default). For backward compatibility
069    // for this deprecated config,we will fall back to read that config when new one is missing.
070    // Only for on heap case, do this fallback mechanism. For off heap it makes no sense.
071    // TODO When to get rid of the deprecated config? ie
072    // "hbase.regionserver.global.memstore.lowerLimit". Can get rid of this boolean passing then.
073    this.globalMemStoreLimitLowMark =
074        (long) (this.globalMemStoreLimit * this.globalMemStoreLimitLowMarkPercent);
075    this.globalOnHeapMemstoreLimit = MemorySizeUtil.getOnheapGlobalMemStoreSize(conf);
076    this.globalOnHeapMemstoreLimitLowMark =
077        (long) (this.globalOnHeapMemstoreLimit * this.globalMemStoreLimitLowMarkPercent);
078  }
079
080  long getGlobalMemStoreLimit() {
081    return this.globalMemStoreLimit;
082  }
083
084  long getGlobalOnHeapMemStoreLimit() {
085    return this.globalOnHeapMemstoreLimit;
086  }
087
088  // Called by the tuners.
089  void setGlobalMemStoreLimits(long newGlobalMemstoreLimit) {
090    if (this.memType == MemoryType.HEAP) {
091      this.globalMemStoreLimit = newGlobalMemstoreLimit;
092      this.globalMemStoreLimitLowMark =
093          (long) (this.globalMemStoreLimit * this.globalMemStoreLimitLowMarkPercent);
094    } else {
095      this.globalOnHeapMemstoreLimit = newGlobalMemstoreLimit;
096      this.globalOnHeapMemstoreLimitLowMark =
097          (long) (this.globalOnHeapMemstoreLimit * this.globalMemStoreLimitLowMarkPercent);
098    }
099  }
100
101  boolean isOffheap() {
102    return this.memType == MemoryType.NON_HEAP;
103  }
104
105  long getGlobalMemStoreLimitLowMark() {
106    return this.globalMemStoreLimitLowMark;
107  }
108
109  float getGlobalMemStoreLimitLowMarkPercent() {
110    return this.globalMemStoreLimitLowMarkPercent;
111  }
112
113  /**
114   * @return the global Memstore data size in the RegionServer
115   */
116  public long getGlobalMemStoreDataSize() {
117    return globalMemStoreDataSize.sum();
118  }
119
120  /**
121   * @return the global memstore heap size in the RegionServer
122   */
123  public long getGlobalMemStoreHeapSize() {
124    return this.globalMemStoreHeapSize.sum();
125  }
126
127  /**
128   * @return the global memstore heap size in the RegionServer
129   */
130  public long getGlobalMemStoreOffHeapSize() {
131    return this.globalMemStoreOffHeapSize.sum();
132  }
133
134  void incGlobalMemStoreSize(MemStoreSize mss) {
135    incGlobalMemStoreSize(mss.getDataSize(), mss.getHeapSize(), mss.getOffHeapSize());
136  }
137
138  public void incGlobalMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta) {
139    globalMemStoreDataSize.add(dataSizeDelta);
140    globalMemStoreHeapSize.add(heapSizeDelta);
141    globalMemStoreOffHeapSize.add(offHeapSizeDelta);
142  }
143
144  public void decGlobalMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta) {
145    globalMemStoreDataSize.add(-dataSizeDelta);
146    globalMemStoreHeapSize.add(-heapSizeDelta);
147    globalMemStoreOffHeapSize.add(-offHeapSizeDelta);
148  }
149
150  /**
151   * Return true if we are above the memstore high water mark
152   * @return the flushtype
153   */
154  public FlushType isAboveHighWaterMark() {
155    // for onheap memstore we check if the global memstore size and the
156    // global heap overhead is greater than the global memstore limit
157    if (memType == MemoryType.HEAP) {
158      if (getGlobalMemStoreHeapSize() >= globalMemStoreLimit) {
159        return FlushType.ABOVE_ONHEAP_HIGHER_MARK;
160      }
161    } else {
162      // If the configured memstore is offheap, check for two things
163      // 1) If the global memstore off-heap size is greater than the configured
164      // 'hbase.regionserver.offheap.global.memstore.size'
165      // 2) If the global memstore heap size is greater than the configured onheap
166      // global memstore limit 'hbase.regionserver.global.memstore.size'.
167      // We do this to avoid OOME incase of scenarios where the heap is occupied with
168      // lot of onheap references to the cells in memstore
169      if (getGlobalMemStoreOffHeapSize() >= globalMemStoreLimit) {
170        // Indicates that global memstore size is above the configured
171        // 'hbase.regionserver.offheap.global.memstore.size'
172        return FlushType.ABOVE_OFFHEAP_HIGHER_MARK;
173      } else if (getGlobalMemStoreHeapSize() >= this.globalOnHeapMemstoreLimit) {
174        // Indicates that the offheap memstore's heap overhead is greater than the
175        // configured 'hbase.regionserver.global.memstore.size'.
176        return FlushType.ABOVE_ONHEAP_HIGHER_MARK;
177      }
178    }
179    return FlushType.NORMAL;
180  }
181
182  /**
183   * Return true if we're above the low watermark
184   */
185  public FlushType isAboveLowWaterMark() {
186    // for onheap memstore we check if the global memstore size and the
187    // global heap overhead is greater than the global memstore lower mark limit
188    if (memType == MemoryType.HEAP) {
189      if (getGlobalMemStoreHeapSize() >= globalMemStoreLimitLowMark) {
190        return FlushType.ABOVE_ONHEAP_LOWER_MARK;
191      }
192    } else {
193      if (getGlobalMemStoreOffHeapSize() >= globalMemStoreLimitLowMark) {
194        // Indicates that the offheap memstore's size is greater than the global memstore
195        // lower limit
196        return FlushType.ABOVE_OFFHEAP_LOWER_MARK;
197      } else if (getGlobalMemStoreHeapSize() >= globalOnHeapMemstoreLimitLowMark) {
198        // Indicates that the offheap memstore's heap overhead is greater than the global memstore
199        // onheap lower limit
200        return FlushType.ABOVE_ONHEAP_LOWER_MARK;
201      }
202    }
203    return FlushType.NORMAL;
204  }
205
206  /**
207   * @return the flush pressure of all stores on this regionserver. The value should be greater than
208   *         or equal to 0.0, and any value greater than 1.0 means we enter the emergency state that
209   *         global memstore size already exceeds lower limit.
210   */
211  public double getFlushPressure() {
212    if (memType == MemoryType.HEAP) {
213      return (getGlobalMemStoreHeapSize()) * 1.0 / globalMemStoreLimitLowMark;
214    } else {
215      return Math.max(getGlobalMemStoreOffHeapSize() * 1.0 / globalMemStoreLimitLowMark,
216          getGlobalMemStoreHeapSize() * 1.0 / globalOnHeapMemstoreLimitLowMark);
217    }
218  }
219
220  /***
221   * Add memStoreSize to replayEditsPerRegion.
222   *
223   * @param regionName region name.
224   * @param memStoreSize the Memstore size will be added to replayEditsPerRegion.
225   */
226  public void addRegionReplayEditsSize(byte[] regionName, MemStoreSize memStoreSize) {
227    MemStoreSizing replayEdistsSize = replayEditsPerRegion.get(regionName);
228    // All ops on the same MemStoreSize object is going to be done by single thread, sequentially
229    // only. First calls to this method to increment the per region reply edits size and then call
230    // to either rollbackRegionReplayEditsSize or clearRegionReplayEditsSize as per the result of
231    // the region open operation. No need to handle multi thread issues on one region's entry in
232    // this Map.
233    if (replayEdistsSize == null) {
234      replayEdistsSize = new ThreadSafeMemStoreSizing();
235      replayEditsPerRegion.put(regionName, replayEdistsSize);
236    }
237    replayEdistsSize.incMemStoreSize(memStoreSize);
238  }
239
240  /**
241   * Roll back the global MemStore size for a specified region when this region
242   * can't be opened.
243   *
244   * @param regionName the region which could not open.
245   */
246  public void rollbackRegionReplayEditsSize(byte[] regionName) {
247    MemStoreSizing replayEditsSizing = replayEditsPerRegion.get(regionName);
248    if (replayEditsSizing != null) {
249      clearRegionReplayEditsSize(regionName);
250      decGlobalMemStoreSize(replayEditsSizing.getDataSize(), replayEditsSizing.getHeapSize(),
251          replayEditsSizing.getOffHeapSize());
252    }
253  }
254
255  /**
256   * Clear a region from replayEditsPerRegion.
257   *
258   * @param regionName region name.
259   */
260  public void clearRegionReplayEditsSize(byte[] regionName) {
261    replayEditsPerRegion.remove(regionName);
262  }
263}