public HostAddress toHostAddress(Host host) { return toHostAddress(host.getAddress().getHostAddress()); }
/** * {@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());
@Override public boolean apply(Host host) { return addresses.contains(host.getAddress()); } });
@Override public boolean apply(Host host) { return !host.getAddress().getHostAddress().endsWith("4"); } }));
@Override public void onAdd(Host host) { if (host.getAddress().getHostAddress().equals(address)) { // for a new node, because of this we also listen for add events. addSignal.countDown(); } } };
public void init(Cluster cluster, Collection<Host> hosts) { this.cluster = cluster; try { for (Host h : hosts) if (h.getAddress().equals(InetAddress.getByName(TestUtils.IP_PREFIX + '1'))) this.theHost = h; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public int compare(Host host1, Host host2) { byte[] address1 = host1.getAddress().getAddress(); byte[] address2 = host2.getAddress().getAddress(); return UnsignedBytes.compare( address1[address1.length - 1], address2[address2.length - 1]); } });
@Override public void onFailure(Throwable t) { if (t instanceof BusyPoolException) { logError(host.getSocketAddress(), t); } else { logger.error("Unexpected error while querying " + host.getAddress(), t); logError(host.getSocketAddress(), t); } findNextHostAndQuery(); } });
@Override public Map<String, Object> getMetrics() { Map<String, Object> results = InsertionOrderUtil.newMap(); for (Host host : this.cluster.getMetadata().getAllHosts()) { String address = host.getAddress().getHostAddress(); results.put(address, this.getMetricsByHost(address)); } return results; }
public ClusterAssert usesControlHost(int node) { String expectedAddress = TestUtils.ipOfNode(node); Host controlHost = actual.manager.controlConnection.connectedHost(); assertThat(controlHost.getAddress().getHostAddress()).isEqualTo(expectedAddress); return this; }
@Override public void onDown(Host host) { if (!init) onDownCalledBeforeInit = true; super.onDown(host); if (host.getAddress().toString().contains(ipOfNode(3))) onDownCalled.countDown(); }
/** Coordinator management/count */ protected void addCoordinator(ResultSet rs) { InetAddress coordinator = rs.getExecutionInfo().getQueriedHost().getAddress(); Integer n = coordinators.get(coordinator); coordinators.put(coordinator, n == null ? 1 : n + 1); }
public static Host findHost(Cluster cluster, String address) { // Note: we can't rely on ProtocolOptions.getPort() to build an InetSocketAddress and call // metadata.getHost, // because the port doesn't have the correct value if addContactPointsWithPorts was used to // create the Cluster // (JAVA-860 will solve that) for (Host host : cluster.getMetadata().allHosts()) { if (host.getAddress().getHostAddress().equals(address)) return host; } return null; }
@Test(groups = "short") public void should_throw_proper_unavailable_exception() { simulateError(1, unavailable); try { query(); fail("expected an UnavailableException"); } catch (UnavailableException e) { assertThat(e.getMessage()) .isEqualTo( "Not enough replicas available for query at consistency LOCAL_ONE (1 required but only 0 alive)"); assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_ONE); assertThat(e.getAliveReplicas()).isEqualTo(0); assertThat(e.getRequiredReplicas()).isEqualTo(1); assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); assertThat(e.getHost()).isEqualTo(host1.getAddress()); } }
} catch (RuntimeException e) { if (connection != null) connection.release(); logger.error("Unexpected error while querying " + host.getAddress(), e); logError(host.getSocketAddress(), e); findNextHostAndQuery();
@Test(groups = "short") public void should_throw_proper_read_timeout_exception() { simulateError(1, read_request_timeout); try { query(); fail("expected a ReadTimeoutException"); } catch (ReadTimeoutException e) { assertThat(e.getMessage()) .isEqualTo( "Cassandra timeout during read query at consistency LOCAL_ONE (1 responses were required but only 0 replica responded)"); assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_ONE); assertThat(e.getReceivedAcknowledgements()).isEqualTo(0); assertThat(e.getRequiredAcknowledgements()).isEqualTo(1); assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); assertThat(e.getHost()).isEqualTo(host1.getAddress()); } }
@Test(groups = "short") public void should_throw_proper_write_timeout_exception() { simulateError(1, write_request_timeout); try { query(); fail("expected a WriteTimeoutException"); } catch (WriteTimeoutException e) { assertThat(e.getMessage()) .isEqualTo( "Cassandra timeout during SIMPLE write query at consistency LOCAL_ONE (1 replica were required but only 0 acknowledged the write)"); assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_ONE); assertThat(e.getReceivedAcknowledgements()).isEqualTo(0); assertThat(e.getRequiredAcknowledgements()).isEqualTo(1); assertThat(e.getWriteType()).isEqualTo(WriteType.SIMPLE); assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); assertThat(e.getHost()).isEqualTo(host1.getAddress()); } }
@Test(groups = "short") @CCMConfig(numberOfNodes = 2, clusterProvider = "updatablePolicy") public void refreshTest() throws Throwable { // Ugly Host[] hosts = new Host[2]; for (Host h : cluster().getMetadata().getAllHosts()) { if (h.getAddress().equals(ccm().addressOfNode(1).getAddress())) hosts[0] = h; else hosts[1] = h; } assertTrue( session().getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session().getState().getConnectedHosts()); assertTrue( !session().getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session().getState().getConnectedHosts()); policy.changeTheHost(hosts[1]); assertTrue( !session().getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session().getState().getConnectedHosts()); assertTrue( session().getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session().getState().getConnectedHosts()); }