| 1 | package fr.sii.ogham.core.builder.retry; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | import java.util.List; | |
| 5 | ||
| 6 | import fr.sii.ogham.core.builder.Builder; | |
| 7 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilder; | |
| 8 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper; | |
| 9 | import fr.sii.ogham.core.builder.configurer.Configurer; | |
| 10 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 11 | import fr.sii.ogham.core.builder.env.EnvironmentBuilder; | |
| 12 | import fr.sii.ogham.core.fluent.AbstractParent; | |
| 13 | import fr.sii.ogham.core.retry.PerExecutionDelayRetry; | |
| 14 | import fr.sii.ogham.core.retry.RetryStrategy; | |
| 15 | ||
| 16 | /** | |
| 17 |  * Configures retry handling based on a specific delay for each execution. | |
| 18 |  *  | |
| 19 |  * Retry several times with a fixed delay to wait after the last execution | |
| 20 |  * failure until the maximum attempts is reached. A specific delay is used for | |
| 21 |  * each execution. If there are more attempts than the configured delays, the | |
| 22 |  * last delays is used for remaining attempts. | |
| 23 |  *  | |
| 24 |  *  | |
| 25 |  * For example: | |
| 26 |  *  | |
| 27 |  * <pre> | |
| 28 |  *    .delays(500, 750, 1800) | |
| 29 |  *    .maxRetries(5) | |
| 30 |  * </pre> | |
| 31 |  *  | |
| 32 |  * Means that a retry will be attempted with specified delays until 5 attempts | |
| 33 |  * are reached (inclusive). For example, you want to connect to an external | |
| 34 |  * system at t1=0 and the connection timeout (100ms) is triggered at t2=100ms. | |
| 35 |  * Using this retry will provide the following behavior: | |
| 36 |  *  | |
| 37 |  * <ul> | |
| 38 |  * <li>0: connect</li> | |
| 39 |  * <li>100: timeout</li> | |
| 40 |  * <li>600: connect</li> | |
| 41 |  * <li>700: timeout</li> | |
| 42 |  * <li>1450: connect</li> | |
| 43 |  * <li>1550: timeout</li> | |
| 44 |  * <li>3350: connect</li> | |
| 45 |  * <li>3450: timeout</li> | |
| 46 |  * <li>5250: connect</li> | |
| 47 |  * <li>5350: timeout</li> | |
| 48 |  * <li>7150: connect</li> | |
| 49 |  * <li>7250: timeout</li> | |
| 50 |  * <li>fail</li> | |
| 51 |  * </ul> | |
| 52 |  *  | |
| 53 |  * @author Aurélien Baudet | |
| 54 |  * | |
| 55 |  * @param <P> | |
| 56 |  *            the type of the parent builder (when calling {@link #and()} | |
| 57 |  *            method) | |
| 58 |  */ | |
| 59 | public class PerExecutionDelayBuilder<P> extends AbstractParent<P> implements Builder<RetryStrategy> { | |
| 60 | 	private final BuildContext buildContext; | |
| 61 | 	private final ConfigurationValueBuilderHelper<PerExecutionDelayBuilder<P>, Integer> maxRetriesValueBuilder; | |
| 62 | 	private final ConfigurationValueBuilderHelper<PerExecutionDelayBuilder<P>, Long[]> delaysValueBuilder; | |
| 63 | ||
| 64 | 	/** | |
| 65 | 	 * Initializes the builder with a parent builder. The parent builder is used | |
| 66 | 	 * when calling {@link #and()} method. The {@link EnvironmentBuilder} is | |
| 67 | 	 * used to evaluate properties when {@link #build()} method is called. | |
| 68 | 	 *  | |
| 69 | 	 * @param parent | |
| 70 | 	 *            the parent builder | |
| 71 | 	 * @param buildContext | |
| 72 | 	 *            for registering instances and property evaluation | |
| 73 | 	 */ | |
| 74 | 	public PerExecutionDelayBuilder(P parent, BuildContext buildContext) { | |
| 75 | 		super(parent); | |
| 76 | 		this.buildContext = buildContext; | |
| 77 | 		maxRetriesValueBuilder = buildContext.newConfigurationValueBuilder(this, Integer.class); | |
| 78 | 		delaysValueBuilder = buildContext.newConfigurationValueBuilder(this, Long[].class); | |
| 79 | 	} | |
| 80 | ||
| 81 | 	/** | |
| 82 | 	 * Set the maximum number of attempts. | |
| 83 | 	 *  | |
| 84 | 	 * <p> | |
| 85 | 	 * The value set using this method takes precedence over any property and | |
| 86 | 	 * default value configured using {@link #maxRetries()}. | |
| 87 | 	 *  | |
| 88 | 	 * <pre> | |
| 89 | 	 * .maxRetries(10) | |
| 90 | 	 * .maxRetries() | |
| 91 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 92 | 	 *   .defaultValue(5) | |
| 93 | 	 * </pre> | |
| 94 | 	 *  | |
| 95 | 	 * <pre> | |
| 96 | 	 * .maxRetries(10) | |
| 97 | 	 * .maxRetries() | |
| 98 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 99 | 	 *   .defaultValue(5) | |
| 100 | 	 * </pre> | |
| 101 | 	 *  | |
| 102 | 	 * In both cases, {@code maxRetries(10)} is used. | |
| 103 | 	 *  | |
| 104 | 	 * <p> | |
| 105 | 	 * If this method is called several times, only the last value is used. | |
| 106 | 	 *  | |
| 107 | 	 * <p> | |
| 108 | 	 * If {@code null} value is set, it is like not setting a value at all. The | |
| 109 | 	 * property/default value configuration is applied. | |
| 110 | 	 *  | |
| 111 | 	 * @param maxRetries | |
| 112 | 	 *            the maximum number of attempts | |
| 113 | 	 * @return this instance for fluent chaining | |
| 114 | 	 */ | |
| 115 | 	public PerExecutionDelayBuilder<P> maxRetries(Integer maxRetries) { | |
| 116 | 2
1. maxRetries : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE 2. maxRetries : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → KILLED | 		this.maxRetriesValueBuilder.setValue(maxRetries); | 
| 117 | 2
1. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → SURVIVED 2. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → NO_COVERAGE | 		return this; | 
| 118 | 	} | |
| 119 | ||
| 120 | 	/** | |
| 121 | 	 * Set the maximum number of attempts. | |
| 122 | 	 *  | |
| 123 | 	 * <p> | |
| 124 | 	 * This method is mainly used by {@link Configurer}s to register some | |
| 125 | 	 * property keys and/or a default value. The aim is to let developer be able | |
| 126 | 	 * to externalize its configuration (using system properties, configuration | |
| 127 | 	 * file or anything else). If the developer doesn't configure any value for | |
| 128 | 	 * the registered properties, the default value is used (if set). | |
| 129 | 	 *  | |
| 130 | 	 * <pre> | |
| 131 | 	 * .maxRetries() | |
| 132 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 133 | 	 *   .defaultValue(5) | |
| 134 | 	 * </pre> | |
| 135 | 	 *  | |
| 136 | 	 * <p> | |
| 137 | 	 * Non-null value set using {@link #maxRetries(Integer)} takes precedence | |
| 138 | 	 * over property values and default value. | |
| 139 | 	 *  | |
| 140 | 	 * <pre> | |
| 141 | 	 * .maxRetries(10) | |
| 142 | 	 * .maxRetries() | |
| 143 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 144 | 	 *   .defaultValue(5) | |
| 145 | 	 * </pre> | |
| 146 | 	 *  | |
| 147 | 	 * The value {@code 10} is used regardless of the value of the properties | |
| 148 | 	 * and default value. | |
| 149 | 	 *  | |
| 150 | 	 * <p> | |
| 151 | 	 * See {@link ConfigurationValueBuilder} for more information. | |
| 152 | 	 *  | |
| 153 | 	 *  | |
| 154 | 	 * @return the builder to configure property keys/default value | |
| 155 | 	 */ | |
| 156 | 	public ConfigurationValueBuilder<PerExecutionDelayBuilder<P>, Integer> maxRetries() { | |
| 157 | 8
1. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → NO_COVERAGE 2. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 3. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 4. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 5. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 6. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 7. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED 8. maxRetries : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::maxRetries → KILLED | 		return maxRetriesValueBuilder; | 
| 158 | 	} | |
| 159 | ||
| 160 | 	/** | |
| 161 | 	 * Set specific delays (in milliseconds) used for each execution. If there | |
| 162 | 	 * are more attempts than the configured delays, the last delays is used for | |
| 163 | 	 * remaining attempts. | |
| 164 | 	 *  | |
| 165 | 	 * <p> | |
| 166 | 	 * The value set using this method takes precedence over any property and | |
| 167 | 	 * default value configured using {@link #delays()}. | |
| 168 | 	 *  | |
| 169 | 	 * <pre> | |
| 170 | 	 * .delays(5000L) | |
| 171 | 	 * .delays() | |
| 172 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 173 | 	 *   .defaultValue(10000L) | |
| 174 | 	 * </pre> | |
| 175 | 	 *  | |
| 176 | 	 * <pre> | |
| 177 | 	 * .delays(5000L) | |
| 178 | 	 * .delays() | |
| 179 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 180 | 	 *   .defaultValue(10000L) | |
| 181 | 	 * </pre> | |
| 182 | 	 *  | |
| 183 | 	 * In both cases, {@code delays(5000L)} is used. | |
| 184 | 	 *  | |
| 185 | 	 * <p> | |
| 186 | 	 * If this method is called several times, only the last value is used. | |
| 187 | 	 *  | |
| 188 | 	 * <p> | |
| 189 | 	 * If {@code null} value is set, it is like not setting a value at all. The | |
| 190 | 	 * property/default value configuration is applied. | |
| 191 | 	 *  | |
| 192 | 	 * @param delays | |
| 193 | 	 *            the delays for each execution | |
| 194 | 	 * @return this instance for fluent chaining | |
| 195 | 	 */ | |
| 196 | 	public PerExecutionDelayBuilder<P> delays(List<Long> delays) { | |
| 197 | 4
1. delays : negated conditional → NO_COVERAGE 2. delays : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE 3. delays : negated conditional → KILLED 4. delays : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → KILLED | 		delaysValueBuilder.setValue(delays == null ? null : delays.toArray(new Long[delays.size()])); | 
| 198 | 2
1. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → NO_COVERAGE 2. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED | 		return this; | 
| 199 | 	} | |
| 200 | ||
| 201 | 	/** | |
| 202 | 	 * Set specific delays (in milliseconds) used for each execution. If there | |
| 203 | 	 * are more attempts than the configured delays, the last delays is used for | |
| 204 | 	 * remaining attempts. | |
| 205 | 	 *  | |
| 206 | 	 * <p> | |
| 207 | 	 * The value set using this method takes precedence over any property and | |
| 208 | 	 * default value configured using {@link #delays()}. | |
| 209 | 	 *  | |
| 210 | 	 * <pre> | |
| 211 | 	 * .delays(5000L) | |
| 212 | 	 * .delays() | |
| 213 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 214 | 	 *   .defaultValue(10000L) | |
| 215 | 	 * </pre> | |
| 216 | 	 *  | |
| 217 | 	 * <pre> | |
| 218 | 	 * .delays(5000L) | |
| 219 | 	 * .delays() | |
| 220 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 221 | 	 *   .defaultValue(10000L) | |
| 222 | 	 * </pre> | |
| 223 | 	 *  | |
| 224 | 	 * In both cases, {@code delays(5000L)} is used. | |
| 225 | 	 *  | |
| 226 | 	 * <p> | |
| 227 | 	 * If this method is called several times, only the last value is used. | |
| 228 | 	 *  | |
| 229 | 	 * <p> | |
| 230 | 	 * If {@code null} value is set, it is like not setting a value at all. The | |
| 231 | 	 * property/default value configuration is applied. | |
| 232 | 	 *  | |
| 233 | 	 * @param delays | |
| 234 | 	 *            the delays for each execution | |
| 235 | 	 * @return this instance for fluent chaining | |
| 236 | 	 */ | |
| 237 | 	public PerExecutionDelayBuilder<P> delays(Long... delays) { | |
| 238 | 1
1. delays : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE | 		delaysValueBuilder.setValue(delays); | 
| 239 | 1
1. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → NO_COVERAGE | 		return this; | 
| 240 | 	} | |
| 241 | ||
| 242 | 	/** | |
| 243 | 	 * Set specific delays (in milliseconds) used for each new execution. If | |
| 244 | 	 * there are more attempts than the configured delays, the last delay is | |
| 245 | 	 * used for remaining attempts. | |
| 246 | 	 *  | |
| 247 | 	 * <p> | |
| 248 | 	 * This method is mainly used by {@link Configurer}s to register some | |
| 249 | 	 * property keys and/or a default value. The aim is to let developer be able | |
| 250 | 	 * to externalize its configuration (using system properties, configuration | |
| 251 | 	 * file or anything else). If the developer doesn't configure any value for | |
| 252 | 	 * the registered properties, the default value is used (if set). | |
| 253 | 	 *  | |
| 254 | 	 * <pre> | |
| 255 | 	 * .delays() | |
| 256 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 257 | 	 *   .defaultValue(10000L) | |
| 258 | 	 * </pre> | |
| 259 | 	 *  | |
| 260 | 	 * <p> | |
| 261 | 	 * Non-null value set using {@link #delays(Long...)} takes precedence over | |
| 262 | 	 * property values and default value. | |
| 263 | 	 *  | |
| 264 | 	 * <pre> | |
| 265 | 	 * .delays(5000L) | |
| 266 | 	 * .delays() | |
| 267 | 	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
| 268 | 	 *   .defaultValue(10000L) | |
| 269 | 	 * </pre> | |
| 270 | 	 *  | |
| 271 | 	 * The value {@code 5000L} is used regardless of the value of the properties | |
| 272 | 	 * and default value. | |
| 273 | 	 *  | |
| 274 | 	 * <p> | |
| 275 | 	 * See {@link ConfigurationValueBuilder} for more information. | |
| 276 | 	 *  | |
| 277 | 	 *  | |
| 278 | 	 * @return the builder to configure property keys/default value | |
| 279 | 	 */ | |
| 280 | 	public ConfigurationValueBuilder<PerExecutionDelayBuilder<P>, Long[]> delays() { | |
| 281 | 8
1. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → NO_COVERAGE 2. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 3. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 4. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 5. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 6. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 7. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED 8. delays : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::delays → KILLED | 		return delaysValueBuilder; | 
| 282 | 	} | |
| 283 | ||
| 284 | 	@Override | |
| 285 | 	public RetryStrategy build() { | |
| 286 | 		int evaluatedMaxRetries = buildMaxRetries(); | |
| 287 | 		List<Long> evaluatedDelays = buildDelays(); | |
| 288 | 9
1. build : negated conditional → NO_COVERAGE 2. build : negated conditional → SURVIVED 3. build : negated conditional → NO_COVERAGE 4. build : negated conditional → SURVIVED 5. build : negated conditional → TIMED_OUT 6. build : negated conditional → KILLED 7. build : negated conditional → KILLED 8. build : negated conditional → KILLED 9. build : negated conditional → KILLED | 		if (evaluatedMaxRetries == 0 || evaluatedDelays.isEmpty()) { | 
| 289 | 			return null; | |
| 290 | 		} | |
| 291 | 3
1. build : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::build → NO_COVERAGE 2. build : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::build → TIMED_OUT 3. build : replaced return value with null for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::build → KILLED | 		return buildContext.register(new PerExecutionDelayRetry(evaluatedMaxRetries, evaluatedDelays)); | 
| 292 | 	} | |
| 293 | ||
| 294 | 	private int buildMaxRetries() { | |
| 295 | 4
1. buildMaxRetries : replaced int return with 0 for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildMaxRetries → NO_COVERAGE 2. buildMaxRetries : replaced int return with 0 for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildMaxRetries → SURVIVED 3. buildMaxRetries : replaced int return with 0 for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildMaxRetries → TIMED_OUT 4. buildMaxRetries : replaced int return with 0 for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildMaxRetries → KILLED | 		return maxRetriesValueBuilder.getValue(0); | 
| 296 | 	} | |
| 297 | ||
| 298 | 	private List<Long> buildDelays() { | |
| 299 | 		Long[] delays = delaysValueBuilder.getValue(new Long[0]); | |
| 300 | 4
1. buildDelays : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildDelays → SURVIVED 2. buildDelays : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildDelays → NO_COVERAGE 3. buildDelays : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildDelays → TIMED_OUT 4. buildDelays : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/retry/PerExecutionDelayBuilder::buildDelays → KILLED | 		return Arrays.asList(delays); | 
| 301 | 	} | |
| 302 | } | |
| Mutations | ||
| 116 | 1.1 2.2 | |
| 117 | 1.1 2.2 | |
| 157 | 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 | |
| 197 | 1.1 2.2 3.3 4.4 | |
| 198 | 1.1 2.2 | |
| 238 | 1.1 | |
| 239 | 1.1 | |
| 281 | 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 | |
| 288 | 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 | |
| 291 | 1.1 2.2 3.3 | |
| 295 | 1.1 2.2 3.3 4.4 | |
| 300 | 1.1 2.2 3.3 4.4 |