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.yetus.audience.InterfaceAudience;
021import org.apache.yetus.audience.InterfaceStability;
022
023/**
024 * Internal interface used to interact with the user/table quota.
025 */
026@InterfaceAudience.Private
027@InterfaceStability.Evolving
028public interface QuotaLimiter {
029  /**
030   * Checks if it is possible to execute the specified operation.
031   * @param writeReqs                    the write requests that will be checked against the
032   *                                     available quota
033   * @param estimateWriteSize            the write size that will be checked against the available
034   *                                     quota
035   * @param readReqs                     the read requests that will be checked against the
036   *                                     available quota
037   * @param estimateReadSize             the read size that will be checked against the available
038   *                                     quota
039   * @param estimateWriteCapacityUnit    the write capacity unit that will be checked against the
040   *                                     available quota
041   * @param estimateReadCapacityUnit     the read capacity unit that will be checked against the
042   *                                     available quota
043   * @param isAtomic                     if the request performs an atomic operation
044   * @param estimateHandlerThreadUsageMs the estimated handler usage time in ms that will be checked
045   *                                     against the available quota
046   * @throws RpcThrottlingException thrown if not enough available resources to perform operation.
047   */
048  void checkQuota(long writeReqs, long estimateWriteSize, long readReqs, long estimateReadSize,
049    long estimateWriteCapacityUnit, long estimateReadCapacityUnit, boolean isAtomic,
050    long estimateHandlerThreadUsageMs) throws RpcThrottlingException;
051
052  /**
053   * Removes the specified write and read amount from the quota. At this point the write and read
054   * amount will be an estimate, that will be later adjusted with a consumeWrite()/consumeRead()
055   * call.
056   * @param writeReqs                    the write requests that will be removed from the current
057   *                                     quota
058   * @param writeSize                    the write size that will be removed from the current quota
059   * @param readReqs                     the read requests that will be removed from the current
060   *                                     quota
061   * @param readSize                     the read size that will be removed from the current quota
062   * @param writeCapacityUnit            the write capacity unit that will be removed from the
063   *                                     current quota
064   * @param readCapacityUnit             the read capacity unit num that will be removed from the
065   *                                     current quota
066   * @param isAtomic                     if the request performs an atomic operation
067   * @param estimateHandlerThreadUsageMs the estimated handler usage time in ms that will be removed
068   *                                     from the available quota
069   */
070  void grabQuota(long writeReqs, long writeSize, long readReqs, long readSize,
071    long writeCapacityUnit, long readCapacityUnit, boolean isAtomic,
072    long estimateHandlerThreadUsageMs);
073
074  /**
075   * Removes or add back some write amount to the quota. (called at the end of an operation in case
076   * the estimate quota was off)
077   */
078  void consumeWrite(long size, long capacityUnit, boolean isAtomic);
079
080  /**
081   * Removes or add back some read amount to the quota. (called at the end of an operation in case
082   * the estimate quota was off)
083   */
084  void consumeRead(long size, long capacityUnit, boolean isAtomic);
085
086  /**
087   * Removes or add back some handler thread usage milliseconds to the quota. (called at the end of
088   * an operation in case the estimate quota was off)
089   * @param handlerMillisUsed the actual elapsed time used processing the request
090   */
091  void consumeTime(long handlerMillisUsed);
092
093  /** Returns true if the limiter is a noop */
094  boolean isBypass();
095
096  /** Returns the number of bytes available to read to avoid exceeding the quota */
097  long getReadAvailable();
098
099  /** Returns the maximum number of bytes ever available to read */
100  long getReadLimit();
101
102  /** Returns the maximum number of bytes ever available to write */
103  long getWriteLimit();
104
105  /** Returns the number of bytes available to write to avoid exceeding the quota */
106  long getWriteAvailable();
107
108  /** Returns the maximum number of requests to allow per TimeUnit */
109  long getRequestNumLimit();
110
111  /** Returns the maximum number of reads to allow per TimeUnit */
112  long getReadNumLimit();
113
114  /** Returns the maximum number of writes to allow per TimeUnit */
115  long getWriteNumLimit();
116
117}