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.util.concurrent.ThreadLocalRandom;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseInterfaceAudience;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.client.TableDescriptor;
025import org.apache.hadoop.hbase.procedure2.util.StringUtils;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * A {@link RegionSplitPolicy} implementation which splits a region as soon as any of its store
032 * files exceeds a maximum configurable size.
033 * <p>
034 * This is the default split policy. From 0.94.0 on the default split policy has changed to
035 * {@link IncreasingToUpperBoundRegionSplitPolicy}
036 * </p>
037 */
038@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
039public class ConstantSizeRegionSplitPolicy extends RegionSplitPolicy {
040  private static final Logger LOG = LoggerFactory.getLogger(ConstantSizeRegionSplitPolicy.class);
041  private long desiredMaxFileSize;
042  private double jitterRate;
043  protected boolean overallHRegionFiles;
044
045  @Override
046  public String toString() {
047    return "ConstantSizeRegionSplitPolicy{" + "desiredMaxFileSize=" + desiredMaxFileSize
048      + ", jitterRate=" + jitterRate + '}';
049  }
050
051  @Override
052  protected void configureForRegion(HRegion region) {
053    super.configureForRegion(region);
054    Configuration conf = getConf();
055    TableDescriptor desc = region.getTableDescriptor();
056    if (desc != null) {
057      this.desiredMaxFileSize = desc.getMaxFileSize();
058    }
059    if (this.desiredMaxFileSize <= 0) {
060      this.desiredMaxFileSize =
061        conf.getLong(HConstants.HREGION_MAX_FILESIZE, HConstants.DEFAULT_MAX_FILE_SIZE);
062    }
063    this.overallHRegionFiles =
064      conf.getBoolean(HConstants.OVERALL_HREGION_FILES, HConstants.DEFAULT_OVERALL_HREGION_FILES);
065    double jitter = conf.getDouble("hbase.hregion.max.filesize.jitter", 0.25D);
066    this.jitterRate = (ThreadLocalRandom.current().nextFloat() - 0.5D) * jitter;
067    long jitterValue = (long) (this.desiredMaxFileSize * this.jitterRate);
068    // Default jitter is ~12% +/-. Make sure the long value won't overflow with jitter
069    if (this.jitterRate > 0 && jitterValue > (Long.MAX_VALUE - this.desiredMaxFileSize)) {
070      this.desiredMaxFileSize = Long.MAX_VALUE;
071    } else {
072      this.desiredMaxFileSize += jitterValue;
073    }
074  }
075
076  @Override
077  protected boolean shouldSplit() {
078    if (!canSplit()) {
079      return false;
080    }
081    return isExceedSize(desiredMaxFileSize);
082  }
083
084  long getDesiredMaxFileSize() {
085    return desiredMaxFileSize;
086  }
087
088  @InterfaceAudience.Private
089  public boolean positiveJitterRate() {
090    return this.jitterRate > 0;
091  }
092
093  /** Returns true if region size exceed the sizeToCheck */
094  protected final boolean isExceedSize(long sizeToCheck) {
095    if (overallHRegionFiles) {
096      long sumSize = 0;
097      for (HStore store : region.getStores()) {
098        sumSize += store.getSize();
099      }
100      if (sumSize > sizeToCheck) {
101        LOG.debug("Should split because region size is big enough " + "sumSize={}, sizeToCheck={}",
102          StringUtils.humanSize(sumSize), StringUtils.humanSize(sizeToCheck));
103        return true;
104      }
105    } else {
106      for (HStore store : region.getStores()) {
107        long size = store.getSize();
108        if (size > sizeToCheck) {
109          LOG.debug("Should split because {} size={}, sizeToCheck={}", store.getColumnFamilyName(),
110            StringUtils.humanSize(size), StringUtils.humanSize(sizeToCheck));
111          return true;
112        }
113      }
114    }
115    return false;
116  }
117}