com.datastax.driver.core.Host Java Examples

The following examples show how to use com.datastax.driver.core.Host. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MutationReplayer.java    From sstable-tools with Apache License 2.0 6 votes vote down vote up
public void sendMutation(Mutation mutation) {
    for (PartitionUpdate partition : mutation.getPartitionUpdates()) {
        Set<Host> replicas = cluster.getMetadata().getReplicas(mutation.getKeyspaceName(),
                partition.partitionKey().getKey());
        // in case theres multiple partitions in this mutation, with topology changes we cant assume can send
        // them in batches so break them up.
        Mutation toSend = new Mutation(mutation.getKeyspaceName(), partition.partitionKey());
        toSend.add(partition);
        for(Host h : replicas) {
            InetAddress target = h.getBroadcastAddress();
            StorageConnection conn = connections.get(target);
            if(conn == null) {
                conn = connections.computeIfAbsent(target, host -> {
                    StorageConnection c = new StorageConnection(host);
                    c.connect();
                    return c;
                });
            }
            try {
                conn.enqueue(toSend.createMessage(), idGen.incrementAndGet());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example #2
Source File: CassandraHealthCheck.java    From emodb with Apache License 2.0 6 votes vote down vote up
private Result pingAll() {
    try {
        StringBuilder message = new StringBuilder();

        OperationResult<CqlStatementResult> astyanaxResult = pingAstyanax();
        message.append("Astyanax: ").append(astyanaxResult.getHost()).append(" ")
                .append(astyanaxResult.getLatency(TimeUnit.MICROSECONDS)).append("us");

        if (astyanaxResult.getAttemptsCount() != 1) {
            message.append(", ").append(astyanaxResult.getAttemptsCount()).append(" attempts");
        }

        Stopwatch cqlTimer = Stopwatch.createStarted();
        ResultSet cqlResult = pingCql();
        long queryDurationMicros = cqlTimer.elapsed(TimeUnit.MICROSECONDS);

        Host host = cqlResult.getExecutionInfo().getQueriedHost();
        message.append(" | CQL: ").append(host).append(" ").append(queryDurationMicros).append("us");

        return Result.healthy(message.toString());
    } catch (Throwable t) {
        return Result.unhealthy(t);
    }
}
 
Example #3
Source File: CassandraMetricBatch.java    From monasca-persister with Apache License 2.0 6 votes vote down vote up
private void logReplicaBatchMap(String name, Map<Set<Host>, Deque<BatchStatement>> map) {
  if (logger.isDebugEnabled()) {
    StringBuilder sb = new StringBuilder(name);
    sb.append(": Size: ").append(map.size());
    sb.append(". Replicas: |");
    for (Entry<Set<Host>, Deque<BatchStatement>> entry : map.entrySet()) {
      for (Host host : entry.getKey()) {
        sb.append(host.getAddress().toString()).append(",");
      }
      sb.append(":");
      for (BatchStatement bs : entry.getValue()) {
        sb.append(bs.size()).append(",");
      }

      sb.append("|");

    }
    logger.debug(sb.toString());
  }
}
 
Example #4
Source File: ClusterHintsPollerTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
private ArgumentMatcher<Statement> getHostStatementMatcher(final Host host, final String query)
        throws Exception {
    return new ArgumentMatcher<Statement>() {
        @Override
        public boolean matches(Object argument) {
            SelectedHostStatement statement = (SelectedHostStatement) argument;

            return ((SimpleStatement)statement.getStatement()).getQueryString().equals(query) &&
                    Objects.equals(statement.getHostCordinator().getAddress(), host.getAddress());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText(format("query:%s host:%s", query, host.getAddress().toString()));
        }
    };
}
 
Example #5
Source File: Installer.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
private void waitForAllNodesToBeUp(Session session) {
    boolean isReady = false;
    int attempts = cassandraConnectionMaxRetries;
    long delay = 2000;

    while (!isReady && !Thread.currentThread().isInterrupted() && attempts-- >= 0) {
        isReady = true;
        for (Host host : session.getCluster().getMetadata().getAllHosts()) {
            if (!host.isUp()) {
                isReady = false;
                logger.warn("Cassandra node {} may not be up yet. Waiting {} ms for node to come up", host, delay);
                try {
                    Thread.sleep(delay);
                    delay = Math.min(delay * 2, cassandraConnectionMaxDelay);
                } catch(InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                break;
            }
        }
    }
    if (!isReady) {
        throw new RuntimeException("It appears that not all nodes in the Cassandra cluster are up " +
                "after " + attempts + " checks. Schema updates cannot proceed without all nodes being up.");
    }
}
 
Example #6
Source File: RxSessionImpl.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
private boolean availableInFlightSlots(Statement st) {
    boolean available = false;
    Iterator<Host> hostIterator = loadBalancingPolicy.newQueryPlan(session.getLoggedKeyspace(), st);
    hostIter: while(hostIterator.hasNext()) {
        Host host = hostIterator.next();
        int inFlightQueries = session.getState().getInFlightQueries(host);
        switch(loadBalancingPolicy.distance(host)) {
            case LOCAL:
                if(inFlightQueries < maxInFlightLocal) {
                    available = true;
                    break hostIter;
                }
                break;
            case REMOTE:
                if(inFlightQueries < maxInFlightRemote) {
                    available = true;
                    break hostIter;
                }
                break;
            default:
                // IGNORED is something we're not going to write to
                break;
        }
    }
    return available;
}
 
Example #7
Source File: ClusterHintsPollerTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Test
public void testClusterHintsPollerWhenNodeDown() throws UnknownHostException {
    ClusterHintsPoller clusterHintsPoller = new ClusterHintsPoller();
    Session mockSession = mock(Session.class);
    Cluster mockCluster = mock(Cluster.class);
    Metadata mockMetadata = mock(Metadata.class);
    when(mockCluster.getMetadata()).thenReturn(mockMetadata);
    when(mockCluster.getClusterName()).thenReturn("test-cluster");
    Host node1 = mock(Host.class);
    when(node1.getAddress()).thenReturn(InetAddress.getByName("127.0.0.1"));
    Host node2 = mock(Host.class);
    when(node2.getAddress()).thenReturn(InetAddress.getByName("127.0.0.2"));
    Host node3 = mock(Host.class);
    when(node3.getAddress()).thenReturn(InetAddress.getByName("127.0.0.3"));

    when(mockSession.getCluster()).thenReturn(mockCluster);
    // The first node queried is down
    when(mockSession.execute(any(Statement.class))).thenThrow(new NoHostAvailableException(ImmutableMap.<InetSocketAddress, Throwable>of()));

    when(mockMetadata.getAllHosts()).thenReturn(ImmutableSet.of(node1, node2, node3));
    HintsPollerResult actualResult = clusterHintsPoller.getOldestHintsInfo(mockSession);

    // Make sure HintsPollerResult fails
    assertFalse(actualResult.areAllHostsPolling(), "Result should show hosts failing");
    assertEquals(actualResult.getHostFailure(), ImmutableSet.of(InetAddress.getByName("127.0.0.1")), "Node 1 should return with host failure");
}
 
Example #8
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Host> newQueryPlan(String keyspace, Statement statement)
{
    List<Host> local = new ArrayList<>(1);
    List<Host> remote = new ArrayList<>(liveReplicaHosts.size());
    for (Host liveReplicaHost : liveReplicaHosts)
    {
        if (isLocalHost(liveReplicaHost))
        {
            local.add(liveReplicaHost);
        }
        else
        {
            remote.add(liveReplicaHost);
        }
    }

    Collections.shuffle(remote);

    logger.debug("Using the following hosts order for the new query plan: {} | {}", local, remote);

    return Iterators.concat(local.iterator(), remote.iterator());
}
 
Example #9
Source File: PeerMetadataIntegrationTest.java    From simulacron with Apache License 2.0 6 votes vote down vote up
@Test
public void testVnodeSupport() throws Exception {
  // Validate that peers as appropriately discovered when connecting to a node and vnodes are
  // assigned.
  try (BoundCluster boundCluster =
          server.register(ClusterSpec.builder().withNumberOfTokens(256).withNodes(3, 3, 3));
      Cluster driverCluster = defaultBuilder(boundCluster).build()) {
    driverCluster.init();

    // Should be 9 hosts
    assertThat(driverCluster.getMetadata().getAllHosts()).hasSize(9);

    Set<Token> allTokens = new HashSet<>();
    for (Host host : driverCluster.getMetadata().getAllHosts()) {
      assertThat(host.getTokens()).hasSize(256);
      allTokens.addAll(host.getTokens());
    }

    // Should be 256*9 unique tokens.
    assertThat(allTokens).hasSize(256 * 9);
  }
}
 
Example #10
Source File: ClusterFactory.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
protected Cluster createCluster() {
   Cluster.Builder builder = Cluster.builder();

   if (this.contactPoints != null && !this.contactPoints.isEmpty()) {
      String[] values = new String[this.contactPoints.size()];
      this.contactPoints.toArray(values);
      builder.addContactPoints(values);
   }

   builder.withPort(this.port);

   Cluster cluster = builder.build();

   Metadata metadata = cluster.getMetadata();

   LOG.info("Connected to Cassandra cluster: " + metadata.getClusterName());
   for (Host host : metadata.getAllHosts()) {
      LOG.info("DataCenter: {}, Rack: {}, Host: {}",
               new Object[]{host.getDatacenter(), host.getRack(), host.getAddress()});
   }

   return cluster;
}
 
Example #11
Source File: CassandraConnector.java    From tutorials with MIT License 6 votes vote down vote up
public void connect(final String node, final Integer port) {

        Builder b = Cluster.builder().addContactPoint(node);

        if (port != null) {
            b.withPort(port);
        }
        cluster = b.build();

        Metadata metadata = cluster.getMetadata();
        LOG.info("Cluster name: " + metadata.getClusterName());

        for (Host host : metadata.getAllHosts()) {
            LOG.info("Datacenter: " + host.getDatacenter() + " Host: " + host.getAddress() + " Rack: " + host.getRack());
        }

        session = cluster.connect();
    }
 
Example #12
Source File: SettingsNode.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public Set<String> resolveAllPermitted(StressSettings settings)
{
    Set<String> r = new HashSet<>();
    switch (settings.mode.api)
    {
        case THRIFT_SMART:
        case JAVA_DRIVER_NATIVE:
            if (!isWhiteList)
            {
                for (Host host : settings.getJavaDriverClient().getCluster().getMetadata().getAllHosts())
                    r.add(host.getAddress().getHostName());
                break;
            }
        case THRIFT:
        case SIMPLE_NATIVE:
            for (InetAddress address : resolveAllSpecified())
                r.add(address.getHostName());
    }
    return r;
}
 
Example #13
Source File: TestHostAddressFactory.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testToHostAddressList()
        throws Exception
{
    Set<Host> hosts = ImmutableSet.of(
            new TestHost(
                    new InetSocketAddress(
                            InetAddress.getByAddress(new byte[] {
                                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
                            }),
                            3000)),
            new TestHost(new InetSocketAddress(InetAddress.getByAddress(new byte[] {1, 2, 3, 4}), 3000)));

    HostAddressFactory hostAddressFactory = new HostAddressFactory();
    List<HostAddress> list = hostAddressFactory.toHostAddressList(hosts);

    assertEquals(list.toString(), "[[102:304:506:708:90a:b0c:d0e:f10], 1.2.3.4]");
}
 
Example #14
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void onUp(Host host)
{
    if (replicaAddresses.contains(host.getAddress()))
    {
        liveReplicaHosts.add(host);
        logger.debug("The host {} is now up", host);
    }
}
 
Example #15
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Cluster cluster, Collection<Host> hosts)
{
    List<Host> replicaHosts = new ArrayList<>();
    for (Host host : hosts)
    {
        if (replicaAddresses.contains(host.getAddress()))
        {
            replicaHosts.add(host);
        }
    }
    liveReplicaHosts.addAll(replicaHosts);
    logger.debug("Initialized with replica hosts: {}", replicaHosts);
}
 
Example #16
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void onAdd(Host host)
{
    if (replicaAddresses.contains(host.getAddress()))
    {
        liveReplicaHosts.add(host);
        logger.debug("Added a new host {}", host);
    }
}
 
Example #17
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public HostDistance distance(Host host)
{
    if (isLocalHost(host))
    {
        return HostDistance.LOCAL;
    }
    else
    {
        return HostDistance.REMOTE;
    }
}
 
Example #18
Source File: LocalMachineLoadBalancingPolicy.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the given new host only if the list of known hosts is empty.
 *
 * @param host the host to add.
 */
@Override
public void onAdd(Host host) {
    if (liveHosts.isEmpty()) {
        onUp(host);
    }
}
 
Example #19
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemove(Host host)
{
    if (liveReplicaHosts.remove(host))
    {
        logger.debug("Removed the host {}", host);
    }
}
 
Example #20
Source File: LocalMachineLoadBalancingPolicy.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the hosts to use for a new query.
 * <p/>
 * The returned plan will try each known host of the cluster. Upon each
 * call to this method, the {@code i}th host of the plans returned will cycle
 * over all the hosts of the cluster in a round-robin fashion.
 *
 * @param loggedKeyspace the keyspace currently logged in on for this
 *                       query.
 * @param statement      the query for which to build the plan.
 * @return a new query plan, i.e. an iterator indicating which host to
 * try first for querying, which one to use as failover, etc...
 */
@Override
public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) {

    // We clone liveHosts because we want a version of the list that
    // cannot change concurrently of the query plan iterator (this
    // would be racy). We use clone() as it don't involve a copy of the
    // underlying array (and thus we rely on liveHosts being a CopyOnWriteArrayList).
    @SuppressWarnings("unchecked")
    final List<Host> hosts = (List<Host>) liveHosts.clone();
    final int startIdx = index.getAndIncrement();

    // Overflow protection; not theoretically thread safe but should be good enough
    if (startIdx > Integer.MAX_VALUE - 10000) {
        index.set(0);
    }

    return new AbstractIterator<Host>() {

        private int idx = startIdx;
        private int remaining = hosts.size();

        @Override
        protected Host computeNext() {
            if (remaining <= 0) {
                return endOfData();
            }

            remaining--;
            int c = idx++ % hosts.size();
            if (c < 0) {
                c += hosts.size();
            }
            return hosts.get(c);
        }
    };
}
 
Example #21
Source File: LocalMachineLoadBalancingPolicy.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(Cluster cluster, Collection<Host> hosts) {

    for (Host h : hosts) {
        if (h.getAddress().equals(host)) {
            this.liveHosts.add(h);
            this.index.set(0);
            break;
        }
    }
}
 
Example #22
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void onDown(Host host)
{
    if (liveReplicaHosts.remove(host))
    {
        logger.debug("The host {} is now down", host);
    }
}
 
Example #23
Source File: ITCassandraDependencies.java    From zipkin-dependencies with Apache License 2.0 5 votes vote down vote up
static void blockWhileInFlight(CassandraStorage storage) {
  // Now, block until writes complete, notably so we can read them.
  Session.State state = storage.session().getState();
  refresh:
  while (true) {
    for (Host host : state.getConnectedHosts()) {
      if (state.getInFlightQueries(host) > 0) {
        Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
        state = storage.session().getState();
        continue refresh;
      }
    }
    break;
  }
}
 
Example #24
Source File: CassandraDriverConnectInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private List<String> getHostList(Object target) {
    if (!(target instanceof Cluster)) {
        return Collections.emptyList();
    }

    final Cluster cluster = (Cluster) target;
    final Set<Host> hosts = cluster.getMetadata().getAllHosts();
    final int port = cluster.getConfiguration().getProtocolOptions().getPort();
    final List<String> hostList = new ArrayList<String>();
    for (Host host : hosts) {
        final String hostAddress = HostAndPort.toHostAndPortString(host.getAddress().getHostAddress(), port);
        hostList.add(hostAddress);
    }
    return hostList;
}
 
Example #25
Source File: CQLService.java    From Doradus with Apache License 2.0 5 votes vote down vote up
private void displayClusterInfo() {
    Metadata metadata = m_cluster.getMetadata();
    m_logger.info("Connected to cluster with topography:");
    RoundRobinPolicy policy = new RoundRobinPolicy();
    for (Host host : metadata.getAllHosts()) {
        m_logger.info("   Host {}: datacenter: {}, rack: {}, distance: {}",
                      new Object[]{host.getAddress(), host.getDatacenter(), 
            host.getRack(), policy.distance(host)});
    }
    m_logger.info("Database contains {} keyspaces", metadata.getKeyspaces().size());
}
 
Example #26
Source File: CassandraConnection.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new CassandraConnection.
 * @param sessionHolder
 * @throws SQLException
 */
public CassandraConnection(SessionHolder sessionHolder) throws SQLException
{
    this.sessionHolder = sessionHolder;
    Properties props = sessionHolder.properties;

    debugMode = props.getProperty(TAG_DEBUG, "").equals("true");
    hostListPrimary = new TreeSet<String>();
    hostListBackup = new TreeSet<String>();
    connectionProps = (Properties)props.clone();
    clientInfo = new Properties();
    url = PROTOCOL + createSubName(props);
    currentKeyspace = props.getProperty(TAG_DATABASE_NAME);
    username = props.getProperty(TAG_USER, "");
    String version = props.getProperty(TAG_CQL_VERSION, DEFAULT_CQL_VERSION);
    connectionProps.setProperty(TAG_ACTIVE_CQL_VERSION, version);
    majorCqlVersion = getMajor(version);
    defaultConsistencyLevel = ConsistencyLevel.valueOf(props.getProperty(TAG_CONSISTENCY_LEVEL, ConsistencyLevel.ONE.name()));

    cSession = sessionHolder.session;

    metadata = cSession.getCluster().getMetadata();
    logger.info("Connected to cluster: %s\n",
        metadata.getClusterName());
    for (Host aHost : metadata.getAllHosts()) {
    	logger.info("Datacenter: %s; Host: %s; Rack: %s\n",
            aHost.getDatacenter(), aHost.getAddress(), aHost.getRack());
    }

    Iterator<Host> hosts = metadata.getAllHosts().iterator();
    if (hosts.hasNext()) {
        Host firstHost = hosts.next();
        // TODO this is shared among all Connections, what if they belong to different clusters?
        CassandraConnection.DB_MAJOR_VERSION = firstHost.getCassandraVersion().getMajor();
        CassandraConnection.DB_MINOR_VERSION = firstHost.getCassandraVersion().getMinor();
        CassandraConnection.DB_REVISION = firstHost.getCassandraVersion().getPatch();
    }
}
 
Example #27
Source File: CassandraTokenSplitManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private List<String> getEndpoints(String keyspace, TokenRange tokenRange)
{
    Set<Host> endpoints = session.getReplicas(keyspace, tokenRange);
    return unmodifiableList(endpoints.stream()
            .map(Host::toString)
            .collect(toList()));
}
 
Example #28
Source File: ITCassandraDependencies.java    From zipkin-dependencies with Apache License 2.0 5 votes vote down vote up
static void blockWhileInFlight(CassandraStorage storage) {
  // Now, block until writes complete, notably so we can read them.
  Session.State state = storage.session().getState();
  refresh:
  while (true) {
    for (Host host : state.getConnectedHosts()) {
      if (state.getInFlightQueries(host) > 0) {
        Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
        state = storage.session().getState();
        continue refresh;
      }
    }
    break;
  }
}
 
Example #29
Source File: CassandraHealthCheck.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
/**
 * Because we are doing quorum reads and writes the system is still healthy when there are N - 1 nodes up, where
 * N is the total number of nodes in the cluster.
 *
 * Check this tool: https://www.ecyrd.com/cassandracalculator/
 *
 * @return
 */
@Override
public HealthCheckResult check() {
    if (cassandraSession.isClosed()) {
        return unhealthy("Cassandra Session appears to be closed");
    }
    
    Set<Host> allHosts = cassandraSession.getCluster().getMetadata().getAllHosts();
    Collection<Host> connectedHosts = cassandraSession.getState().getConnectedHosts();

    if(allHosts.size() - connectedHosts.size() > 1) {
        return unhealthy("Not enough connected hosts to do QUORUM reads and writes");
    }

    /* This query caused cassandra machines to go oom on the production cluster
    try {
        String query = QueryBuilder.select().countAll().from("\"ElasticActors\"", "\"PersistentActors\"").getQueryString();
        ResultSet results = cassandraSession.execute(query);
        if (results.one() == null) {
            return unhealthy("No results found in Cassandra ElasticActors table");
        }
    } catch (Exception e) {
        return unhealthy("Unable to query Cassandra ElasticActors table: " + e.getMessage(), e);
    }
    */

    return healthy();
}
 
Example #30
Source File: SmartThriftClient.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private Client get(ByteBuffer pk)
{
    Set<Host> hosts = metadata.getReplicas(metadata.quote(keyspace), pk);
    InetAddress address = null;
    if (hosts.size() > 0)
    {
        int pos = roundrobin.incrementAndGet() % hosts.size();
        for (int i = 0 ; address == null && i < hosts.size() ; i++)
        {
            if (pos < 0)
                pos = -pos;
            Host host = Iterators.get(hosts.iterator(), (pos + i) % hosts.size());
            if (whiteset == null || whiteset.contains(host.getAddress()))
                address = host.getAddress();
        }
    }
    if (address == null)
        address = whitelist.get(ThreadLocalRandom.current().nextInt(whitelist.size()));
    ConcurrentLinkedQueue<Client> q = cache.get(address);
    if (q == null)
    {
        ConcurrentLinkedQueue<Client> newQ = new ConcurrentLinkedQueue<Client>();
        q = cache.putIfAbsent(address, newQ);
        if (q == null)
            q = newQ;
    }
    Client tclient = q.poll();
    if (tclient != null)
        return tclient;
    return new Client(settings.getRawThriftClient(address.getHostAddress()), address);
}