chenboat commented on a change in pull request #5336: URL: https://github.com/apache/incubator-pinot/pull/5336#discussion_r448548586
########## File path: pinot-core/src/main/java/org/apache/pinot/core/util/PeerServerSegmentFinder.java ########## @@ -0,0 +1,111 @@ +/** + * 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.pinot.core.util; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.collections.ListUtils; +import org.apache.helix.HelixAdmin; +import org.apache.helix.HelixManager; +import org.apache.helix.model.ExternalView; +import org.apache.helix.model.InstanceConfig; +import org.apache.pinot.common.utils.CommonConstants; +import org.apache.pinot.common.utils.LLCSegmentName; +import org.apache.pinot.common.utils.StringUtil; +import org.apache.pinot.common.utils.helix.HelixHelper; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.utils.builder.TableNameBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * PeerServerSegmentFinder discovers all the servers having the input segment in a ONLINE state through external view of + * a Pinot table. + */ +public class PeerServerSegmentFinder { + private static final Logger _logger = LoggerFactory.getLogger(PeerServerSegmentFinder.class); + /** + * + * @param segmentName + * @param downloadScheme Can be either http or https. + * @param helixManager + * @return a list of uri strings of the form http(s)://hostname:port/segments/tablenameWithType/segmentName + * for the servers hosting ONLINE segments; empty list if no such server found. + */ + public static List<URI> getPeerServerURIs(String segmentName, String downloadScheme, HelixManager helixManager) { + LLCSegmentName llcSegmentName = new LLCSegmentName(segmentName); + String tableNameWithType = + TableNameBuilder.forType(TableType.REALTIME).tableNameWithType(llcSegmentName.getTableName()); + + HelixAdmin helixAdmin = helixManager.getClusterManagmentTool(); + String clusterName = helixManager.getClusterName(); + if (clusterName == null) { + _logger.error("ClusterName not found"); + return ListUtils.EMPTY_LIST; + } + ExternalView externalViewForResource = + HelixHelper.getExternalViewForResource(helixAdmin, clusterName, tableNameWithType); + if (externalViewForResource == null) { + _logger.warn("External View not found for table {}", tableNameWithType); + return ListUtils.EMPTY_LIST; + } + List<URI> onlineServerURIs = new ArrayList<>(); + // Find out the ONLINE server serving the segment. + for (String segment : externalViewForResource.getPartitionSet()) { Review comment: good catch. I was using the getStateMaps a few lines below. Revised. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org