-
Notifications
You must be signed in to change notification settings - Fork 862
/
TokenAwarePolicy.java
192 lines (168 loc) · 7.02 KB
/
TokenAwarePolicy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 2012-2017 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.core.policies;
import com.datastax.driver.core.*;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Lists;
import java.nio.ByteBuffer;
import java.util.*;
/**
* A wrapper load balancing policy that add token awareness to a child policy.
* <p/>
* This policy encapsulates another policy. The resulting policy works in
* the following way:
* <ul>
* <li>the {@code distance} method is inherited from the child policy.</li>
* <li>the iterator return by the {@code newQueryPlan} method will first
* return the {@code LOCAL} replicas for the query (based on {@link Statement#getRoutingKey})
* <i>if possible</i> (i.e. if the query {@code getRoutingKey} method
* doesn't return {@code null} and if {@link Metadata#getReplicas}
* returns a non empty set of replicas for that partition key). If no
* local replica can be either found or successfully contacted, the rest
* of the query plan will fallback to one of the child policy.</li>
* </ul>
* <p/>
* Do note that only replica for which the child policy {@code distance}
* method returns {@code HostDistance.LOCAL} will be considered having
* priority. For example, if you wrap {@link DCAwareRoundRobinPolicy} with this
* token aware policy, replicas from remote data centers may only be
* returned after all the host of the local data center.
*/
public class TokenAwarePolicy implements ChainableLoadBalancingPolicy {
private final LoadBalancingPolicy childPolicy;
private final boolean shuffleReplicas;
private volatile Metadata clusterMetadata;
private volatile ProtocolVersion protocolVersion;
private volatile CodecRegistry codecRegistry;
/**
* Creates a new {@code TokenAware} policy.
*
* @param childPolicy the load balancing policy to wrap with token awareness.
* @param shuffleReplicas whether to shuffle the replicas returned by {@code getRoutingKey}.
* Note that setting this parameter to {@code true} might decrease the
* effectiveness of caching (especially at consistency level ONE), since
* the same row will be retrieved from any replica (instead of only the
* "primary" replica without shuffling).
* On the other hand, shuffling will better distribute writes, and can
* alleviate hotspots caused by "fat" partitions.
*/
public TokenAwarePolicy(LoadBalancingPolicy childPolicy, boolean shuffleReplicas) {
this.childPolicy = childPolicy;
this.shuffleReplicas = shuffleReplicas;
}
/**
* Creates a new {@code TokenAware} policy with shuffling of replicas.
*
* @param childPolicy the load balancing policy to wrap with token
* awareness.
* @see #TokenAwarePolicy(LoadBalancingPolicy, boolean)
*/
public TokenAwarePolicy(LoadBalancingPolicy childPolicy) {
this(childPolicy, true);
}
@Override
public LoadBalancingPolicy getChildPolicy() {
return childPolicy;
}
@Override
public void init(Cluster cluster, Collection<Host> hosts) {
clusterMetadata = cluster.getMetadata();
protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
codecRegistry = cluster.getConfiguration().getCodecRegistry();
childPolicy.init(cluster, hosts);
}
/**
* Return the HostDistance for the provided host.
*
* @param host the host of which to return the distance of.
* @return the HostDistance to {@code host} as returned by the wrapped policy.
*/
@Override
public HostDistance distance(Host host) {
return childPolicy.distance(host);
}
/**
* Returns the hosts to use for a new query.
* <p/>
* The returned plan will first return replicas (whose {@code HostDistance}
* for the child policy is {@code LOCAL}) for the query if it can determine
* them (i.e. mainly if {@code statement.getRoutingKey()} is not {@code null}).
* Following what it will return the plan of the child policy.
*
* @param statement the query for which to build the plan.
* @return the new query plan.
*/
@Override
public Iterator<Host> newQueryPlan(final String loggedKeyspace, final Statement statement) {
ByteBuffer partitionKey = statement.getRoutingKey(protocolVersion, codecRegistry);
String keyspace = statement.getKeyspace();
if (keyspace == null)
keyspace = loggedKeyspace;
if (partitionKey == null || keyspace == null)
return childPolicy.newQueryPlan(keyspace, statement);
final Set<Host> replicas = clusterMetadata.getReplicas(Metadata.quote(keyspace), partitionKey);
if (replicas.isEmpty())
return childPolicy.newQueryPlan(loggedKeyspace, statement);
final Iterator<Host> iter;
if (shuffleReplicas) {
List<Host> l = Lists.newArrayList(replicas);
Collections.shuffle(l);
iter = l.iterator();
} else {
iter = replicas.iterator();
}
return new AbstractIterator<Host>() {
private Iterator<Host> childIterator;
@Override
protected Host computeNext() {
while (iter.hasNext()) {
Host host = iter.next();
if (host.isUp() && childPolicy.distance(host) == HostDistance.LOCAL)
return host;
}
if (childIterator == null)
childIterator = childPolicy.newQueryPlan(loggedKeyspace, statement);
while (childIterator.hasNext()) {
Host host = childIterator.next();
// Skip it if it was already a local replica
if (!replicas.contains(host) || childPolicy.distance(host) != HostDistance.LOCAL)
return host;
}
return endOfData();
}
};
}
@Override
public void onUp(Host host) {
childPolicy.onUp(host);
}
@Override
public void onDown(Host host) {
childPolicy.onDown(host);
}
@Override
public void onAdd(Host host) {
childPolicy.onAdd(host);
}
@Override
public void onRemove(Host host) {
childPolicy.onRemove(host);
}
@Override
public void close() {
childPolicy.close();
}
}