【问题标题】:Apache Curator - Zookeeper connection loss exception, possible memory leakApache Curator - Zookeeper 连接丢失异常,可能存在内存泄漏
【发布时间】:2016-01-12 04:30:52
【问题描述】:

我一直在研究一个持续监控分布式原子长计数器的进程。它使用以下类ZkClient 的方法getCounter 每分钟监控一次。事实上,我有多个线程运行,每个线程都在监视存储在 Zookeeper 节点中的不同计数器(分布式原子长)。每个线程通过getCounter方法的参数指定计数器的路径。

public class TagserterZookeeperManager {

public enum ZkClient {
    COUNTER("10.11.18.25:2181");  // Integration URL

    private CuratorFramework client;
    private ZkClient(String servers) {
        Properties props = TagserterConfigs.ZOOKEEPER.getProperties();
        String zkFromConfig = props.getProperty("servers", "");
        if (zkFromConfig != null && !zkFromConfig.isEmpty()) {
            servers = zkFromConfig.trim();
        }
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.newClient(servers, exponentialBackoffRetry);
        client.start();
    }

    public CuratorFramework getClient() {
        return client;
    }
}

public static String buildPath(String ... node) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < node.length; i++) {
        if (node[i] != null && !node[i].isEmpty()) {
            sb.append("/");
            sb.append(node[i]);
        }
    }
    return sb.toString();
}

public static DistributedAtomicLong getCounter(String taskType, int hid, String jobId, String countType) {
    String path = buildPath(taskType, hid+"", jobId, countType);
    Builder builder = PromotedToLock.builder().lockPath(path + "/lock").retryPolicy(new ExponentialBackoffRetry(10, 10));
    DistributedAtomicLong count = new DistributedAtomicLong(ZkClient.COUNTER.getClient(), path, new RetryNTimes(5, 20), builder.build());
    return count;
}

}

在线程中,这就是我调用此方法的方式:

    DistributedAtomicLong counterTotal = TagserterZookeeperManager
                        .getCounter("testTopic", hid, jobId, "test");

现在似乎在线程运行了几个小时之后,在某个阶段我开始在尝试读取计数的getCounter 方法中收到以下org.apache.zookeeper.KeeperException$ConnectionLossException 异常:

org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /contentTaskProd 在 org.apache.zookeeper.KeeperException.create(KeeperException.java:99) 在 org.apache.zookeeper.KeeperException.create(KeeperException.java:51) 在 org.apache.zookeeper.ZooKeeper.exists(ZooKeeper.java:1045) 在 org.apache.zookeeper.ZooKeeper.exists(ZooKeeper.java:1073) 在 org.apache.curator.utils.ZKPaths.mkdirs(ZKPaths.java:215) 在 org.apache.curator.utils.EnsurePath$InitialHelper$1.call(EnsurePath.java:148) 在 org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107) 在 org.apache.curator.utils.EnsurePath$InitialHelper.ensure(EnsurePath.java:141) 在 org.apache.curator.utils.EnsurePath.ensure(EnsurePath.java:99) 在 org.apache.curator.framework.recipes.atomic.DistributedAtomicValue.getCurrentValue(DistributedAtomicValue.java:254) 在 org.apache.curator.framework.recipes.atomic.DistributedAtomicValue.get(DistributedAtomicValue.java:91) 在 org.apache.curator.framework.recipes.atomic.DistributedAtomicLong.get(DistributedAtomicLong.java:72) ...

我在一段时间内不断收到此异常,我感觉它导致了一些内部内存泄漏,最终导致 OutOfMemory 错误,整个过程退出。有谁知道这可能是什么原因?为什么 Zookeeper 会突然开始抛出连接丢失异常?进程退出后,我可以通过我编写的另一个小型控制台程序(也使用 curator)手动连接到 Zookeeper,一切看起来都很好。

【问题讨论】:

  • 嗨,你最后是怎么解决这个问题的?即使在 Curator Framework 上显式调用 close(),我似乎也遇到了同样的问题。
  • @SumitNigam 很抱歉这么晚才回复你。实际上我已经停止了那个项目,从那以后已经有一段时间了。事实证明,出于各种其他原因,我们可能需要重写和重构项目的主要部分。对此感到抱歉。

标签: java memory-leaks distributed-computing apache-curator apache-zookeeper


【解决方案1】:

为了使用curator 监控 Zookeeper 中的节点,您可以使用 NodeCache 这不会解决您的连接问题....但是不是每分钟轮询一次节点,您可以获得推送事件它改变。

根据我的经验,NodeCache 可以很好地处理断开连接和恢复连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多