【问题标题】:Cleanup for locks created using apache curator清理使用 apache curator 创建的锁
【发布时间】:2020-04-07 10:08:07
【问题描述】:

我是 Apache Curator 的新手,想确认一些与使用 Apache Curator 的分布式锁相关的事情:

  1. 在创建 InterProcessMutex 实例时,我将锁定路径提供为“lock/unique_id”。 InterProcessMutex dMutex = new InterProcessMutex(curatorClient, "lock/<id>"); 由于会根据“id”的值创建多个锁,如何确保删除旧锁。这是馆长处理的吗?如果是,这个锁会在多长时间后被删除?

  2. 如果我的JVM实例被杀死(原来是创建了InterProcessMutex对象),会不会自动删除对应的锁?

【问题讨论】:

    标签: apache-zookeeper apache-curator


    【解决方案1】:

    如果您将 Curator 与 ZooKeeper 3.5+ 一起使用,那么,是的,Curator 配方的“父”节点将创建为容器 ZNode(请参阅http://zookeeper.apache.org/doc/r3.5.7/zookeeperProgrammers.html#Container+Nodes),并在它们没有子 ZNode 的一段时间后自动删除。

    【讨论】:

    【解决方案2】:

    如果仍然相关:)

    我正在使用 ZooKeeper 3.4.10,正如@Randgalt 提到的,您必须使用已弃用的 Reaper。一个例子是:

    private void createClient(String zkConnect, int reapingThresholdMs) throws Exception {
    
     client = CuratorFrameworkFactory.newClient(zkConnect, new ExponentialBackoffRetry(1000, 3));
    
        client.start();
    
        reaper = new Reaper(client, reapingThresholdMs);
        reaper.start();
    }
    
    public DistributedLock createLock(String lockPath) {
            String fullLockPath = checkPathAndAddPrefix(lockPath);
            reaper.addPath(fullLockPath, Reaper.Mode.REAP_UNTIL_DELETE);
            return new DistributedLock(client, fullLockPath);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-02
      • 2020-08-06
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多