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.quotas;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/*
027 * Internal class used to check and consume quota if exceed throttle quota is enabled. Exceed
028 * throttle quota means, user can over consume user/namespace/table quota if region server has
029 * additional available quota because other users don't consume at the same time.
030 *
031 * There are some limits when enable exceed throttle quota:
032 * 1. Must set at least one read and one write region server throttle quota;
033 * 2. All region server throttle quotas must be in seconds time unit. Because once previous requests
034 * exceed their quota and consume region server quota, quota in other time units may be refilled in
035 * a long time, this may affect later requests.
036 */
037@InterfaceAudience.Private
038@InterfaceStability.Evolving
039public class ExceedOperationQuota extends DefaultOperationQuota {
040  private static final Logger LOG = LoggerFactory.getLogger(ExceedOperationQuota.class);
041  private QuotaLimiter regionServerLimiter;
042
043  public ExceedOperationQuota(final Configuration conf, QuotaLimiter regionServerLimiter,
044    final QuotaLimiter... limiters) {
045    super(conf, limiters);
046    this.regionServerLimiter = regionServerLimiter;
047  }
048
049  @Override
050  public void checkQuota(int numWrites, int numReads, int numScans) throws RpcThrottlingException {
051    if (regionServerLimiter.isBypass()) {
052      // If region server limiter is bypass, which means no region server quota is set, check and
053      // throttle by all other quotas. In this condition, exceed throttle quota will not work.
054      LOG.warn("Exceed throttle quota is enabled but no region server quotas found");
055      super.checkQuota(numWrites, numReads, numScans);
056    } else {
057      // 1. Update estimate quota which will be consumed
058      updateEstimateConsumeQuota(numWrites, numReads, numScans);
059      // 2. Check if region server limiter is enough. If not, throw RpcThrottlingException.
060      regionServerLimiter.checkQuota(numWrites, writeConsumed, numReads + numScans, readConsumed,
061        writeCapacityUnitConsumed, readCapacityUnitConsumed);
062      // 3. Check if other limiters are enough. If not, exceed other limiters because region server
063      // limiter is enough.
064      boolean exceed = false;
065      try {
066        super.checkQuota(numWrites, numReads, numScans);
067      } catch (RpcThrottlingException e) {
068        exceed = true;
069        if (LOG.isDebugEnabled()) {
070          LOG.debug("Read/Write requests num exceeds quota: writes:{} reads:{} scan:{}, "
071            + "try use region server quota", numWrites, numReads, numScans);
072        }
073      }
074      // 4. Region server limiter is enough and grab estimated consume quota.
075      readAvailable = Math.max(readAvailable, regionServerLimiter.getReadAvailable());
076      regionServerLimiter.grabQuota(numWrites, writeConsumed, numReads + numScans, readConsumed,
077        writeCapacityUnitConsumed, writeCapacityUnitConsumed);
078      if (exceed) {
079        // 5. Other quota limiter is exceeded and has not been grabbed (because throw
080        // RpcThrottlingException in Step 3), so grab it.
081        for (final QuotaLimiter limiter : limiters) {
082          limiter.grabQuota(numWrites, writeConsumed, numReads + numScans, readConsumed,
083            writeCapacityUnitConsumed, writeCapacityUnitConsumed);
084        }
085      }
086    }
087  }
088
089  @Override
090  public void close() {
091    super.close();
092    if (writeDiff != 0) {
093      regionServerLimiter.consumeWrite(writeDiff, writeCapacityUnitDiff);
094    }
095    if (readDiff != 0) {
096      regionServerLimiter.consumeRead(readDiff, readCapacityUnitDiff);
097    }
098  }
099}