Distributed Concurrency Control

前端 未结 13 1530
栀梦
栀梦 2021-01-29 20:35

I\'ve been working on this for a few days now, and I\'ve found several solutions but none of them incredibly simple or lightweight. The problem is basically this: We have a cl

相关标签:
13条回答
  • 2021-01-29 21:14

    You might also consider Cacheonix for distributed locks. Unlike anything else mentioned here Cacheonix support ReadWrite locks with lock escalation from read to write when needed:

    ReadWriteLock rwLock = Cacheonix.getInstance().getCluster().getReadWriteLock();
    Lock lock = rwLock.getWriteLock();
    try {
      ...
    } finally {
      lock.unlock();
    }
    

    Full disclosure: I am a Cacheonix developer.

    0 讨论(0)
  • 2021-01-29 21:16

    I have done a lot of work with Coherence, which allowed several approaches to implementing a distributed lock. The naive approach was to request to lock the same logical object on all participating nodes. In Coherence terms this was locking a key on a Replicated Cache. This approach doesn't scale that well because the network traffic increases linearly as you add nodes. A smarter way was to use a Distributed Cache, where each node in the cluster is naturally responsible for a portion of the key space, so locking a key in such a cache always involved communication with at most one node. You could roll your own approach based on this idea, or better still, get Coherence. It really is the scalability toolkit of your dreams.

    I would add that any half decent multi-node network based locking mechanism would have to be reasonably sophisticated to act correctly in the event of any network failure.

    0 讨论(0)
  • 2021-01-29 21:20

    Since you are already connecting to a database, before adding another infra piece, take a look at JdbcSemaphore, it is simple to use:

    JdbcSemaphore semaphore = new JdbcSemaphore(ds, semName, maxReservations);
    boolean acq = semaphore.acquire(acquire, 1, TimeUnit.MINUTES);
    if (acq) {
     // do stuff
     semaphore.release();
    } else {
      throw new TimeoutException();
    }
    

    It is part of spf4j library.

    0 讨论(0)
  • 2021-01-29 21:22

    I made a simple RMI service with two methods: lock and release. both methods take a key (my data model used UUIDs as pk so that was also the locking key).

    RMI is a good solution for this because it's centralized. you can't do this with EJBs (specialially in a cluster as you don't know on which machine your call will land). plus, it's easy.

    it worked for me.

    0 讨论(0)
  • 2021-01-29 21:26

    Back in the day, we'd use a specific "lock server" on the network to handle this. Bleh.

    Your database server might have resources specifically for doing this kind of thing. MS-SQL Server has application locks usable through the sp_getapplock/sp_releaseapplock procedures.

    0 讨论(0)
  • 2021-01-29 21:28

    you might want to consider using Hazelcast distributed locks. Super lite and easy.

    java.util.concurrent.locks.Lock lock = Hazelcast.getLock ("mymonitor");
    lock.lock ();
    try {
    // do your stuff
    }finally {
       lock.unlock();
    }
    

    Hazelcast - Distributed Queue, Map, Set, List, Lock

    0 讨论(0)
提交回复
热议问题