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 java.util.concurrent.atomic.AtomicInteger; 021import java.util.concurrent.atomic.AtomicLong; 022import org.apache.yetus.audience.InterfaceAudience; 023 024/** 025 * Accounting of current heap and data sizes. Thread-safe. Many threads can do updates against this 026 * single instance. 027 * @see NonThreadSafeMemStoreSizing 028 * @see MemStoreSize 029 */ 030@InterfaceAudience.Private 031class ThreadSafeMemStoreSizing implements MemStoreSizing { 032 // We used to tie the update of these thread counters so 033 // they all changed together under one lock. This was 034 // undone. Doesn't seem necessary. 035 private final AtomicLong dataSize = new AtomicLong(); 036 private final AtomicLong heapSize = new AtomicLong(); 037 private final AtomicLong offHeapSize = new AtomicLong(); 038 private final AtomicInteger cellsCount = new AtomicInteger(); 039 040 ThreadSafeMemStoreSizing() { 041 this(0, 0, 0, 0); 042 } 043 044 ThreadSafeMemStoreSizing(MemStoreSize mss) { 045 this(mss.getDataSize(), mss.getHeapSize(), mss.getOffHeapSize(), mss.getCellsCount()); 046 } 047 048 ThreadSafeMemStoreSizing(long dataSize, long heapSize, long offHeapSize, int cellsCount) { 049 incMemStoreSize(dataSize, heapSize, offHeapSize, cellsCount); 050 } 051 052 public MemStoreSize getMemStoreSize() { 053 return new MemStoreSize(getDataSize(), getHeapSize(), getOffHeapSize(), getCellsCount()); 054 } 055 056 @Override 057 public long incMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta, 058 int cellsCountDelta) { 059 this.offHeapSize.addAndGet(offHeapSizeDelta); 060 this.heapSize.addAndGet(heapSizeDelta); 061 this.cellsCount.addAndGet(cellsCountDelta); 062 return this.dataSize.addAndGet(dataSizeDelta); 063 } 064 065 @Override 066 public boolean compareAndSetDataSize(long expected, long updated) { 067 return dataSize.compareAndSet(expected, updated); 068 } 069 070 @Override 071 public long getDataSize() { 072 return dataSize.get(); 073 } 074 075 @Override 076 public long getHeapSize() { 077 return heapSize.get(); 078 } 079 080 @Override 081 public long getOffHeapSize() { 082 return offHeapSize.get(); 083 } 084 085 @Override 086 public int getCellsCount() { 087 return cellsCount.get(); 088 } 089 090 @Override 091 public String toString() { 092 return getMemStoreSize().toString(); 093 } 094}