|
24 | 24 | import java.util.Iterator; |
25 | 25 | import java.util.List; |
26 | 26 | import java.util.Map; |
| 27 | +import java.util.Random; |
27 | 28 | import java.util.Set; |
28 | 29 | import java.util.concurrent.ConcurrentLinkedQueue; |
29 | 30 |
|
@@ -122,8 +123,10 @@ public void verify(String hostname, String[] cns, String[] subjectAlts) |
122 | 123 | private ApacheHttpClient4 client; |
123 | 124 | private WebResource connection; |
124 | 125 |
|
125 | | - private int maxRetries = 64; |
126 | | - private int delayMillis = 125; |
| 126 | + private Random randRetry = new Random(); |
| 127 | + private int maxRetries = 9; |
| 128 | + private int delayFloor = 120; |
| 129 | + private int delayMultiplier = 5; |
127 | 130 |
|
128 | 131 | private boolean isFirstRequest = false; |
129 | 132 |
|
@@ -366,7 +369,7 @@ public void deleteDocument(RequestLogger reqlog, DocumentDescriptor desc, |
366 | 369 | response.close(); |
367 | 370 |
|
368 | 371 | try { |
369 | | - Thread.sleep(delayMillis); |
| 372 | + Thread.sleep(calculateDelay(randRetry, retry)); |
370 | 373 | } catch (InterruptedException e) {} |
371 | 374 | } |
372 | 375 | if (retry >= maxRetries) { |
@@ -480,7 +483,7 @@ private boolean getDocumentImpl(RequestLogger reqlog, |
480 | 483 | response.close(); |
481 | 484 |
|
482 | 485 | try { |
483 | | - Thread.sleep(delayMillis); |
| 486 | + Thread.sleep(calculateDelay(randRetry, retry)); |
484 | 487 | } catch (InterruptedException e) {} |
485 | 488 | } |
486 | 489 | if (retry >= maxRetries) { |
@@ -578,7 +581,7 @@ private boolean getDocumentImpl(RequestLogger reqlog, |
578 | 581 | response.close(); |
579 | 582 |
|
580 | 583 | try { |
581 | | - Thread.sleep(delayMillis); |
| 584 | + Thread.sleep(calculateDelay(randRetry, retry)); |
582 | 585 | } catch (InterruptedException e) {} |
583 | 586 | } |
584 | 587 | if (retry >= maxRetries) { |
@@ -688,7 +691,7 @@ public DocumentDescriptor head(RequestLogger reqlog, String uri, |
688 | 691 | response.close(); |
689 | 692 |
|
690 | 693 | try { |
691 | | - Thread.sleep(delayMillis); |
| 694 | + Thread.sleep(calculateDelay(randRetry, retry)); |
692 | 695 | } catch (InterruptedException e) {} |
693 | 696 | } |
694 | 697 | if (retry >= maxRetries) { |
@@ -840,7 +843,7 @@ private void putDocumentImpl(RequestLogger reqlog, DocumentDescriptor desc, |
840 | 843 | if (!isResendable) |
841 | 844 | throw new ResourceNotResendableException("Cannot retry request for "+uri); |
842 | 845 | try { |
843 | | - Thread.sleep(delayMillis); |
| 846 | + Thread.sleep(calculateDelay(randRetry, retry)); |
844 | 847 | } catch (InterruptedException e) {} |
845 | 848 | } |
846 | 849 | if (retry >= maxRetries) { |
@@ -937,7 +940,7 @@ private void putDocumentImpl(RequestLogger reqlog, DocumentDescriptor desc, |
937 | 940 | if (hasStreamingPart) |
938 | 941 | throw new ResourceNotResendableException("Cannot retry request for "+uri); |
939 | 942 | try { |
940 | | - Thread.sleep(delayMillis); |
| 943 | + Thread.sleep(calculateDelay(randRetry, retry)); |
941 | 944 | } catch (InterruptedException e) {} |
942 | 945 | } |
943 | 946 | if (retry >= maxRetries) { |
@@ -1008,7 +1011,7 @@ public String openTransaction(String name, int timeLimit) |
1008 | 1011 | response.close(); |
1009 | 1012 |
|
1010 | 1013 | try { |
1011 | | - Thread.sleep(delayMillis); |
| 1014 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1012 | 1015 | } catch (InterruptedException e) {} |
1013 | 1016 | } |
1014 | 1017 | if (retry >= maxRetries) { |
@@ -1083,7 +1086,7 @@ private void completeTransaction(String transactionId, String result) |
1083 | 1086 | response.close(); |
1084 | 1087 |
|
1085 | 1088 | try { |
1086 | | - Thread.sleep(delayMillis); |
| 1089 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1087 | 1090 | } catch (InterruptedException e) {} |
1088 | 1091 | } |
1089 | 1092 | if (retry >= maxRetries) { |
@@ -1413,7 +1416,7 @@ else if (payloadFormat != Format.XML && payloadFormat != Format.JSON) |
1413 | 1416 | response.close(); |
1414 | 1417 |
|
1415 | 1418 | try { |
1416 | | - Thread.sleep(delayMillis); |
| 1419 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1417 | 1420 | } catch (InterruptedException e) { |
1418 | 1421 | } |
1419 | 1422 | } |
@@ -1478,7 +1481,7 @@ public void deleteSearch(RequestLogger reqlog, DeleteQueryDefinition queryDef, S |
1478 | 1481 | response.close(); |
1479 | 1482 |
|
1480 | 1483 | try { |
1481 | | - Thread.sleep(delayMillis); |
| 1484 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1482 | 1485 | } catch (InterruptedException e) { |
1483 | 1486 | } |
1484 | 1487 | } |
@@ -1612,7 +1615,7 @@ public <T> T values(Class<T> as, ValuesDefinition valDef, String mimetype, |
1612 | 1615 | response.close(); |
1613 | 1616 |
|
1614 | 1617 | try { |
1615 | | - Thread.sleep(delayMillis); |
| 1618 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1616 | 1619 | } catch (InterruptedException e) {} |
1617 | 1620 | } |
1618 | 1621 | if (retry >= maxRetries) { |
@@ -1673,7 +1676,7 @@ public <T> T valuesList(Class<T> as, ValuesListDefinition valDef, |
1673 | 1676 | response.close(); |
1674 | 1677 |
|
1675 | 1678 | try { |
1676 | | - Thread.sleep(delayMillis); |
| 1679 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1677 | 1680 | } catch (InterruptedException e) {} |
1678 | 1681 | } |
1679 | 1682 | if (retry >= maxRetries) { |
@@ -1728,7 +1731,7 @@ public <T> T optionsList(Class<T> as, String mimetype, String transactionId) |
1728 | 1731 | response.close(); |
1729 | 1732 |
|
1730 | 1733 | try { |
1731 | | - Thread.sleep(delayMillis); |
| 1734 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1732 | 1735 | } catch (InterruptedException e) {} |
1733 | 1736 | } |
1734 | 1737 | if (retry >= maxRetries) { |
@@ -1780,7 +1783,7 @@ public <T> T getValue(RequestLogger reqlog, String type, String key, |
1780 | 1783 | response.close(); |
1781 | 1784 |
|
1782 | 1785 | try { |
1783 | | - Thread.sleep(delayMillis); |
| 1786 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1784 | 1787 | } catch (InterruptedException e) {} |
1785 | 1788 | } |
1786 | 1789 | if (retry >= maxRetries) { |
@@ -1839,7 +1842,7 @@ public <T> T getValues(RequestLogger reqlog, String type, String mimetype, |
1839 | 1842 | response.close(); |
1840 | 1843 |
|
1841 | 1844 | try { |
1842 | | - Thread.sleep(delayMillis); |
| 1845 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1843 | 1846 | } catch (InterruptedException e) {} |
1844 | 1847 | } |
1845 | 1848 | if (retry >= maxRetries) { |
@@ -1989,7 +1992,7 @@ private void putPostValueImpl(RequestLogger reqlog, String method, |
1989 | 1992 | throw new ResourceNotResendableException( |
1990 | 1993 | "Cannot retry request for "+connectPath); |
1991 | 1994 | try { |
1992 | | - Thread.sleep(delayMillis); |
| 1995 | + Thread.sleep(calculateDelay(randRetry, retry)); |
1993 | 1996 | } catch (InterruptedException e) {} |
1994 | 1997 | } |
1995 | 1998 | if (retry >= maxRetries) { |
@@ -2045,7 +2048,7 @@ public void deleteValue(RequestLogger reqlog, String type, String key) |
2045 | 2048 | response.close(); |
2046 | 2049 |
|
2047 | 2050 | try { |
2048 | | - Thread.sleep(delayMillis); |
| 2051 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2049 | 2052 | } catch (InterruptedException e) {} |
2050 | 2053 | } |
2051 | 2054 | if (retry >= maxRetries) { |
@@ -2093,7 +2096,7 @@ public void deleteValues(RequestLogger reqlog, String type) |
2093 | 2096 | response.close(); |
2094 | 2097 |
|
2095 | 2098 | try { |
2096 | | - Thread.sleep(delayMillis); |
| 2099 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2097 | 2100 | } catch (InterruptedException e) {} |
2098 | 2101 | } |
2099 | 2102 | if (retry >= maxRetries) { |
@@ -2140,7 +2143,7 @@ public <R extends AbstractReadHandle> R getResource( |
2140 | 2143 | response.close(); |
2141 | 2144 |
|
2142 | 2145 | try { |
2143 | | - Thread.sleep(delayMillis); |
| 2146 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2144 | 2147 | } catch (InterruptedException e) {} |
2145 | 2148 | } |
2146 | 2149 | if (retry >= maxRetries) { |
@@ -2183,7 +2186,7 @@ public ServiceResultIterator getIteratedResource( |
2183 | 2186 | response.close(); |
2184 | 2187 |
|
2185 | 2188 | try { |
2186 | | - Thread.sleep(delayMillis); |
| 2189 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2187 | 2190 | } catch (InterruptedException e) {} |
2188 | 2191 | } |
2189 | 2192 | if (retry >= maxRetries) { |
@@ -2237,7 +2240,7 @@ R putResource(RequestLogger reqlog, String path, RequestParameters params, |
2237 | 2240 | if (!isResendable) |
2238 | 2241 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2239 | 2242 | try { |
2240 | | - Thread.sleep(delayMillis); |
| 2243 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2241 | 2244 | } catch (InterruptedException e) {} |
2242 | 2245 | } |
2243 | 2246 | if (retry >= maxRetries) { |
@@ -2296,7 +2299,7 @@ R putResource(RequestLogger reqlog, String path, RequestParameters params, |
2296 | 2299 | if (hasStreamingPart) |
2297 | 2300 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2298 | 2301 | try { |
2299 | | - Thread.sleep(delayMillis); |
| 2302 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2300 | 2303 | } catch (InterruptedException e) {} |
2301 | 2304 | } |
2302 | 2305 | if (retry >= maxRetries) { |
@@ -2353,7 +2356,7 @@ public <R extends AbstractReadHandle> R postResource( |
2353 | 2356 | if (!isResendable) |
2354 | 2357 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2355 | 2358 | try { |
2356 | | - Thread.sleep(delayMillis); |
| 2359 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2357 | 2360 | } catch (InterruptedException e) {} |
2358 | 2361 | } |
2359 | 2362 | if (retry >= maxRetries) { |
@@ -2408,7 +2411,7 @@ public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResou |
2408 | 2411 | if (hasStreamingPart) |
2409 | 2412 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2410 | 2413 | try { |
2411 | | - Thread.sleep(delayMillis); |
| 2414 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2412 | 2415 | } catch (InterruptedException e) {} |
2413 | 2416 | } |
2414 | 2417 | if (retry >= maxRetries) { |
@@ -2463,7 +2466,7 @@ public ServiceResultIterator postIteratedResource( |
2463 | 2466 | if (!isResendable) |
2464 | 2467 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2465 | 2468 | try { |
2466 | | - Thread.sleep(delayMillis); |
| 2469 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2467 | 2470 | } catch (InterruptedException e) {} |
2468 | 2471 | } |
2469 | 2472 | if (retry >= maxRetries) { |
@@ -2509,7 +2512,7 @@ public <W extends AbstractWriteHandle> ServiceResultIterator postIteratedResourc |
2509 | 2512 | if (hasStreamingPart) |
2510 | 2513 | throw new ResourceNotResendableException("Cannot retry request for "+path); |
2511 | 2514 | try { |
2512 | | - Thread.sleep(delayMillis); |
| 2515 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2513 | 2516 | } catch (InterruptedException e) {} |
2514 | 2517 | } |
2515 | 2518 | if (retry >= maxRetries) { |
@@ -2554,7 +2557,7 @@ public <R extends AbstractReadHandle> R deleteResource( |
2554 | 2557 | response.close(); |
2555 | 2558 |
|
2556 | 2559 | try { |
2557 | | - Thread.sleep(delayMillis); |
| 2560 | + Thread.sleep(calculateDelay(randRetry, retry)); |
2558 | 2561 | } catch (InterruptedException e) {} |
2559 | 2562 | } |
2560 | 2563 | if (retry >= maxRetries) { |
@@ -2998,6 +3001,14 @@ private String stringJoin(Collection collection, String separator, |
2998 | 3001 | return (builder != null) ? builder.toString() : null; |
2999 | 3002 | } |
3000 | 3003 |
|
| 3004 | + private int calculateDelay(Random rand, int iteration) { |
| 3005 | + int base = (iteration == 0) ? delayMultiplier : |
| 3006 | + (1 << iteration) * delayMultiplier; |
| 3007 | + return delayFloor + base + rand.nextInt( |
| 3008 | + (iteration + 1 < maxRetries) ? base : base / 2 |
| 3009 | + ); |
| 3010 | + } |
| 3011 | + |
3001 | 3012 | public class JerseyResult implements ServiceResult { |
3002 | 3013 | private RequestLogger reqlog; |
3003 | 3014 | private BodyPart part; |
|
0 commit comments