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. The value is used by 042 * 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) throws IOException { 065 if (!enabled(admin.getConfiguration())) { 066 LOG.info("Region size calculation disabled."); 067 return; 068 } 069 070 if (regionLocator.getName().isSystemTable()) { 071 LOG.info("Region size calculation disabled for system tables."); 072 return; 073 } 074 075 LOG.info("Calculating region sizes for table \"" + regionLocator.getName() + "\"."); 076 077 // Get the servers which host regions of the table 078 Set<ServerName> tableServers = getRegionServersOfTable(regionLocator); 079 080 for (ServerName tableServerName : tableServers) { 081 for (RegionMetrics regionLoad : admin.getRegionMetrics(tableServerName, 082 regionLocator.getName())) { 083 084 byte[] regionId = regionLoad.getRegionName(); 085 long regionSizeBytes = 086 ((long) regionLoad.getStoreFileSize().get(Size.Unit.MEGABYTE)) * MEGABYTE; 087 088 sizeMap.put(regionId, regionSizeBytes); 089 090 if (LOG.isDebugEnabled()) { 091 LOG.debug("Region " + regionLoad.getNameAsString() + " has size " + regionSizeBytes); 092 } 093 } 094 } 095 LOG.debug("Region sizes calculated"); 096 } 097 098 private Set<ServerName> getRegionServersOfTable(RegionLocator regionLocator) throws IOException { 099 100 Set<ServerName> tableServers = Sets.newHashSet(); 101 for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) { 102 tableServers.add(regionLocation.getServerName()); 103 } 104 return tableServers; 105 } 106 107 boolean enabled(Configuration configuration) { 108 return configuration.getBoolean(ENABLE_REGIONSIZECALCULATOR, true); 109 } 110 111 /** 112 * Returns size of given region in bytes. Returns 0 if region was not found. 113 */ 114 public long getRegionSize(byte[] regionId) { 115 Long size = sizeMap.get(regionId); 116 if (size == null) { 117 LOG.debug("Unknown region:" + Arrays.toString(regionId)); 118 return 0; 119 } else { 120 return size; 121 } 122 } 123 124 public Map<byte[], Long> getRegionSizeMap() { 125 return Collections.unmodifiableMap(sizeMap); 126 } 127}