ankitsultana commented on code in PR #15203: URL: https://github.com/apache/pinot/pull/15203#discussion_r2082760034
########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/BalancedInstanceSelector.java: ########## @@ -69,17 +72,18 @@ Pair<Map<String, String>, Map<String, String>> select(List<String> segments, int if (candidates == null) { continue; } - List<String> candidateInstances = new ArrayList<>(candidates.size()); - for (SegmentInstanceCandidate candidate : candidates) { - candidateInstances.add(candidate.getInstance()); - } - String selectedInstance = _adaptiveServerSelector.select(candidateInstances); + SegmentInstanceCandidate candidate = _priorityGroupInstanceSelector.select(ctx, candidates); + // If candidates is not null, candidates is always non-empty because segments with no enabled online servers + // are placed in segmentStates.getUnavailableSegments() + assert candidate != null; + _brokerMetrics.addMeteredValue(BrokerMeter.REPLICA_SEG_QUERIES, 1, Review Comment: can we avoid this metric call here? it will get run for every segment. (same in the else block) ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/BaseInstanceSelector.java: ########## @@ -119,16 +126,20 @@ abstract class BaseInstanceSelector implements InstanceSelector { _tableNameHashForFixedReplicaRouting = TableNameBuilder.extractRawTableName(tableNameWithType).hashCode() & 0x7FFFFFFF; + _priorityGroupInstanceSelector = _adaptiveServerSelector == null ? null : new PriorityGroupInstanceSelector( + _adaptiveServerSelector); Review Comment: nit: format (indent) ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/BaseInstanceSelector.java: ########## @@ -458,6 +471,19 @@ public Set<String> getServingInstances() { return _segmentStates.getServingInstances(); } + @VisibleForTesting + int getGroup(String instanceID) { + int group = DEFAULT_SERVER_REPLICA_GROUP_OF_BROKER_VIEW; + ServerInstance server = _enabledServerStore.get(instanceID); + if (server == null) { + LOGGER.warn("Failed to find server {} in the enabledServerManager when update segmentsMap for table {}", Review Comment: What are the scenarios where this can happen? ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,205 @@ +/** + * 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.broker.routing.adaptiveserverselector; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.pinot.broker.routing.instanceselector.SegmentInstanceCandidate; + + +public class PriorityGroupInstanceSelector { + + private final AdaptiveServerSelector _adaptiveServerSelector; + + private static final int SENTINEL_GROUP_OF_NON_PREFERRED_SERVERS = Integer.MAX_VALUE; + + public PriorityGroupInstanceSelector(AdaptiveServerSelector adaptiveServerSelector) { + assert adaptiveServerSelector != null; + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * <ol> + * <li>Groups all candidates by their replica group</li> + * <li>Iterates through the ordered preferred groups in priority order</li> + * <li>For the first group that has available servers, uses adaptiveServerSelector to choose one</li> + * <li>If no preferred groups have servers, falls back to selecting from remaining servers</li> + * </ol> + * + * <p>Example 1 - Preferred group has servers:</p> + * <pre> + * Candidates: + * - server1 (replica group 1) + * - server2 (replica group 2) + * - server3 (replica group 1) + * Preferred groups: [2, 1] + * Result: server2 is selected (from group 2, highest priority) + * </pre> + * + * <p>Example 2 - Fallback to second preferred group:</p> + * <pre> + * Candidates: + * - server1 (replica group 1) + * - server3 (replica group 1) + * - server4 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server1 and server3 (from group 1) + * </pre> + * + * <p>Example 3 - Fallback to non-preferred group:</p> + * <pre> + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * </pre> + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return the selected server instance as a SegmentInstanceCandidate, or null if no candidates are available + */ + public SegmentInstanceCandidate select(ServerSelectionContext ctx, Review Comment: Method needs to be marked with Nullable ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/BrokerRoutingManager.java: ########## @@ -455,7 +455,8 @@ public synchronized void buildRouting(String tableNameWithType) { InstanceSelector instanceSelector = InstanceSelectorFactory.getInstanceSelector(tableConfig, _propertyStore, _brokerMetrics, adaptiveServerSelector, _pinotConfig); - instanceSelector.init(_routableServers, idealState, externalView, preSelectedOnlineSegments); + instanceSelector.init(_routableServers, _enabledServerInstanceMap, Review Comment: nit: format (args stay on the same line until they spill the col limit) ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,205 @@ +/** + * 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.broker.routing.adaptiveserverselector; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.pinot.broker.routing.instanceselector.SegmentInstanceCandidate; + + +public class PriorityGroupInstanceSelector { + + private final AdaptiveServerSelector _adaptiveServerSelector; + + private static final int SENTINEL_GROUP_OF_NON_PREFERRED_SERVERS = Integer.MAX_VALUE; + + public PriorityGroupInstanceSelector(AdaptiveServerSelector adaptiveServerSelector) { + assert adaptiveServerSelector != null; + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * <ol> + * <li>Groups all candidates by their replica group</li> + * <li>Iterates through the ordered preferred groups in priority order</li> + * <li>For the first group that has available servers, uses adaptiveServerSelector to choose one</li> + * <li>If no preferred groups have servers, falls back to selecting from remaining servers</li> + * </ol> + * + * <p>Example 1 - Preferred group has servers:</p> + * <pre> + * Candidates: + * - server1 (replica group 1) + * - server2 (replica group 2) + * - server3 (replica group 1) + * Preferred groups: [2, 1] + * Result: server2 is selected (from group 2, highest priority) + * </pre> + * + * <p>Example 2 - Fallback to second preferred group:</p> + * <pre> + * Candidates: + * - server1 (replica group 1) + * - server3 (replica group 1) + * - server4 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server1 and server3 (from group 1) + * </pre> + * + * <p>Example 3 - Fallback to non-preferred group:</p> + * <pre> + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * </pre> + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return the selected server instance as a SegmentInstanceCandidate, or null if no candidates are available + */ + public SegmentInstanceCandidate select(ServerSelectionContext ctx, + @Nullable List<SegmentInstanceCandidate> candidates) { + assert _adaptiveServerSelector != null; + if (candidates == null || candidates.isEmpty()) { + return null; + } + // intentional copy to avoid modifying the original list; we will add Integer.MAX_VALUE + // as a sentinel value to the end of the list to ensure non-preferred servers are processed last + List<Integer> groups = new ArrayList<>(ctx.getOrderedPreferredGroups()); + if (groups.isEmpty()) { + return choose(candidates); + } + Set<Integer> groupSet = new HashSet<>(groups); + Map<Integer, List<Integer>> groupToServerPos = new HashMap<>(); Review Comment: Storing `List<Integer>` here is unnecessary right? I see that soon after this we end up using `candidates::get` anyways. From perf perspective also there shouldn't be any difference afaict -- 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: commits-unsubscr...@pinot.apache.org 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