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.Iterator;
020import java.util.Map.Entry;
021import java.util.concurrent.ConcurrentHashMap;
022
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 = ClassSize.align(
039      ClassSize.CONCURRENT_HASHMAP_ENTRY
040      + ClassSize.OBJECT + Bytes.SIZEOF_LONG
041      // TODO Have RegionInfo implement HeapSize. 100B is an approximation based on a heapdump.
042      + ClassSize.OBJECT + 100);
043  private final ConcurrentHashMap<RegionInfo,RegionSize> store;
044
045  public RegionSizeStoreImpl() {
046    store = new ConcurrentHashMap<>();
047  }
048
049  @Override
050  public Iterator<Entry<RegionInfo,RegionSize>> iterator() {
051    return store.entrySet().iterator();
052  }
053
054  @Override
055  public RegionSize getRegionSize(RegionInfo regionInfo) {
056    return store.get(regionInfo);
057  }
058
059  @Override
060  public void put(RegionInfo regionInfo, long size) {
061    if (LOG.isTraceEnabled()) {
062      LOG.trace("Setting space quota size for " + regionInfo + " to " + size);
063    }
064    // Atomic. Either sets the new size for the first time, or replaces the existing value.
065    store.compute(regionInfo,
066      (key,value) -> value == null ? new RegionSizeImpl(size) : value.setSize(size));
067  }
068
069  @Override
070  public void incrementRegionSize(RegionInfo regionInfo, long delta) {
071    if (LOG.isTraceEnabled()) {
072      LOG.trace("Updating space quota size for " + regionInfo + " with a delta of " + delta);
073    }
074    // Atomic. Recomputes the stored value with the delta if there is one, otherwise use the delta.
075    store.compute(regionInfo,
076      (key,value) -> value == null ? new RegionSizeImpl(delta) : value.incrementSize(delta));
077  }
078
079  @Override
080  public RegionSize remove(RegionInfo regionInfo) {
081    return store.remove(regionInfo);
082  }
083
084  @Override
085  public long heapSize() {
086    // Will have to iterate over each element if RegionInfo implements HeapSize, for now it's just
087    // a simple calculation.
088    return sizeOfEntry * store.size();
089  }
090
091  @Override
092  public int size() {
093    return store.size();
094  }
095
096  @Override
097  public boolean isEmpty() {
098    return store.isEmpty();
099  }
100
101  @Override
102  public void clear() {
103    store.clear();
104  }
105}