Package com.linkedin.helix.util

Source Code of com.linkedin.helix.util.ZKClientPool

/**
* Copyright (C) 2012 LinkedIn Inc <opensource@linkedin.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.linkedin.helix.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.zookeeper.ZooKeeper.States;

import com.linkedin.helix.manager.zk.ZNRecordSerializer;
import com.linkedin.helix.manager.zk.ZkClient;

public class ZKClientPool
{
  static final Map<String, ZkClient> _zkClientMap = new ConcurrentHashMap<String, ZkClient>();
  static final int DEFAULT_SESSION_TIMEOUT = 30 * 1000;

  public static ZkClient getZkClient(String zkServer)
  {
    // happy path that we cache the zkclient and it's still connected
    if (_zkClientMap.containsKey(zkServer))
    {
      ZkClient zkClient = _zkClientMap.get(zkServer);
      if (zkClient.getConnection().getZookeeperState() == States.CONNECTED)
      {
        return zkClient;
      }
    }

    synchronized (_zkClientMap)
    {
      // if we cache a stale zkclient, purge it
      if (_zkClientMap.containsKey(zkServer))
      {
        ZkClient zkClient = _zkClientMap.get(zkServer);
        if (zkClient.getConnection().getZookeeperState() != States.CONNECTED)
        {
          _zkClientMap.remove(zkServer);
        }
      }

      // get a new zkclient
      if (!_zkClientMap.containsKey(zkServer))
      {
        ZkClient zkClient = new ZkClient(zkServer, DEFAULT_SESSION_TIMEOUT,
            ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());

        _zkClientMap.put(zkServer, zkClient);
      }
      return _zkClientMap.get(zkServer);
    }
  }

  public static void reset()
  {
    _zkClientMap.clear();
  }
}
TOP

Related Classes of com.linkedin.helix.util.ZKClientPool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.