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