@Test public void testInit() { DataCenterAwarePolicy policy = DataCenterAwarePolicy.builder().withChildPolicy(myChildPolicy).withLocalDc(myLocalDc).build(); policy.init(myClusterMock, myHostList); verify(myChildPolicy, times(1)).init(myClusterMock, myHostList); }
@Override public void init(Cluster cluster, Collection<Host> hosts) { LOG.info("Using provided data-center name '{}' for DataCenterAwareLoadBalancingPolicy", myLocalDc); myClusterMetadata = cluster.getMetadata(); myProtocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); myCodecRegistry = cluster.getConfiguration().getCodecRegistry(); ArrayList<String> notInLocalDC = new ArrayList<>(); for (Host host : hosts) { String dc = getDc(host); if (!dc.equals(myLocalDc)) { notInLocalDC.add(String.format("%s (%s)", host.toString(), dc)); } CopyOnWriteArrayList<Host> hostList = myPerDcLiveHosts.get(dc); if (hostList == null) { myPerDcLiveHosts.put(dc, new CopyOnWriteArrayList<>(Collections.singletonList(host))); } else { hostList.addIfAbsent(host); } } if (!notInLocalDC.isEmpty()) { String nonLocalHosts = Joiner.on(",").join(notInLocalDC); LOG.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", myLocalDc, nonLocalHosts); } myIndex.set(new SecureRandom().nextInt(Math.max(hosts.size(), 1))); myChildPolicy.init(cluster, hosts); }
DataCenterAwarePolicy implements ChainableLoadBalancingPolicy { @Override public void init(Cluster cluster, Collection<Host> hosts) { LOG.info("Using provided data-center name '{}' for DataCenterAwareLoadBalancingPolicy", myLocalDc); myClusterMetadata = cluster.getMetadata(); myProtocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); myCodecRegistry = cluster.getConfiguration().getCodecRegistry(); ArrayList<String> notInLocalDC = new ArrayList<>(); for (Host host : hosts) { String dc = getDc(host); if (!dc.equals(myLocalDc)) { notInLocalDC.add(String.format("%s (%s)", host.toString(), dc)); } CopyOnWriteArrayList<Host> hostList = myPerDcLiveHosts.get(dc); if (hostList == null) { myPerDcLiveHosts.put(dc, new CopyOnWriteArrayList<>(Collections.singletonList(host))); } else { hostList.addIfAbsent(host); } } if (!notInLocalDC.isEmpty()) { String nonLocalHosts = Joiner.on(",").join(notInLocalDC); LOG.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", myLocalDc, nonLocalHosts); } myIndex.set(new SecureRandom().nextInt(Math.max(hosts.size(), 1))); myChildPolicy.init(cluster, hosts); } }
DataCenterAwarePolicy implements ChainableLoadBalancingPolicy { @Override public void init(Cluster cluster, Collection<Host> hosts) { LOG.info("Using provided data-center name '{}' for DataCenterAwareLoadBalancingPolicy", myLocalDc); myClusterMetadata = cluster.getMetadata(); myProtocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); myCodecRegistry = cluster.getConfiguration().getCodecRegistry(); ArrayList<String> notInLocalDC = new ArrayList<>(); for (Host host : hosts) { String dc = getDc(host); if (!dc.equals(myLocalDc)) { notInLocalDC.add(String.format("%s (%s)", host.toString(), dc)); } CopyOnWriteArrayList<Host> hostList = myPerDcLiveHosts.get(dc); if (hostList == null) { myPerDcLiveHosts.put(dc, new CopyOnWriteArrayList<>(Collections.singletonList(host))); } else { hostList.addIfAbsent(host); } } if (!notInLocalDC.isEmpty()) { String nonLocalHosts = Joiner.on(",").join(notInLocalDC); LOG.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", myLocalDc, nonLocalHosts); } myIndex.set(new SecureRandom().nextInt(Math.max(hosts.size(), 1))); myChildPolicy.init(cluster, hosts); } DataCenterAwarePolicy(String localDc, LoadBalancingPolicy childPolicy); }
DataCenterAwarePolicy implements ChainableLoadBalancingPolicy { @Override public void init(Cluster cluster, Collection<Host> hosts) { LOG.info("Using provided data-center name '{}' for DataCenterAwareLoadBalancingPolicy", myLocalDc); myClusterMetadata = cluster.getMetadata(); myProtocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); myCodecRegistry = cluster.getConfiguration().getCodecRegistry(); ArrayList<String> notInLocalDC = new ArrayList<>(); for (Host host : hosts) { String dc = getDc(host); if (!dc.equals(myLocalDc)) { notInLocalDC.add(String.format("%s (%s)", host.toString(), dc)); } CopyOnWriteArrayList<Host> hostList = myPerDcLiveHosts.get(dc); if (hostList == null) { myPerDcLiveHosts.put(dc, new CopyOnWriteArrayList<>(Collections.singletonList(host))); } else { hostList.addIfAbsent(host); } } if (!notInLocalDC.isEmpty()) { String nonLocalHosts = Joiner.on(",").join(notInLocalDC); LOG.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", myLocalDc, nonLocalHosts); } myIndex.set(new SecureRandom().nextInt(Math.max(hosts.size(), 1))); myChildPolicy.init(cluster, hosts); } DataCenterAwarePolicy(String localDc, LoadBalancingPolicy childPolicy); static Builder builder(); @Override LoadBalancingPolicy getChildPolicy(); @Override void init(Cluster cluster, Collection<Host> hosts); @Override HostDistance distance(Host host); HostDistance distance(Host host, String dataCenter); @Override Iterator<Host> newQueryPlan(final String loggedKeyspace, final Statement statement); @Override void onUp(Host host); @Override void onDown(Host host); @Override void onAdd(Host host); @Override void onRemove(Host host); @Override void close(); }
DataCenterAwarePolicy implements ChainableLoadBalancingPolicy { @Override public void init(Cluster cluster, Collection<Host> hosts) { LOG.info("Using provided data-center name '{}' for DataCenterAwareLoadBalancingPolicy", myLocalDc); myClusterMetadata = cluster.getMetadata(); myProtocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); myCodecRegistry = cluster.getConfiguration().getCodecRegistry(); ArrayList<String> notInLocalDC = new ArrayList<>(); for (Host host : hosts) { String dc = getDc(host); if (!dc.equals(myLocalDc)) { notInLocalDC.add(String.format("%s (%s)", host.toString(), dc)); } CopyOnWriteArrayList<Host> hostList = myPerDcLiveHosts.get(dc); if (hostList == null) { myPerDcLiveHosts.put(dc, new CopyOnWriteArrayList<>(Collections.singletonList(host))); } else { hostList.addIfAbsent(host); } } if (!notInLocalDC.isEmpty()) { String nonLocalHosts = Joiner.on(",").join(notInLocalDC); LOG.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", myLocalDc, nonLocalHosts); } myIndex.set(new SecureRandom().nextInt(Math.max(hosts.size(), 1))); myChildPolicy.init(cluster, hosts); } DataCenterAwarePolicy(String localDc, LoadBalancingPolicy childPolicy); static Builder builder(); @Override LoadBalancingPolicy getChildPolicy(); @Override void init(Cluster cluster, Collection<Host> hosts); @Override HostDistance distance(Host host); HostDistance distance(Host host, String dataCenter); @Override Iterator<Host> newQueryPlan(final String loggedKeyspace, final Statement statement); @Override void onUp(Host host); @Override void onDown(Host host); @Override void onAdd(Host host); @Override void onRemove(Host host); @Override void close();  }