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