ritegarg commented on code in PR #2075: URL: https://github.com/apache/phoenix/pull/2075#discussion_r2004579112
########## phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HAGroupStoreClient.java: ########## @@ -0,0 +1,183 @@ +/* + * 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.jdbc; + + +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.PathChildrenCache; +import org.apache.curator.utils.ZKPaths; +import org.apache.hadoop.conf.Configuration; +import org.apache.phoenix.thirdparty.com.google.common.collect.ImmutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Closeable; +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type.INITIALIZED; + + +/** + * Write-through cache for HAGroupStore. + * Uses {@link PathChildrenCache} from {@link org.apache.curator.framework.CuratorFramework}. + */ +public class HAGroupStoreClient implements Closeable { + + private static final long HA_CACHE_INITIALIZATION_TIMEOUT_MS = 30000L; + private static HAGroupStoreClient cacheInstance; + private final PhoenixHAAdmin phoenixHaAdmin; + private static final Logger LOGGER = LoggerFactory.getLogger(HAGroupStoreClient.class); + // Map contains <ClusterRole, Map<HAGroupName(String), ClusterRoleRecord>> + private final ConcurrentHashMap<ClusterRoleRecord.ClusterRole, ConcurrentHashMap<String, ClusterRoleRecord>> clusterRoleToCRRMap + = new ConcurrentHashMap<>(); + private final PathChildrenCache pathChildrenCache; + private boolean isHealthy; + + /** + * Creates/gets an instance of HAGroupStoreClient. + * + * @param conf configuration + * @return cache + */ + public static HAGroupStoreClient getInstance(Configuration conf) throws Exception { + HAGroupStoreClient result = cacheInstance; + if (result == null) { + synchronized (HAGroupStoreClient.class) { + result = cacheInstance; + if (result == null) { + cacheInstance = result = new HAGroupStoreClient(conf); + } + } + } + return result; + } + + private HAGroupStoreClient(final Configuration conf) throws Exception { + this.phoenixHaAdmin = new PhoenixHAAdmin(conf); + final PathChildrenCache pathChildrenCache = new PathChildrenCache(phoenixHaAdmin.getCurator(), ZKPaths.PATH_SEPARATOR, true); + final CountDownLatch latch = new CountDownLatch(1); + pathChildrenCache.getListenable().addListener((client, event) -> { + LOGGER.info("HAGroupStoreClient PathChildrenCache Received event for type {}", event.getType()); + final ChildData childData = event.getData(); + ClusterRoleRecord eventCRR = extractCRROrNull(childData); + switch (event.getType()) { + case CHILD_ADDED: + case CHILD_UPDATED: + if (eventCRR != null && eventCRR.getHaGroupName() != null) { + updateClusterRoleRecordMap(eventCRR); + } + break; + case CHILD_REMOVED: + // In case of CHILD_REMOVED, we get the old version of data that was just deleted. + if (eventCRR != null && eventCRR.getHaGroupName() != null + && !eventCRR.getHaGroupName().isEmpty() + && eventCRR.getRole(phoenixHaAdmin.getZkUrl()) != null) { + final ClusterRoleRecord.ClusterRole role = eventCRR.getRole(phoenixHaAdmin.getZkUrl()); Review Comment: Put in CRR with different format and try 3 formats should work fine hostname1,hostname2,hostname3:port:/hbase hostname1:port,hostname2:port:/hbase hostname1\\\:port,hostname2\\\:port\\\:/hbase -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@phoenix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org