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  public long incMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
058      int cellsCount) {
059    this.offHeapSize.addAndGet(offHeapSizeDelta);
060    this.heapSize.addAndGet(heapSizeDelta);
061    this.cellsCount.addAndGet(cellsCount);
062    return this.dataSize.addAndGet(dataSizeDelta);
063  }
064
065  @Override
066  public long getDataSize() {
067    return dataSize.get();
068  }
069
070  @Override
071  public long getHeapSize() {
072    return heapSize.get();
073  }
074
075  @Override
076  public long getOffHeapSize() {
077    return offHeapSize.get();
078  }
079
080  @Override
081  public int getCellsCount() {
082    return cellsCount.get();
083  }
084
085  @Override
086  public String toString() {
087    return getMemStoreSize().toString();
088  }
089}