|
| 1 | +// SPDX-FileCopyrightText: 2022-present Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package io.atomix.client; |
| 6 | + |
| 7 | +import com.google.common.collect.Lists; |
| 8 | +import com.google.common.collect.Maps; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * Service config builder used to configure the internal retry |
| 16 | + * mechanism of the gRPC services. |
| 17 | + */ |
| 18 | +public class ServiceConfigBuilder { |
| 19 | + public static final String MAX_ATTEMPTS = "maxAttempts"; |
| 20 | + public static final String INITIAL_BACKOFF = "initialBackoff"; |
| 21 | + public static final String MAX_BACKOFF = "maxBackoff"; |
| 22 | + public static final String BACKOFF_MULTIPLIER = "backoffMultiplier"; |
| 23 | + public static final String RETRYABLE_STATUS_CODES = "retryableStatusCodes"; |
| 24 | + public static final String SERVICE = "service"; |
| 25 | + public static final String NAME = "name"; |
| 26 | + public static final String RETRY_POLICY = "retryPolicy"; |
| 27 | + public static final String METHOD_CONFIG = "methodConfig"; |
| 28 | + |
| 29 | + private Double maxAttempts = 5.0; |
| 30 | + private String initialBackoff = "0.5s"; |
| 31 | + private String maxBackoff = "30s"; |
| 32 | + private Double backoffMultiplier = 2.0; |
| 33 | + private List<String> retryableStatusCodes = List.of("UNAVAILABLE"); |
| 34 | + |
| 35 | + /** |
| 36 | + * Set the maximum number of attempts for retries |
| 37 | + * |
| 38 | + * @param maxAttempts max attempts to be performed |
| 39 | + * @return this |
| 40 | + */ |
| 41 | + ServiceConfigBuilder withMaxAttempts(double maxAttempts) { |
| 42 | + this.maxAttempts = maxAttempts; |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Set the initial backoff for retries |
| 48 | + * |
| 49 | + * @param initialBackoff the initial backoff |
| 50 | + * @return this |
| 51 | + */ |
| 52 | + ServiceConfigBuilder withInitialBackoff(String initialBackoff) { |
| 53 | + this.initialBackoff = initialBackoff; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set the maximum backoff for retries |
| 59 | + * |
| 60 | + * @param maxBackoff the maximum backoff |
| 61 | + * @return this |
| 62 | + */ |
| 63 | + ServiceConfigBuilder withMaxBackoff(String maxBackoff) { |
| 64 | + this.maxBackoff = maxBackoff; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the backoff multiplier for retries |
| 70 | + * |
| 71 | + * @param backoffMultiplier the backoff multiplier |
| 72 | + * @return this |
| 73 | + */ |
| 74 | + ServiceConfigBuilder withBackoffMultiplier(double backoffMultiplier) { |
| 75 | + this.backoffMultiplier = backoffMultiplier; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Define the retryable status codes. |
| 81 | + * |
| 82 | + * @param retryableStatusCodes the retryable status codes |
| 83 | + * @return this |
| 84 | + */ |
| 85 | + ServiceConfigBuilder withRetryableStatusCodes(List<String> retryableStatusCodes) { |
| 86 | + this.retryableStatusCodes = retryableStatusCodes; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Builds the band. |
| 92 | + * |
| 93 | + * @return a band |
| 94 | + */ |
| 95 | + Map<String, ?> build() { |
| 96 | + // For further infos look at service_config.proto in the grpc repo |
| 97 | + // https://github.com/grpc/proposal/blob/master/A6-client-retries.md#detailed-design |
| 98 | + Map<String, Object> methodConfig = Maps.newHashMap(); |
| 99 | + // method config is the overarching map |
| 100 | + List<Map<String, Object>> methodConfigs = Lists.newArrayList(); |
| 101 | + for (PrimitiveService primitiveService : PrimitiveService.values()) { |
| 102 | + // retryPolicy opaque map |
| 103 | + Map<String, Object> retryPolicy = Maps.newHashMap(); |
| 104 | + retryPolicy.put(MAX_ATTEMPTS, maxAttempts); |
| 105 | + retryPolicy.put(INITIAL_BACKOFF, initialBackoff); |
| 106 | + retryPolicy.put(MAX_BACKOFF, maxBackoff); |
| 107 | + retryPolicy.put(BACKOFF_MULTIPLIER, backoffMultiplier); |
| 108 | + retryPolicy.put(RETRYABLE_STATUS_CODES, retryableStatusCodes); |
| 109 | + |
| 110 | + // name defines the grpc services affected by the retry policy |
| 111 | + // we do that for all the primitives we want to support |
| 112 | + List<Map<String, Object>> name = Lists.newArrayList(); |
| 113 | + |
| 114 | + Map<String, Object> serviceName = Maps.newHashMap(); |
| 115 | + serviceName.put(SERVICE, primitiveService.getServiceName()); |
| 116 | + name.add(serviceName); |
| 117 | + |
| 118 | + Map<String, Object> serviceConfig = Maps.newHashMap(); |
| 119 | + serviceConfig.put(NAME, name); |
| 120 | + serviceConfig.put(RETRY_POLICY, retryPolicy); |
| 121 | + methodConfigs.add(serviceConfig); |
| 122 | + } |
| 123 | + methodConfig.put(METHOD_CONFIG, methodConfigs); |
| 124 | + return methodConfig; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments