Giter VIP home page Giter VIP logo

cassandra-lock's Introduction

cassandra-lock

Java implementation of distributed lock based on lightweight transactions in Cassandra database.

Inspired by Consensus on Cassandra

Installation

Add Maven dependency in your project:

<dependency>
  <groupId>com.dekses</groupId>
  <artifactId>cassandra-lock</artifactId>
  <version>0.0.3</version>
</dependency>

More details for other build systems and latest version available at Maven Central repository.

Usage

LockFactory

Create lock_leases table in your project keyspace, it's definition can be found in tables.cql file.

CREATE TABLE lock_leases (
    name text PRIMARY KEY,
    owner text,
    value text
) WITH default_time_to_live = 60;

In your Java code create LockFactory instance using existing Cassandra session object or by providing cluster contact points and keyspace:

// with session
LockFactory lockFactory = new LockFactory(session);

// with contact points and keyspace
LockFactory lockFactory = new LockFactory("127.0.0.1", "casslock_test");

Alternatively, if you prefer to use factory singleton object, you can initialize it and then retrieve using getInstance():

// Initialize
// with session
LockFactory.initialize(session);

// with contact points and keyspace
LockFactory.initialize("127.0.0.1", "casslock_test");

// Retrieve factory object
LockFactory lockFactory = LockFactory.getInstance();

Lock

Create new Lock instances using LockFactory object:

Lock lock = lockFactory.getLock("my-resource");
if (lock.tryLock()) {
    try {
      // do something related to my-resource
      // ...

      // call periodically to keep lock
      lock.keepAlive();
    } finally {
      lock.unlock();
    }
} else {
    // Can not acquire lock on resource,
    // it's already taken by other owner/process
}

TTL (time to live)

Each lock lease has TTL measured in seconds. It is 60 seconds by default, if lease is not updated during this interval using keepAlive() method call it will be automatically removed.

You can change default TTL for LockFactory, it affects every lock created by this factory (excluding locks that already exist).

// local object
lockFactory.setDefaultTTL(120);

// singleton
LockFactory.getInstance().setDefaultTTL(120);

Also TTL can be specified for each individual lock object.

Lock lock = lockFactory.getLock("my-resource", 120);

cassandra-lock's People

Contributors

chrisribble avatar lucius-pl avatar ysubach avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

cassandra-lock's Issues

Is alive?

Is this project alive? I've made a few modification and I wonder if pull requests makes sense. Thanks.

Thoughts on using LOCAL_SERIAL instead of SERIAL

Hello,
thanks for your work. We are using your solution in production for some years now.

As we are operating in two DCs, I wonder if it breaks the guarantees of your library when I start using LOCAL_SERIAL instead of SERIAL and LOCAL_QUORUM instead of QUORUM?
Our point of using LOCAL_x is that we want to survive the outage of one of our two DCs.

Do you have any thoughts / recommendations on that?

Thank you very much!

Stefan

Maven central

Would be possible to upload this project to Maven central. I use AWS CodeBuild and it would be very helpful for me. Thanks in advance.

What is the purpose of the `value` column?

I see from the create table statement, that the lock_leases table has a value text column:

CREATE TABLE lock_leases (
    name text PRIMARY KEY,
    owner text,
    value text
) WITH default_time_to_live = 60;

But the column is never used anywhere in the repository. Does it serve some other purpose that isn't obvious?

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.