1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.predicates; |
20 | |
21 | import static org.jclouds.util.Throwables2.getFirstThrowableOfType; |
22 | |
23 | import java.util.Date; |
24 | import java.util.concurrent.ExecutionException; |
25 | import java.util.concurrent.TimeUnit; |
26 | import java.util.concurrent.TimeoutException; |
27 | |
28 | import javax.annotation.Resource; |
29 | |
30 | import org.jclouds.logging.Logger; |
31 | |
32 | import com.google.common.base.Predicate; |
33 | |
34 | /** |
35 | * |
36 | * Retries a condition until it is met or a timeout occurs. |
37 | * |
38 | * @author Adrian Cole |
39 | */ |
40 | public class RetryablePredicate<T> implements Predicate<T> { |
41 | private final long maxWait; |
42 | private final long period; |
43 | private final long maxPeriod; |
44 | private final Predicate<T> predicate; |
45 | |
46 | @Resource |
47 | protected Logger logger = Logger.NULL; |
48 | |
49 | public RetryablePredicate(Predicate<T> predicate, long maxWait, long period, long maxPeriod, TimeUnit unit) { |
50 | this.predicate = predicate; |
51 | this.maxWait = unit.toMillis(maxWait); |
52 | this.period = unit.toMillis(period); |
53 | this.maxPeriod = unit.toMillis(maxPeriod); |
54 | } |
55 | |
56 | public RetryablePredicate(Predicate<T> predicate, long maxWait, long period, TimeUnit unit) { |
57 | this(predicate, maxWait, period, period * 10l, unit); |
58 | } |
59 | |
60 | public RetryablePredicate(Predicate<T> predicate, long maxWait) { |
61 | this(predicate, maxWait, 50l, 1000l, TimeUnit.MILLISECONDS); |
62 | } |
63 | |
64 | @Override |
65 | public boolean apply(T input) { |
66 | try { |
67 | long i = 1l; |
68 | for (Date end = new Date(System.currentTimeMillis() + maxWait); before(end); Thread.sleep(nextMaxInterval(i++, |
69 | end))) { |
70 | if (predicate.apply(input)) { |
71 | return true; |
72 | } else if (atOrAfter(end)) { |
73 | return false; |
74 | } |
75 | } |
76 | } catch (InterruptedException e) { |
77 | logger.warn(e, "predicate %s on %s interrupted, returning false", input, predicate); |
78 | } catch (RuntimeException e) { |
79 | if (getFirstThrowableOfType(e, ExecutionException.class) != null) { |
80 | logger.warn(e, "predicate %s on %s errored [%s], returning false", input, predicate, e.getMessage()); |
81 | return false; |
82 | } else if (getFirstThrowableOfType(e, IllegalStateException.class) != null) { |
83 | logger.warn(e, "predicate %s on %s illegal state [%s], returning false", input, predicate, e.getMessage()); |
84 | return false; |
85 | } else if (getFirstThrowableOfType(e, TimeoutException.class) != null) { |
86 | logger.warn(e, "predicate %s on %s timed out [%s], returning false", input, predicate, e.getMessage()); |
87 | return false; |
88 | } else |
89 | throw e; |
90 | } |
91 | return false; |
92 | } |
93 | |
94 | protected long nextMaxInterval(long attempt, Date end) { |
95 | long interval = (period * (long) Math.pow(attempt, 1.5)); |
96 | interval = interval > maxPeriod ? maxPeriod : interval; |
97 | long max = end.getTime() - System.currentTimeMillis(); |
98 | return (interval > max) ? max : interval; |
99 | } |
100 | |
101 | protected boolean before(Date end) { |
102 | return new Date().compareTo(end) <= 1; |
103 | } |
104 | |
105 | protected boolean atOrAfter(Date end) { |
106 | return new Date().compareTo(end) >= 0; |
107 | } |
108 | } |