Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

3/6/2019

Reading time:1 min

paradoxical-io/cassandra.leadership

by John Doe

A library to help elect leaders using cassandra. Cassandra is already leveraging paxos, and withcassandra's expiring columns we can build a simple leadership election module.InstallationTo install<dependency> <groupId>io.paradoxical</groupId> <artifactId>cassandra-leadership</artifactId> <version>1.0</version></dependency>UsageFirst you need some sort of leadership election tracking table. You can custom the schema names, but you must have something like:CREATE TABLE leadership_election ( group text PRIMARY KEY, leader_id text);In order for this to work. The default schema uses the one above.LeadershipSchema.DefaultElectionOnce you have your leadership tables created you can create a leadership election factory. The factory takes a LeadershipGroupas a key. This lets you create leadership per group. For example, if you want to elect the leader for a group of workers,pin all the workers to the same leadership group. Each worker should have its own unique ID and it will be elected leader.While doing work, if you want to maintain leadership status you will need to heartbeat. It is recommended to heartbeat half the ttl.For example, if you set the lease time TTL to be 30 seconds, you should attempt to heartbeat no later than every 15 seconds.Leadership here is re-entrant. If a leader is already active and that same leader asks to elect a leader it will return itself until eitherits lease time is up, or it explicitly renounces leadership.As an examplefinal LeadershipElection leadership = new CassandraLeadershipElectionFactory(session).create(LeadershipGroup.random());final LeaderIdentity user1 = LeaderIdentity.valueOf("user1");final LeaderIdentity user2 = LeaderIdentity.valueOf("user2");assertThat(leadership.tryClaimLeader(user1, Duration.ofSeconds(2))).isPresent();Thread.sleep(Duration.ofSeconds(3).toMillis());assertThat(leadership.tryClaimLeader(user2, Duration.ofSeconds(3))).isPresent();Listing current leader groupsListing current leader groups is provided for via the LeadershipStatus contract which will list you the current group and its leader id. It also givesyou the amount of seconds that the leader will be active for. When the leader is no longer active the entry is removed.Wiring into GuiceIf you use guice, you can easily create your factoriespublic class LeadershipModule extends AbstractModule { @Override protected void configure() { bind(LeadershipSchema.class).toInstance(LeadershipSchema.Default); bind(LeadershipStatus.class).to(LeadershipStatusImpl.class); bind(LeadershipElectionFactory.class).to(CassandraLeadershipElectionFactory.class); }}

Illustration Image

Build status

A library to help elect leaders using cassandra. Cassandra is already leveraging paxos, and with cassandra's expiring columns we can build a simple leadership election module.

Installation

To install

<dependency>
    <groupId>io.paradoxical</groupId>
    <artifactId>cassandra-leadership</artifactId>
    <version>1.0</version>
</dependency>

Usage

First you need some sort of leadership election tracking table. You can custom the schema names, but you must have something like:

CREATE TABLE leadership_election (
    group text PRIMARY KEY,
    leader_id text
);

In order for this to work. The default schema uses the one above.

LeadershipSchema.Default

Election

Once you have your leadership tables created you can create a leadership election factory. The factory takes a LeadershipGroup as a key. This lets you create leadership per group. For example, if you want to elect the leader for a group of workers, pin all the workers to the same leadership group. Each worker should have its own unique ID and it will be elected leader.

While doing work, if you want to maintain leadership status you will need to heartbeat. It is recommended to heartbeat half the ttl.

For example, if you set the lease time TTL to be 30 seconds, you should attempt to heartbeat no later than every 15 seconds.

Leadership here is re-entrant. If a leader is already active and that same leader asks to elect a leader it will return itself until either its lease time is up, or it explicitly renounces leadership.

As an example

final LeadershipElection leadership = new CassandraLeadershipElectionFactory(session).create(LeadershipGroup.random());
final LeaderIdentity user1 = LeaderIdentity.valueOf("user1");
final LeaderIdentity user2 = LeaderIdentity.valueOf("user2");
assertThat(leadership.tryClaimLeader(user1, Duration.ofSeconds(2))).isPresent();
Thread.sleep(Duration.ofSeconds(3).toMillis());
assertThat(leadership.tryClaimLeader(user2, Duration.ofSeconds(3))).isPresent();

Listing current leader groups

Listing current leader groups is provided for via the LeadershipStatus contract which will list you the current group and its leader id. It also gives you the amount of seconds that the leader will be active for. When the leader is no longer active the entry is removed.

Wiring into Guice

If you use guice, you can easily create your factories

public class LeadershipModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(LeadershipSchema.class).toInstance(LeadershipSchema.Default);
        bind(LeadershipStatus.class).to(LeadershipStatusImpl.class);
        
        bind(LeadershipElectionFactory.class).to(CassandraLeadershipElectionFactory.class);
    }
}

Related Articles

cluster
troubleshooting
datastax

GitHub - arodrime/Montecristo: Datastax Cluster Health Check Tooling

arodrime

4/3/2024

cassandra
cluster

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

cassandra