Class BalancedInstanceSelector

  • All Implemented Interfaces:
    InstanceSelector

    public class BalancedInstanceSelector
    extends Object
    Instance selector to balance the number of segments served by each selected server instance.

    If AdaptiveServerSelection is enabled, the request is routed to the best available server for a segment when it is processed below. This is a best effort approach in distributing the query to all available servers. If some servers are performing poorly, they might not end up being picked for any of the segments. For example, there's a query for Segments 1 (Seg1), 2 (Seg2) and Seg3). The servers are S1, S2, S3. The algorithm works as follows: Step1: Process seg1. Fetch server rankings. Pick the best server. Step2: Process seg2. Fetch server rankings (could have changed or not since Step 1). Pick the best server. Step3: Process seg3. Fetch server rankings (could have changed or not since Step 2). Pick the best server.

    If AdaptiveServerSelection is disabled, the selection algorithm will always evenly distribute the traffic to all replicas of each segment, and will try to select different replica id for each segment. The algorithm is very light-weight and will do best effort to balance the number of segments served by each selected server instance.

    • Method Summary

      Modifier and Type Method Description
      void init​(Set<String> enabledInstances, org.apache.helix.model.IdealState idealState, org.apache.helix.model.ExternalView externalView, Set<String> onlineSegments)
      Initializes the instance selector with the enabled instances, ideal state, external view and online segments (segments with ONLINE/CONSUMING instances in the ideal state and pre-selected by the SegmentPreSelector).
      void onAssignmentChange​(org.apache.helix.model.IdealState idealState, org.apache.helix.model.ExternalView externalView, Set<String> onlineSegments)
      Processes the segment assignment (ideal state or external view) change based on the given online segments (segments with ONLINE/CONSUMING instances in the ideal state and pre-selected by the SegmentPreSelector).
      void onInstancesChange​(Set<String> enabledInstances, List<String> changedInstances)
      Processes the instances change.
      InstanceSelector.SelectionResult select​(org.apache.pinot.common.request.BrokerRequest brokerRequest, List<String> segments, long requestId)
      Selects the server instances for the given segments queried by the given broker request, returns a map from segment to selected server instance hosting the segment and a set of unavailable segments (no enabled instance or all enabled instances are in ERROR state).
    • Constructor Detail

      • BalancedInstanceSelector

        public BalancedInstanceSelector​(String tableNameWithType,
                                        org.apache.pinot.common.metrics.BrokerMetrics brokerMetrics,
                                        @Nullable
                                        AdaptiveServerSelector adaptiveServerSelector)
    • Method Detail

      • init

        public void init​(Set<String> enabledInstances,
                         org.apache.helix.model.IdealState idealState,
                         org.apache.helix.model.ExternalView externalView,
                         Set<String> onlineSegments)
        Description copied from interface: InstanceSelector
        Initializes the instance selector with the enabled instances, ideal state, external view and online segments (segments with ONLINE/CONSUMING instances in the ideal state and pre-selected by the SegmentPreSelector). Should be called only once before calling other methods.
        Specified by:
        init in interface InstanceSelector
      • onInstancesChange

        public void onInstancesChange​(Set<String> enabledInstances,
                                      List<String> changedInstances)
        Processes the instances change. Changed instances are pre-computed based on the current and previous enabled instances only once on the caller side and passed to all the instance selectors.

        Updates the cached enabled instances and re-calculates segmentToEnabledInstancesMap and unavailableSegments based on the cached states.

        Specified by:
        onInstancesChange in interface InstanceSelector
      • onAssignmentChange

        public void onAssignmentChange​(org.apache.helix.model.IdealState idealState,
                                       org.apache.helix.model.ExternalView externalView,
                                       Set<String> onlineSegments)
        Processes the segment assignment (ideal state or external view) change based on the given online segments (segments with ONLINE/CONSUMING instances in the ideal state and pre-selected by the SegmentPreSelector).

        Updates the cached maps (segmentToOnlineInstancesMap, segmentToOfflineInstancesMap and instanceToSegmentsMap) and re-calculates segmentToEnabledInstancesMap and unavailableSegments based on the cached states.

        Specified by:
        onAssignmentChange in interface InstanceSelector
      • select

        public InstanceSelector.SelectionResult select​(org.apache.pinot.common.request.BrokerRequest brokerRequest,
                                                       List<String> segments,
                                                       long requestId)
        Description copied from interface: InstanceSelector
        Selects the server instances for the given segments queried by the given broker request, returns a map from segment to selected server instance hosting the segment and a set of unavailable segments (no enabled instance or all enabled instances are in ERROR state).
        Specified by:
        select in interface InstanceSelector
        Parameters:
        brokerRequest - BrokerRequest for the query
        segments - segments for which instance needs to be selected
        requestId - requestId generated by the Broker for a query
        Returns:
        instance of SelectionResult which describes the instance to pick for a given segment