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.regionserver;
019
020import java.io.IOException;
021import java.util.List;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031
032/**
033 * Split size is the number of regions that are on this server that all are
034 * of the same table, cubed, times 2x the region flush size OR the maximum
035 * region split size, whichever is smaller.
036 * <p>
037 * For example, if the flush size is 128MB, then after two flushes (256MB) we
038 * will split which will make two regions that will split when their size is
039 * {@code 2^3 * 128MB*2 = 2048MB}.
040 * <p>
041 * If one of these regions splits, then there are three regions and now the
042 * split size is {@code 3^3 * 128MB*2 = 6912MB}, and so on until we reach the configured
043 * maximum file size and then from there on out, we'll use that.
044 */
045@InterfaceAudience.Private
046public class IncreasingToUpperBoundRegionSplitPolicy extends ConstantSizeRegionSplitPolicy {
047  private static final Logger LOG =
048      LoggerFactory.getLogger(IncreasingToUpperBoundRegionSplitPolicy.class);
049
050  protected long initialSize;
051
052  @Override
053  protected void configureForRegion(HRegion region) {
054    super.configureForRegion(region);
055    Configuration conf = getConf();
056    initialSize = conf.getLong("hbase.increasing.policy.initial.size", -1);
057    if (initialSize > 0) {
058      return;
059    }
060    TableDescriptor desc = region.getTableDescriptor();
061    if (desc != null) {
062      initialSize = 2 * desc.getMemStoreFlushSize();
063    }
064    if (initialSize <= 0) {
065      initialSize = 2 * conf.getLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
066                                     TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE);
067    }
068  }
069
070  @Override
071  protected boolean shouldSplit() {
072    if (!canSplit()) {
073      return false;
074    }
075    // Get count of regions that have the same common table as this.region
076    int tableRegionsCount = getCountOfCommonTableRegions();
077    // Get size to check
078    long sizeToCheck = getSizeToCheck(tableRegionsCount);
079    boolean shouldSplit = isExceedSize(sizeToCheck);
080    if (shouldSplit) {
081      LOG.debug("regionsWithCommonTable={}", tableRegionsCount);
082    }
083    return shouldSplit;
084  }
085
086  /**
087   * @return Count of regions on this server that share the table this.region
088   * belongs to
089   */
090  private int getCountOfCommonTableRegions() {
091    RegionServerServices rss = region.getRegionServerServices();
092    // Can be null in tests
093    if (rss == null) {
094      return 0;
095    }
096    TableName tablename = region.getTableDescriptor().getTableName();
097    int tableRegionsCount = 0;
098    try {
099      List<? extends Region> hri = rss.getRegions(tablename);
100      tableRegionsCount = hri == null || hri.isEmpty() ? 0 : hri.size();
101    } catch (IOException e) {
102      LOG.debug("Failed getOnlineRegions " + tablename, e);
103    }
104    return tableRegionsCount;
105  }
106
107  /**
108   * @return Region max size or {@code count of regions cubed * 2 * flushsize},
109   * which ever is smaller; guard against there being zero regions on this server.
110   */
111  protected long getSizeToCheck(final int tableRegionsCount) {
112    // safety check for 100 to avoid numerical overflow in extreme cases
113    return tableRegionsCount == 0 || tableRegionsCount > 100
114               ? getDesiredMaxFileSize()
115               : Math.min(getDesiredMaxFileSize(),
116                          initialSize * tableRegionsCount * tableRegionsCount * tableRegionsCount);
117  }
118
119}