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.quotas; 019 020import java.util.concurrent.atomic.AtomicLong; 021import org.apache.hadoop.hbase.util.ClassSize; 022import org.apache.yetus.audience.InterfaceAudience; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026/** 027 * An object encapsulating a Region's size and whether it's been reported to the master since the 028 * value last changed. 029 */ 030@InterfaceAudience.Private 031public class RegionSizeImpl implements RegionSize { 032 private static final Logger LOG = LoggerFactory.getLogger(RegionSizeImpl.class); 033 private static final long HEAP_SIZE = 034 ClassSize.OBJECT + ClassSize.ATOMIC_LONG + ClassSize.REFERENCE; 035 private final AtomicLong size; 036 037 public RegionSizeImpl(long initialSize) { 038 // A region can never be negative in size. We can prevent this from being a larger problem, but 039 // we will need to leave ourselves a note to figure out how we got here. 040 if (initialSize < 0L && LOG.isTraceEnabled()) { 041 LOG.trace("Nonsensical negative Region size being constructed, this is likely an error", 042 new Exception()); 043 } 044 this.size = new AtomicLong(initialSize < 0L ? 0L : initialSize); 045 } 046 047 @Override 048 public long heapSize() { 049 return HEAP_SIZE; 050 } 051 052 @Override 053 public RegionSizeImpl setSize(long newSize) { 054 // Set the new size before advertising that we need to tell the master about it. Worst case 055 // we have to wait for the next period to report it. 056 size.set(newSize); 057 return this; 058 } 059 060 @Override 061 public RegionSizeImpl incrementSize(long delta) { 062 size.addAndGet(delta); 063 return this; 064 } 065 066 @Override 067 public long getSize() { 068 return size.get(); 069 } 070}