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
026/**
027 * Interface that allows to check the quota available for an operation.
028 */
029@InterfaceAudience.Private
030@InterfaceStability.Evolving
031public interface OperationQuota {
032  public enum OperationType {
033    MUTATE,
034    GET,
035    SCAN
036  }
037
038  /**
039   * Checks if it is possible to execute the specified operation. The quota will be estimated based
040   * on the number of operations to perform and the average size accumulated during time.
041   * @param numWrites number of write operation that will be performed
042   * @param numReads  number of small-read operation that will be performed
043   * @param numScans  number of long-read operation that will be performed
044   * @throws RpcThrottlingException if the operation cannot be performed because RPC quota is
045   *                                exceeded.
046   */
047  void checkQuota(int numWrites, int numReads, int numScans) throws RpcThrottlingException;
048
049  /** Cleanup method on operation completion */
050  void close();
051
052  /**
053   * Add a get result. This will be used to calculate the exact quota and have a better short-read
054   * average size for the next time.
055   */
056  void addGetResult(Result result);
057
058  /**
059   * Add a scan result. This will be used to calculate the exact quota and have a better long-read
060   * average size for the next time.
061   */
062  void addScanResult(List<Result> results);
063
064  /**
065   * Add a mutation result. This will be used to calculate the exact quota and have a better
066   * mutation average size for the next time.
067   */
068  void addMutation(Mutation mutation);
069
070  /** @return the number of bytes available to read to avoid exceeding the quota */
071  long getReadAvailable();
072}