Skip to content

Commit

Permalink
Merge pull request #43 from dkropachev/dk/java-have-resolve-future-cache
Browse files Browse the repository at this point in the history
Java: have a future cache
  • Loading branch information
dkropachev authored Nov 15, 2024
2 parents 4c296a6 + 7f1adff commit c6f2c02
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.scylladb.alternator;

import java.net.URI;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

import software.amazon.awssdk.endpoints.Endpoint;
import software.amazon.awssdk.services.dynamodb.endpoints.DynamoDbEndpointParams;
Expand All @@ -11,19 +13,26 @@
// choose a different endpoint for each request. Here we implement an
// AlternatorEndpointProvider, which maintains up-to-date knowledge of the
// live nodes in Alternator data center (by holding a AlternatorLiveNodes
// object), and choose a different node for each request.
// object), and choose a different node for each request.
public class AlternatorEndpointProvider implements DynamoDbEndpointProvider {
AlternatorLiveNodes liveNodes;
private final AlternatorLiveNodes liveNodes;
private final Map<URI, CompletableFuture<Endpoint>> futureCache;

public AlternatorEndpointProvider(URI seedURI) {
futureCache = new ConcurrentHashMap<>();
liveNodes = AlternatorLiveNodes.create(seedURI);
}

@Override
public CompletableFuture<Endpoint> resolveEndpoint(DynamoDbEndpointParams endpointParams) {
Endpoint ret = Endpoint.builder().url(liveNodes.nextAsURI()).build();
CompletableFuture<Endpoint> f = new CompletableFuture<Endpoint>();
f.complete(ret);
return f;
URI uri = liveNodes.nextAsURI();
CompletableFuture<Endpoint> endpoint = futureCache.getOrDefault(uri, null);
if (endpoint != null) {
return endpoint;
}
endpoint = new CompletableFuture<>();
endpoint.complete(Endpoint.builder().url(uri).build());
futureCache.put(uri, endpoint);
return endpoint;
}

}

0 comments on commit c6f2c02

Please sign in to comment.