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