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.Iterator; 021import java.util.Map.Entry; 022import java.util.concurrent.ConcurrentHashMap; 023import org.apache.hadoop.hbase.client.RegionInfo; 024import org.apache.hadoop.hbase.util.Bytes; 025import org.apache.hadoop.hbase.util.ClassSize; 026import org.apache.yetus.audience.InterfaceAudience; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * A {@link RegionSizeStore} implementation backed by a ConcurrentHashMap. We expected similar 032 * amounts of reads and writes to the "store", so using a RWLock is not going to provide any 033 * exceptional gains. 034 */ 035@InterfaceAudience.Private 036public class RegionSizeStoreImpl implements RegionSizeStore { 037 private static final Logger LOG = LoggerFactory.getLogger(RegionSizeStoreImpl.class); 038 private static final long sizeOfEntry = 039 ClassSize.align(ClassSize.CONCURRENT_HASHMAP_ENTRY + ClassSize.OBJECT + Bytes.SIZEOF_LONG 040 // TODO Have RegionInfo implement HeapSize. 100B is an approximation based on a heapdump. 041 + ClassSize.OBJECT + 100); 042 private final ConcurrentHashMap<RegionInfo, RegionSize> store; 043 044 public RegionSizeStoreImpl() { 045 store = new ConcurrentHashMap<>(); 046 } 047 048 @Override 049 public Iterator<Entry<RegionInfo, RegionSize>> iterator() { 050 return store.entrySet().iterator(); 051 } 052 053 @Override 054 public RegionSize getRegionSize(RegionInfo regionInfo) { 055 return store.get(regionInfo); 056 } 057 058 @Override 059 public void put(RegionInfo regionInfo, long size) { 060 if (LOG.isTraceEnabled()) { 061 LOG.trace("Setting space quota size for " + regionInfo + " to " + size); 062 } 063 // Atomic. Either sets the new size for the first time, or replaces the existing value. 064 store.compute(regionInfo, 065 (key, value) -> value == null ? new RegionSizeImpl(size) : value.setSize(size)); 066 } 067 068 @Override 069 public void incrementRegionSize(RegionInfo regionInfo, long delta) { 070 if (LOG.isTraceEnabled()) { 071 LOG.trace("Updating space quota size for " + regionInfo + " with a delta of " + delta); 072 } 073 // Atomic. Recomputes the stored value with the delta if there is one, otherwise use the delta. 074 store.compute(regionInfo, 075 (key, value) -> value == null ? new RegionSizeImpl(delta) : value.incrementSize(delta)); 076 } 077 078 @Override 079 public RegionSize remove(RegionInfo regionInfo) { 080 return store.remove(regionInfo); 081 } 082 083 @Override 084 public long heapSize() { 085 // Will have to iterate over each element if RegionInfo implements HeapSize, for now it's just 086 // a simple calculation. 087 return sizeOfEntry * store.size(); 088 } 089 090 @Override 091 public int size() { 092 return store.size(); 093 } 094 095 @Override 096 public boolean isEmpty() { 097 return store.isEmpty(); 098 } 099 100 @Override 101 public void clear() { 102 store.clear(); 103 } 104}