Skip to content

CQL embedded cassandra server

DuyHai DOAN edited this page Mar 27, 2021 · 16 revisions

Achilles exposes a simple builder to start an embedded Cassandra server and optionally bootstrap the framework.

Such feature is already available for JUnit testing using the AchillesTestResourceBuilder discussed previously. However there is still a need to start an embedded Cassandra outside of testing context and more importantly, to connect to several keyspaces.

To use the embedded Cassandra server, you must pull the following dependency:

 	<dependency>	
 		<groupId>info.archinnov</groupId>
 		<artifactId>achilles-embedded</artifactId>
 		<version>${achilles.version}</version>
 	</dependency>

The builder API:

    CassandraShutdownHook shutdownHook = new CassandraShutdownHook();
    
    CassandraEmbeddedServerBuilder
        .builder()
        .withClusterName("Test Cluster")
        .withListenAddress("127.0.0.1")
        .withRpcAddress("127.0.0.1")
        .withBroadcastAddress("127.0.0.1")
        .withBroadcastRpcAddress("127.0.0.1")		
        .withCommitLogFolder("/home/user/cassandra/commitlog")
        .withConcurrentReads(16)
        .withConcurrentWrites(16)
        .withCQLPort(9042)
        .withConnectionProtocol(ProtocolVersion.V4)    
        .withDataFolder("/home/user/cassandra/data")
        .withDurableWrite(true)
        .withHintsFolder("/home/user/cassandra/hints")
        .withCdcRawFolder("/home/user/cassandra/cdc_raw")
        .withKeyspaceName("achilles_test")
        .withParams(new TypedMap(...))
        .withSavedCachesFolder("/home/user/cassandra/saved_caches")
        .withScript("src/test/resources/startup_script.cql")
        .withScriptTemplate("src/test/resources/startup_script_template.cql", values)
        .withStoragePort(7990)
        .withStorageSSLPort(7999)
        .withThriftPort(9160)
        .withShutdownHook(shutdownHook)
        .cleanDataFilesAtStartup(true)
        .buildNativeSession() || buildNativeCluster() || buildServer();
	
	...
	
    // Control the shutdown of the embedded Cassandra instance
    shutdownHook.shutdownNow();

Most of the parameters are optional, below are the default values:

  • listenAddress: localhost
  • rpcAddress: localhost
  • broadcastAddress: localhost
  • broadcastRpcAddress: localhost
  • clusterName: Achilles Embedded Cassandra Cluster
  • keyspaceName: achilles_embedded
  • dataFolder: target/cassandra_embedded/data
  • commitLogFolder: target/cassandra_embedded/commitlog
  • savedCachesFolder: target/cassandra_embedded/saved_caches
  • cleanDataFilesAtStartup: true
  • concurrenReads: number of concurrent reads
  • concurrenWrites: number of concurrent writes
  • cqlPort: randomized at runtime
  • thriftPort: randomized at runtime
  • storagePort: randomized at runtime
  • storageSSLPort: randomized at runtime
  • durableWrite: true
  • script: startup CQL script to be executed.
  • connection protocol: the connection protocol to be used by the Java driver. Be default it is set to ProtocolVersion.V4 You can specify many scripts by calling the method withScript() multiple times
  • script template: startup CQL script template to be executed given provided values. You can specify many script templates by calling the method withScriptTemplate() multiple times

The builder can return:

  1. either a com.datastax.driver.core.Cluster object
  2. or a com.datastax.driver.core.Session object
  3. or an Achilles CassandraEmbeddedServerBuilder object

There will be at most one embedded Cassandra server started per JVM (more precisely, per classloader) event if many CassandraEmbeddedServer are built. But because the keyspace name is provided each time, the builder will create as many keyspaces as specified.

The shutdown hook

You can inject a shutdown hook to control when to shutdown the embedded Cassandra process.

    CassandraShutDownHook shutdownHook = new CassandraShutDownHook();

    Session session = CassandraEmbeddedServerBuilder.builder()
        .withShutdownHook(shutdownHook)
        ...
        .buildNativeSession();

    ...

    shutdownHook.shutdownNow();

Please note that upon call on shutdownNow(), Achilles will trigger the shutdown of:

  • the embedded Cassandra server
  • the associated Cluster object
  • the associated Session object

Home

Clone this wiki locally