|
| 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 | +} |
0 commit comments