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