|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pinot.systemtable.provider; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import javax.annotation.Nullable; |
| 24 | +import org.apache.helix.HelixAdmin; |
| 25 | +import org.apache.helix.model.InstanceConfig; |
| 26 | +import org.apache.pinot.common.utils.config.InstanceUtils; |
| 27 | +import org.apache.pinot.spi.utils.InstanceTypeUtils; |
| 28 | +import org.slf4j.Logger; |
| 29 | + |
| 30 | + |
| 31 | +final class HelixControllerUtils { |
| 32 | + private HelixControllerUtils() { |
| 33 | + } |
| 34 | + |
| 35 | + static List<String> discoverControllerBaseUrls(HelixAdmin helixAdmin, String clusterName, Logger logger) { |
| 36 | + List<String> urls = new ArrayList<>(); |
| 37 | + List<String> instanceIds; |
| 38 | + try { |
| 39 | + instanceIds = helixAdmin.getInstancesInCluster(clusterName); |
| 40 | + } catch (Exception e) { |
| 41 | + logger.warn("Failed to list instances in cluster '{}' while discovering controllers", clusterName, e); |
| 42 | + return List.of(); |
| 43 | + } |
| 44 | + for (String instanceId : instanceIds) { |
| 45 | + if (!InstanceTypeUtils.isController(instanceId)) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + String baseUrl = getInstanceBaseUrl(helixAdmin, clusterName, instanceId, logger); |
| 49 | + if (baseUrl != null) { |
| 50 | + urls.add(baseUrl); |
| 51 | + } |
| 52 | + } |
| 53 | + return urls; |
| 54 | + } |
| 55 | + |
| 56 | + private static @Nullable String getInstanceBaseUrl(HelixAdmin helixAdmin, String clusterName, String instanceId, |
| 57 | + Logger logger) { |
| 58 | + InstanceConfig instanceConfig; |
| 59 | + try { |
| 60 | + instanceConfig = helixAdmin.getInstanceConfig(clusterName, instanceId); |
| 61 | + } catch (Exception e) { |
| 62 | + logger.warn("Failed to fetch Helix InstanceConfig for instance '{}' in cluster '{}'", instanceId, clusterName, e); |
| 63 | + return null; |
| 64 | + } |
| 65 | + if (instanceConfig == null) { |
| 66 | + logger.warn("Missing Helix InstanceConfig for instance '{}' in cluster '{}'", instanceId, clusterName); |
| 67 | + return null; |
| 68 | + } |
| 69 | + String baseUrl = InstanceUtils.getInstanceBaseUri(instanceConfig); |
| 70 | + if (baseUrl.isEmpty()) { |
| 71 | + logger.warn("Failed to build instance base URL from Helix InstanceConfig for instance '{}' in cluster '{}'", |
| 72 | + instanceId, clusterName); |
| 73 | + return null; |
| 74 | + } |
| 75 | + return baseUrl; |
| 76 | + } |
| 77 | +} |
0 commit comments