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;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * Accounting of current heap and data sizes.
024 * Tracks 3 sizes:
025 * <ol>
026 * <li></li>data size: the aggregated size of all key-value not including meta data such as
027 * index, time range etc.
028 * </li>
029 * <li>heap size: the aggregated size of all data that is allocated on-heap including all
030 * key-values that reside on-heap and the metadata that resides on-heap
031 * </li>
032 * <li></li>off-heap size: the aggregated size of all data that is allocated off-heap including all
033 * key-values that reside off-heap and the metadata that resides off-heap
034 * </li>
035 * </ol>
036 *
037 * 3 examples to illustrate their usage:
038 * <p>
039 * Consider a store with 100MB of key-values allocated on-heap and 20MB of metadata allocated
040 * on-heap. The counters are <100MB, 120MB, 0>, respectively.
041 * </p>
042 * <p>Consider a store with 100MB of key-values allocated off-heap and 20MB of metadata
043 * allocated on-heap (e.g, CAM index). The counters are <100MB, 20MB, 100MB>, respectively.
044 * </p>
045 * <p>
046 * Consider a store with 100MB of key-values from which 95MB are allocated off-heap and 5MB
047 * are allocated on-heap (e.g., due to upserts) and 20MB of metadata from which 15MB allocated
048 * off-heap (e.g, CCM index) and 5MB allocated on-heap (e.g, CSLM index in active).
049 * The counters are <100MB, 10MB, 110MB>, respectively.
050 * </p>
051 *
052 * Like {@link TimeRangeTracker}, it has thread-safe and non-thread-safe implementations.
053 */
054@InterfaceAudience.Private
055public interface MemStoreSizing {
056  MemStoreSizing DUD = new MemStoreSizing() {
057    private final MemStoreSize mss = new MemStoreSize();
058
059    @Override
060    public MemStoreSize getMemStoreSize() {
061      return this.mss;
062    }
063
064    @Override
065    public long getDataSize() {
066      return this.mss.getDataSize();
067    }
068
069    @Override
070    public long getHeapSize() {
071      return this.mss.getHeapSize();
072    }
073
074    @Override
075    public long getOffHeapSize() {
076      return this.mss.getOffHeapSize();
077    }
078
079    @Override
080    public int getCellsCount() {
081      return this.mss.getCellsCount();
082    }
083
084    @Override
085    public long incMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
086        int cellsCountDelta) {
087      throw new RuntimeException("I'm a DUD, you can't use me!");
088    }
089  };
090
091  /**
092   * @return The new dataSize ONLY as a convenience
093   */
094  long incMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
095      int cellsCount);
096
097  default long incMemStoreSize(MemStoreSize delta) {
098    return incMemStoreSize(delta.getDataSize(), delta.getHeapSize(), delta.getOffHeapSize(),
099      delta.getCellsCount());
100  }
101
102  /**
103   * @return The new dataSize ONLY as a convenience
104   */
105  default long decMemStoreSize(long dataSizeDelta, long heapSizeDelta,
106      long offHeapSizeDelta, int cellsCountDelta) {
107    return incMemStoreSize(-dataSizeDelta, -heapSizeDelta, -offHeapSizeDelta, -cellsCountDelta);
108  }
109
110  default long decMemStoreSize(MemStoreSize delta) {
111    return incMemStoreSize(-delta.getDataSize(), -delta.getHeapSize(), -delta.getOffHeapSize(),
112      -delta.getCellsCount());
113  }
114
115  long getDataSize();
116  long getHeapSize();
117  long getOffHeapSize();
118  int getCellsCount();
119
120  /**
121   * @return Use this datastructure to return all three settings, {@link #getDataSize()},
122   * {@link #getHeapSize()}, and {@link #getOffHeapSize()}, in the one go.
123   */
124  MemStoreSize getMemStoreSize();
125}