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, squared, times the region flush size OR the maximum
33   * region split size, whichever is smaller.  For example, if the flush size
34   * is 128M, then on first flush we will split which will make two regions
35   * that will split when their size is 2 * 2 * 128M = 512M.  If one of these
36   * regions splits, then there are three regions and now the split size is
37   * 3 * 3 * 128M =  1152M, 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 flushSize;
45  
46    @Override
47    protected void configureForRegion(HRegion region) {
48      super.configureForRegion(region);
49      Configuration conf = getConf();
50      HTableDescriptor desc = region.getTableDesc();
51      if (desc != null) {
52        this.flushSize = desc.getMemStoreFlushSize();
53      }
54      if (this.flushSize <= 0) {
55        this.flushSize = conf.getLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
56          HTableDescriptor.DEFAULT_MEMSTORE_FLUSH_SIZE);
57      }
58    }
59  
60    @Override
61    protected boolean shouldSplit() {
62      if (region.shouldForceSplit()) return true;
63      boolean foundABigStore = false;
64      // Get count of regions that have the same common table as this.region
65      int tableRegionsCount = getCountOfCommonTableRegions();
66      // Get size to check
67      long sizeToCheck = getSizeToCheck(tableRegionsCount);
68  
69      for (Store store : region.getStores().values()) {
70        // If any of the stores is unable to split (eg they contain reference files)
71        // then don't split
72        if ((!store.canSplit())) {
73          return false;
74        }
75  
76        // Mark if any store is big enough
77        long size = store.getSize();
78        if (size > sizeToCheck) {
79          LOG.debug("ShouldSplit because " + store.getColumnFamilyName() +
80            " size=" + size + ", sizeToCheck=" + sizeToCheck +
81            ", regionsWithCommonTable=" + tableRegionsCount);
82          foundABigStore = true;
83        }
84      }
85  
86      return foundABigStore;
87    }
88  
89    /**
90     * @return Region max size or <code>count of regions squared * flushsize, which ever is
91     * smaller; guard against there being zero regions on this server.
92     */
93    long getSizeToCheck(final int tableRegionsCount) {
94      return tableRegionsCount == 0? getDesiredMaxFileSize():
95        Math.min(getDesiredMaxFileSize(),
96          this.flushSize * (tableRegionsCount * (long)tableRegionsCount));
97    }
98  
99    /**
100    * @return Count of regions on this server that share the table this.region
101    * belongs to
102    */
103   private int getCountOfCommonTableRegions() {
104     RegionServerServices rss = this.region.getRegionServerServices();
105     // Can be null in tests
106     if (rss == null) return 0;
107     TableName tablename = this.region.getTableDesc().getTableName();
108     int tableRegionsCount = 0;
109     try {
110       List<HRegion> hri = rss.getOnlineRegions(tablename);
111       tableRegionsCount = hri == null || hri.isEmpty()? 0: hri.size();
112     } catch (IOException e) {
113       LOG.debug("Failed getOnlineRegions " + tablename, e);
114     }
115     return tableRegionsCount;
116   }
117 }