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.
12 package org
.apache
.hadoop
.hbase
.quotas
;
14 import java
.util
.regex
.Matcher
;
15 import java
.util
.regex
.Pattern
;
16 import org
.apache
.hadoop
.hbase
.HBaseIOException
;
17 import org
.apache
.hadoop
.util
.StringUtils
;
19 import org
.apache
.yetus
.audience
.InterfaceAudience
;
22 * Describe the throttling result. TODO: At some point this will be handled on the client side to
23 * prevent operation to go on the server if the waitInterval is greater than the one got as result
26 @InterfaceAudience.Public
27 public class RpcThrottlingException
extends HBaseIOException
{
29 @InterfaceAudience.Public
31 NumRequestsExceeded
, RequestSizeExceeded
, NumReadRequestsExceeded
, NumWriteRequestsExceeded
,
32 WriteSizeExceeded
, ReadSizeExceeded
, RequestCapacityUnitExceeded
, ReadCapacityUnitExceeded
,
33 WriteCapacityUnitExceeded
36 private static final String
[] MSG_TYPE
=
37 new String
[] { "number of requests exceeded", "request size limit exceeded",
38 "number of read requests exceeded", "number of write requests exceeded",
39 "write size limit exceeded", "read size limit exceeded", "request capacity unit exceeded",
40 "read capacity unit exceeded", "write capacity unit exceeded" };
42 private static final String MSG_WAIT
= " - wait ";
44 private long waitInterval
;
47 public RpcThrottlingException(String msg
) {
50 // Dirty workaround to get the information after
51 // ((RemoteException)e.getCause()).unwrapRemoteException()
52 for (int i
= 0; i
< MSG_TYPE
.length
; ++i
) {
53 int index
= msg
.indexOf(MSG_TYPE
[i
]);
55 String waitTimeStr
= msg
.substring(index
+ MSG_TYPE
[i
].length() + MSG_WAIT
.length());
56 type
= Type
.values()[i
];
57 waitInterval
= timeFromString(waitTimeStr
);
63 public RpcThrottlingException(final Type type
, final long waitInterval
, final String msg
) {
65 this.waitInterval
= waitInterval
;
69 public Type
getType() {
73 public long getWaitInterval() {
74 return this.waitInterval
;
77 public static void throwNumRequestsExceeded(final long waitInterval
) throws
78 RpcThrottlingException
{
79 throwThrottlingException(Type
.NumRequestsExceeded
, waitInterval
);
82 public static void throwRequestSizeExceeded(final long waitInterval
)
83 throws RpcThrottlingException
{
84 throwThrottlingException(Type
.RequestSizeExceeded
, waitInterval
);
87 public static void throwNumReadRequestsExceeded(final long waitInterval
)
88 throws RpcThrottlingException
{
89 throwThrottlingException(Type
.NumReadRequestsExceeded
, waitInterval
);
92 public static void throwNumWriteRequestsExceeded(final long waitInterval
)
93 throws RpcThrottlingException
{
94 throwThrottlingException(Type
.NumWriteRequestsExceeded
, waitInterval
);
97 public static void throwWriteSizeExceeded(final long waitInterval
) throws RpcThrottlingException
{
98 throwThrottlingException(Type
.WriteSizeExceeded
, waitInterval
);
101 public static void throwReadSizeExceeded(final long waitInterval
) throws RpcThrottlingException
{
102 throwThrottlingException(Type
.ReadSizeExceeded
, waitInterval
);
105 public static void throwRequestCapacityUnitExceeded(final long waitInterval
)
106 throws RpcThrottlingException
{
107 throwThrottlingException(Type
.RequestCapacityUnitExceeded
, waitInterval
);
110 public static void throwReadCapacityUnitExceeded(final long waitInterval
)
111 throws RpcThrottlingException
{
112 throwThrottlingException(Type
.ReadCapacityUnitExceeded
, waitInterval
);
115 public static void throwWriteCapacityUnitExceeded(final long waitInterval
)
116 throws RpcThrottlingException
{
117 throwThrottlingException(Type
.WriteCapacityUnitExceeded
, waitInterval
);
120 private static void throwThrottlingException(final Type type
, final long waitInterval
)
121 throws RpcThrottlingException
{
122 String msg
= MSG_TYPE
[type
.ordinal()] + MSG_WAIT
+ StringUtils
.formatTime(waitInterval
);
123 throw new RpcThrottlingException(type
, waitInterval
, msg
);
126 private static long timeFromString(String timeDiff
) {
128 new Pattern
[] { Pattern
.compile("^(\\d+\\.\\d\\d)sec"),
129 Pattern
.compile("^(\\d+)mins, (\\d+\\.\\d\\d)sec"),
130 Pattern
.compile("^(\\d+)hrs, (\\d+)mins, (\\d+\\.\\d\\d)sec") };
132 for (int i
= 0; i
< patterns
.length
; ++i
) {
133 Matcher m
= patterns
[i
].matcher(timeDiff
);
135 long time
= Math
.round(Float
.parseFloat(m
.group(1 + i
)) * 1000);
137 time
+= Long
.parseLong(m
.group(i
)) * (60 * 1000);
140 time
+= Long
.parseLong(m
.group(i
- 1)) * (60 * 60 * 1000);