|
18 | 18 |
|
19 | 19 | package org.apache.hadoop.fs.azurebfs.services; |
20 | 20 |
|
21 | | -import java.io.IOException; |
22 | | -import java.net.SocketException; |
23 | | -import java.net.UnknownHostException; |
24 | | -import java.util.ArrayList; |
| 21 | +import java.util.LinkedList; |
25 | 22 | import java.util.List; |
26 | 23 |
|
27 | | -import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; |
28 | | -import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.HTTP_STATUS_CATEGORY_QUOTIENT; |
| 24 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.ClientErrorRetryReason; |
| 25 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.ConnectionResetRetryReason; |
| 26 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.ConnectionTimeoutRetryReason; |
| 27 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.ReadTimeoutRetryReason; |
| 28 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.RetryReasonCategory; |
| 29 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.ServerErrorRetryReason; |
| 30 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.UnknownHostRetryReason; |
| 31 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.UnknownIOExceptionRetryReason; |
| 32 | +import org.apache.hadoop.fs.azurebfs.services.retryReasonCategories.UnknownSocketExceptionRetryReason; |
| 33 | + |
29 | 34 |
|
30 | 35 | /** |
31 | | - * In case of retry, this enum would give the information on the reason for |
32 | | - * previous API call. |
| 36 | + * This utility class exposes methods to convert a server response-error to a |
| 37 | + * category of error. |
33 | 38 | * */ |
34 | | -public enum RetryReason { |
35 | | - CONNECTION_TIMEOUT(2, |
36 | | - ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
37 | | - if (exceptionCaptured != null && "connect timed out".equalsIgnoreCase( |
38 | | - exceptionCaptured.getMessage())) { |
39 | | - return "CT"; |
40 | | - } |
41 | | - return null; |
42 | | - })), |
43 | | - READ_TIMEOUT(2, ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
44 | | - if (exceptionCaptured != null && "Read timed out".equalsIgnoreCase( |
45 | | - exceptionCaptured.getMessage())) { |
46 | | - return "RT"; |
47 | | - } |
48 | | - return null; |
49 | | - })), |
50 | | - UNKNOWN_HOST(2, ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
51 | | - if (exceptionCaptured instanceof UnknownHostException) { |
52 | | - return "UH"; |
53 | | - } |
54 | | - return null; |
55 | | - })), |
56 | | - CONNECTION_RESET(2, ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
57 | | - if (exceptionCaptured != null && exceptionCaptured.getMessage() != null |
58 | | - && exceptionCaptured.getMessage().contains("Connection reset")) { |
59 | | - return "CR"; |
60 | | - } |
61 | | - return null; |
62 | | - })), |
63 | | - STATUS_5XX(0, ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
64 | | - if (statusCode == null || statusCode / HTTP_STATUS_CATEGORY_QUOTIENT != 5) { |
65 | | - return null; |
66 | | - } |
67 | | - if (statusCode == HTTP_UNAVAILABLE) { |
68 | | - serverErrorMessage = serverErrorMessage.split(System.lineSeparator(), |
69 | | - 2)[0]; |
70 | | - if ("Ingress is over the account limit.".equalsIgnoreCase( |
71 | | - serverErrorMessage)) { |
72 | | - return "ING"; |
73 | | - } |
74 | | - if ("Egress is over the account limit.".equalsIgnoreCase( |
75 | | - serverErrorMessage)) { |
76 | | - return "EGR"; |
77 | | - } |
78 | | - if ("Operations per second is over the account limit.".equalsIgnoreCase( |
79 | | - serverErrorMessage)) { |
80 | | - return "OPR"; |
81 | | - } |
82 | | - return HTTP_UNAVAILABLE + ""; |
83 | | - } |
84 | | - return statusCode + ""; |
85 | | - })), |
86 | | - STATUS_4XX(0, ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
87 | | - if (statusCode == null || statusCode / HTTP_STATUS_CATEGORY_QUOTIENT != 4) { |
88 | | - return null; |
89 | | - } |
90 | | - return statusCode + ""; |
91 | | - })), |
92 | | - UNKNOWN_SOCKET_EXCEPTION(1, |
93 | | - ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
94 | | - if (exceptionCaptured instanceof SocketException) { |
95 | | - return "SE"; |
96 | | - } |
97 | | - return null; |
98 | | - })), |
99 | | - UNKNOWN_IO_EXCEPTION(0, |
100 | | - ((exceptionCaptured, statusCode, serverErrorMessage) -> { |
101 | | - if (exceptionCaptured instanceof IOException) { |
102 | | - return "IOE"; |
103 | | - } |
104 | | - return null; |
105 | | - })); |
106 | | - |
107 | | - private RetryReasonAbbreviationCreator retryReasonAbbreviationCreator = null; |
108 | | - |
109 | | - private int rank = 0; |
| 39 | +class RetryReason { |
110 | 40 |
|
111 | 41 | /** |
112 | | - * Constructor to have rank and the implementation of {@link RetryReasonAbbreviationCreator}. |
113 | | - * @param rank rank of a given enum. For example SocketTimeoutException is |
114 | | - * subclass of IOException. Rank of SocketTimeoutException enum has to be |
115 | | - * more than that of IOException enum. |
116 | | - * @param abbreviationCreator The implementation of {@link RetryReasonAbbreviationCreator} |
117 | | - * which would give the information if a given enum can be mapped to an error or not. |
| 42 | + * Linked-list of the implementations of RetryReasonCategory. The objects in the |
| 43 | + * list are arranged by the rank of their significance. |
| 44 | + * <ul> |
| 45 | + * <li>ServerError (statusCode==5XX), ClientError (statusCode==4XX) are |
| 46 | + * independent of other retryReason categories.</li> |
| 47 | + * <li>Since {@link java.net.SocketException} is subclass of |
| 48 | + * {@link java.io.IOException}, |
| 49 | + * hence, {@link UnknownIOExceptionRetryReason} is placed before |
| 50 | + * {@link UnknownSocketExceptionRetryReason}</li> |
| 51 | + * <li>Since, connectionTimeout, readTimeout, and connectionReset are |
| 52 | + * {@link java.net.SocketTimeoutException} exceptions with different messages, |
| 53 | + * hence, {@link ConnectionTimeoutRetryReason}, {@link ReadTimeoutRetryReason}, |
| 54 | + * {@link ConnectionResetRetryReason} are above {@link UnknownIOExceptionRetryReason}. |
| 55 | + * There is no order between the three reasons as they are differentiated |
| 56 | + * by exception-message.</li> |
| 57 | + * <li>Since, {@link java.net.UnknownHostException} is subclass of |
| 58 | + * {@link java.io.IOException}, {@link UnknownHostRetryReason} is placed |
| 59 | + * over {@link UnknownIOExceptionRetryReason}</li> |
| 60 | + * </ul> |
118 | 61 | * */ |
119 | | - RetryReason(int rank, |
120 | | - RetryReasonAbbreviationCreator abbreviationCreator) { |
121 | | - this.rank = rank; |
122 | | - this.retryReasonAbbreviationCreator = abbreviationCreator; |
123 | | - } |
| 62 | + private static List<RetryReasonCategory> rankedReasonCategories |
| 63 | + = new LinkedList<RetryReasonCategory>() {{ |
| 64 | + add(new ServerErrorRetryReason()); |
| 65 | + add(new ClientErrorRetryReason()); |
| 66 | + add(new UnknownIOExceptionRetryReason()); |
| 67 | + add(new UnknownSocketExceptionRetryReason()); |
| 68 | + add(new ConnectionTimeoutRetryReason()); |
| 69 | + add(new ReadTimeoutRetryReason()); |
| 70 | + add(new UnknownHostRetryReason()); |
| 71 | + add(new ConnectionResetRetryReason()); |
| 72 | + }}; |
124 | 73 |
|
125 | | - private static List<RetryReason> retryReasonSortedList; |
| 74 | + private RetryReason() { |
126 | 75 |
|
127 | | - /** |
128 | | - * Synchronized method to assign sorted list in {@link RetryReason#retryReasonSortedList}. |
129 | | - * Method would check if list is assigned or not. If yes, method would return. This is required |
130 | | - * because multiple threads could be waiting to get into this method, and once a thread is done |
131 | | - * with this method, other thread would get into this method. Since the list would be assigned by |
132 | | - * first thread, the second thread need not run the whole mechanism of sorting. |
133 | | - * The enums are sorted on the ascending order of their rank. |
134 | | - * */ |
135 | | - private static synchronized void sortRetryReason() { |
136 | | - if (retryReasonSortedList != null) { |
137 | | - return; |
138 | | - } |
139 | | - List<RetryReason> list = new ArrayList<>(); |
140 | | - for (RetryReason reason : values()) { |
141 | | - list.add(reason); |
142 | | - } |
143 | | - list.sort((c1, c2) -> { |
144 | | - return c1.rank - c2.rank; |
145 | | - }); |
146 | | - retryReasonSortedList = list; |
147 | 76 | } |
148 | 77 |
|
149 | 78 | /** |
150 | 79 | * Method to get correct abbreviation for a given set of exception, statusCode, |
151 | 80 | * storageStatusCode. |
152 | | - * Method would iterate through the {@link RetryReason#retryReasonSortedList}, |
153 | | - * and would return the abbreviation returned by highest enum to be applicable on the group. |
154 | | - * For example, if SocketTimeoutException(rank 2) and IOException(rank 0) can be |
155 | | - * applied on the group, the abbreviation of SocketTimeoutException has to be returned. |
156 | 81 | * |
157 | 82 | * @param ex exception caught during server communication. |
158 | 83 | * @param statusCode statusCode in the server response. |
159 | 84 | * @param storageErrorMessage storageErrorMessage in the server response. |
| 85 | + * |
| 86 | + * @return abbreviation for the the given set of exception, statusCode, storageStatusCode. |
160 | 87 | * */ |
161 | 88 | static String getAbbreviation(Exception ex, |
162 | 89 | Integer statusCode, |
163 | 90 | String storageErrorMessage) { |
164 | 91 | String result = null; |
165 | | - if (retryReasonSortedList == null) { |
166 | | - sortRetryReason(); |
167 | | - } |
168 | | - for (RetryReason retryReason : retryReasonSortedList) { |
169 | | - String enumCapturedAndAbbreviate |
170 | | - = retryReason.retryReasonAbbreviationCreator.capturableAndGetAbbreviation( |
171 | | - ex, statusCode, storageErrorMessage); |
172 | | - if (enumCapturedAndAbbreviate != null) { |
173 | | - result = enumCapturedAndAbbreviate; |
| 92 | + for (RetryReasonCategory retryReasonCategory : rankedReasonCategories) { |
| 93 | + final String abbreviation |
| 94 | + = retryReasonCategory.captureAndGetAbbreviation(ex, |
| 95 | + statusCode, storageErrorMessage); |
| 96 | + if (abbreviation != null) { |
| 97 | + result = abbreviation; |
174 | 98 | } |
175 | 99 | } |
176 | 100 | return result; |
|
0 commit comments