Github user joshelser commented on a diff in the pull request: https://github.com/apache/phoenix/pull/236#discussion_r123116460 --- Diff: phoenix-load-balancer/src/main/java/org/apache/phoenix/loadbalancer/service/LoadBalancer.java --- @@ -0,0 +1,170 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.phoenix.loadbalancer.service; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.net.HostAndPort; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.api.UnhandledErrorListener; +import org.apache.curator.framework.recipes.cache.PathChildrenCache; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.utils.CloseableUtils; +import org.apache.hadoop.hbase.HBaseConfiguration; + +import java.net.ConnectException; +import java.util.ArrayList; +import java.util.concurrent.ThreadLocalRandom; +import java.io.Closeable; +import java.util.List; + + +public class LoadBalancer { + + private static final LoadBalanceZookeeperConf CONFIG = new LoadBalanceZookeeperConfImpl(HBaseConfiguration.create()); + private static CuratorFramework curaFramework = null; + protected static final Log LOG = LogFactory.getLog(LoadBalancer.class); + private static PathChildrenCache cache = null; + private static final LoadBalancer loadBalancer = new LoadBalancer(); + private ConnectionStateListener connectionStateListener = null; + private UnhandledErrorListener unhandledErrorListener = null; + private List<Closeable> closeAbles = Lists.newArrayList(); + + private LoadBalancer() { + try { + start(); + }catch(Exception ex){ + LOG.error("Exception while creating a zookeeper clients and cache",ex); + if ((curaFramework != null) && (connectionStateListener != null)){ + curaFramework.getConnectionStateListenable() + .removeListener(connectionStateListener); + } + if ((curaFramework != null) && (unhandledErrorListener != null)){ + curaFramework.getUnhandledErrorListenable() + .removeListener(unhandledErrorListener); + } + for (Closeable closeable : closeAbles) { + CloseableUtils.closeQuietly(closeable); + } + } + } + + + + /** + * Return Singleton Load Balancer every single time. + * @return LoadBalancer + */ + public static LoadBalancer getLoadBalancer() { + return loadBalancer; + } + + /** + * return the location of Phoenix Query Server --- End diff -- Javadoc could use some updating to reflect the use of HostAndPort and alert users that they will see an exception if there are no registered PQS instances.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---