gortiz commented on code in PR #15203: URL: https://github.com/apache/pinot/pull/15203#discussion_r2041666301
########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,194 @@ +/** + * 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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +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) { + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * 1. Groups all candidates by their replica group + * 2. Iterates through the ordered preferred groups in priority order + * 3. For the first group that has available servers, uses adaptiveServerSelector to choose one + * 4. If no preferred groups have servers, falls back to selecting from remaining servers + * + * Example 1 - Preferred group has servers: + * 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) + * + * Example 2 - Fallback to second preferred group: + * 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) + * + * Example 3 - Fallback to non-preferred group: + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return an Optional containing the selected server instance, or empty if no candidates are available + */ + public Optional<SegmentInstanceCandidate> select(ServerSelectionContext ctx, + List<SegmentInstanceCandidate> candidates) { + assert _adaptiveServerSelector != null; Review Comment: I don't think this assert is correct here. We should either remove it (we assume something that is not explicitly marked as nullable is not nullable) or move it to the constructor so that in case this object is built with a null adaptative server selector, it fails at build time instead of at select. ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,194 @@ +/** + * 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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +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) { + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * 1. Groups all candidates by their replica group + * 2. Iterates through the ordered preferred groups in priority order + * 3. For the first group that has available servers, uses adaptiveServerSelector to choose one + * 4. If no preferred groups have servers, falls back to selecting from remaining servers + * + * Example 1 - Preferred group has servers: + * 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) + * + * Example 2 - Fallback to second preferred group: + * 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) + * + * Example 3 - Fallback to non-preferred group: + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return an Optional containing the selected server instance, or empty if no candidates are available + */ Review Comment: This is not going to be correctly rendered in Javadoc. I would recommend to use actual javadoc tags (ie HTML) or use the new markdown javadoc (starting lines with `///`) ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,194 @@ +/** + * 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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +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) { + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * 1. Groups all candidates by their replica group + * 2. Iterates through the ordered preferred groups in priority order + * 3. For the first group that has available servers, uses adaptiveServerSelector to choose one + * 4. If no preferred groups have servers, falls back to selecting from remaining servers + * + * Example 1 - Preferred group has servers: + * 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) + * + * Example 2 - Fallback to second preferred group: + * 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) + * + * Example 3 - Fallback to non-preferred group: + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return an Optional containing the selected server instance, or empty if no candidates are available + */ + public Optional<SegmentInstanceCandidate> select(ServerSelectionContext ctx, + List<SegmentInstanceCandidate> candidates) { Review Comment: Please mark the List with `@Nullable` ########## pinot-broker/src/main/java/org/apache/pinot/broker/routing/adaptiveserverselector/PriorityGroupInstanceSelector.java: ########## @@ -0,0 +1,194 @@ +/** + * 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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +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) { + _adaptiveServerSelector = adaptiveServerSelector; + } + + /** + * Selects a server instance from the given candidates based on replica group preferences. + * The selection process follows these steps: + * 1. Groups all candidates by their replica group + * 2. Iterates through the ordered preferred groups in priority order + * 3. For the first group that has available servers, uses adaptiveServerSelector to choose one + * 4. If no preferred groups have servers, falls back to selecting from remaining servers + * + * Example 1 - Preferred group has servers: + * 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) + * + * Example 2 - Fallback to second preferred group: + * 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) + * + * Example 3 - Fallback to non-preferred group: + * Candidates: + * - server4 (replica group 3) + * - server5 (replica group 3) + * Preferred groups: [2, 1] + * Result: adaptiveServerSelector chooses between server4 and server5 (from group 3) + * + * @param ctx the server selection context containing ordered preferred groups + * @param candidates the list of server candidates to choose from + * @return an Optional containing the selected server instance, or empty if no candidates are available + */ + public Optional<SegmentInstanceCandidate> select(ServerSelectionContext ctx, Review Comment: nit: Usually we don't use Optional but normal classes annotated with `@Nullable` -- 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