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