Giter Club home page Giter Club logo

henicea's Introduction

Henicea

Build Status codecov.io Codacy Badge Maven Central Javadocs

Henicea was the sister of Cassandra in the Greek Mythology.

Migration

Henicea provides a simple migration mechanism for Java projects.

Example config class for Spring Boot:

CassandraConfig.java

@Configuration
@Slf4j
public class CassandraConfig {

    @Autowired
    private ResourcePatternResolver resourceResolver;

    @Autowired
    private Environment environment;

    @Bean
    public Migrator migrator() {
        return new Migrator();
    }

    @Bean
    public Cluster cluster() throws IOException {
        Cluster cluster = Cluster.builder()
                .addContactPoints(environment.getProperty("cassandra.contactPoints").split(","))
                .withPort(environment.getProperty("cassandra.port", Integer.class))
                .build();

        log.info("Running migrations");
        migrator().execute(cluster, environment.getProperty("cassandra.keyspace"), getMigrations());

        return cluster;
    }

    @Bean
    public Session session() throws Exception {
        return cluster().connect(environment.getProperty("cassandra.keyspace"));
    }

    private Resource[] getMigrations() throws IOException {
        return resourceResolver.getResources("classpath:/cassandra/*.cql");
    }
}

application.properties

cassandra.contactPoints=cassandra-node1,cassandra-node2
cassandra.port=9042
cassandra.keyspace=myapp

Migration files

The migration file can be named in any pattern. By default the files are sorted by name but the resourceComparator in the Migrator class has a setter to allow custom strategies.

Due to limitations in the driver, each migration file can have only one statement.

Waiting for Cassandra to start

This is a common situation when you use Docker for local development and functional tests, the Cassandra database will start in a separate container and it may take longer to get ready than your application. This library comes with a simple utility to help with retry and exponential back-off.

Cluster cluster = new Retryer()
        .onError((attempt, exception) -> log.warn("Could not connect to cassandra cluster on attempt number {}. Wrapped exception", attempt, exception))
        .withWait(TimeUnit.SECONDS, 1, 2, 4, 8, 10, 10)
        .run(() -> {
            Cluster candidate = Cluster.builder()
                    .addContactPoints("localhost")
                    .withPort(9042)
                    .build();

            candidate.init(); // will throw exception if none of the contact points can be reached

            return candidate;
        });

The run method will throw the last thrown exception if all the attempts failed.

Health check

Henicea provides a simple health check through Spring Boot Actuator. The only requirement is to have a Cassandra Session object in the Spring context.

To auto configure a health check add

@Bean
public CassandraHealthIndicator cassandraHealthIndicator() {
    return new CassandraHealthIndicator();
}

to your Java config class.

henicea's People

Contributors

jrglee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

bcoulaud bala-27

henicea's Issues

Configurable replication factor

Right now the replication factor is hardcoded with value '1'. We need to have a 'setter' to allow the value to be changed prior to the migration execution.

Let the default value to be 1 since we use single node clusters in dev.

Add utility to retry connections

When using Cassandra with Docker for local dev, you can have the application being ready before Cassandra is fully initialized. The migrator will fail to connect to the database and throw an exception stopping the whole process. As a solution the migration tool can retry to connect with an exponential back-off before it gives up the migration completely.

The DataStax driver probably have some utility to help to check the connection state.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.