View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  
31  /**
32   * Split size is the number of regions that are on this server that all are
33   * of the same table, cubed, times 2x the region flush size OR the maximum
34   * region split size, whichever is smaller.  For example, if the flush size
35   * is 128M, then after two flushes (256MB) we will split which will make two regions
36   * that will split when their size is 2^3 * 128M*2 = 2048M.  If one of these
37   * regions splits, then there are three regions and now the split size is
38   * 3^3 * 128M*2 =  6912M, and so on until we reach the configured
39   * maximum filesize and then from there on out, we'll use that.
40   */
41  @InterfaceAudience.Private
42  public class IncreasingToUpperBoundRegionSplitPolicy
43  extends ConstantSizeRegionSplitPolicy {
44    static final Log LOG =
45      LogFactory.getLog(IncreasingToUpperBoundRegionSplitPolicy.class);
46    private long initialSize;
47  
48    @Override
49    protected void configureForRegion(HRegion region) {
50      super.configureForRegion(region);
51      Configuration conf = getConf();
52      this.initialSize = conf.getLong("hbase.increasing.policy.initial.size", -1);
53      if (this.initialSize > 0) {
54        return;
55      }
56      HTableDescriptor desc = region.getTableDesc();
57      if (desc != null) {
58        this.initialSize = 2*desc.getMemStoreFlushSize();
59      }
60      if (this.initialSize <= 0) {
61        this.initialSize = 2*conf.getLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
62          HTableDescriptor.DEFAULT_MEMSTORE_FLUSH_SIZE);
63      }
64    }
65  
66    @Override
67    protected boolean shouldSplit() {
68      if (region.shouldForceSplit()) return true;
69      boolean foundABigStore = false;
70      // Get count of regions that have the same common table as this.region
71      int tableRegionsCount = getCountOfCommonTableRegions();
72      // Get size to check
73      long sizeToCheck = getSizeToCheck(tableRegionsCount);
74  
75      for (Store store : region.getStores().values()) {
76        // If any of the stores is unable to split (eg they contain reference files)
77        // then don't split
78        if ((!store.canSplit())) {
79          return false;
80        }
81  
82        // Mark if any store is big enough
83        long size = store.getSize();
84        if (size > sizeToCheck) {
85          LOG.debug("ShouldSplit because " + store.getColumnFamilyName() +
86            " size=" + size + ", sizeToCheck=" + sizeToCheck +
87            ", regionsWithCommonTable=" + tableRegionsCount);
88          foundABigStore = true;
89        }
90      }
91  
92      return foundABigStore;
93    }
94  
95    /**
96     * @return Region max size or <code>count of regions squared * flushsize, which ever is
97     * smaller; guard against there being zero regions on this server.
98     */
99    protected long getSizeToCheck(final int tableRegionsCount) {
100     // safety check for 100 to avoid numerical overflow in extreme cases
101     return tableRegionsCount == 0 || tableRegionsCount > 100 ? getDesiredMaxFileSize():
102       Math.min(getDesiredMaxFileSize(),
103         this.initialSize * tableRegionsCount * tableRegionsCount * tableRegionsCount);
104   }
105 
106   /**
107    * @return Count of regions on this server that share the table this.region
108    * belongs to
109    */
110   private int getCountOfCommonTableRegions() {
111     RegionServerServices rss = this.region.getRegionServerServices();
112     // Can be null in tests
113     if (rss == null) return 0;
114     TableName tablename = this.region.getTableDesc().getTableName();
115     int tableRegionsCount = 0;
116     try {
117       List<HRegion> hri = rss.getOnlineRegions(tablename);
118       tableRegionsCount = hri == null || hri.isEmpty()? 0: hri.size();
119     } catch (IOException e) {
120       LOG.debug("Failed getOnlineRegions " + tablename, e);
121     }
122     return tableRegionsCount;
123   }
124 }