kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8dc429c29f603dab81684813094bbe67f67bb4a5360f429eba31ca2872953d27 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-11-03 12:21 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-11-05 18:58 +01:00 |
| Parents | 2d8132f27b8f |
3 files changed
modified changelog/README.md +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [bug] JAVA-656: Fix NPE in ControlConnection.updateLocationInfo. |
| 13 | 13 | - [bug] JAVA-966: Count uninitialized connections in conviction policy. |
| 14 | 14 | - [improvement] JAVA-917: Document SSL configuration. |
| 15 | +- [improvement] JAVA-652: Add DCAwareRoundRobinPolicy builder. | |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | ### 2.0.11 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +105 −83
| @@ -22,8 +22,7 @@ | ||
| 22 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 23 | |
| 24 | 24 | 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.*; | |
| 27 | 26 | import com.google.common.collect.AbstractIterator; |
| 28 | 27 | import org.slf4j.Logger; |
| 29 | 28 | import org.slf4j.LoggerFactory; |
| @@ -46,15 +45,23 @@ | ||
| 46 | 45 | * data center can be reached. |
| 47 | 46 | * <p> |
| 48 | 47 | * 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. | |
| 52 | 50 | */ |
| 53 | 51 | public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy { |
| 54 | 52 | |
| 55 | 53 | private static final Logger logger = LoggerFactory.getLogger(DCAwareRoundRobinPolicy.class); |
| 56 | 54 | |
| 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 = ""; | |
| 58 | 65 | |
| 59 | 66 | private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>(); |
| 60 | 67 | private final AtomicInteger index = new AtomicInteger(); |
| @@ -68,101 +75,35 @@ | ||
| 68 | 75 | private volatile Configuration configuration; |
| 69 | 76 | |
| 70 | 77 | /** |
| 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()}. | |
| 83 | 80 | */ |
| 81 | + @Deprecated | |
| 84 | 82 | public DCAwareRoundRobinPolicy() { |
| 85 | 83 | this(null, 0, false, true); |
| 86 | 84 | } |
| 87 | 85 | |
| 88 | 86 | /** |
| 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()}. | |
| 101 | 89 | */ |
| 102 | 90 | public DCAwareRoundRobinPolicy(String localDc) { |
| 103 | 91 | this(localDc, 0, false, false); |
| 104 | 92 | } |
| 105 | 93 | |
| 106 | 94 | /** |
| 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()}. | |
| 133 | 98 | */ |
| 134 | 99 | public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc) { |
| 135 | 100 | this(localDc, usedHostsPerRemoteDc, false, false); |
| 136 | 101 | } |
| 137 | 102 | |
| 138 | 103 | /** |
| 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()}. | |
| 166 | 107 | */ |
| 167 | 108 | public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc, boolean allowRemoteDCsForLocalConsistencyLevel) { |
| 168 | 109 | this(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, false); |
| @@ -381,4 +322,85 @@ | ||
| 381 | 322 | public void close() { |
| 382 | 323 | // nothing to do |
| 383 | 324 | } |
| 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 | + } | |
| 384 | 406 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/Policies.java +1 −1
| @@ -87,7 +87,7 @@ | ||
| 87 | 87 | public static LoadBalancingPolicy defaultLoadBalancingPolicy() { |
| 88 | 88 | // Note: balancing policies are stateful, so we can't store that in a static or that would screw thing |
| 89 | 89 | // up if multiple Cluster instance are started in the same JVM. |
| 90 | - return new TokenAwarePolicy(new DCAwareRoundRobinPolicy()); | |
| 90 | + return new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |