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.Objects; 025import java.util.Set; 026import java.util.TreeMap; 027import java.util.stream.Collectors; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.HRegionLocation; 030import org.apache.hadoop.hbase.RegionMetrics; 031import org.apache.hadoop.hbase.ServerName; 032import org.apache.hadoop.hbase.Size; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.RegionLocator; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.yetus.audience.InterfaceAudience; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 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 = ((long) (regionLoad.getStoreFileSize().get(Size.Unit.MEGABYTE) 086 + regionLoad.getMemStoreSize().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 // The region locations could contain `null` ServerName instances if the region is currently 100 // in transition, we filter those out for now, which impacts the size calculation for these 101 // regions temporarily until the ServerName gets filled in later 102 return regionLocator.getAllRegionLocations().stream().map(HRegionLocation::getServerName) 103 .filter(Objects::nonNull).collect(Collectors.toSet()); 104 } 105 106 boolean enabled(Configuration configuration) { 107 return configuration.getBoolean(ENABLE_REGIONSIZECALCULATOR, true); 108 } 109 110 /** 111 * Returns size of given region in bytes. Returns 0 if region was not found. 112 */ 113 public long getRegionSize(byte[] regionId) { 114 Long size = sizeMap.get(regionId); 115 if (size == null) { 116 LOG.debug("Unknown region:" + Arrays.toString(regionId)); 117 return 0; 118 } else { 119 return size; 120 } 121 } 122 123 public Map<byte[], Long> getRegionSizeMap() { 124 return Collections.unmodifiableMap(sizeMap); 125 } 126}