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 org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
021
022import java.util.Random;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.HTableDescriptor;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030
031/**
032 * A {@link RegionSplitPolicy} implementation which splits a region
033 * as soon as any of its store files exceeds a maximum configurable
034 * size.
035 * <p>
036 * This is the default split policy. From 0.94.0 on the default split policy has
037 * changed to {@link IncreasingToUpperBoundRegionSplitPolicy}
038 * </p>
039 */
040@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
041public class ConstantSizeRegionSplitPolicy extends RegionSplitPolicy {
042  private static final Random RANDOM = new Random();
043
044  private long desiredMaxFileSize;
045  private double jitterRate;
046
047  @Override
048  protected void configureForRegion(HRegion region) {
049    super.configureForRegion(region);
050    Configuration conf = getConf();
051    TableDescriptor desc = region.getTableDescriptor();
052    if (desc != null) {
053      this.desiredMaxFileSize = desc.getMaxFileSize();
054    }
055    if (this.desiredMaxFileSize <= 0) {
056      this.desiredMaxFileSize = conf.getLong(HConstants.HREGION_MAX_FILESIZE,
057        HConstants.DEFAULT_MAX_FILE_SIZE);
058    }
059    double jitter = conf.getDouble("hbase.hregion.max.filesize.jitter", 0.25D);
060    this.jitterRate = (RANDOM.nextFloat() - 0.5D) * jitter;
061    long jitterValue = (long) (this.desiredMaxFileSize * this.jitterRate);
062    // make sure the long value won't overflow with jitter
063    if (this.jitterRate > 0 && jitterValue > (Long.MAX_VALUE - this.desiredMaxFileSize)) {
064      this.desiredMaxFileSize = Long.MAX_VALUE;
065    } else {
066      this.desiredMaxFileSize += jitterValue;
067    }
068  }
069
070  @Override
071  protected boolean shouldSplit() {
072    boolean force = region.shouldForceSplit();
073    boolean foundABigStore = false;
074
075    for (HStore store : region.getStores()) {
076      // If any of the stores are unable to split (eg they contain reference files)
077      // then don't split
078      if ((!store.canSplit())) {
079        return false;
080      }
081
082      // Mark if any store is big enough
083      if (store.getSize() > desiredMaxFileSize) {
084        foundABigStore = true;
085      }
086    }
087
088    return foundABigStore || force;
089  }
090
091  long getDesiredMaxFileSize() {
092    return desiredMaxFileSize;
093  }
094
095  @VisibleForTesting
096  public boolean positiveJitterRate() {
097    return this.jitterRate > 0;
098  }
099}