Giter VIP home page Giter VIP logo

mongo-distributed-locking's Introduction

mongo-distributed-locking

PROBLEM: Simple distributed locking mechanism using MongoDB

================================================================================================================

Create a simple distributed locking mechanism using MongoDB implementing the following interface:

/**
* Distributed lock service which prevents race conditions and other concurrency related problems in a clustered environment.
*/
public interface DistributedLockService {

    /**
     * Try to acquire a lock. If lock with the provided key is already acquired, will return false.
     *
     * @param key lock key
     * @return true on success (lock is not acquired yet), false on failure (lock is already acquired)
     */
    boolean tryLock(String key);

    /**
     * Release a lock. If lock with the provided key is not acquired yet, will return false.
     *
     * @param key lock key
     * @return true on success (acquired lock is released), false on failure (lock is not yet acquired)
     */
    boolean releaseLock(String key);
}

Output:

Test Output

SOLUTION

================================================================================================================

  1. Building:

    • The solution is provided in the form of a Gradle project. It can thus be built by invoking the "build" goal so:
    $ ./gradlew clean build

    Note that this goal runs all JUnit tests included in the solution.

  2. How to use this Library:

    • This library can be used by injecting DistributedLockServiceImpl.java as a bean. As a constructor the application expects MongoClient, Mongo database connection details and inactiveLockTimeout in milliseconds.
     DistributedLockServiceImpl(final MongoClient mongoClient,
                                 final DistributedLockTimeOutOptions distributedLockTimeOutOptions,
                                 final DistributedLockServiceConfig distributedLockServiceConfig) {
        this.mongoClient = mongoClient;
        this.distributedLockTimeOutOptions = distributedLockTimeOutOptions;
        this.distributedLockServiceConfig = distributedLockServiceConfig;
      }

    There are two helpers method tryLock and releaseLock.

  3. Approach:

    • Basically when a client try to create a Lock, the application inserts lockState as Locked and track them by key/UniqueId. There is a concurrent HashMap which holds the state of distributed lock. The application states get refreshed once the client requests to release the lock.
  4. TODO:

    • Add embedded MongoDb plugin to create an integration tests for LockDao.class
    • More Unit test coverage to support the actual logic
    • Add lock heartbeat thread is responsible for sending updates to the lock doc every X seconds (when the lock is owned by the current process). This library uses missing/stale/old heartbeats to timeout locks that have not been closed properly (based on the lock/unlock) contract. This can happen when processes die unexpectedly (e.g., out of memory) or when they are not stopped properly (e.g., kill -9).
    • add LockTimeout and LockUnlocked Thread as a Monitor.

mongo-distributed-locking's People

Watchers

 avatar

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.