本文介绍了通过MySql/GigaSpaces/Netapp的分布式锁定服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将使用哪种分布式锁定服务?

Which distributed lock service would you use?

要求是:

  1. 可以从不同的进程/机器看到的互斥(锁定)
  2. 锁定...释放语义
  3. 在一定的超时后自动释放锁-如果锁持有人死亡,它将在X秒后自动释放
  4. Java实现
  5. 轻松部署-不得要求Netapp,MySql或GigaSpaces之外的复杂部署.必须与这些产品配合使用(尤其是GigaSpaces,这就是TerraCotta被排除的原因).
  6. 很高兴:.Net实现
  7. 如果免费,请执行以下操作:死锁检测/缓解
  1. A mutual exclusion (lock) that can be seen from different processes/machines
  2. lock...release semantics
  3. Automatic lock release after a certain timeout - if lock holder dies, it will automatically be freed after X seconds
  4. Java implementation
  5. Easy deployment - must not require complicated deployment beyond either Netapp, MySql or GigaSpaces. Must play well with those products (especially GigaSpaces - this is why TerraCotta was ruled out).
  6. Nice to have: .Net implementation
  7. If it's free: Deadlock detection / mitigation

我对诸如可以通过数据库完成"或可以通过JavaSpaces完成"之类的答案不感兴趣-我知道.相关答案应仅包含现成的,经过验证的实施.

I'm not interested in answers like "it can be done over a database", or "it can be done over JavaSpaces" - I know. Relevant answers should only contain a ready, out-of-the-box, proven implementation.

推荐答案

下面是满足您条件的基于GigaSpaces的答案的轮廓,具体取决于您在条件3中的含义.我使用的是.Net而不是Java的GigaSpaces. :

Here's the outline of a GigaSpaces based answer that meets your criteria, depending on exactly what you mean in criteria 3. I work with GigaSpaces from .Net, not Java:

创建一个具有SpaceID + SpaceRouting属性ID(锁定的内容)和DataMember bool属性Unlocked的锁定类:

Create a locking class with a SpaceID+SpaceRouting property IDing what is locked and a DataMember bool property Unlocked:

sealed public class IdLockTrans
{
    [SpaceID]
    [SpaceRouting]
    public string ID
    {
        get;
        set;
    }

    [DataMember]
    public bool Unlocked
    {
        get;
        set;
    }

    public IdLockTrans(Id id)
    {
        ID = id.ID;
    }

    public IdLockTrans()
    {
    }
}

一定时间后,您将使用GigaSpaces的租约时间进行锁定释放暂停.这将导致GigaSpaces删除IdLockTrans对象他们闲置超时后会自动从空间中移出时期.缺少ID的IdLockTrans意味着该ID已解锁.

You will use GigaSpaces' lease time for lock release after a certaintimeout. This will cause GigaSpaces to remove IdLockTrans objectsfrom the space automatically after they have been idle for the timeoutperiod. The lack of an IdLockTrans for an ID means the ID is unlocked.

您的储物柜类将定义并初始化这些类成员

Your locker class will define and initialize these class members

    private readonly ISpaceProxy _proxy;
    private readonly long _leaseTime;

    public override bool Lock(Id id, long timeout)
    {
        bool locked;
        IdLockTrans lockTransTemplate = new IdLockTrans(id);
        // Assume that this is a new id.
        try
        {
            _proxy.Write(lockTransTemplate, null, _leaseTime, 0, UpdateModifiers.WriteOnly);
            locked = true;
        }
        catch (EntryAlreadyInSpaceException)
        {
            using (ITransaction tx = _proxy.CreateLocalTransaction())
            {
                try
                {
                    lockTransTemplate.Unlocked = true;
                    IdLockTrans lockTrans = _proxy.Take(lockTransTemplate, tx, timeout);
                    locked = (lockTrans != null);
                    if (lockTrans != null)
                    {
                        lockTrans.Unlocked = false;
                        _proxy.Write(lockTrans, tx, _leaseTime, 0, UpdateModifiers.WriteOnly);
                    }
                    tx.Commit();
                }
                catch
                {
                    tx.Abort();
                    throw;
                }
            }
        }
        return locked;
    }

    public override void Unlock(Id lockedId)
    {
        IdLockTrans lockTrans = new IdLockTrans(lockedId);
        lockTrans.Unlocked = true;
        try
        {
            _proxy.Update(lockTrans, null, _leaseTime, 0, UpdateModifiers.UpdateOnly);
        }
        catch (EntryNotInSpaceException)
        {
            throw new Exception("IdLockTrans for " + lockTrans.ID
                + " not found on Unlock. Lock time exceeded lease time.");
        }
    }

这篇关于通过MySql/GigaSpaces/Netapp的分布式锁定服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 00:48