|
| 1 | +package com.datastax.driver.core.policies; |
| 2 | + |
| 3 | +import com.datastax.driver.core.*; |
| 4 | +import com.google.common.collect.AbstractIterator; |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.util.*; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | + |
| 9 | +/** |
| 10 | + * A wrapper load balancing policy that adds "Power of 2 Choice" algorithm to a child policy. |
| 11 | + * |
| 12 | + * <p>This policy encapsulates another policy. The resulting policy works in the following way: |
| 13 | + * |
| 14 | + * <ul> |
| 15 | + * <li>the {@code distance} method is inherited from the child policy. |
| 16 | + * <li>the {@code newQueryPlan} method will compare first two hosts (by number of inflight requests) returned from |
| 17 | + * the {@code newQueryPlan} method of the child policy, and the host with fewer number of inflight requests will be |
| 18 | + * returned the first. It will allow to always avoid the worst option (comparing by number of inflight requests). |
| 19 | + * <li>besides the first two hosts returned by the child policy's {@code newQueryPlan} method, the ordering of the |
| 20 | + * rest of the hosts will remain the same. |
| 21 | + * </ul> |
| 22 | + * |
| 23 | + * <p>If you wrap the {@code RandomTwoChoicePolicy} policy with {@code TokenAwarePolicy}, it will compare the first two |
| 24 | + * replicas by the number of inflight requests, and the worse option will always be avoided. In that case, it is recommended |
| 25 | + * to use the TokenAwarePolicy with {@code ReplicaOrdering.RANDOM strategy}, which will return the replicas in a shuffled |
| 26 | + * order and thus will make the "Power of 2 Choice" algorithm more efficient. |
| 27 | + */ |
| 28 | +public class RandomTwoChoicePolicy implements ChainableLoadBalancingPolicy { |
| 29 | + private final LoadBalancingPolicy childPolicy; |
| 30 | + private volatile Metrics metrics; |
| 31 | + private volatile Metadata clusterMetadata; |
| 32 | + private volatile ProtocolVersion protocolVersion; |
| 33 | + private volatile CodecRegistry codecRegistry; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new {@code RandomTwoChoicePolicy}. |
| 37 | + * |
| 38 | + * @param childPolicy the load balancing policy to wrap with "Power of 2 Choice" algorithm. |
| 39 | + */ |
| 40 | + public RandomTwoChoicePolicy(LoadBalancingPolicy childPolicy) { |
| 41 | + this.childPolicy = childPolicy; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public LoadBalancingPolicy getChildPolicy() { |
| 46 | + return childPolicy; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void init(Cluster cluster, Collection<Host> hosts) { |
| 51 | + this.metrics = cluster.getMetrics(); |
| 52 | + this.clusterMetadata = cluster.getMetadata(); |
| 53 | + this.protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); |
| 54 | + this.codecRegistry = cluster.getConfiguration().getCodecRegistry(); |
| 55 | + childPolicy.init(cluster, hosts); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritDoc} |
| 60 | + * |
| 61 | + * <p>This implementation always returns distances as reported by the wrapped policy. |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public HostDistance distance(Host host) { |
| 65 | + return childPolicy.distance(host); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritDoc} |
| 70 | + * |
| 71 | + * <p>The returned plan will compare (by the number of inflight requests) the first 2 hosts returned by the child |
| 72 | + * policy's {@code newQueryPlan} method, and the host with fewer inflight requests will be returned the first. |
| 73 | + * The rest of the child policy's query plan will be left intact. |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { |
| 77 | + String keyspace = statement.getKeyspace(); |
| 78 | + if (keyspace == null) keyspace = loggedKeyspace; |
| 79 | + |
| 80 | + ByteBuffer routingKey = statement.getRoutingKey(protocolVersion, codecRegistry); |
| 81 | + if (routingKey == null || keyspace == null) { |
| 82 | + return childPolicy.newQueryPlan(loggedKeyspace, statement); |
| 83 | + } |
| 84 | + |
| 85 | + final Token t = clusterMetadata.newToken(statement.getPartitioner(), routingKey); |
| 86 | + final Iterator<Host> childIterator = childPolicy.newQueryPlan(keyspace, statement); |
| 87 | + |
| 88 | + final Host host1 = childIterator.hasNext() ? childIterator.next() : null; |
| 89 | + final Host host2 = childIterator.hasNext() ? childIterator.next() : null; |
| 90 | + |
| 91 | + final AtomicInteger host1ShardInflightRequests = new AtomicInteger(0); |
| 92 | + final AtomicInteger host2ShardInflightRequests = new AtomicInteger(0); |
| 93 | + |
| 94 | + if (host1 != null) { |
| 95 | + final int host1ShardId = host1.getShardingInfo().shardId(t); |
| 96 | + host1ShardInflightRequests.set( |
| 97 | + metrics.getPerShardInflightRequestInfo().getValue().get(host1).get(host1ShardId)); |
| 98 | + } |
| 99 | + |
| 100 | + if (host2 != null) { |
| 101 | + final int host2ShardId = host2.getShardingInfo().shardId(t); |
| 102 | + host2ShardInflightRequests.set( |
| 103 | + metrics.getPerShardInflightRequestInfo().getValue().get(host2).get(host2ShardId)); |
| 104 | + } |
| 105 | + |
| 106 | + return new AbstractIterator<Host>() { |
| 107 | + private final Host firstChosenHost = |
| 108 | + host1ShardInflightRequests.get() < host2ShardInflightRequests.get() ? host1 : host2; |
| 109 | + private final Host secondChosenHost = |
| 110 | + host1ShardInflightRequests.get() < host2ShardInflightRequests.get() ? host2 : host1; |
| 111 | + private int index = 0; |
| 112 | + |
| 113 | + @Override |
| 114 | + protected Host computeNext() { |
| 115 | + if (index == 0) { |
| 116 | + index++; |
| 117 | + return firstChosenHost; |
| 118 | + } else if (index == 1) { |
| 119 | + index++; |
| 120 | + return secondChosenHost; |
| 121 | + } else if (childIterator.hasNext()) { |
| 122 | + return childIterator.next(); |
| 123 | + } |
| 124 | + |
| 125 | + return endOfData(); |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onAdd(Host host) { |
| 132 | + childPolicy.onAdd(host); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onUp(Host host) { |
| 137 | + childPolicy.onUp(host); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void onDown(Host host) { |
| 142 | + childPolicy.onDown(host); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void onRemove(Host host) { |
| 147 | + childPolicy.onRemove(host); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void close() { |
| 152 | + childPolicy.close(); |
| 153 | + } |
| 154 | +} |
0 commit comments