kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 7e733280fc0a8c2f150528bc63da4dbca4f9860255f9974668c44f34fa57819c |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2014-03-17 20:24 +01:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2014-03-17 20:24 +01:00 |
| Parents | df49422ab9a3 |
4 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | - [new] Add method to check if a Cluster/Session instance has been closed already (JAVA-114) |
| 8 | 8 | - [new] Expose the name of the partitioner in use in the cluster metadata (JAVA-179) |
| 9 | 9 | - [new] Add new WhiteListPolicy to limit the nodes connected to a particular list |
| 10 | +- [improvement] Do not hop DC for LOCAL_* CL in DCAwareRoundRobinPolicy (JAVA-289) | |
| 10 | 11 | |
| 11 | 12 | |
| 12 | 13 | 1.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/ConsistencyLevel.java +9 −0
| @@ -61,4 +61,13 @@ | ||
| 61 | 61 | } |
| 62 | 62 | throw new AssertionError(); |
| 63 | 63 | } |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Whether or not the the consistency level applies to the local data-center only. | |
| 67 | + * | |
| 68 | + * @return whether this consistency level is {@code LOCAL_ONE} or {@code LOCAL_QUORUM}. | |
| 69 | + */ | |
| 70 | + public boolean isDCLocal() { | |
| 71 | + return this == LOCAL_ONE || this == LOCAL_QUORUM; | |
| 72 | + } | |
| 64 | 73 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +47 −1
| @@ -49,6 +49,7 @@ | ||
| 49 | 49 | private final AtomicInteger index = new AtomicInteger(); |
| 50 | 50 | private final String localDc; |
| 51 | 51 | private final int usedHostsPerRemoteDc; |
| 52 | + private final boolean dontHopForLocalCL; | |
| 52 | 53 | |
| 53 | 54 | /** |
| 54 | 55 | * Creates a new datacenter aware round robin policy given the name of |
| @@ -74,6 +75,15 @@ | ||
| 74 | 75 | * <p> |
| 75 | 76 | * The name of the local datacenter provided must be the local |
| 76 | 77 | * datacenter name as known by Cassandra. |
| 78 | + * <p> | |
| 79 | + * If {@code usedHostsPerRemoteDc > 0}, then if for a query no host | |
| 80 | + * in the local datacenter can be reached and if the consistency | |
| 81 | + * level of the query is not {@code LOCAL_ONE} or {@code LOCAL_QUORUM}, | |
| 82 | + * then up to {@code usedHostsPerRemoteDc} host per remote data-center | |
| 83 | + * will be tried by the policy as a fallback. Please note that no | |
| 84 | + * remote host will be used for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} | |
| 85 | + * since this would change the meaning of the consistency level (and | |
| 86 | + * thus somewhat break the consistency contract). | |
| 77 | 87 | * |
| 78 | 88 | * @param localDc the name of the local datacenter (as known by |
| 79 | 89 | * Cassandra). |
| @@ -86,8 +96,41 @@ | ||
| 86 | 96 | * connections to them will be maintained). |
| 87 | 97 | */ |
| 88 | 98 | public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc) { |
| 99 | + this(localDc, usedHostsPerRemoteDc, false); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Creates a new DCAwareRoundRobin policy given the name of the local | |
| 104 | + * datacenter and that uses the provided number of host per remote | |
| 105 | + * datacenter as failover for the local hosts. | |
| 106 | + * <p> | |
| 107 | + * This constructor is equivalent to {@link DCAwareRoundRobinPolicy(String, int)} | |
| 108 | + * but allows to override the policy of never using remote data-center | |
| 109 | + * nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is | |
| 110 | + * however inadvisable to do so in almost all cases, as this would | |
| 111 | + * potentially break consistency guarantees and if you are fine with that, | |
| 112 | + * it's probably better to use a weaker consitency like {@code ONE}, {@code | |
| 113 | + * TWO} or {@code THREE}. As such, this constructor should generally | |
| 114 | + * be avoided in favor of {@link DCAwareRoundRobinPolicy(String, int)}. | |
| 115 | + * Use it only if you know and understand what you do. | |
| 116 | + * | |
| 117 | + * @param localDc the name of the local datacenter (as known by | |
| 118 | + * Cassandra). | |
| 119 | + * @param usedHostsPerRemoteDc the number of host per remote | |
| 120 | + * datacenter that policies created by the returned factory should | |
| 121 | + * consider. Created policies {@code distance} method will return a | |
| 122 | + * {@code HostDistance.REMOTE} distance for only {@code | |
| 123 | + * usedHostsPerRemoteDc} hosts per remote datacenter. Other hosts | |
| 124 | + * of the remote datacenters will be ignored (and thus no | |
| 125 | + * connections to them will be maintained). | |
| 126 | + * @param allowRemoteDCsForLocalConsistencyLevel whether or not the | |
| 127 | + * policy may return remote host when building query plan for query | |
| 128 | + * having consitency {@code LOCAL_ONE} and {@code LOCAL_QUORUM}. | |
| 129 | + */ | |
| 130 | + public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc, boolean allowRemoteDCsForLocalConsistencyLevel) { | |
| 89 | 131 | this.localDc = localDc; |
| 90 | 132 | this.usedHostsPerRemoteDc = usedHostsPerRemoteDc; |
| 133 | + this.dontHopForLocalCL = !allowRemoteDCsForLocalConsistencyLevel; | |
| 91 | 134 | } |
| 92 | 135 | |
| 93 | 136 | @Override |
| @@ -158,7 +201,7 @@ | ||
| 158 | 201 | * try first for querying, which one to use as failover, etc... |
| 159 | 202 | */ |
| 160 | 203 | @Override |
| 161 | - public Iterator<Host> newQueryPlan(Query query) { | |
| 204 | + public Iterator<Host> newQueryPlan(final Query query) { | |
| 162 | 205 | |
| 163 | 206 | CopyOnWriteArrayList<Host> localLiveHosts = perDcLiveHosts.get(localDc); |
| 164 | 207 | final List<Host> hosts = localLiveHosts == null ? Collections.<Host>emptyList() : cloneList(localLiveHosts); |
| @@ -192,6 +235,9 @@ | ||
| 192 | 235 | return currentDcHosts.get(c); |
| 193 | 236 | } |
| 194 | 237 | |
| 238 | + if (dontHopForLocalCL && query.getConsistencyLevel().isDCLocal()) | |
| 239 | + return endOfData(); | |
| 240 | + | |
| 195 | 241 | if (remoteDcs == null) { |
| 196 | 242 | Set<String> copy = new HashSet<String>(perDcLiveHosts.keySet()); |
| 197 | 243 | copy.remove(localDc); |
modified driver-core/src/main/java/com/datastax/driver/core/policies/RoundRobinPolicy.java +13 −0
| @@ -23,6 +23,8 @@ | ||
| 23 | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| 24 | 24 | |
| 25 | 25 | import com.google.common.collect.AbstractIterator; |
| 26 | +import org.slf4j.Logger; | |
| 27 | +import org.slf4j.LoggerFactory; | |
| 26 | 28 | |
| 27 | 29 | import com.datastax.driver.core.Cluster; |
| 28 | 30 | import com.datastax.driver.core.Host; |
| @@ -43,9 +45,13 @@ | ||
| 43 | 45 | */ |
| 44 | 46 | public class RoundRobinPolicy implements LoadBalancingPolicy { |
| 45 | 47 | |
| 48 | + private static final Logger logger = LoggerFactory.getLogger(RoundRobinPolicy.class); | |
| 49 | + | |
| 46 | 50 | private final CopyOnWriteArrayList<Host> liveHosts = new CopyOnWriteArrayList<Host>(); |
| 47 | 51 | private final AtomicInteger index = new AtomicInteger(); |
| 48 | 52 | |
| 53 | + private volatile boolean hasLoggedLocalCLUse; | |
| 54 | + | |
| 49 | 55 | /** |
| 50 | 56 | * Creates a load balancing policy that picks host to query in a round robin |
| 51 | 57 | * fashion (on all the hosts of the Cassandra cluster). |
| @@ -87,6 +93,13 @@ | ||
| 87 | 93 | @Override |
| 88 | 94 | public Iterator<Host> newQueryPlan(Query query) { |
| 89 | 95 | |
| 96 | + if (!hasLoggedLocalCLUse && query.getConsistencyLevel().isDCLocal()) { | |
| 97 | + hasLoggedLocalCLUse = true; | |
| 98 | + logger.warn("Detected request at Consistency Level {} but the non-DC aware RoundRobinPolicy is in use. " | |
| 99 | + + "It is strongly advised to use DCAwareRoundRobinPolicy if you have multiple DCs/use DC-aware consistency levels " | |
| 100 | + + "(note: this message will only be logged once)", query.getConsistencyLevel()); | |
| 101 | + } | |
| 102 | + | |
| 90 | 103 | // We clone liveHosts because we want a version of the list that |
| 91 | 104 | // cannot change concurrently of the query plan iterator (this |
| 92 | 105 | // would be racy). We use clone() as it don't involve a copy of the |