Elffuss BraidHub

kikocis/java-driver public

JAVA-652: Add DCAwareRoundRobinPolicy builder.
Changeset8dc429c29f603dab81684813094bbe67f67bb4a5360f429eba31ca2872953d27
Authorolim7t <omichallat+github@gmail.com> · 2015-11-03 12:21 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-11-05 18:58 +01:00
Parents2d8132f27b8f

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -12,6 +12,7 @@
1212 - [bug] JAVA-656: Fix NPE in ControlConnection.updateLocationInfo.
1313 - [bug] JAVA-966: Count uninitialized connections in conviction policy.
1414 - [improvement] JAVA-917: Document SSL configuration.
15+- [improvement] JAVA-652: Add DCAwareRoundRobinPolicy builder.
1516
1617
1718 ### 2.0.11
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +105 −83
@@ -22,8 +22,7 @@
2222 import java.util.concurrent.atomic.AtomicInteger;
2323
2424 import com.google.common.annotations.VisibleForTesting;
25-import com.google.common.base.Joiner;
26-import com.google.common.base.Strings;
25+import com.google.common.base.*;
2726 import com.google.common.collect.AbstractIterator;
2827 import org.slf4j.Logger;
2928 import org.slf4j.LoggerFactory;
@@ -46,15 +45,23 @@
4645 * data center can be reached.
4746 * <p>
4847 * If used with a single data center, this policy is equivalent to the
49- * {@code LoadBalancingPolicy.RoundRobin} policy, but its DC awareness
50- * incurs a slight overhead so the {@code LoadBalancingPolicy.RoundRobin}
51- * policy could be preferred to this policy in that case.
48+ * {@link RoundRobinPolicy}, but its DC awareness incurs a slight overhead
49+ * so the latter should be preferred to this policy in that case.
5250 */
5351 public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy {
5452
5553 private static final Logger logger = LoggerFactory.getLogger(DCAwareRoundRobinPolicy.class);
5654
57- private final String UNSET = "";
55+ /**
56+ * Returns a builder to create a new instance.
57+ *
58+ * @return the builder.
59+ */
60+ public static Builder builder() {
61+ return new Builder();
62+ }
63+
64+ private static final String UNSET = "";
5865
5966 private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
6067 private final AtomicInteger index = new AtomicInteger();
@@ -68,101 +75,35 @@
6875 private volatile Configuration configuration;
6976
7077 /**
71- * Creates a new datacenter aware round robin policy that auto-discover
72- * the local data-center.
73- * <p>
74- * If this constructor is used, the data-center used as local will the
75- * data-center of the first Cassandra node the driver connects to. This
76- * will always be ok if all the contact points use at {@code Cluster}
77- * creation are in the local data-center. If it's not the case, you should
78- * provide the local data-center name yourself by using one of the other
79- * constructor of this class.
80- * <p>
81- * This constructor is a shortcut for {@code new DCAwareRoundRobinPolicy(null)},
82- * and as such will ignore all hosts in remote data-centers.
78+ * @deprecated {@link #builder()} is the recommended way to build instances of this class. This constructor was preserved for
79+ * backward-compatibility purposes, it is equivalent to {@code DCAwareRoundRobinPolicy.builder().build()}.
8380 */
81+ @Deprecated
8482 public DCAwareRoundRobinPolicy() {
8583 this(null, 0, false, true);
8684 }
8785
8886 /**
89- * Creates a new datacenter aware round robin policy given the name of
90- * the local datacenter.
91- * <p>
92- * The name of the local datacenter provided must be the local
93- * datacenter name as known by Cassandra.
94- * <p>
95- * The policy created will ignore all remote hosts. In other words,
96- * this is equivalent to {@code new DCAwareRoundRobinPolicy(localDc, 0)}.
97- *
98- * @param localDc the name of the local datacenter (as known by
99- * Cassandra). If this is {@code null}, the policy will default to the
100- * data-center of the first node connected to.
87+ * @deprecated {@link #builder()} is the recommended way to build instances of this class. This constructor was preserved for
88+ * backward-compatibility purposes, it is equivalent to {@code DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).build()}.
10189 */
10290 public DCAwareRoundRobinPolicy(String localDc) {
10391 this(localDc, 0, false, false);
10492 }
10593
10694 /**
107- * Creates a new DCAwareRoundRobin policy given the name of the local
108- * datacenter and that uses the provided number of host per remote
109- * datacenter as failover for the local hosts.
110- * <p>
111- * The name of the local datacenter provided must be the local
112- * datacenter name as known by Cassandra.
113- * <p>
114- * If {@code usedHostsPerRemoteDc > 0}, then if for a query no host
115- * in the local datacenter can be reached and if the consistency
116- * level of the query is not {@code LOCAL_ONE} or {@code LOCAL_QUORUM},
117- * then up to {@code usedHostsPerRemoteDc} host per remote data-center
118- * will be tried by the policy as a fallback. Please note that no
119- * remote host will be used for {@code LOCAL_ONE} and {@code LOCAL_QUORUM}
120- * since this would change the meaning of the consistency level (and
121- * thus somewhat break the consistency contract).
122- *
123- * @param localDc the name of the local datacenter (as known by
124- * Cassandra). If this is {@code null}, the policy will default to the
125- * data-center of the first node connected to.
126- * @param usedHostsPerRemoteDc the number of host per remote
127- * datacenter that policies created by the returned factory should
128- * consider. Created policies {@code distance} method will return a
129- * {@code HostDistance.REMOTE} distance for only {@code
130- * usedHostsPerRemoteDc} hosts per remote datacenter. Other hosts
131- * of the remote datacenters will be ignored (and thus no
132- * connections to them will be maintained).
95+ * @deprecated {@link #builder()} is the recommended way to build instances of this class. This constructor was preserved for
96+ * backward-compatibility purposes, it is equivalent to
97+ * {@code DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).build()}.
13398 */
13499 public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc) {
135100 this(localDc, usedHostsPerRemoteDc, false, false);
136101 }
137102
138103 /**
139- * Creates a new DCAwareRoundRobin policy given the name of the local
140- * datacenter and that uses the provided number of host per remote
141- * datacenter as failover for the local hosts.
142- * <p>
143- * This constructor is equivalent to {@link #DCAwareRoundRobinPolicy(String, int)}
144- * but allows to override the policy of never using remote data-center
145- * nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is
146- * however inadvisable to do so in almost all cases, as this would
147- * potentially break consistency guarantees and if you are fine with that,
148- * it's probably better to use a weaker consitency like {@code ONE}, {@code
149- * TWO} or {@code THREE}. As such, this constructor should generally
150- * be avoided in favor of {@link #DCAwareRoundRobinPolicy(String, int)}.
151- * Use it only if you know and understand what you do.
152- *
153- * @param localDc the name of the local datacenter (as known by
154- * Cassandra). If this is {@code null}, the policy will default to the
155- * data-center of the first node connected to.
156- * @param usedHostsPerRemoteDc the number of host per remote
157- * datacenter that policies created by the returned factory should
158- * consider. Created policies {@code distance} method will return a
159- * {@code HostDistance.REMOTE} distance for only {@code
160- * usedHostsPerRemoteDc} hosts per remote datacenter. Other hosts
161- * of the remote datacenters will be ignored (and thus no
162- * connections to them will be maintained).
163- * @param allowRemoteDCsForLocalConsistencyLevel whether or not the
164- * policy may return remote host when building query plan for query
165- * having consitency {@code LOCAL_ONE} and {@code LOCAL_QUORUM}.
104+ * @deprecated {@link #builder()} is the recommended way to build instances of this class. This constructor was preserved for
105+ * backward-compatibility purposes, it is equivalent to
106+ * {@code DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).allowRemoteDCsForLocalConsistencyLevel().build()}.
166107 */
167108 public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc, boolean allowRemoteDCsForLocalConsistencyLevel) {
168109 this(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, false);
@@ -381,4 +322,85 @@
381322 public void close() {
382323 // nothing to do
383324 }
325+
326+ /**
327+ * Helper class to build the policy.
328+ */
329+ public static class Builder {
330+ private String localDc;
331+ private int usedHostsPerRemoteDc;
332+ private boolean allowRemoteDCsForLocalConsistencyLevel;
333+
334+ /**
335+ * Sets the name of the datacenter that will be considered "local" by the policy.
336+ * <p>
337+ * This must be the name as known by Cassandra (in other words, the name in that appears in
338+ * {@code system.peers}, or in the output of admin tools like nodetool).
339+ * <p>
340+ * If this method isn't called, the policy will default to the datacenter of the first node
341+ * connected to. This will always be ok if all the contact points use at {@code Cluster}
342+ * creation are in the local data-center. Otherwise, you should provide the name yourself
343+ * with this method.
344+ *
345+ * @param localDc the name of the datacenter. It should not be {@code null}.
346+ * @return this builder.
347+ */
348+ public Builder withLocalDc(String localDc) {
349+ Preconditions.checkArgument(!Strings.isNullOrEmpty(localDc),
350+ "localDc name can't be null or empty. If you want to let the policy autodetect the datacenter, don't call Builder.withLocalDC");
351+ this.localDc = localDc;
352+ return this;
353+ }
354+
355+ /**
356+ * Sets the number of hosts per remote datacenter that the policy should consider.
357+ * <p>
358+ * The policy's {@code distance()} method will return a {@code HostDistance.REMOTE} distance for only {@code usedHostsPerRemoteDc}
359+ * hosts per remote datacenter. Other hosts of the remote datacenters will be ignored (and thus no connections to them will be
360+ * maintained).
361+ * <p>
362+ * If {@code usedHostsPerRemoteDc > 0}, then if for a query no host in the local datacenter can be reached and if the consistency
363+ * level of the query is not {@code LOCAL_ONE} or {@code LOCAL_QUORUM}, then up to {@code usedHostsPerRemoteDc} hosts per remote
364+ * datacenter will be tried by the policy as a fallback. By default, no remote host will be used for {@code LOCAL_ONE} and
365+ * {@code LOCAL_QUORUM}, since this would change the meaning of the consistency level, somewhat breaking the consistency contract
366+ * (this can be overridden with {@link #allowRemoteDCsForLocalConsistencyLevel()}).
367+ * <p>
368+ * If this method isn't called, the policy will default to 0.
369+ *
370+ * @param usedHostsPerRemoteDc the number.
371+ * @return this builder.
372+ */
373+ public Builder withUsedHostsPerRemoteDc(int usedHostsPerRemoteDc) {
374+ Preconditions.checkArgument(usedHostsPerRemoteDc >= 0,
375+ "usedHostsPerRemoteDc must be equal or greater than 0");
376+ this.usedHostsPerRemoteDc = usedHostsPerRemoteDc;
377+ return this;
378+ }
379+
380+ /**
381+ * Allows the policy to return remote hosts when building query plans for queries having consistency level {@code LOCAL_ONE}
382+ * or {@code LOCAL_QUORUM}.
383+ * <p>
384+ * When used in conjunction with {@link #withUsedHostsPerRemoteDc(int) usedHostsPerRemoteDc} > 0, this overrides the policy of
385+ * never using remote datacenter nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is however inadvisable to do
386+ * so in almost all cases, as this would potentially break consistency guarantees and if you are fine with that, it's probably
387+ * better to use a weaker consitency like {@code ONE}, {@code TWO} or {@code THREE}. As such, this method should generally be
388+ * avoided; use it only if you know and understand what you do.
389+ *
390+ * @return this builder.
391+ */
392+ public Builder allowRemoteDCsForLocalConsistencyLevel() {
393+ this.allowRemoteDCsForLocalConsistencyLevel = true;
394+ return this;
395+ }
396+
397+ /**
398+ * Builds the policy configured by this builder.
399+ *
400+ * @return the policy.
401+ */
402+ public DCAwareRoundRobinPolicy build() {
403+ return new DCAwareRoundRobinPolicy(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, true);
404+ }
405+ }
384406 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/Policies.java +1 −1
@@ -87,7 +87,7 @@
8787 public static LoadBalancingPolicy defaultLoadBalancingPolicy() {
8888 // Note: balancing policies are stateful, so we can't store that in a static or that would screw thing
8989 // up if multiple Cluster instance are started in the same JVM.
90- return new TokenAwarePolicy(new DCAwareRoundRobinPolicy());
90+ return new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build());
9191 }
9292
9393 /**