kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | eff7ed791a6cc9cb3425121ba4687c5ba4458ca57f2b25313ff818f18faf3137 |
|---|---|
| Author | Dominique Villard <dominique.villard@orange.com> · 2015-09-03 14:39 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-11-05 19:00 +01:00 |
| Parents | 8dc429c29f60 |
4 files changed
modified changelog/README.md +1 −0
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | - [bug] JAVA-966: Count uninitialized connections in conviction policy. |
| 14 | 14 | - [improvement] JAVA-917: Document SSL configuration. |
| 15 | 15 | - [improvement] JAVA-652: Add DCAwareRoundRobinPolicy builder. |
| 16 | +- [improvement] JAVA-808: Add generic filtering policy that can be used to exclude specific DCs. | |
| 16 | 17 | |
| 17 | 18 | |
| 18 | 19 | ### 2.0.11 |
added driver-core/src/main/java/com/datastax/driver/core/policies/HostFilterPolicy.java +184 −0
| @@ -0,0 +1,184 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.Collection; | |
| 21 | +import java.util.Iterator; | |
| 22 | +import java.util.List; | |
| 23 | + | |
| 24 | +import com.google.common.base.Predicate; | |
| 25 | +import com.google.common.base.Predicates; | |
| 26 | +import com.google.common.collect.ImmutableSet; | |
| 27 | + | |
| 28 | +import com.datastax.driver.core.Cluster; | |
| 29 | +import com.datastax.driver.core.Host; | |
| 30 | +import com.datastax.driver.core.HostDistance; | |
| 31 | +import com.datastax.driver.core.Statement; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * A load balancing policy wrapper that ensures that only hosts matching the predicate | |
| 35 | + * will ever be returned. | |
| 36 | + * <p> | |
| 37 | + * This policy wraps another load balancing policy and will delegate the choice | |
| 38 | + * of hosts to the wrapped policy with the exception that only hosts matching | |
| 39 | + * the predicate provided when constructing this policy will ever be | |
| 40 | + * returned. Any host not matching the predicate will be considered {@code IGNORED} | |
| 41 | + * and thus will not be connected to. | |
| 42 | + */ | |
| 43 | +public class HostFilterPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 44 | + private final LoadBalancingPolicy childPolicy; | |
| 45 | + private final Predicate<Host> predicate; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Create a new policy that wraps the provided child policy but only "allows" hosts | |
| 49 | + * matching the predicate. | |
| 50 | + * | |
| 51 | + * @param childPolicy the wrapped policy. | |
| 52 | + * @param predicate the host predicate. Only hosts matching this predicate may get connected | |
| 53 | + * to (whether they will get connected to or not depends on the child policy). | |
| 54 | + */ | |
| 55 | + public HostFilterPolicy(LoadBalancingPolicy childPolicy, Predicate<Host> predicate) { | |
| 56 | + this.childPolicy = childPolicy; | |
| 57 | + this.predicate = predicate; | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public LoadBalancingPolicy getChildPolicy() { | |
| 62 | + return childPolicy; | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * {@inheritDoc} | |
| 67 | + * | |
| 68 | + * @throws IllegalArgumentException if none of the host in {@code hosts} | |
| 69 | + * (which will correspond to the contact points) matches the predicate. | |
| 70 | + */ | |
| 71 | + @Override | |
| 72 | + public void init(Cluster cluster, Collection<Host> hosts) { | |
| 73 | + List<Host> whiteHosts = new ArrayList<Host>(hosts.size()); | |
| 74 | + for (Host host : hosts) | |
| 75 | + if (predicate.apply(host)) | |
| 76 | + whiteHosts.add(host); | |
| 77 | + | |
| 78 | + if (whiteHosts.isEmpty()) | |
| 79 | + throw new IllegalArgumentException(String.format("Cannot use HostFilterPolicy where the filter allows none of the contacts points (%s)", hosts)); | |
| 80 | + | |
| 81 | + childPolicy.init(cluster, whiteHosts); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * {@inheritDoc} | |
| 86 | + * | |
| 87 | + * @return {@link HostDistance#IGNORED} if {@code host} is not matching the predicate, the HostDistance | |
| 88 | + * as returned by the wrapped policy otherwise. | |
| 89 | + */ | |
| 90 | + @Override | |
| 91 | + public HostDistance distance(Host host) { | |
| 92 | + return predicate.apply(host) | |
| 93 | + ? childPolicy.distance(host) | |
| 94 | + : HostDistance.IGNORED; | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * {@inheritDoc} | |
| 99 | + * <p> | |
| 100 | + * It is guaranteed that only hosts matching the predicate will be returned. | |
| 101 | + */ | |
| 102 | + @Override | |
| 103 | + public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 104 | + // Just delegate to the child policy, since we filter the hosts not white | |
| 105 | + // listed upfront, the child policy will never see a host that is not white | |
| 106 | + // listed and thus can't return one. | |
| 107 | + return childPolicy.newQueryPlan(loggedKeyspace, statement); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Override | |
| 111 | + public void onUp(Host host) { | |
| 112 | + if (predicate.apply(host)) | |
| 113 | + childPolicy.onUp(host); | |
| 114 | + } | |
| 115 | + | |
| 116 | + @SuppressWarnings("deprecation") | |
| 117 | + @Override | |
| 118 | + public void onSuspected(Host host) { | |
| 119 | + if (predicate.apply(host)) | |
| 120 | + childPolicy.onSuspected(host); | |
| 121 | + } | |
| 122 | + | |
| 123 | + @Override | |
| 124 | + public void onDown(Host host) { | |
| 125 | + if (predicate.apply(host)) | |
| 126 | + childPolicy.onDown(host); | |
| 127 | + } | |
| 128 | + | |
| 129 | + @Override | |
| 130 | + public void onAdd(Host host) { | |
| 131 | + if (predicate.apply(host)) | |
| 132 | + childPolicy.onAdd(host); | |
| 133 | + } | |
| 134 | + | |
| 135 | + @Override | |
| 136 | + public void onRemove(Host host) { | |
| 137 | + if (predicate.apply(host)) | |
| 138 | + childPolicy.onRemove(host); | |
| 139 | + } | |
| 140 | + | |
| 141 | + @Override | |
| 142 | + public void close() { | |
| 143 | + if (childPolicy instanceof CloseableLoadBalancingPolicy) | |
| 144 | + ((CloseableLoadBalancingPolicy)childPolicy).close(); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Create a new policy that wraps the provided child policy but only "allows" hosts | |
| 149 | + * whose DC belongs to the provided list. | |
| 150 | + * | |
| 151 | + * @param childPolicy the wrapped policy. | |
| 152 | + * @param dcs the DCs. | |
| 153 | + * | |
| 154 | + * @return the policy. | |
| 155 | + */ | |
| 156 | + public static HostFilterPolicy fromDCWhiteList(LoadBalancingPolicy childPolicy, Iterable<String> dcs) { | |
| 157 | + return new HostFilterPolicy(childPolicy, hostDCPredicate(dcs, true)); | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Create a new policy that wraps the provided child policy but only "forbids" hosts | |
| 162 | + * whose DC belongs to the provided list. | |
| 163 | + * | |
| 164 | + * @param childPolicy the wrapped policy. | |
| 165 | + * @param dcs the DCs. | |
| 166 | + * | |
| 167 | + * @return the policy. | |
| 168 | + */ | |
| 169 | + public static HostFilterPolicy fromDCBlackList(LoadBalancingPolicy childPolicy, Iterable<String> dcs) { | |
| 170 | + return new HostFilterPolicy(childPolicy, Predicates.not(hostDCPredicate(dcs, false))); | |
| 171 | + } | |
| 172 | + | |
| 173 | + private static Predicate<Host> hostDCPredicate(Iterable<String> dcs, final boolean includeNullDC) { | |
| 174 | + final ImmutableSet<String> _dcs = ImmutableSet.copyOf(dcs); | |
| 175 | + return new Predicate<Host>() { | |
| 176 | + @Override | |
| 177 | + public boolean apply(Host host) { | |
| 178 | + String hdc = host.getDatacenter(); | |
| 179 | + return (hdc == null) ? includeNullDC : _dcs.contains(hdc); | |
| 180 | + } | |
| 181 | + }; | |
| 182 | + } | |
| 183 | + | |
| 184 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/policies/WhiteListPolicy.java +14 −106
| @@ -16,18 +16,12 @@ | ||
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | 18 | import java.net.InetSocketAddress; |
| 19 | -import java.util.ArrayList; | |
| 20 | 19 | import java.util.Collection; |
| 21 | -import java.util.Iterator; | |
| 22 | -import java.util.List; | |
| 23 | -import java.util.Set; | |
| 24 | 20 | |
| 21 | +import com.google.common.base.Predicate; | |
| 25 | 22 | import com.google.common.collect.ImmutableSet; |
| 26 | 23 | |
| 27 | -import com.datastax.driver.core.Cluster; | |
| 28 | 24 | import com.datastax.driver.core.Host; |
| 29 | -import com.datastax.driver.core.HostDistance; | |
| 30 | -import com.datastax.driver.core.Statement; | |
| 31 | 25 | |
| 32 | 26 | /** |
| 33 | 27 | * A load balancing policy wrapper that ensure that only hosts from a provided |
| @@ -46,13 +40,13 @@ | ||
| 46 | 40 | * If all you want to do is limiting connections to hosts of the local |
| 47 | 41 | * data-center then you should use DCAwareRoundRobinPolicy and *not* this policy |
| 48 | 42 | * in particular. |
| 43 | + * | |
| 44 | + * @see HostFilterPolicy | |
| 49 | 45 | */ |
| 50 | -public class WhiteListPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 51 | - private final LoadBalancingPolicy childPolicy; | |
| 52 | - private final Set<InetSocketAddress> whiteList; | |
| 46 | +public class WhiteListPolicy extends HostFilterPolicy { | |
| 53 | 47 | |
| 54 | 48 | /** |
| 55 | - * Create a new policy that wraps the provided child policy but only "allow" hosts | |
| 49 | + * Creates a new policy that wraps the provided child policy but only "allows" hosts | |
| 56 | 50 | * from the provided while list. |
| 57 | 51 | * |
| 58 | 52 | * @param childPolicy the wrapped policy. |
| @@ -60,103 +54,17 @@ | ||
| 60 | 54 | * to (whether they will get connected to or not depends on the child policy). |
| 61 | 55 | */ |
| 62 | 56 | public WhiteListPolicy(LoadBalancingPolicy childPolicy, Collection<InetSocketAddress> whiteList) { |
| 63 | - this.childPolicy = childPolicy; | |
| 64 | - this.whiteList = ImmutableSet.copyOf(whiteList); | |
| 57 | + super(childPolicy, buildPredicate(whiteList)); | |
| 65 | 58 | } |
| 66 | 59 | |
| 67 | - @Override | |
| 68 | - public LoadBalancingPolicy getChildPolicy() { | |
| 69 | - return childPolicy; | |
| 60 | + private static Predicate<Host> buildPredicate(Collection<InetSocketAddress> whiteList) { | |
| 61 | + final ImmutableSet<InetSocketAddress> hosts = ImmutableSet.copyOf(whiteList); | |
| 62 | + return new Predicate<Host>() { | |
| 63 | + @Override | |
| 64 | + public boolean apply(Host host) { | |
| 65 | + return hosts.contains(host.getSocketAddress()); | |
| 66 | + } | |
| 67 | + }; | |
| 70 | 68 | } |
| 71 | 69 | |
| 72 | - /** | |
| 73 | - * Initialize this load balancing policy. | |
| 74 | - * | |
| 75 | - * @param cluster the {@code Cluster} instance for which the policy is created. | |
| 76 | - * @param hosts the initial hosts to use. | |
| 77 | - * | |
| 78 | - * @throws IllegalArgumentException if none of the host in {@code hosts} | |
| 79 | - * (which will correspond to the contact points) are part of the white list. | |
| 80 | - */ | |
| 81 | - @Override | |
| 82 | - public void init(Cluster cluster, Collection<Host> hosts) { | |
| 83 | - List<Host> whiteHosts = new ArrayList<Host>(hosts.size()); | |
| 84 | - for (Host host : hosts) | |
| 85 | - if (whiteList.contains(host.getSocketAddress())) | |
| 86 | - whiteHosts.add(host); | |
| 87 | - | |
| 88 | - if (whiteHosts.isEmpty()) | |
| 89 | - throw new IllegalArgumentException(String.format("Cannot use WhiteListPolicy where the white list (%s) contains none of the contacts points (%s)", whiteList, hosts)); | |
| 90 | - | |
| 91 | - childPolicy.init(cluster, whiteHosts); | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** | |
| 95 | - * Return the HostDistance for the provided host. | |
| 96 | - * | |
| 97 | - * @param host the host of which to return the distance of. | |
| 98 | - * @return {@link HostDistance#IGNORED} if {@code host} is not part of the white list, the HostDistance | |
| 99 | - * as returned by the wrapped policy otherwise. | |
| 100 | - */ | |
| 101 | - @Override | |
| 102 | - public HostDistance distance(Host host) { | |
| 103 | - return whiteList.contains(host.getSocketAddress()) | |
| 104 | - ? childPolicy.distance(host) | |
| 105 | - : HostDistance.IGNORED; | |
| 106 | - } | |
| 107 | - | |
| 108 | - /** | |
| 109 | - * Returns the hosts to use for a new query. | |
| 110 | - * <p> | |
| 111 | - * It is guaranteed that only hosts from the white list will be returned. | |
| 112 | - * | |
| 113 | - * @param loggedKeyspace the currently logged keyspace (the one set through either | |
| 114 | - * {@link Cluster#connect(String)} or by manually doing a {@code USE} query) for | |
| 115 | - * the session on which this plan need to be built. This can be {@code null} if | |
| 116 | - * the corresponding session has no keyspace logged in. | |
| 117 | - * @param statement the query for which to build a plan. | |
| 118 | - */ | |
| 119 | - @Override | |
| 120 | - public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 121 | - // Just delegate to the child policy, since we filter the hosts not white | |
| 122 | - // listed upfront, the child policy will never see a host that is not white | |
| 123 | - // listed and thus can't return one. | |
| 124 | - return childPolicy.newQueryPlan(loggedKeyspace, statement); | |
| 125 | - } | |
| 126 | - | |
| 127 | - @Override | |
| 128 | - public void onUp(Host host) { | |
| 129 | - if (whiteList.contains(host.getSocketAddress())) | |
| 130 | - childPolicy.onUp(host); | |
| 131 | - } | |
| 132 | - | |
| 133 | - @Override | |
| 134 | - public void onSuspected(Host host) { | |
| 135 | - if (whiteList.contains(host.getSocketAddress())) | |
| 136 | - childPolicy.onSuspected(host); | |
| 137 | - } | |
| 138 | - | |
| 139 | - @Override | |
| 140 | - public void onDown(Host host) { | |
| 141 | - if (whiteList.contains(host.getSocketAddress())) | |
| 142 | - childPolicy.onDown(host); | |
| 143 | - } | |
| 144 | - | |
| 145 | - @Override | |
| 146 | - public void onAdd(Host host) { | |
| 147 | - if (whiteList.contains(host.getSocketAddress())) | |
| 148 | - childPolicy.onAdd(host); | |
| 149 | - } | |
| 150 | - | |
| 151 | - @Override | |
| 152 | - public void onRemove(Host host) { | |
| 153 | - if (whiteList.contains(host.getSocketAddress())) | |
| 154 | - childPolicy.onRemove(host); | |
| 155 | - } | |
| 156 | - | |
| 157 | - @Override | |
| 158 | - public void close() { | |
| 159 | - if (childPolicy instanceof CloseableLoadBalancingPolicy) | |
| 160 | - ((CloseableLoadBalancingPolicy)childPolicy).close(); | |
| 161 | - } | |
| 162 | 70 | } |
added driver-core/src/test/java/com/datastax/driver/core/policies/HostFilterPolicyTest.java +168 −0
| @@ -0,0 +1,168 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | +import java.util.Collection; | |
| 20 | + | |
| 21 | +import com.google.common.base.Predicate; | |
| 22 | +import com.google.common.base.Predicates; | |
| 23 | +import com.google.common.collect.Iterators; | |
| 24 | +import com.google.common.collect.Lists; | |
| 25 | +import org.mockito.ArgumentCaptor; | |
| 26 | +import org.mockito.Captor; | |
| 27 | +import org.mockito.Mock; | |
| 28 | +import org.mockito.MockitoAnnotations; | |
| 29 | +import org.testng.annotations.BeforeMethod; | |
| 30 | +import org.testng.annotations.Test; | |
| 31 | + | |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 33 | +import static org.mockito.Mockito.*; | |
| 34 | + | |
| 35 | +import com.datastax.driver.core.Cluster; | |
| 36 | +import com.datastax.driver.core.Host; | |
| 37 | +import com.datastax.driver.core.HostDistance; | |
| 38 | +import com.datastax.driver.core.Statement; | |
| 39 | + | |
| 40 | +public class HostFilterPolicyTest { | |
| 41 | + @Mock | |
| 42 | + Cluster cluster; | |
| 43 | + | |
| 44 | + @Mock | |
| 45 | + Host host1, host2, host3; | |
| 46 | + | |
| 47 | + InetSocketAddress address1 = InetSocketAddress.createUnresolved("192.168.1.1", 2345); | |
| 48 | + InetSocketAddress address2 = InetSocketAddress.createUnresolved("192.168.1.2", 9876); | |
| 49 | + InetSocketAddress address3 = InetSocketAddress.createUnresolved("192.168.1.3", 6666); | |
| 50 | + | |
| 51 | + @Mock | |
| 52 | + CloseableLoadBalancingPolicy wrappedPolicy; | |
| 53 | + | |
| 54 | + @Captor | |
| 55 | + ArgumentCaptor<Collection<Host>> hostsCaptor; | |
| 56 | + | |
| 57 | + @BeforeMethod(groups = "unit") | |
| 58 | + public void setup() { | |
| 59 | + MockitoAnnotations.initMocks(this); | |
| 60 | + | |
| 61 | + when(host1.getSocketAddress()).thenReturn(address1); | |
| 62 | + when(host2.getSocketAddress()).thenReturn(address2); | |
| 63 | + when(host3.getSocketAddress()).thenReturn(address3); | |
| 64 | + | |
| 65 | + when(wrappedPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); | |
| 66 | + } | |
| 67 | + | |
| 68 | + @Test(groups = "unit") | |
| 69 | + public void should_delegate_to_wrapped_policy_when_predicate_is_true() { | |
| 70 | + Predicate<Host> predicate = Predicates.alwaysTrue(); | |
| 71 | + HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); | |
| 72 | + | |
| 73 | + policy.onAdd(host1); | |
| 74 | + verify(wrappedPolicy).onAdd(host1); | |
| 75 | + | |
| 76 | + policy.onDown(host1); | |
| 77 | + verify(wrappedPolicy).onDown(host1); | |
| 78 | + | |
| 79 | + policy.onUp(host1); | |
| 80 | + verify(wrappedPolicy).onUp(host1); | |
| 81 | + | |
| 82 | + policy.onRemove(host1); | |
| 83 | + verify(wrappedPolicy).onRemove(host1); | |
| 84 | + | |
| 85 | + assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); | |
| 86 | + | |
| 87 | + policy.close(); | |
| 88 | + verify(wrappedPolicy).close(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + @Test(groups = "unit") | |
| 92 | + public void should_not_delegate_to_wrapped_policy_when_predicate_is_false() { | |
| 93 | + Predicate<Host> predicate = Predicates.alwaysFalse(); | |
| 94 | + HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); | |
| 95 | + | |
| 96 | + policy.onAdd(host1); | |
| 97 | + verify(wrappedPolicy, never()).onAdd(host1); | |
| 98 | + | |
| 99 | + policy.onDown(host1); | |
| 100 | + verify(wrappedPolicy, never()).onDown(host1); | |
| 101 | + | |
| 102 | + policy.onUp(host1); | |
| 103 | + verify(wrappedPolicy, never()).onUp(host1); | |
| 104 | + | |
| 105 | + policy.onRemove(host1); | |
| 106 | + verify(wrappedPolicy, never()).onRemove(host1); | |
| 107 | + | |
| 108 | + assertThat(policy.distance(host1)).isSameAs(HostDistance.IGNORED); | |
| 109 | + } | |
| 110 | + | |
| 111 | + @Test(groups = "unit") | |
| 112 | + public void should_filter_init_hosts_with_predicate() { | |
| 113 | + Predicate<Host> predicate = Predicates.in(Lists.newArrayList(host1, host2)); | |
| 114 | + HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); | |
| 115 | + | |
| 116 | + policy.init(cluster, Lists.newArrayList(host1, host2, host3)); | |
| 117 | + | |
| 118 | + verify(wrappedPolicy).init(eq(cluster), hostsCaptor.capture()); | |
| 119 | + assertThat(hostsCaptor.getValue()).containsOnly(host1, host2); | |
| 120 | + } | |
| 121 | + | |
| 122 | + @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) | |
| 123 | + public void should_throw_if_predicate_filters_out_all_init_hosts() { | |
| 124 | + Predicate<Host> predicate = Predicates.alwaysFalse(); | |
| 125 | + HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); | |
| 126 | + | |
| 127 | + policy.init(cluster, Lists.newArrayList(host1, host2, host3)); | |
| 128 | + } | |
| 129 | + | |
| 130 | + @Test(groups = "unit") | |
| 131 | + public void should_return_query_plan_of_wrapped_policy() { | |
| 132 | + when(wrappedPolicy.newQueryPlan(any(String.class), any(Statement.class))) | |
| 133 | + .thenReturn(Iterators.forArray(host1, host2, host3)); | |
| 134 | + | |
| 135 | + HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, null); | |
| 136 | + | |
| 137 | + assertThat(policy.newQueryPlan("keyspace", mock(Statement.class))) | |
| 138 | + .containsExactly(host1, host2, host3); | |
| 139 | + } | |
| 140 | + | |
| 141 | + @Test(groups = "unit") | |
| 142 | + public void should_ignore_DCs_in_black_list() { | |
| 143 | + when(host1.getDatacenter()).thenReturn("dc1"); | |
| 144 | + when(host2.getDatacenter()).thenReturn("dc2"); | |
| 145 | + when(host3.getDatacenter()).thenReturn(null); | |
| 146 | + | |
| 147 | + HostFilterPolicy policy = HostFilterPolicy.fromDCBlackList(wrappedPolicy, | |
| 148 | + Lists.newArrayList("dc2")); | |
| 149 | + | |
| 150 | + assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); | |
| 151 | + assertThat(policy.distance(host2)).isSameAs(HostDistance.IGNORED); | |
| 152 | + assertThat(policy.distance(host3)).isSameAs(HostDistance.LOCAL); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Test(groups = "unit") | |
| 156 | + public void should_ignore_DCs_not_in_white_list_and_not_null() { | |
| 157 | + when(host1.getDatacenter()).thenReturn("dc1"); | |
| 158 | + when(host2.getDatacenter()).thenReturn("dc2"); | |
| 159 | + when(host3.getDatacenter()).thenReturn(null); | |
| 160 | + | |
| 161 | + HostFilterPolicy policy = HostFilterPolicy.fromDCWhiteList(wrappedPolicy, | |
| 162 | + Lists.newArrayList("dc1")); | |
| 163 | + | |
| 164 | + assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); | |
| 165 | + assertThat(policy.distance(host2)).isSameAs(HostDistance.IGNORED); | |
| 166 | + assertThat(policy.distance(host3)).isSameAs(HostDistance.LOCAL); | |
| 167 | + } | |
| 168 | +} |