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 */ 018 019package org.apache.hadoop.hbase.regionserver; 020 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hbase.HBaseInterfaceAudience; 023import org.apache.yetus.audience.InterfaceAudience; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 027 028/** 029 * This class represents a split policy which makes the split decision based 030 * on how busy a region is. The metric that is used here is the fraction of 031 * total write requests that are blocked due to high memstore utilization. 032 * This fractional rate is calculated over a running window of 033 * "hbase.busy.policy.aggWindow" milliseconds. The rate is a time-weighted 034 * aggregated average of the rate in the current window and the 035 * true average rate in the previous window. 036 * 037 */ 038 039@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) 040public class BusyRegionSplitPolicy extends IncreasingToUpperBoundRegionSplitPolicy { 041 042 private static final Logger LOG = LoggerFactory.getLogger(BusyRegionSplitPolicy.class); 043 044 // Maximum fraction blocked write requests before region is considered for split 045 private float maxBlockedRequests; 046 public static final float DEFAULT_MAX_BLOCKED_REQUESTS = 0.2f; 047 048 // Minimum age of the region in milliseconds before it is considered for split 049 private long minAge = -1; 050 public static final long DEFAULT_MIN_AGE_MS = 600000; // 10 minutes 051 052 // The window time in milliseconds over which the blocked requests rate is calculated 053 private long aggregationWindow; 054 public static final long DEFAULT_AGGREGATION_WINDOW = 300000; // 5 minutes 055 056 private HRegion region; 057 private long prevTime; 058 private long startTime; 059 private long writeRequestCount; 060 private long blockedRequestCount; 061 private float blockedRate; 062 063 @Override 064 protected void configureForRegion(final HRegion region) { 065 super.configureForRegion(region); 066 this.region = region; 067 Configuration conf = getConf(); 068 069 maxBlockedRequests = conf.getFloat("hbase.busy.policy.blockedRequests", 070 DEFAULT_MAX_BLOCKED_REQUESTS); 071 minAge = conf.getLong("hbase.busy.policy.minAge", DEFAULT_MIN_AGE_MS); 072 aggregationWindow = conf.getLong("hbase.busy.policy.aggWindow", 073 DEFAULT_AGGREGATION_WINDOW); 074 075 if (maxBlockedRequests < 0.00001f || maxBlockedRequests > 0.99999f) { 076 LOG.warn("Threshold for maximum blocked requests is set too low or too high, " 077 + " resetting to default of " + DEFAULT_MAX_BLOCKED_REQUESTS); 078 maxBlockedRequests = DEFAULT_MAX_BLOCKED_REQUESTS; 079 } 080 081 if (aggregationWindow <= 0) { 082 LOG.warn("Aggregation window size is too low: " + aggregationWindow 083 + ". Resetting it to default of " + DEFAULT_AGGREGATION_WINDOW); 084 aggregationWindow = DEFAULT_AGGREGATION_WINDOW; 085 } 086 087 init(); 088 } 089 090 private synchronized void init() { 091 startTime = EnvironmentEdgeManager.currentTime(); 092 prevTime = startTime; 093 blockedRequestCount = region.getBlockedRequestsCount(); 094 writeRequestCount = region.getWriteRequestsCount(); 095 } 096 097 @Override 098 protected boolean shouldSplit() { 099 float blockedReqRate = updateRate(); 100 if (super.shouldSplit()) { 101 return true; 102 } 103 104 if (EnvironmentEdgeManager.currentTime() < startTime + minAge) { 105 return false; 106 } 107 108 for (HStore store: region.getStores()) { 109 if (!store.canSplit()) { 110 return false; 111 } 112 } 113 114 if (blockedReqRate >= maxBlockedRequests) { 115 if (LOG.isDebugEnabled()) { 116 LOG.debug("Going to split region " + region.getRegionInfo().getRegionNameAsString() 117 + " because it's too busy. Blocked Request rate: " + blockedReqRate); 118 } 119 return true; 120 } 121 122 return false; 123 } 124 125 /** 126 * Update the blocked request rate based on number of blocked and total write requests in the 127 * last aggregation window, or since last call to this method, whichever is farthest in time. 128 * Uses weighted rate calculation based on the previous rate and new data. 129 * 130 * @return Updated blocked request rate. 131 */ 132 private synchronized float updateRate() { 133 float aggBlockedRate; 134 long curTime = EnvironmentEdgeManager.currentTime(); 135 136 long newBlockedReqs = region.getBlockedRequestsCount(); 137 long newWriteReqs = region.getWriteRequestsCount(); 138 139 aggBlockedRate = 140 (newBlockedReqs - blockedRequestCount) / (newWriteReqs - writeRequestCount + 0.00001f); 141 142 if (curTime - prevTime >= aggregationWindow) { 143 blockedRate = aggBlockedRate; 144 prevTime = curTime; 145 blockedRequestCount = newBlockedReqs; 146 writeRequestCount = newWriteReqs; 147 } else if (curTime - startTime >= aggregationWindow) { 148 // Calculate the aggregate blocked rate as the weighted sum of 149 // previous window's average blocked rate and blocked rate in this window so far. 150 float timeSlice = (curTime - prevTime) / (aggregationWindow + 0.0f); 151 aggBlockedRate = (1 - timeSlice) * blockedRate + timeSlice * aggBlockedRate; 152 } else { 153 aggBlockedRate = 0.0f; 154 } 155 return aggBlockedRate; 156 } 157}