Reduce number of connections in core tests and ensure the datastax provider is a singleton.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/3f8e7cba Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/3f8e7cba Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/3f8e7cba Branch: refs/heads/master Commit: 3f8e7cbaf4a7f8351185e803e7bd43dafc49a9a9 Parents: 2203f43 Author: Michael Russo <[email protected]> Authored: Thu Feb 11 17:11:59 2016 -0800 Committer: Michael Russo <[email protected]> Committed: Thu Feb 11 17:11:59 2016 -0800 ---------------------------------------------------------------------- .../resources/usergrid-custom-test.properties | 2 +- .../core/datastax/DatastaxSessionProvider.java | 2 + .../core/datastax/impl/DatastaxClusterImpl.java | 60 ++++++++++++++++++-- .../migration/schema/MigrationManagerImpl.java | 2 +- 4 files changed, 59 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/3f8e7cba/stack/core/src/test/resources/usergrid-custom-test.properties ---------------------------------------------------------------------- diff --git a/stack/core/src/test/resources/usergrid-custom-test.properties b/stack/core/src/test/resources/usergrid-custom-test.properties index 0fa9573..c8b4d48 100644 --- a/stack/core/src/test/resources/usergrid-custom-test.properties +++ b/stack/core/src/test/resources/usergrid-custom-test.properties @@ -15,7 +15,7 @@ # these settings allow tests to run and consistently pass on 16GB MacBook Pro # with ug.heapmax=5000m and ug.heapmin=3000m (set in Maven settings.xml) cassandra.timeout=2000 -cassandra.connections=1000 +cassandra.connections=50 #Not a good number for real systems. Write shards should be 2x cluster size from our tests http://git-wip-us.apache.org/repos/asf/usergrid/blob/3f8e7cba/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/DatastaxSessionProvider.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/DatastaxSessionProvider.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/DatastaxSessionProvider.java index 1b39cb8..5e9a633 100644 --- a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/DatastaxSessionProvider.java +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/DatastaxSessionProvider.java @@ -22,7 +22,9 @@ package org.apache.usergrid.persistence.core.datastax; import com.datastax.driver.core.Session; import com.google.inject.Inject; import com.google.inject.Provider; +import com.google.inject.Singleton; +@Singleton public class DataStaxSessionProvider implements Provider<Session> { private final DataStaxCluster dataStaxCluster; http://git-wip-us.apache.org/repos/asf/usergrid/blob/3f8e7cba/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/impl/DatastaxClusterImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/impl/DatastaxClusterImpl.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/impl/DatastaxClusterImpl.java index ffe61e6..a0d65e0 100644 --- a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/impl/DatastaxClusterImpl.java +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/impl/DatastaxClusterImpl.java @@ -24,6 +24,7 @@ import com.datastax.driver.core.policies.LoadBalancingPolicy; import com.google.inject.Inject; import com.google.inject.Singleton; import org.apache.usergrid.persistence.core.astyanax.CassandraFig; +import org.apache.usergrid.persistence.core.datastax.CQLUtils; import org.apache.usergrid.persistence.core.datastax.DataStaxCluster; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,18 +99,17 @@ public class DataStaxClusterImpl implements DataStaxCluster { cluster.getConfiguration().getPoolingOptions().getIdleTimeoutSeconds(), cluster.getConfiguration().getPoolingOptions().getPoolTimeoutMillis() / 1000); - - - - + createOrUpdateKeyspace(); } + @Override public Cluster getCluster(){ return cluster; } + @Override public Session getClusterSession(){ if ( clusterSession == null || clusterSession.isClosed() ){ @@ -119,13 +119,63 @@ public class DataStaxClusterImpl implements DataStaxCluster { return clusterSession; } + @Override public Session getApplicationSession(){ if ( applicationSession == null || applicationSession.isClosed() ){ - applicationSession = cluster.connect( "\""+cassandraFig.getApplicationKeyspace()+"\"" ); + applicationSession = cluster.connect( CQLUtils.quote(cassandraFig.getApplicationKeyspace() ) ); } return applicationSession; } + private void createOrUpdateKeyspace() throws Exception { + + clusterSession = getClusterSession(); + + final String createApplicationKeyspace = String.format( + "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH replication = %s", + cassandraFig.getApplicationKeyspace(), + CQLUtils.getFormattedReplication( cassandraFig.getStrategy(), cassandraFig.getStrategyOptions() ) + + ); + + final String updateApplicationKeyspace = String.format( + "ALTER KEYSPACE \"%s\" WITH replication = %s", + cassandraFig.getApplicationKeyspace(), + CQLUtils.getFormattedReplication( cassandraFig.getStrategy(), cassandraFig.getStrategyOptions() ) + ); + + logger.info("Creating application keyspace with the following CQL: {}", createApplicationKeyspace); + clusterSession.execute(createApplicationKeyspace); + logger.info("Updating application keyspace with the following CQL: {}", updateApplicationKeyspace); + clusterSession.executeAsync(updateApplicationKeyspace); + + // this session pool is only used when running database setup so close it when finished to clear resources + clusterSession.close(); + + waitForSchemaAgreement(); + } + + /** + * Wait until all Cassandra nodes agree on the schema. Sleeps 100ms between checks. + * + */ + private void waitForSchemaAgreement() { + + while ( true ) { + + if( cluster.getMetadata().checkSchemaAgreement() ){ + return; + } + + //sleep and try it again + try { + Thread.sleep( 100 ); + } + catch ( InterruptedException e ) { + //swallow + } + } + } } http://git-wip-us.apache.org/repos/asf/usergrid/blob/3f8e7cba/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/MigrationManagerImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/MigrationManagerImpl.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/MigrationManagerImpl.java index e7e0cb5..8eb0576 100644 --- a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/MigrationManagerImpl.java +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/MigrationManagerImpl.java @@ -147,7 +147,7 @@ public class MigrationManagerImpl implements MigrationManager { logger.info("Creating application keyspace with the following CQL: {}", createApplicationKeyspace); clusterSession.execute(createApplicationKeyspace); logger.info("Updating application keyspace with the following CQL: {}", updateApplicationKeyspace); - clusterSession.execute(updateApplicationKeyspace); + clusterSession.executeAsync(updateApplicationKeyspace); // this session pool is only used when running database setup so close it when finished to clear resources clusterSession.close();
