com.datastax.driver.core.exceptions.NoHostAvailableException Java Examples

The following examples show how to use com.datastax.driver.core.exceptions.NoHostAvailableException. 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: CassandraTarget.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private boolean checkCassandraReachable(List<ConfigIssue> issues) {
  boolean isReachable = true;
  try (Cluster validationCluster = getCluster()) {
    Session validationSession = validationCluster.connect();
    validationSession.close();
  } catch (NoHostAvailableException | AuthenticationException | IllegalStateException | StageException e) {
    isReachable = false;
    Target.Context context = getContext();
    LOG.error(Errors.CASSANDRA_05.getMessage(), e.toString(), e);
    issues.add(
        context.createConfigIssue(
        Groups.CASSANDRA.name(),
        CONTACT_NODES_LABEL,
        Errors.CASSANDRA_05, e.toString()
        )
    );
  }
  return isReachable;
}
 
Example #2
Source File: CassandraTest.java    From copper-engine with Apache License 2.0 6 votes vote down vote up
@BeforeClass
    public synchronized static void setUpBeforeClass() throws Exception {
        if (factory == null) {
//            logger.info("Starting embedded cassandra...");
//            EmbeddedCassandraServerHelper.startEmbeddedCassandra("unittest-cassandra.yaml", "./build/cassandra");
//            Thread.sleep(100);
//            logger.info("Successfully started embedded cassandra.");

            final Cluster cluster = new Builder().addContactPoint("localhost").withPort(CASSANDRA_PORT).build();
//            final Session session = cluster.newSession();
//            session.execute("CREATE KEYSPACE copper WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");

            factory = new UnitTestCassandraEngineFactory(false);
            factory.setCassandraPort(CASSANDRA_PORT);
            try {
                factory.getEngine().startup();
            } catch (NoHostAvailableException e) {
                factory = null;
            }
        }
    }
 
Example #3
Source File: DataStaxClusterImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Session getApplicationLocalSession(){

    // always grab cluster from getCluster() in case it was prematurely closed
    if ( queueMessageSession == null || queueMessageSession.isClosed() ){
        int retries = 3;
        int retryCount = 0;
        while ( retryCount < retries){
            try{
                retryCount++;
                queueMessageSession = getCluster().connect( CQLUtils.quote( cassandraConfig.getApplicationLocalKeyspace() ) );
                break;
            }catch(NoHostAvailableException e){
                if(retryCount == retries){
                    throw e;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    // swallow
                }
            }
        }
    }
    return queueMessageSession;
}
 
Example #4
Source File: DataStaxClusterImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Session getApplicationSession(){

    // always grab cluster from getCluster() in case it was prematurely closed
    if ( applicationSession == null || applicationSession.isClosed() ){
        int retries = 3;
        int retryCount = 0;
        while ( retryCount < retries){
            try{
                retryCount++;
                applicationSession = getCluster().connect( CQLUtils.quote( cassandraConfig.getApplicationKeyspace() ) );
                break;
            }catch(NoHostAvailableException e){
                if(retryCount == retries){
                    throw e;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    // swallow
                }
            }
        }
    }
    return applicationSession;
}
 
Example #5
Source File: AbstractCassandraProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(ProcessContext context) {
    final boolean connectionProviderIsSet = context.getProperty(CONNECTION_PROVIDER_SERVICE).isSet();

    if (connectionProviderIsSet) {
        CassandraSessionProviderService sessionProvider = context.getProperty(CONNECTION_PROVIDER_SERVICE).asControllerService(CassandraSessionProviderService.class);
        cluster.set(sessionProvider.getCluster());
        cassandraSession.set(sessionProvider.getCassandraSession());
        return;
    }

    try {
        connectToCassandra(context);
    } catch (NoHostAvailableException nhae) {
        getLogger().error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae);
        getLogger().error(nhae.getCustomMessage(10, true, false));
        throw new ProcessException(nhae);
    } catch (AuthenticationException ae) {
        getLogger().error("Invalid username/password combination", ae);
        throw new ProcessException(ae);
    }
}
 
Example #6
Source File: ConnectionTest.java    From jmeter-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
    public void testBadConnection() {
        CassandraConnection cc = new CassandraConnection();

        cc.setProperty("contactPoints", "127.1.1.1");
//        cc.setProperty("keyspace", "testks");
        cc.setProperty("sessionName", "testsession");

        Boolean exeptionCaught=false;

        try {
            cc.testStarted();
        } catch (NoHostAvailableException e) {
            exeptionCaught = true;
        }
        assertTrue(exeptionCaught, "NoHostAvailable did not occur.");

        cc.testEnded();
    }
 
Example #7
Source File: Sessions.java    From glowroot with Apache License 2.0 6 votes vote down vote up
static Session createSession() throws Exception {
    Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1")
            // long read timeout is sometimes needed on slow travis ci machines
            .withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000))
            .withQueryOptions(getQueryOptions())
            .build();
    Session session = cluster.connect();
    session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION ="
            + " { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
    session.execute("CREATE TABLE IF NOT EXISTS test.users"
            + " (id int PRIMARY KEY, fname text, lname text)");
    try {
        session.execute("TRUNCATE test.users");
    } catch (NoHostAvailableException e) {
        // sometimes slow, so give it a second chance
        session.execute("TRUNCATE test.users");
    }
    for (int i = 0; i < 10; i++) {
        session.execute("INSERT INTO test.users (id, fname, lname) VALUES (" + i + ", 'f" + i
                + "', 'l" + i + "')");
    }
    return session;
}
 
Example #8
Source File: CassandraSinkBaseTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = NoHostAvailableException.class)
public void testHostNotFoundErrorHandling() throws Exception {
	CassandraSinkBase base = new CassandraSinkBase(new ClusterBuilder() {
		@Override
		protected Cluster buildCluster(Cluster.Builder builder) {
			return builder
				.addContactPoint("127.0.0.1")
				.withoutJMXReporting()
				.withoutMetrics().build();
		}
	}, CassandraSinkBaseConfig.newBuilder().build(), new NoOpCassandraFailureHandler()) {
		@Override
		public ListenableFuture send(Object value) {
			return null;
		}
	};

	base.open(new Configuration());
}
 
Example #9
Source File: CCMBridge.java    From cassandra-jdbc-wrapper with Apache License 2.0 6 votes vote down vote up
public void createCluster() {
    erroredOut = false;
    schemaCreated = false;
    cassandraCluster = CCMBridge.create("test", 1);
    try {
        Builder builder = Cluster.builder();
        builder = configure(builder);
        cluster = builder.addContactPoints(IP_PREFIX + '1').build();
        session = cluster.connect();
    } catch (NoHostAvailableException e) {
        erroredOut = true;
        for (Map.Entry<InetSocketAddress, Throwable> entry : e.getErrors().entrySet())
            logger.info("Error connecting to " + entry.getKey() + ": " + entry.getValue());
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: CassandraConfig.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
public CassandraConfig(DataService dataService, String configId, Map<String, String> properties,
                       boolean odataEnable) throws DataServiceFault {
    super(dataService, configId, DataSourceTypes.CASSANDRA, properties, odataEnable);
    Builder builder = Cluster.builder();
    this.populateSettings(builder, properties);
    String keyspace = properties.get(DBConstants.Cassandra.KEYSPACE);
    this.cluster = builder.build();
    try {
        if (keyspace != null && keyspace.trim().length() > 0) {
            this.session = this.cluster.connect(keyspace);
        } else {
            this.session = this.cluster.connect();
        }
        this.nativeBatchRequestsSupported = this.session.getCluster().
                getConfiguration().getProtocolOptions().getProtocolVersion().toInt() > 1;
    } catch (NoHostAvailableException e) {
        throw new DataServiceFault(e, DBConstants.FaultCodes.CONNECTION_UNAVAILABLE_ERROR, e.getMessage());
    }
}
 
Example #11
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 #12
Source File: CassandraSinkBaseTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test(expected = NoHostAvailableException.class)
public void testHostNotFoundErrorHandling() throws Exception {
	CassandraSinkBase base = new CassandraSinkBase(new ClusterBuilder() {
		@Override
		protected Cluster buildCluster(Cluster.Builder builder) {
			return builder
				.addContactPoint("127.0.0.1")
				.withoutJMXReporting()
				.withoutMetrics().build();
		}
	}, CassandraSinkBaseConfig.newBuilder().build(), new NoOpCassandraFailureHandler()) {
		@Override
		public ListenableFuture send(Object value) {
			return null;
		}
	};

	base.open(new Configuration());
}
 
Example #13
Source File: CassandraSinkBaseTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = NoHostAvailableException.class)
public void testHostNotFoundErrorHandling() throws Exception {
	CassandraSinkBase base = new CassandraSinkBase(new ClusterBuilder() {
		@Override
		protected Cluster buildCluster(Cluster.Builder builder) {
			return builder
				.addContactPoint("127.0.0.1")
				.withoutJMXReporting()
				.withoutMetrics().build();
		}
	}, CassandraSinkBaseConfig.newBuilder().build(), new NoOpCassandraFailureHandler()) {
		@Override
		public ListenableFuture send(Object value) {
			return null;
		}
	};

	base.open(new Configuration());
}
 
Example #14
Source File: PutCassandraQL.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) {
    ComponentLog log = getLogger();
    try {
        connectToCassandra(context);
    } catch (final NoHostAvailableException nhae) {
        log.error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae);
        // Log up to 10 error messages. Otherwise if a 1000-node cluster was specified but there was no connectivity,
        // a thousand error messages would be logged. However we would like information from Cassandra itself, so
        // cap the error limit at 10, format the messages, and don't include the stack trace (it is displayed by the
        // logger message above).
        log.error(nhae.getCustomMessage(10, true, false));
        throw new ProcessException(nhae);
    } catch (final AuthenticationException ae) {
        log.error("Invalid username/password combination", ae);
        throw new ProcessException(ae);
    }
}
 
Example #15
Source File: QueryCassandra.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) {
    ComponentLog log = getLogger();
    try {
        connectToCassandra(context);
        final int fetchSize = context.getProperty(FETCH_SIZE).asInteger();
        if (fetchSize > 0) {
            synchronized (cluster.get()) {
                cluster.get().getConfiguration().getQueryOptions().setFetchSize(fetchSize);
            }
        }
    } catch (final NoHostAvailableException nhae) {
        log.error("No host in the Cassandra cluster can be contacted successfully to execute this query", nhae);
        // Log up to 10 error messages. Otherwise if a 1000-node cluster was specified but there was no connectivity,
        // a thousand error messages would be logged. However we would like information from Cassandra itself, so
        // cap the error limit at 10, format the messages, and don't include the stack trace (it is displayed by the
        // logger message above).
        log.error(nhae.getCustomMessage(10, true, false));
        throw new ProcessException(nhae);
    } catch (final AuthenticationException ae) {
        log.error("Invalid username/password combination", ae);
        throw new ProcessException(ae);
    }
}
 
Example #16
Source File: HealthCheck.java    From cassandra-sidecar with Apache License 2.0 6 votes vote down vote up
/**
 * The actual health check
 *
 * @return
 */
private boolean check()
{
    try
    {
        if (session != null && session.getLocalCql() != null)
        {
            ResultSet rs = session.getLocalCql().execute("SELECT release_version FROM system.local");
            boolean result = (rs.one() != null);
            logger.debug("HealthCheck status: {}", result);
            return result;
        }
    }
    catch (NoHostAvailableException nha)
    {
        logger.trace("NoHostAvailableException in HealthCheck - Cassandra Down");
    }
    catch (Exception e)
    {
        logger.error("Failed to reach Cassandra.", e);
    }
    return false;
}
 
Example #17
Source File: CassandraWrapper.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static void waitForCassandra() throws InterruptedException {
    while (true) {
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        try {
            cluster.connect();
            cluster.close();
            return;
        } catch (NoHostAvailableException e) {
            cluster.close();
            SECONDS.sleep(1);
        }
    }
}
 
Example #18
Source File: SchemaUpgradeIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
static void updateSchemaWithRetry(com.datastax.driver.core.Session wrappedSession,
        String query) throws InterruptedException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(SECONDS) < 60) {
        try {
            wrappedSession.execute(query);
            return;
        } catch (NoHostAvailableException e) {
        }
        SECONDS.sleep(1);
    }
    // try one last time and let exception bubble up
    wrappedSession.execute(query);
}
 
Example #19
Source File: CassandraWrapper.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static void waitForCassandra() throws InterruptedException {
    while (true) {
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        try {
            cluster.connect();
            cluster.close();
            return;
        } catch (NoHostAvailableException e) {
            cluster.close();
            SECONDS.sleep(1);
        }
    }
}
 
Example #20
Source File: CassandraWrapper.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static void waitForCassandra() throws InterruptedException {
    while (true) {
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        try {
            cluster.connect();
            cluster.close();
            return;
        } catch (NoHostAvailableException e) {
            cluster.close();
            SECONDS.sleep(1);
        }
    }
}
 
Example #21
Source File: Installer.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private Session initSession() throws InterruptedException {
    long retry = 5000;
    while (true) {
        try {
            return createSession();
        } catch (NoHostAvailableException e) {
            logger.info("Cassandra may not be up yet. Retrying in {} ms", retry);
            Thread.sleep(retry);
        }
    }
}
 
Example #22
Source File: CCMBridge.java    From jmeter-cassandra with Apache License 2.0 5 votes vote down vote up
public static void createCluster() {
    erroredOut = false;
    schemaCreated = false;
    cassandraCluster = CCMBridge.create("test", 1);
    try {
        cluster = Cluster.builder().addContactPoints(IP_PREFIX + '1').build();
        session = cluster.connect();
    } catch (NoHostAvailableException e) {
        erroredOut = true;
        for (Map.Entry<InetSocketAddress, Throwable> entry : e.getErrors().entrySet())
            logger.info("Error connecting to " + entry.getKey() + ": " + entry.getValue());
        throw new RuntimeException(e);
    }
}
 
Example #23
Source File: CCMBridge.java    From jmeter-cassandra with Apache License 2.0 5 votes vote down vote up
private CCMCluster(CCMBridge cassandraCluster, Cluster.Builder builder, int totalNodes) {
    this.cassandraCluster = cassandraCluster;
    try {
        this.cluster = builder.addContactPoints(IP_PREFIX + '1').build();
        this.session = cluster.connect();
    } catch (NoHostAvailableException e) {
        for (Map.Entry<InetSocketAddress, Throwable> entry : e.getErrors().entrySet())
            logger.info("Error connecting to " + entry.getKey() + ": " + entry.getValue());
        throw new RuntimeException(e);
    }
}
 
Example #24
Source File: ClusterManagerTest.java    From scalardb with Apache License 2.0 5 votes vote down vote up
@Test
public void getMetadata_NoHostAvailable_ShouldThrowConnectionException() {
  // Arrange
  manager.getSession();
  NoHostAvailableException toThrow = mock(NoHostAvailableException.class);
  when(cluster.getMetadata()).thenThrow(toThrow);

  // Act Assert
  assertThatThrownBy(
          () -> {
            manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);
          })
      .isInstanceOf(ConnectionException.class)
      .hasCause(toThrow);
}
 
Example #25
Source File: PutCassandraQLTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessorNoHostAvailableException() {
    setUpStandardTestConfig();

    processor.setExceptionToThrow(new NoHostAvailableException(new HashMap<>()));
    testRunner.enqueue("UPDATE users SET cities = [ 'New York', 'Los Angeles' ] WHERE user_id = 'coast2coast';");
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(PutCassandraQL.REL_RETRY, 1);
}
 
Example #26
Source File: QueryCassandraTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessorNoInputFlowFileAndExceptions() {
    setUpStandardProcessorConfig();

    // Test no input flowfile
    testRunner.setIncomingConnection(false);
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_SUCCESS, 1);
    testRunner.clearTransferState();

    // Test exceptions
    processor.setExceptionToThrow(new NoHostAvailableException(new HashMap<InetSocketAddress, Throwable>()));
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(
            new ReadTimeoutException(new InetSocketAddress("localhost", 9042), ConsistencyLevel.ANY, 0, 1, false));
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(
            new InvalidQueryException(new InetSocketAddress("localhost", 9042), "invalid query"));
    testRunner.run(1, true, true);
    // No files transferred to failure if there was no incoming connection
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 0);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(new ProcessException());
    testRunner.run(1, true, true);
    // No files transferred to failure if there was no incoming connection
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 0);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(null);

}
 
Example #27
Source File: QueryCassandraTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessorEmptyFlowFileAndExceptions() {
    setUpStandardProcessorConfig();

    // Run with empty flowfile
    testRunner.setIncomingConnection(true);
    processor.setExceptionToThrow(null);
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_SUCCESS, 1);
    testRunner.clearTransferState();

    // Test exceptions
    processor.setExceptionToThrow(new NoHostAvailableException(new HashMap<InetSocketAddress, Throwable>()));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(
            new ReadTimeoutException(new InetSocketAddress("localhost", 9042), ConsistencyLevel.ANY, 0, 1, false));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(
            new InvalidQueryException(new InetSocketAddress("localhost", 9042), "invalid query"));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 1);
    testRunner.clearTransferState();

    processor.setExceptionToThrow(new ProcessException());
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 1);
}
 
Example #28
Source File: CQLExecutor.java    From Rhombus with MIT License 5 votes vote down vote up
public ResultSet executeSync(Statement cql) {
	if(logCql) {
		logger.debug("Executing QueryBuilder Query: {}", cql.toString());
	}
	//just run a normal execute without a prepared statement
	try {
		return session.execute(cql);
	} catch(NoHostAvailableException e) {
		throw new RhombusTimeoutException(e);
	} catch(QueryExecutionException e2) {
		throw new RhombusTimeoutException(e2);
	}
}
 
Example #29
Source File: CQLExecutor.java    From Rhombus with MIT License 5 votes vote down vote up
public void executeBatch(List<CQLStatementIterator> statementIterators) {
	BatchStatement batchStatement = new BatchStatement(BatchStatement.Type.UNLOGGED);
	for(CQLStatementIterator statementIterator : statementIterators) {
		while(statementIterator.hasNext()) {
			CQLStatement statement = statementIterator.next();
			batchStatement.add(getBoundStatement(session, statement));
		}
	} try {
		session.execute(batchStatement);
	} catch(NoHostAvailableException e) {
		throw new RhombusTimeoutException(e);
	} catch(QueryExecutionException e2) {
		throw new RhombusTimeoutException(e2);
	}
}
 
Example #30
Source File: DatastaxIO.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private static void connect() {
    Set<InetSocketAddress> dbHosts = ioconfig.getUniqueBinaryTransportHostsAsInetSocketAddresses();

    int readTimeoutMaxRetries = ioconfig.getReadTimeoutMaxRetries();
    int writeTimeoutMaxRetries = ioconfig.getWriteTimeoutMaxRetries();
    int unavailableMaxRetries = ioconfig.getUnavailableMaxRetries();

    CodecRegistry codecRegistry = new CodecRegistry();

    cluster = Cluster.builder()
            .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(ioconfig.getDatacenterName()).build(), false))
            .withPoolingOptions(getPoolingOptions())
            .withRetryPolicy(new RetryNTimes(readTimeoutMaxRetries, writeTimeoutMaxRetries, unavailableMaxRetries))
            .withCodecRegistry(codecRegistry)
            .withSocketOptions(getSocketOptions())
            .addContactPointsWithPorts(dbHosts)
            .build();

    QueryLogger queryLogger = QueryLogger.builder()
            .withConstantThreshold(5000)
            .build();

    cluster.register(queryLogger);

    if ( LOG.isDebugEnabled() ) {
        logDebugConnectionInfo();
    }

    try {
        session = cluster.connect( CassandraModel.QUOTED_KEYSPACE );
    }
    catch (NoHostAvailableException e){
        // TODO: figure out how to bubble this up
        throw new RuntimeException(e);
    }
}