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