HBASE-26286: Add support for specifying store file tracker when restoring or cloning...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / IncreasingToUpperBoundRegionSplitPolicy.java
blob97f9851d4ccd3b630638bb070033b3b6f6350031
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.regionserver;
20 import java.io.IOException;
21 import java.util.List;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HConstants;
25 import org.apache.hadoop.hbase.TableName;
26 import org.apache.yetus.audience.InterfaceAudience;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.apache.hadoop.hbase.client.TableDescriptor;
30 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
32 /**
33 * Split size is the number of regions that are on this server that all are
34 * of the same table, cubed, times 2x the region flush size OR the maximum
35 * region split size, whichever is smaller.
36 * <p>
37 * For example, if the flush size is 128MB, then after two flushes (256MB) we
38 * will split which will make two regions that will split when their size is
39 * {@code 2^3 * 128MB*2 = 2048MB}.
40 * <p>
41 * If one of these regions splits, then there are three regions and now the
42 * split size is {@code 3^3 * 128MB*2 = 6912MB}, and so on until we reach the configured
43 * maximum file size and then from there on out, we'll use that.
45 @InterfaceAudience.Private
46 public class IncreasingToUpperBoundRegionSplitPolicy extends ConstantSizeRegionSplitPolicy {
47 private static final Logger LOG =
48 LoggerFactory.getLogger(IncreasingToUpperBoundRegionSplitPolicy.class);
50 protected long initialSize;
52 @Override
53 public String toString() {
54 return "IncreasingToUpperBoundRegionSplitPolicy{" + "initialSize=" + initialSize +
55 ", " + super.toString() + '}';
58 @Override
59 protected void configureForRegion(HRegion region) {
60 super.configureForRegion(region);
61 Configuration conf = getConf();
62 initialSize = conf.getLong("hbase.increasing.policy.initial.size", -1);
63 if (initialSize > 0) {
64 return;
66 TableDescriptor desc = region.getTableDescriptor();
67 if (desc != null) {
68 initialSize = 2 * desc.getMemStoreFlushSize();
70 if (initialSize <= 0) {
71 initialSize = 2 * conf.getLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
72 TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE);
76 @Override
77 protected boolean shouldSplit() {
78 if (!canSplit()) {
79 return false;
81 // Get count of regions that have the same common table as this.region
82 int tableRegionsCount = getCountOfCommonTableRegions();
83 // Get size to check
84 long sizeToCheck = getSizeToCheck(tableRegionsCount);
85 boolean shouldSplit = isExceedSize(sizeToCheck);
86 if (shouldSplit) {
87 LOG.debug("regionsWithCommonTable={}", tableRegionsCount);
89 return shouldSplit;
92 /**
93 * @return Count of regions on this server that share the table this.region
94 * belongs to
96 private int getCountOfCommonTableRegions() {
97 RegionServerServices rss = region.getRegionServerServices();
98 // Can be null in tests
99 if (rss == null) {
100 return 0;
102 TableName tablename = region.getTableDescriptor().getTableName();
103 int tableRegionsCount = 0;
104 try {
105 List<? extends Region> hri = rss.getRegions(tablename);
106 tableRegionsCount = hri == null || hri.isEmpty() ? 0 : hri.size();
107 } catch (IOException e) {
108 LOG.debug("Failed getOnlineRegions " + tablename, e);
110 return tableRegionsCount;
114 * @return Region max size or {@code count of regions cubed * 2 * flushsize},
115 * which ever is smaller; guard against there being zero regions on this server.
117 protected long getSizeToCheck(final int tableRegionsCount) {
118 // safety check for 100 to avoid numerical overflow in extreme cases
119 return tableRegionsCount == 0 || tableRegionsCount > 100
120 ? getDesiredMaxFileSize()
121 : Math.min(getDesiredMaxFileSize(),
122 initialSize * tableRegionsCount * tableRegionsCount * tableRegionsCount);