1
2
3
4
5
6
7
8
9
10
11
12 package org.apache.hadoop.hbase.quotas;
13
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.apache.hadoop.hbase.classification.InterfaceAudience;
18 import org.apache.hadoop.hbase.classification.InterfaceStability;
19
20
21
22
23
24
25 @InterfaceAudience.Public
26 @InterfaceStability.Evolving
27 public class ThrottlingException extends QuotaExceededException {
28 private static final long serialVersionUID = 1406576492085155743L;
29
30 @InterfaceAudience.Public
31 @InterfaceStability.Evolving
32 public enum Type {
33 NumRequestsExceeded, NumReadRequestsExceeded, NumWriteRequestsExceeded, WriteSizeExceeded,
34 ReadSizeExceeded,
35 }
36
37 private static final String[] MSG_TYPE = new String[] { "number of requests exceeded",
38 "number of read requests exceeded", "number of write requests exceeded",
39 "write size limit exceeded", "read size limit exceeded", };
40
41 private static final String MSG_WAIT = " - wait ";
42
43 private long waitInterval;
44 private Type type;
45
46 public ThrottlingException(String msg) {
47 super(msg);
48
49
50
51 for (int i = 0; i < MSG_TYPE.length; ++i) {
52 int index = msg.indexOf(MSG_TYPE[i]);
53 if (index >= 0) {
54 String waitTimeStr = msg.substring(index + MSG_TYPE[i].length() + MSG_WAIT.length());
55 type = Type.values()[i];
56 waitInterval = timeFromString(waitTimeStr);
57 break;
58 }
59 }
60 }
61
62 public ThrottlingException(final Type type, final long waitInterval, final String msg) {
63 super(msg);
64 this.waitInterval = waitInterval;
65 this.type = type;
66 }
67
68 public Type getType() {
69 return this.type;
70 }
71
72 public long getWaitInterval() {
73 return this.waitInterval;
74 }
75
76 public static void throwNumRequestsExceeded(final long waitInterval) throws ThrottlingException {
77 throwThrottlingException(Type.NumRequestsExceeded, waitInterval);
78 }
79
80 public static void throwNumReadRequestsExceeded(final long waitInterval)
81 throws ThrottlingException {
82 throwThrottlingException(Type.NumReadRequestsExceeded, waitInterval);
83 }
84
85 public static void throwNumWriteRequestsExceeded(final long waitInterval)
86 throws ThrottlingException {
87 throwThrottlingException(Type.NumWriteRequestsExceeded, waitInterval);
88 }
89
90 public static void throwWriteSizeExceeded(final long waitInterval) throws ThrottlingException {
91 throwThrottlingException(Type.WriteSizeExceeded, waitInterval);
92 }
93
94 public static void throwReadSizeExceeded(final long waitInterval) throws ThrottlingException {
95 throwThrottlingException(Type.ReadSizeExceeded, waitInterval);
96 }
97
98 private static void throwThrottlingException(final Type type, final long waitInterval)
99 throws ThrottlingException {
100 String msg = MSG_TYPE[type.ordinal()] + MSG_WAIT + formatTime(waitInterval);
101 throw new ThrottlingException(type, waitInterval, msg);
102 }
103
104 public static String formatTime(long timeDiff) {
105 StringBuilder buf = new StringBuilder();
106 long hours = timeDiff / (60 * 60 * 1000);
107 long rem = (timeDiff % (60 * 60 * 1000));
108 long minutes = rem / (60 * 1000);
109 rem = rem % (60 * 1000);
110 float seconds = rem / 1000.0f;
111
112 if (hours != 0) {
113 buf.append(hours);
114 buf.append("hrs, ");
115 }
116 if (minutes != 0) {
117 buf.append(minutes);
118 buf.append("mins, ");
119 }
120 buf.append(String.format("%.2fsec", seconds));
121 return buf.toString();
122 }
123
124 private static long timeFromString(String timeDiff) {
125 Pattern[] patterns =
126 new Pattern[] { Pattern.compile("^(\\d+\\.\\d\\d)sec"),
127 Pattern.compile("^(\\d+)mins, (\\d+\\.\\d\\d)sec"),
128 Pattern.compile("^(\\d+)hrs, (\\d+)mins, (\\d+\\.\\d\\d)sec") };
129
130 for (int i = 0; i < patterns.length; ++i) {
131 Matcher m = patterns[i].matcher(timeDiff);
132 if (m.find()) {
133 long time = Math.round(Float.parseFloat(m.group(1 + i)) * 1000);
134 if (i > 0) {
135 time += Long.parseLong(m.group(i)) * (60 * 1000);
136 }
137 if (i > 1) {
138 time += Long.parseLong(m.group(i - 1)) * (60 * 60 * 1000);
139 }
140 return time;
141 }
142 }
143
144 return -1;
145 }
146 }