Question

I am getting the following error "astyanax.connectionpool.exceptions.PoolTimeoutException:" when trying to use client Astyanax to connect to Cassandra on a EC2 instance. Need help

Following is my code snippet.
   import org.mortbay.jetty.servlet.Context;

  import com.netflix.astyanax.AstyanaxContext;
  import com.netflix.astyanax.Keyspace;
  import com.netflix.astyanax.MutationBatch;
  import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
  import com.netflix.astyanax.connectionpool.OperationResult;
  import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
  import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
  import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
  import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
  import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
  import com.netflix.astyanax.model.Column;
  import com.netflix.astyanax.model.ColumnFamily;
  import com.netflix.astyanax.model.ColumnList;
  import com.netflix.astyanax.model.CqlResult;
  import com.netflix.astyanax.serializers.StringSerializer;
  import com.netflix.astyanax.thrift.ThriftFamilyFactory;


  public class MetadataRS {


    public static void main(String args[]){
    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .forCluster("ClusterName")
    .forKeyspace("KeyspaceName")
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()   
        .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
        .setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
    )
    .withConnectionPoolConfiguration(new     ConnectionPoolConfigurationImpl("MyConnectionPool")
        .setPort(9042)
        .setMaxConnsPerHost(40)
        .setSeeds("<EC2-IP>:9042")
        .setConnectTimeout(5000)
    )
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    Keyspace keyspace = context.getEntity();
    System.out.println(keyspace);

    ColumnFamily<String, String> CF_USER_INFO =
              new ColumnFamily<String, String>(
                "Standard1",              // Column Family Name
                StringSerializer.get(),   // Key Serializer
                StringSerializer.get());  // Column 

    OperationResult<ColumnList<String>> result = null;
    try {
        result = keyspace.prepareQuery(CF_USER_INFO)
            .getKey("user_id_hash")
            .execute();
    } catch (ConnectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            ColumnList<String> columns = result.getResult();

            // Lookup columns in response by name 

            String uid   = columns.getColumnByName("user_id_hash").getStringValue();

            System.out.println(uid);
            // Or, iterate through the columns
            for (Column<String> c : result.getResult()) {
              System.out.println(c.getName());
            }
 }
 }

Error com.netflix.astyanax.thrift.ThriftKeyspaceImpl@1961f4 com.netflix.astyanax.connectionpool.exceptions.PoolTimeoutException: PoolTimeoutException: [host=():9042, latency=5001(5001), attempts=1] Timed out waiting for connection at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.waitForConnection(SimpleHostConnectionPool.java:201) at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.borrowConnection(SimpleHostConnectionPool.java:158) at com.netflix.astyanax.connectionpool.impl.RoundRobinExecuteWithFailover.borrowConnection(RoundRobinExecuteWithFailover.java:60) at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:50) at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:229) at com.netflix.astyanax.thrift.ThriftColumnFamilyQueryImpl$1.execute(ThriftColumnFamilyQueryImpl.java:180) at com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:57) Exception in thread "main" java.lang.NullPointerException at com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:62)

Was it helpful?

Solution

Since you are running cassandra on EC2 instance, check that the cassandra's port no. (which you have choosen as 9042) is in the allowed list of ec2 security group and that you have access to it. If not add the port no. in the inbound list of the ec2 security group and set the ip ranges as 0.0.0.0. Alos check the firewall on the ec2 is turned off. By default its false, but its good to check it anyway.

If you have done this, then your client might be behind a firewall that prevents outbound traffic to your choosen port (9042).

Lastly if you have not used any elastic ip, its better to use the ec2 instance dns name, both in your setSeeds section and in the rpc_address of cassandra.yaml

OTHER TIPS

Your issue is not in your code. You have a connectivity problem to the node you specified as your seed. So either that node isn't running, or you can't reach it from the machine running your client.

I finally upgraded libthrift to 0.9 and changed my code to the following nd it working fine now.

public Keyspace getDBConnection() {



    if (poolConfig == null) {

        poolConfig = new ConnectionPoolConfigurationImpl(
                "CassandraPool").setPort(port).setMaxConnsPerHost(1)
                .setSeeds((new StringBuilder(seedHost).append(":").append(port).toString()))
                .setLatencyAwareUpdateInterval(latencyAwareUpdateInterval) // Will resort hosts per
                                                        // token partition every
                                                        // 10 seconds
                .setLatencyAwareResetInterval(latencyAwareResetInterval) // Will clear the latency
                                                        // every 10 seconds. In
                                                        // practice I set this
                                                        // to 0 which is the
                                                        // default. It's better
                                                        // to be 0.
                .setLatencyAwareBadnessThreshold(latencyAwareBadnessThreshold) // Will sort hosts if a host
                                                    // is more than 100% slower
                                                    // than the best and always
                                                    // assign connections to the
                                                    // fastest host, otherwise
                                                    // will use round robin
                .setLatencyAwareWindowSize(latencyAwareWindowSize) // Uses last 100 latency
                                                // samples. These samples are in
                                                // a FIFO q and will just cycle
                                                // themselves.
                .setTimeoutWindow(60000)
        ;
    }

    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
            .forCluster(clusterName)
            .forKeyspace(keyspaceName)
            .withAstyanaxConfiguration(
                    new AstyanaxConfigurationImpl().setDiscoveryType(
                            NodeDiscoveryType.NONE)
                            .setConnectionPoolType(
                                    ConnectionPoolType.ROUND_ROBIN)
                            .setCqlVersion("3.0.0")
                            .setTargetCassandraVersion("2.0"))
            .withConnectionPoolConfiguration(poolConfig)
            .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
            .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    return context.getClient();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top