|
| 1 | +package com.aws.ssa.keyspaces.loadbalancing; |
| 2 | + |
| 3 | +import com.datastax.oss.driver.api.core.context.DriverContext; |
| 4 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 5 | +import com.datastax.oss.driver.api.core.session.Request; |
| 6 | +import com.datastax.oss.driver.api.core.session.Session; |
| 7 | +import com.datastax.oss.driver.internal.core.loadbalancing.BasicLoadBalancingPolicy; |
| 8 | +import com.datastax.oss.driver.internal.core.loadbalancing.helper.MandatoryLocalDcHelper; |
| 9 | +import com.datastax.oss.driver.internal.core.pool.ChannelPool; |
| 10 | +import com.datastax.oss.driver.internal.core.session.DefaultSession; |
| 11 | +import com.datastax.oss.driver.internal.core.util.ArrayUtils; |
| 12 | +import com.datastax.oss.driver.internal.core.util.collection.QueryPlan; |
| 13 | +import com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan; |
| 14 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 15 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | +import java.util.concurrent.ThreadLocalRandom; |
| 21 | + |
| 22 | +/*** |
| 23 | + * AmazonKeyspacesLoadBalancingPolicy is a round robin policy that randomizes host order. While you may see a three to nine |
| 24 | + * node cluster when connecting to Amazon Keyspaces, connections are loadbalanced service side to multiple request handlers. This |
| 25 | + * policy provides even distribution across the driver connection pool. Traditional token-aware policies and latency aware policies |
| 26 | + * are not necessary for good performance in Amazon Keyspaces. |
| 27 | + */ |
| 28 | +public class AmazonKeyspacesRoundRobinLoadBalancingPolicy extends BasicLoadBalancingPolicy { |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(AmazonKeyspacesRoundRobinLoadBalancingPolicy.class); |
| 30 | + |
| 31 | + public AmazonKeyspacesRoundRobinLoadBalancingPolicy(@NonNull DriverContext context, @NonNull String profileName) { |
| 32 | + super(context, profileName); |
| 33 | + } |
| 34 | + |
| 35 | + public void init(@NonNull Map<UUID, Node> nodes, @NonNull DistanceReporter distanceReporter) { |
| 36 | + super.init(nodes, distanceReporter); |
| 37 | + LOG.info("Total number of nodes visible to driver: " + ((nodes == null)?0:nodes.size())); |
| 38 | + } |
| 39 | + |
| 40 | + @NonNull |
| 41 | + protected Optional<String> discoverLocalDc(@NonNull Map<UUID, Node> nodes) { |
| 42 | + return (new MandatoryLocalDcHelper(this.context, this.profile, this.logPrefix)).discoverLocalDc(nodes); |
| 43 | + } |
| 44 | + |
| 45 | + protected int getInFlight(@NonNull Node node, @NonNull Session session) { |
| 46 | + ChannelPool pool = (ChannelPool)((DefaultSession)session).getPools().get(node); |
| 47 | + return pool == null ? 0 : pool.getInFlight(); |
| 48 | + } |
| 49 | + protected int getSize(@NonNull Node node, @NonNull Session session) { |
| 50 | + ChannelPool pool = (ChannelPool)((DefaultSession)session).getPools().get(node); |
| 51 | + return pool == null ? 0 : pool.size(); |
| 52 | + } |
| 53 | + /*** |
| 54 | + * Fisher–Yates or Richard Durstenfeld shuffle implemented from lowest index to highest |
| 55 | + * https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm |
| 56 | + * @param currentNodes |
| 57 | + * @return shuffledNodes |
| 58 | + */ |
| 59 | + public static void reverseDurstenfeldShuffle(Object[] currentNodes, ThreadLocalRandom random){ |
| 60 | + int totalNodes = currentNodes.length; |
| 61 | + |
| 62 | + for(int currentNodeIndex = 0; currentNodeIndex < totalNodes-1; currentNodeIndex++) { |
| 63 | + |
| 64 | + int swapNodeIndex = random.nextInt(currentNodeIndex, totalNodes); |
| 65 | + |
| 66 | + ArrayUtils.swap(currentNodes, currentNodeIndex, swapNodeIndex); |
| 67 | + } |
| 68 | + } |
| 69 | + @NonNull |
| 70 | + public Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session session) { |
| 71 | + |
| 72 | + Object[] currentNodes = this.getLiveNodes().dc(this.getLocalDatacenter()).toArray(); |
| 73 | + |
| 74 | + Queue<Node> queryPlan = newQueryPlan(request, session, currentNodes); |
| 75 | + |
| 76 | + int totalNodes = currentNodes.length; |
| 77 | + |
| 78 | + if (LOG.isTraceEnabled()) { |
| 79 | + if (totalNodes > 0) { |
| 80 | + //int currentSize = getSize((Node) currentNodes[0], session); |
| 81 | + |
| 82 | + int inflight = getInFlight((Node) currentNodes[0], session); |
| 83 | + |
| 84 | + String firstNode = ((Node) currentNodes[0]).getEndPoint().toString(); |
| 85 | + |
| 86 | + int openConnections = ((Node) currentNodes[0]).getOpenConnections(); |
| 87 | + |
| 88 | + int requestPerMostUsedConnection = (openConnections > 0) ? (inflight / openConnections) : 0; |
| 89 | + |
| 90 | + LOG.trace(" Total local nodes: [{}], First Node [{}], Number of Connections: [{}], Total inflight:[{}], Number of Request per connection: [{}]", totalNodes, firstNode, openConnections, inflight, requestPerMostUsedConnection); |
| 91 | + } |
| 92 | + } |
| 93 | + return queryPlan; |
| 94 | + } |
| 95 | + |
| 96 | + @NonNull |
| 97 | + public Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session session, Object[] currentNodes ) { |
| 98 | + int totalNodes = currentNodes.length; |
| 99 | + |
| 100 | + if(totalNodes == 0) { |
| 101 | + |
| 102 | + LOG.trace(" Total local nodes is 0, returning empty query plan"); |
| 103 | + |
| 104 | + return this.maybeAddDcFailover(request, QueryPlan.EMPTY); |
| 105 | + } |
| 106 | + if(totalNodes > 1){ |
| 107 | + |
| 108 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 109 | + |
| 110 | + reverseDurstenfeldShuffle(currentNodes, random); |
| 111 | + } |
| 112 | + |
| 113 | + QueryPlan plan = new SimpleQueryPlan(currentNodes); |
| 114 | + |
| 115 | + return this.maybeAddDcFailover(request, (Queue) plan); |
| 116 | + } |
| 117 | +} |
0 commit comments