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