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 java.util.List;
021import org.apache.hadoop.hbase.client.Mutation;
022import org.apache.hadoop.hbase.client.Result;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.yetus.audience.InterfaceStability;
025
026import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
027
028/**
029 * Interface that allows to check the quota available for an operation.
030 */
031@InterfaceAudience.Private
032@InterfaceStability.Evolving
033public interface OperationQuota {
034  public enum OperationType {
035    MUTATE,
036    GET,
037    SCAN,
038    CHECK_AND_MUTATE
039  }
040
041  /**
042   * If false, the default, then IO based throttles will consume read availability based on the
043   * block bytes scanned by the given request. If true then IO based throttles will use result size
044   * rather than block bytes scanned. Using block bytes scanned should be preferable to using result
045   * size, because otherwise access patterns like heavily filtered scans may be able to produce a
046   * significant and effectively un-throttled workload.
047   */
048  String USE_RESULT_SIZE_BYTES = "hbase.quota.use.result.size.bytes";
049  boolean USE_RESULT_SIZE_BYTES_DEFAULT = false;
050
051  /**
052   * Checks if it is possible to execute the specified operation. The quota will be estimated based
053   * on the number of operations to perform and the average size accumulated during time.
054   * @param numWrites number of write operation that will be performed
055   * @param numReads  number of small-read operation that will be performed
056   * @throws RpcThrottlingException if the operation cannot be performed because RPC quota is
057   *                                exceeded.
058   */
059  void checkBatchQuota(int numWrites, int numReads) throws RpcThrottlingException;
060
061  /**
062   * Checks if it is possible to execute the scan. The quota will be estimated based on the
063   * composition of the scan.
064   * @param scanRequest                     the given scan operation
065   * @param maxScannerResultSize            the maximum bytes to be returned by the scanner
066   * @param maxBlockBytesScanned            the maximum bytes scanned in a single RPC call by the
067   *                                        scanner
068   * @param prevBlockBytesScannedDifference the difference between BBS of the previous two next
069   *                                        calls
070   * @throws RpcThrottlingException if the operation cannot be performed because RPC quota is
071   *                                exceeded.
072   */
073  void checkScanQuota(ClientProtos.ScanRequest scanRequest, long maxScannerResultSize,
074    long maxBlockBytesScanned, long prevBlockBytesScannedDifference) throws RpcThrottlingException;
075
076  /** Cleanup method on operation completion */
077  void close();
078
079  /**
080   * Add a get result. This will be used to calculate the exact quota and have a better short-read
081   * average size for the next time.
082   */
083  void addGetResult(Result result);
084
085  /**
086   * Add a scan result. This will be used to calculate the exact quota and have a better long-read
087   * average size for the next time.
088   */
089  void addScanResult(List<Result> results);
090
091  /**
092   * Add a mutation result. This will be used to calculate the exact quota and have a better
093   * mutation average size for the next time.
094   */
095  void addMutation(Mutation mutation);
096
097  /** Returns the number of bytes available to read to avoid exceeding the quota */
098  long getReadAvailable();
099
100  /** Returns the number of bytes consumed from the quota by the operation */
101  long getReadConsumed();
102
103  /**
104   * Returns the maximum result size to be returned by the given operation. This is the greater of
105   * two numbers: the bytes available, or the bytes already consumed
106   */
107  default long getMaxResultSize() {
108    return Math.max(getReadAvailable(), getReadConsumed());
109  }
110}