- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.9k
A68 random subsetting (part 1) #12377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Zgoda91
  wants to merge
  20
  commits into
  grpc:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Zgoda91:A68_random_subsetting
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            20 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8d349df
              
                Moved `XxHash64` library to enable shared use across projects
              
              
                Zgoda91 8ec136a
              
                Implemented random_subsetting LB policy
              
              
                Zgoda91 1870e6f
              
                Implemented random_subsetting converter for xDS
              
              
                Zgoda91 40af1a8
              
                Optimized seed lifetime
              
              
                Zgoda91 d78cf20
              
                Optimized memory allocations for arrays
              
              
                Zgoda91 4dfa2e7
              
                Changed visibility of LB to package-private
              
              
                Zgoda91 f5147ee
              
                Revert "Moved `XxHash64` library to enable shared use across projects"
              
              
                Zgoda91 e404564
              
                Changed hashing function to `murmur3_128`
              
              
                Zgoda91 7684629
              
                Removed leftover dependency from `BUILD.bazel`
              
              
                Zgoda91 4fbf81d
              
                Changed hash code comparator
              
              
                Zgoda91 5745d0a
              
                Improved test assertion to get better failure message
              
              
                Zgoda91 eb06b9e
              
                Excluded new LB from javadoc
              
              
                Zgoda91 e168032
              
                Improved `subsetSize` parsing
              
              
                Zgoda91 56d33a7
              
                Adjusted expectation for flaky test
              
              
                Zgoda91 04c4beb
              
                Changed way of storing hashes
              
              
                Zgoda91 9163438
              
                Improved config builder
              
              
                Zgoda91 9534d22
              
                Updated xDS converting function
              
              
                Zgoda91 94be9ac
              
                Updated status for subset size parsing failure
              
              
                Zgoda91 370570b
              
                Added deterministic seed setup for LB tests
              
              
                Zgoda91 000e2bb
              
                Reverted xDS changes
              
              
                Zgoda91 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test") | ||
|  | ||
| java_library( | ||
| name = "zero-allocation-hashing", | ||
| srcs = [ | ||
| "src/main/java/io/grpc/tp/zah/XxHash64.java", | ||
| ], | ||
| deps = [ | ||
| "@maven//:com_google_guava_guava", | ||
| ], | ||
| visibility = [ | ||
| "//xds:__pkg__", | ||
| "//util:__pkg__", | ||
| ], | ||
| ) | ||
|  | ||
| java_test( | ||
| name = "XxHash64Test", | ||
| size = "small", | ||
| srcs = ["src/test/java/io/grpc/tp/zah/XxHash64Test.java"], | ||
| deps = [ | ||
| ":zero-allocation-hashing", | ||
| "@maven//:com_google_guava_guava", | ||
| "@maven//:junit_junit", | ||
| ], | ||
| ) | 
            File renamed without changes.
          
    
            File renamed without changes.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| plugins { | ||
| id "java-library" | ||
| } | ||
|  | ||
| description = 'gRPC: Zero Allocation Hashing' | ||
|  | ||
| dependencies { | ||
| implementation libraries.guava | ||
|  | ||
| testImplementation libraries.junit | ||
| } | ||
|  | ||
| tasks.named("jar").configure { | ||
| manifest { | ||
| attributes('Automatic-Module-Name': 'io.grpc.tp.zah') | ||
| } | ||
| } | ||
|  | ||
| tasks.named("checkstyleMain").configure { | ||
| enabled = false | ||
| } | ||
|  | ||
| tasks.named("checkstyleTest").configure { | ||
| enabled = false | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            158 changes: 158 additions & 0 deletions
          
          158 
        
  util/src/main/java/io/grpc/util/RandomSubsettingLoadBalancer.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright 2025 The gRPC Authors | ||
| * | ||
| * Licensed 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 io.grpc.util; | ||
|  | ||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|  | ||
| import io.grpc.EquivalentAddressGroup; | ||
| import io.grpc.Internal; | ||
| import io.grpc.LoadBalancer; | ||
| import io.grpc.Status; | ||
| import io.grpc.tp.zah.XxHash64; | ||
| import java.security.SecureRandom; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
|  | ||
|  | ||
| /** | ||
| * Wraps a child {@code LoadBalancer}, separating the total set of backends into smaller subsets for | ||
| * the child balancer to balance across. | ||
| * | ||
| * <p>This implements random subsetting gRFC: | ||
| * https://https://github.com/grpc/proposal/blob/master/A68-random-subsetting.md | ||
| */ | ||
| @Internal | ||
| public final class RandomSubsettingLoadBalancer extends LoadBalancer { | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| private final GracefulSwitchLoadBalancer switchLb; | ||
|  | ||
| public RandomSubsettingLoadBalancer(Helper helper) { | ||
| switchLb = new GracefulSwitchLoadBalancer(checkNotNull(helper, "helper")); | ||
| } | ||
|  | ||
| @Override | ||
| public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) { | ||
| RandomSubsettingLoadBalancerConfig config = | ||
| (RandomSubsettingLoadBalancerConfig) | ||
| resolvedAddresses.getLoadBalancingPolicyConfig(); | ||
|  | ||
| ResolvedAddresses subsetAddresses = filterEndpoints( | ||
| resolvedAddresses, config.subsetSize, new SecureRandom().nextLong()); | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| return switchLb.acceptResolvedAddresses( | ||
| subsetAddresses.toBuilder() | ||
| .setLoadBalancingPolicyConfig(config.childConfig) | ||
| .build()); | ||
| } | ||
|  | ||
| // implements the subsetting algorithm, as described in A68: | ||
| // https://github.com/grpc/proposal/pull/423 | ||
| private ResolvedAddresses filterEndpoints( | ||
| ResolvedAddresses resolvedAddresses, long subsetSize, long seed) { | ||
| // configured subset sizes in the range [Integer.MAX_VALUE, Long.MAX_VALUE] will always fall | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // into this if statement due to collection indexing limitations in JVM | ||
| if (subsetSize >= resolvedAddresses.getAddresses().size()) { | ||
| return resolvedAddresses; | ||
| } | ||
|  | ||
| XxHash64 hashFunc = new XxHash64(seed); | ||
| ArrayList<EndpointWithHash> endpointWithHashList = new ArrayList<>(); | ||
|  | ||
| for (EquivalentAddressGroup addressGroup : resolvedAddresses.getAddresses()) { | ||
| endpointWithHashList.add( | ||
| new EndpointWithHash( | ||
| addressGroup, | ||
| hashFunc.hashAsciiString(addressGroup.getAddresses().get(0).toString()))); | ||
| } | ||
|  | ||
| Collections.sort(endpointWithHashList, new HashAddressComparator()); | ||
|  | ||
| ArrayList<EquivalentAddressGroup> addressGroups = new ArrayList<>(); | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| // for loop is executed for subset sizes in range [0, Integer.MAX_VALUE), therefore indexing | ||
| // variable is not going to overflow here | ||
| for (int idx = 0; idx < subsetSize; ++idx) { | ||
| addressGroups.add(endpointWithHashList.get(idx).addressGroup); | ||
| } | ||
|  | ||
| return resolvedAddresses.toBuilder().setAddresses(addressGroups).build(); | ||
| } | ||
|  | ||
| @Override | ||
| public void handleNameResolutionError(Status error) { | ||
| switchLb.handleNameResolutionError(error); | ||
| } | ||
|  | ||
| @Override | ||
| public void shutdown() { | ||
| switchLb.shutdown(); | ||
| } | ||
|  | ||
| private static final class EndpointWithHash { | ||
| public final EquivalentAddressGroup addressGroup; | ||
| public final long hash; | ||
|  | ||
| public EndpointWithHash(EquivalentAddressGroup addressGroup, long hash) { | ||
| this.addressGroup = addressGroup; | ||
| this.hash = hash; | ||
| } | ||
| } | ||
|  | ||
| private static final class HashAddressComparator implements Comparator<EndpointWithHash> { | ||
| @Override | ||
| public int compare(EndpointWithHash lhs, EndpointWithHash rhs) { | ||
| return Long.compare(lhs.hash, rhs.hash); | ||
| } | ||
| } | ||
|  | ||
| public static final class RandomSubsettingLoadBalancerConfig { | ||
| public final long subsetSize; | ||
| public final Object childConfig; | ||
|  | ||
| private RandomSubsettingLoadBalancerConfig(long subsetSize, Object childConfig) { | ||
| this.subsetSize = subsetSize; | ||
| this.childConfig = childConfig; | ||
| } | ||
|  | ||
| public static class Builder { | ||
| Long subsetSize; | ||
| Object childConfig; | ||
|  | ||
| public Builder setSubsetSize(Integer subsetSize) { | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| checkNotNull(subsetSize, "subsetSize"); | ||
| // {@code Integer.toUnsignedLong(int)} is not part of Android API level 21, therefore doing | ||
|         
                  Zgoda91 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // it manually | ||
| Long subsetSizeAsLong = ((long) subsetSize) & 0xFFFFFFFFL; | ||
| checkArgument(subsetSizeAsLong > 0L, "Subset size must be greater than 0"); | ||
| this.subsetSize = subsetSizeAsLong; | ||
| return this; | ||
| } | ||
|  | ||
| public Builder setChildConfig(Object childConfig) { | ||
| this.childConfig = checkNotNull(childConfig, "childConfig"); | ||
| return this; | ||
| } | ||
|  | ||
| public RandomSubsettingLoadBalancerConfig build() { | ||
| return new RandomSubsettingLoadBalancerConfig( | ||
| checkNotNull(subsetSize, "subsetSize"), | ||
| checkNotNull(childConfig, "childConfig")); | ||
| } | ||
| } | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.