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 @Override 091 public boolean compareAndSetDataSize(long expected, long updated) { 092 throw new RuntimeException("I'm a DUD, you can't use me!"); 093 } 094 }; 095 096 /** 097 * @return The new dataSize ONLY as a convenience 098 */ 099 long incMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta, 100 int cellsCountDelta); 101 102 default long incMemStoreSize(MemStoreSize delta) { 103 return incMemStoreSize(delta.getDataSize(), delta.getHeapSize(), delta.getOffHeapSize(), 104 delta.getCellsCount()); 105 } 106 107 /** 108 * @return The new dataSize ONLY as a convenience 109 */ 110 default long decMemStoreSize(long dataSizeDelta, long heapSizeDelta, 111 long offHeapSizeDelta, int cellsCountDelta) { 112 return incMemStoreSize(-dataSizeDelta, -heapSizeDelta, -offHeapSizeDelta, -cellsCountDelta); 113 } 114 115 default long decMemStoreSize(MemStoreSize delta) { 116 return incMemStoreSize(-delta.getDataSize(), -delta.getHeapSize(), -delta.getOffHeapSize(), 117 -delta.getCellsCount()); 118 } 119 120 boolean compareAndSetDataSize(long expected, long updated); 121 122 long getDataSize(); 123 long getHeapSize(); 124 long getOffHeapSize(); 125 int getCellsCount(); 126 127 /** 128 * @return Use this datastructure to return all three settings, {@link #getDataSize()}, 129 * {@link #getHeapSize()}, and {@link #getOffHeapSize()}, in the one go. 130 */ 131 MemStoreSize getMemStoreSize(); 132}