View Javadoc

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.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   * Describe the throttling result. TODO: At some point this will be handled on the client side to
22   * prevent operation to go on the server if the waitInterval is grater than the one got as result of
23   * this exception.
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      // Dirty workaround to get the information after
50      // ((RemoteException)e.getCause()).unwrapRemoteException()
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 }