001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to you under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.hadoop.hbase.quotas; 018 019import java.util.concurrent.atomic.AtomicLong; 020 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 028 * the 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 = ClassSize.OBJECT + ClassSize.ATOMIC_LONG + 034 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}