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