/** * {@inheritDoc} */ @Override public synchronized Session connect() throws NoHostAvailableException { if (isDisconnected()) { LOG.info("Connected to cluster: {}", cluster.getClusterName()); for (Host host : getAllHosts()) LOG.info("Datacenter: {}; Host: {}; Rack: {}", host.getDatacenter(), host.getAddress(), host.getRack()); LOG.info("Connect to cluster using keyspace %s", keyspace); session = cluster.connect(keyspace); } else { LOG.warn("{} - Already connected to cluster: {}", getExecutorName(), cluster.getClusterName()); } if (session.isClosed()) { LOG.warn("Session has been closed - create new one!"); this.session = cluster.newSession(); } return session; }
@Override public Cluster get() { String host = configuration.getHostAddress(); int port = configuration.getPort(); LOGGER.info("Connecting to cassandra cluster with host:{}, port:{}", host, port); Cluster cluster = Cluster.builder() .addContactPoint(host) .withPort(port) .build(); Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: {}", metadata.getClusterName()); metadata.getAllHosts().forEach(h -> { LOGGER.info("Datacenter:{}, host:{}, rack: {}", h.getDatacenter(), h.getAddress(), h.getRack()); }); return cluster; } }
discoveredHost.getDatacenter(), discoveredHost.getAddress(), discoveredHost.getRack());
private String dc(Host host) { String dc = host.getDatacenter(); return dc == null ? localDc : dc; }
@Override public boolean apply(Host host) { String hdc = host.getDatacenter(); return (hdc == null) ? includeNullDC : _dcs.contains(hdc); } };
private Map<String, Set<String>> getRacksInDcs(Iterable<Host> hosts) { Map<String, Set<String>> result = new HashMap<String, Set<String>>(); for (Host host : hosts) { Set<String> racks = result.get(host.getDatacenter()); if (racks == null) { racks = new HashSet<String>(); result.put(host.getDatacenter(), racks); } racks.add(host.getRack()); } return result; }
public HostAssert isInDatacenter(String datacenter) { assertThat(actual.getDatacenter()).isEqualTo(datacenter); return this; }
String dc = host.getDatacenter(); if (dcHostCount.get(dc) == null) { dcHostCount.put(dc, 0); for (int j = 0; j < ring.size() && !allDone(allDcReplicas, dcHostCount); j++) { Host h = tokenToPrimary.get(getTokenWrapping(i + j, ring)); String dc = h.getDatacenter(); if (dc == null || !allDcReplicas.containsKey(dc)) continue;
@Test(groups = "unit") public void should_ignore_DCs_in_black_list() { when(host1.getDatacenter()).thenReturn("dc1"); when(host2.getDatacenter()).thenReturn("dc2"); when(host3.getDatacenter()).thenReturn(null); HostFilterPolicy policy = HostFilterPolicy.fromDCBlackList(wrappedPolicy, Lists.newArrayList("dc2")); assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); assertThat(policy.distance(host2)).isSameAs(HostDistance.IGNORED); assertThat(policy.distance(host3)).isSameAs(HostDistance.LOCAL); }
@Test(groups = "unit") public void should_ignore_DCs_not_in_white_list_and_not_null() { when(host1.getDatacenter()).thenReturn("dc1"); when(host2.getDatacenter()).thenReturn("dc2"); when(host3.getDatacenter()).thenReturn(null); HostFilterPolicy policy = HostFilterPolicy.fromDCWhiteList(wrappedPolicy, Lists.newArrayList("dc1")); assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); assertThat(policy.distance(host2)).isSameAs(HostDistance.IGNORED); assertThat(policy.distance(host3)).isSameAs(HostDistance.LOCAL); } }
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); }
private Map<String, Set<String>> getRacksInDcs(Iterable<Host> hosts) { Map<String, Set<String>> result = new HashMap<String, Set<String>>(); for (Host host : hosts) { Set<String> racks = result.get(host.getDatacenter()); if (racks == null) { racks = new HashSet<String>(); result.put(host.getDatacenter(), racks); } racks.add(host.getRack()); } return result; }
assertThat(initHostsCaptor.getValue()).containsExactly(host1); assertThat(policy.localDc).isEqualTo(host1.getDatacenter());
assertThat(initHostsCaptor.getValue()).containsOnly(host1); assertThat(policy.localDc).isEqualTo(host1.getDatacenter());
private RingMember(Host host) { Assert.notNull(host, "Host must not be null"); this.hostName = host.getAddress().getHostName(); this.address = host.getAddress().getHostAddress(); this.dc = host.getDatacenter(); this.rack = host.getRack(); }
for (Host host : metadata.getAllHosts()) { LOGGER.info("Datatacenter: {}; Host: {}; Rack: {}\n", host.getDatacenter(), host.getAddress(), host.getRack());
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); }
@Test(groups = "unit") public void should_ignore_DCs_not_in_white_list_and_not_null() { when(host1.getDatacenter()).thenReturn("dc1"); when(host2.getDatacenter()).thenReturn("dc2"); when(host3.getDatacenter()).thenReturn(null); HostFilterPolicy policy = HostFilterPolicy.fromDCWhiteList(wrappedPolicy, Lists.newArrayList("dc1")); assertThat(policy.distance(host1)).isSameAs(HostDistance.LOCAL); assertThat(policy.distance(host2)).isSameAs(HostDistance.IGNORED); assertThat(policy.distance(host3)).isSameAs(HostDistance.LOCAL); } }
private static void updateLocationInfo(Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) { if (Objects.equal(host.getDatacenter(), datacenter) && Objects.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); }
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); }