|
6 | 6 | import java.util.concurrent.ConcurrentHashMap;
|
7 | 7 | import java.util.logging.Level;
|
8 | 8 | import java.util.logging.Logger;
|
9 |
| - |
10 | 9 | import software.amazon.awssdk.endpoints.Endpoint;
|
11 | 10 | import software.amazon.awssdk.services.dynamodb.endpoints.DynamoDbEndpointParams;
|
12 | 11 | import software.amazon.awssdk.services.dynamodb.endpoints.DynamoDbEndpointProvider;
|
|
16 | 15 | // AlternatorEndpointProvider, which maintains up-to-date knowledge of the
|
17 | 16 | // live nodes in Alternator data center (by holding a AlternatorLiveNodes
|
18 | 17 | // object), and choose a different node for each request.
|
| 18 | +/** |
| 19 | + * AlternatorEndpointProvider class. |
| 20 | + * |
| 21 | + * @author dmitry.kropachev |
| 22 | + */ |
19 | 23 | public class AlternatorEndpointProvider implements DynamoDbEndpointProvider {
|
20 |
| - private final AlternatorLiveNodes liveNodes; |
21 |
| - private final Map<URI, CompletableFuture<Endpoint>> futureCache; |
22 |
| - private static Logger logger = Logger.getLogger(AlternatorEndpointProvider.class.getName()); |
| 24 | + private final AlternatorLiveNodes liveNodes; |
| 25 | + private final Map<URI, CompletableFuture<Endpoint>> futureCache; |
| 26 | + private static Logger logger = Logger.getLogger(AlternatorEndpointProvider.class.getName()); |
23 | 27 |
|
24 |
| - public AlternatorEndpointProvider(URI seedURI) { |
25 |
| - this(seedURI, "", ""); |
26 |
| - } |
| 28 | + /** |
| 29 | + * Constructor for AlternatorEndpointProvider. |
| 30 | + * |
| 31 | + * @param seedURI a {@link java.net.URI} object |
| 32 | + */ |
| 33 | + public AlternatorEndpointProvider(URI seedURI) { |
| 34 | + this(seedURI, "", ""); |
| 35 | + } |
27 | 36 |
|
28 |
| - public AlternatorEndpointProvider(URI seedURI, String datacenter, String rack) { |
29 |
| - futureCache = new ConcurrentHashMap<>(); |
30 |
| - liveNodes = new AlternatorLiveNodes(seedURI, datacenter, rack); |
31 |
| - try { |
32 |
| - liveNodes.validate(); |
33 |
| - liveNodes.checkIfRackAndDatacenterSetCorrectly(); |
34 |
| - if (!datacenter.isEmpty() || !rack.isEmpty()) { |
35 |
| - if (!liveNodes.checkIfRackDatacenterFeatureIsSupported()) { |
36 |
| - logger.log(Level.SEVERE, String.format("server %s does not support rack or datacenter filtering", seedURI)); |
37 |
| - } |
38 |
| - } |
39 |
| - } catch (AlternatorLiveNodes.ValidationError | AlternatorLiveNodes.FailedToCheck e) { |
40 |
| - throw new RuntimeException(e); |
| 37 | + /** |
| 38 | + * Constructor for AlternatorEndpointProvider. |
| 39 | + * |
| 40 | + * @param seedURI a {@link java.net.URI} object |
| 41 | + * @param datacenter a {@link java.lang.String} object |
| 42 | + * @param rack a {@link java.lang.String} object |
| 43 | + * @since 1.0.1 |
| 44 | + */ |
| 45 | + public AlternatorEndpointProvider(URI seedURI, String datacenter, String rack) { |
| 46 | + futureCache = new ConcurrentHashMap<>(); |
| 47 | + liveNodes = new AlternatorLiveNodes(seedURI, datacenter, rack); |
| 48 | + try { |
| 49 | + liveNodes.validate(); |
| 50 | + liveNodes.checkIfRackAndDatacenterSetCorrectly(); |
| 51 | + if (!datacenter.isEmpty() || !rack.isEmpty()) { |
| 52 | + if (!liveNodes.checkIfRackDatacenterFeatureIsSupported()) { |
| 53 | + logger.log( |
| 54 | + Level.SEVERE, |
| 55 | + String.format("server %s does not support rack or datacenter filtering", seedURI)); |
41 | 56 | }
|
42 |
| - liveNodes.start(); |
43 |
| - } |
| 57 | + } |
| 58 | + } catch (AlternatorLiveNodes.ValidationError | AlternatorLiveNodes.FailedToCheck e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + liveNodes.start(); |
| 62 | + } |
44 | 63 |
|
45 |
| - @Override |
46 |
| - public CompletableFuture<Endpoint> resolveEndpoint(DynamoDbEndpointParams endpointParams) { |
47 |
| - URI uri = liveNodes.nextAsURI(); |
48 |
| - CompletableFuture<Endpoint> endpoint = futureCache.getOrDefault(uri, null); |
49 |
| - if (endpoint != null) { |
50 |
| - return endpoint; |
51 |
| - } |
52 |
| - endpoint = new CompletableFuture<>(); |
53 |
| - endpoint.complete(Endpoint.builder().url(uri).build()); |
54 |
| - futureCache.put(uri, endpoint); |
55 |
| - return endpoint; |
56 |
| - } |
| 64 | + /** {@inheritDoc} */ |
| 65 | + @Override |
| 66 | + public CompletableFuture<Endpoint> resolveEndpoint(DynamoDbEndpointParams endpointParams) { |
| 67 | + URI uri = liveNodes.nextAsURI(); |
| 68 | + CompletableFuture<Endpoint> endpoint = futureCache.getOrDefault(uri, null); |
| 69 | + if (endpoint != null) { |
| 70 | + return endpoint; |
| 71 | + } |
| 72 | + endpoint = new CompletableFuture<>(); |
| 73 | + endpoint.complete(Endpoint.builder().url(uri).build()); |
| 74 | + futureCache.put(uri, endpoint); |
| 75 | + return endpoint; |
| 76 | + } |
57 | 77 | }
|
0 commit comments