@Override public HostDistance distance(Host host) { return m_writePolicy.distance(host); }
@Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { if (statement instanceof BatchStatement) { return m_writePolicy.newQueryPlan(loggedKeyspace, statement); } else { return m_queryPolicy.newQueryPlan(loggedKeyspace, statement); } }
@Test(groups = "unit") public void should_not_delegate_to_wrapped_policy_when_predicate_is_false() { Predicate<Host> predicate = Predicates.alwaysFalse(); HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); policy.onAdd(host1); verify(wrappedPolicy, never()).onAdd(host1); policy.onDown(host1); verify(wrappedPolicy, never()).onDown(host1); policy.onUp(host1); verify(wrappedPolicy, never()).onUp(host1); policy.onRemove(host1); verify(wrappedPolicy, never()).onRemove(host1); assertThat(policy.distance(host1)).isSameAs(HostDistance.IGNORED); }
loadBalancingPolicy().init(Cluster.this, contactPointHosts); for (Host.StateListener listener : listeners) listener.onRegister(Cluster.this); for (Host host : removedContactPointHosts) { loadBalancingPolicy().onRemove(host); for (Host.StateListener listener : listeners) listener.onRemove(host); loadBalancingPolicy().onDown(host); for (Host.StateListener listener : listeners) listener.onDown(host); startPeriodicReconnectionAttempt(host, true); if (!contactPointHosts.contains(host)) loadBalancingPolicy().onAdd(host);
private static void updateLocationInfo( Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) { if (MoreObjects.equal(host.getDatacenter(), datacenter) && MoreObjects.equal(host.getRack(), rack)) return; // If the dc/rack information changes for an existing node, we need to update the load balancing // policy. // For that, we remove and re-add the node against the policy. Not the most elegant, and assumes // that the policy will update correctly, but in practice this should work. if (!isInitialConnection) cluster.loadBalancingPolicy().onDown(host); host.setLocationInfo(datacenter, rack); if (!isInitialConnection) cluster.loadBalancingPolicy().onAdd(host); }
@Override protected Host computeNext() { while (replicasIterator.hasNext()) { Host host = replicasIterator.next(); if (host.isUp() && childPolicy.distance(host) == HostDistance.LOCAL) return host; } if (childIterator == null) childIterator = childPolicy.newQueryPlan(loggedKeyspace, statement); while (childIterator.hasNext()) { Host host = childIterator.next(); // Skip it if it was already a local replica if (!replicas.contains(host) || childPolicy.distance(host) != HostDistance.LOCAL) return host; } return endOfData(); } };
@Test(groups = "unit") public void should_delegate_to_wrapped_policy_when_predicate_is_true() { Predicate<Host> predicate = Predicates.alwaysTrue(); HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, predicate); policy.onAdd(host1); verify(wrappedPolicy).onAdd(host1); policy.onDown(host1); verify(wrappedPolicy).onDown(host1); policy.onUp(host1); verify(wrappedPolicy).onUp(host1); policy.onRemove(host1); verify(wrappedPolicy).onRemove(host1); assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); policy.close(); verify(wrappedPolicy).close(); }
@Override public void onUp(Host host) { childPolicy.onUp(host); }
private static void updateLocationInfo(Host host, String datacenter, String rack, Cluster.Manager cluster) { if (Objects.equal(host.getDatacenter(), datacenter) && Objects.equal(host.getRack(), rack)) return; // If the dc/rack information changes, we need to update the load balancing policy. // For that, we remove and re-add the node against the policy. Not the most elegant, and assumes // that the policy will update correctly, but in practice this should work. cluster.loadBalancingPolicy().onDown(host); host.setLocationInfo(datacenter, rack); cluster.loadBalancingPolicy().onAdd(host); }
@BeforeMethod(groups = "unit") public void initMocks() { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); Metadata metadata = mock(Metadata.class); childPolicy = mock(LoadBalancingPolicy.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getReplicas(Metadata.quote("keyspace"), routingKey)) .thenReturn(Sets.newLinkedHashSet(host1, host2)); when(childPolicy.newQueryPlan("keyspace", statement)) .thenReturn(Sets.newLinkedHashSet(host4, host3, host2, host1).iterator()); when(childPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); when(host1.isUp()).thenReturn(true); when(host2.isUp()).thenReturn(true); when(host3.isUp()).thenReturn(true); when(host4.isUp()).thenReturn(true); }
@Override public void onRemove(Host host) { childPolicy.onRemove(host); }
@Override public void close() { childPolicy.close(); } }