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.mapreduce;
019
020import java.io.IOException;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.Map;
024import java.util.Set;
025import java.util.TreeMap;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HRegionLocation;
028import org.apache.hadoop.hbase.RegionMetrics;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.Size;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.client.RegionLocator;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.yetus.audience.InterfaceAudience;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
039
040/**
041 * Computes size of each region for given table and given column families.
042 * The value is used by MapReduce for better scheduling.
043 * */
044@InterfaceAudience.Private
045public class RegionSizeCalculator {
046
047  private static final Logger LOG = LoggerFactory.getLogger(RegionSizeCalculator.class);
048
049  /**
050   * Maps each region to its size in bytes.
051   * */
052  private final Map<byte[], Long> sizeMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
053
054  static final String ENABLE_REGIONSIZECALCULATOR = "hbase.regionsizecalculator.enable";
055  private static final long MEGABYTE = 1024L * 1024L;
056
057  /**
058   * Computes size of each region for table and given column families.
059   * */
060  public RegionSizeCalculator(RegionLocator regionLocator, Admin admin) throws IOException {
061    init(regionLocator, admin);
062  }
063
064  private void init(RegionLocator regionLocator, Admin admin)
065      throws IOException {
066    if (!enabled(admin.getConfiguration())) {
067      LOG.info("Region size calculation disabled.");
068      return;
069    }
070
071    if (regionLocator.getName().isSystemTable()) {
072      LOG.info("Region size calculation disabled for system tables.");
073      return;
074    }
075
076    LOG.info("Calculating region sizes for table \"" + regionLocator.getName() + "\".");
077
078    // Get the servers which host regions of the table
079    Set<ServerName> tableServers = getRegionServersOfTable(regionLocator);
080
081    for (ServerName tableServerName : tableServers) {
082      for (RegionMetrics regionLoad : admin.getRegionMetrics(
083        tableServerName,regionLocator.getName())) {
084
085        byte[] regionId = regionLoad.getRegionName();
086        long regionSizeBytes
087          = ((long) regionLoad.getStoreFileSize().get(Size.Unit.MEGABYTE)) * MEGABYTE;
088
089        sizeMap.put(regionId, regionSizeBytes);
090
091        if (LOG.isDebugEnabled()) {
092          LOG.debug("Region " + regionLoad.getNameAsString() + " has size " + regionSizeBytes);
093        }
094      }
095    }
096    LOG.debug("Region sizes calculated");
097  }
098
099  private Set<ServerName> getRegionServersOfTable(RegionLocator regionLocator)
100      throws IOException {
101
102    Set<ServerName> tableServers = Sets.newHashSet();
103    for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) {
104      tableServers.add(regionLocation.getServerName());
105    }
106    return tableServers;
107  }
108
109  boolean enabled(Configuration configuration) {
110    return configuration.getBoolean(ENABLE_REGIONSIZECALCULATOR, true);
111  }
112
113  /**
114   * Returns size of given region in bytes. Returns 0 if region was not found.
115   * */
116  public long getRegionSize(byte[] regionId) {
117    Long size = sizeMap.get(regionId);
118    if (size == null) {
119      LOG.debug("Unknown region:" + Arrays.toString(regionId));
120      return 0;
121    } else {
122      return size;
123    }
124  }
125
126  public Map<byte[], Long> getRegionSizeMap() {
127    return Collections.unmodifiableMap(sizeMap);
128  }
129}