1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional information regarding
4 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9 * for the specific language governing permissions and limitations under the License.
10 */
11
12 package org.apache.hadoop.hbase.quotas;
13
14 import java.util.List;
15
16 import org.apache.hadoop.hbase.classification.InterfaceAudience;
17 import org.apache.hadoop.hbase.classification.InterfaceStability;
18 import org.apache.hadoop.hbase.client.Mutation;
19 import org.apache.hadoop.hbase.client.Result;
20
21 /**
22 * Interface that allows to check the quota available for an operation.
23 */
24 @InterfaceAudience.Private
25 @InterfaceStability.Evolving
26 public interface OperationQuota {
27 public enum OperationType {
28 MUTATE, GET, SCAN
29 }
30
31 /**
32 * Keeps track of the average data size of operations like get, scan, mutate
33 */
34 public class AvgOperationSize {
35 private final long[] sizeSum;
36 private final long[] count;
37
38 public AvgOperationSize() {
39 int size = OperationType.values().length;
40 sizeSum = new long[size];
41 count = new long[size];
42 for (int i = 0; i < size; ++i) {
43 sizeSum[i] = 0;
44 count[i] = 0;
45 }
46 }
47
48 public void addOperationSize(OperationType type, long size) {
49 if (size > 0) {
50 int index = type.ordinal();
51 sizeSum[index] += size;
52 count[index]++;
53 }
54 }
55
56 public long getAvgOperationSize(OperationType type) {
57 int index = type.ordinal();
58 return count[index] > 0 ? sizeSum[index] / count[index] : 0;
59 }
60
61 public long getOperationSize(OperationType type) {
62 return sizeSum[type.ordinal()];
63 }
64
65 public void addGetResult(final Result result) {
66 long size = QuotaUtil.calculateResultSize(result);
67 addOperationSize(OperationType.GET, size);
68 }
69
70 public void addScanResult(final List<Result> results) {
71 long size = QuotaUtil.calculateResultSize(results);
72 addOperationSize(OperationType.SCAN, size);
73 }
74
75 public void addMutation(final Mutation mutation) {
76 long size = QuotaUtil.calculateMutationSize(mutation);
77 addOperationSize(OperationType.MUTATE, size);
78 }
79 }
80
81 /**
82 * Checks if it is possible to execute the specified operation. The quota will be estimated based
83 * on the number of operations to perform and the average size accumulated during time.
84 * @param numWrites number of write operation that will be performed
85 * @param numReads number of small-read operation that will be performed
86 * @param numScans number of long-read operation that will be performed
87 * @throws ThrottlingException if the operation cannot be performed
88 */
89 void checkQuota(int numWrites, int numReads, int numScans) throws ThrottlingException;
90
91 /** Cleanup method on operation completion */
92 void close();
93
94 /**
95 * Add a get result. This will be used to calculate the exact quota and have a better short-read
96 * average size for the next time.
97 */
98 void addGetResult(Result result);
99
100 /**
101 * Add a scan result. This will be used to calculate the exact quota and have a better long-read
102 * average size for the next time.
103 */
104 void addScanResult(List<Result> results);
105
106 /**
107 * Add a mutation result. This will be used to calculate the exact quota and have a better
108 * mutation average size for the next time.
109 */
110 void addMutation(Mutation mutation);
111
112 /** @return the number of bytes available to read to avoid exceeding the quota */
113 long getReadAvailable();
114
115 /** @return the number of bytes available to write to avoid exceeding the quota */
116 long getWriteAvailable();
117
118 /** @return the average data size of the specified operation */
119 long getAvgOperationSize(OperationType type);
120 }