Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 2cdd4af

Browse files
Dominique Villardolim7t
authored andcommitted
JAVA-808: Add generic filtering policy that can be used to exclude specific DCs.
1 parent 7e70c44 commit 2cdd4af

4 files changed

Lines changed: 367 additions & 106 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [bug] JAVA-966: Count uninitialized connections in conviction policy.
1414
- [improvement] JAVA-917: Document SSL configuration.
1515
- [improvement] JAVA-652: Add DCAwareRoundRobinPolicy builder.
16+
- [improvement] JAVA-808: Add generic filtering policy that can be used to exclude specific DCs.
1617

1718

1819
### 2.0.11
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

driver-core/src/main/java/com/datastax/driver/core/policies/WhiteListPolicy.java

Lines changed: 14 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@
1616
package com.datastax.driver.core.policies;
1717

1818
import java.net.InetSocketAddress;
19-
import java.util.ArrayList;
2019
import java.util.Collection;
21-
import java.util.Iterator;
22-
import java.util.List;
23-
import java.util.Set;
2420

21+
import com.google.common.base.Predicate;
2522
import com.google.common.collect.ImmutableSet;
2623

27-
import com.datastax.driver.core.Cluster;
2824
import com.datastax.driver.core.Host;
29-
import com.datastax.driver.core.HostDistance;
30-
import com.datastax.driver.core.Statement;
3125

3226
/**
3327
* A load balancing policy wrapper that ensure that only hosts from a provided
@@ -46,117 +40,31 @@
4640
* If all you want to do is limiting connections to hosts of the local
4741
* data-center then you should use DCAwareRoundRobinPolicy and *not* this policy
4842
* in particular.
43+
*
44+
* @see HostFilterPolicy
4945
*/
50-
public class WhiteListPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy {
51-
private final LoadBalancingPolicy childPolicy;
52-
private final Set<InetSocketAddress> whiteList;
46+
public class WhiteListPolicy extends HostFilterPolicy {
5347

5448
/**
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
5650
* from the provided while list.
5751
*
5852
* @param childPolicy the wrapped policy.
5953
* @param whiteList the white listed hosts. Only hosts from this list may get connected
6054
* to (whether they will get connected to or not depends on the child policy).
6155
*/
6256
public WhiteListPolicy(LoadBalancingPolicy childPolicy, Collection<InetSocketAddress> whiteList) {
63-
this.childPolicy = childPolicy;
64-
this.whiteList = ImmutableSet.copyOf(whiteList);
65-
}
66-
67-
@Override
68-
public LoadBalancingPolicy getChildPolicy() {
69-
return childPolicy;
70-
}
71-
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);
57+
super(childPolicy, buildPredicate(whiteList));
13158
}
13259

133-
@Override
134-
public void onSuspected(Host host) {
135-
if (whiteList.contains(host.getSocketAddress()))
136-
childPolicy.onSuspected(host);
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+
};
13768
}
13869

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-
}
16270
}

0 commit comments

Comments
 (0)