Skip to content
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

feat(lb): deterministic subsetting algorithm #1289

Merged
merged 12 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.core.env.PropertyResolver;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedCaseInsensitiveMap;

Expand All @@ -40,6 +42,7 @@
*
* @author Olga Maciaszek-Sharma
* @author Gandhimathi Velusamy
* @author Zhuozhi Ji
* @since 2.2.1
*/
public class LoadBalancerProperties {
Expand Down Expand Up @@ -85,6 +88,12 @@ public class LoadBalancerProperties {
*/
private boolean callGetWithRequestOnDelegates = true;

/**
jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
* Properties for
* {@link org.springframework.cloud.loadbalancer.core.SubsetServiceInstanceListSupplier}.
*/
private Subset subset = new Subset();

public HealthCheck getHealthCheck() {
return healthCheck;
}
Expand Down Expand Up @@ -142,6 +151,14 @@ public boolean isCallGetWithRequestOnDelegates() {
return callGetWithRequestOnDelegates;
}

public Subset getSubset() {
return subset;
}

public void setSubset(Subset subset) {
this.subset = subset;
}

public void setCallGetWithRequestOnDelegates(boolean callGetWithRequestOnDelegates) {
this.callGetWithRequestOnDelegates = callGetWithRequestOnDelegates;
}
Expand Down Expand Up @@ -490,4 +507,35 @@ public void setEnabled(boolean enabled) {

}

public static class Subset {

/**
* Instance id of deterministic subsetting. If not set,
* {@link IdUtils#getDefaultInstanceId(PropertyResolver)} will be used.
*/
private String instanceId = "";

/**
* Max subset size of deterministic subsetting.
*/
private int size = 100;

public String getInstanceId() {
return instanceId;
}

public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public ServiceInstanceListSupplier weightedServiceInstanceListSupplier(Configura
.build(context);
}

@Bean
jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnMissingBean
@Conditional(SubsetConfigurationCondition.class)
public ServiceInstanceListSupplier subsetServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withSubset().withCaching()
.build(context);
}

}

@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -353,4 +362,14 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)

}

static class SubsetConfigurationCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return LoadBalancerEnvironmentPropertyUtils.equalToForClientOrDefault(context.getEnvironment(),
"configurations", "subset");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertyResolver;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -301,6 +302,16 @@ public ServiceInstanceListSupplierBuilder withHints() {
return this;
}

public ServiceInstanceListSupplierBuilder withSubset() {
DelegateCreator creator = (context, delegate) -> {
PropertyResolver resolver = context.getBean(PropertyResolver.class);
LoadBalancerClientFactory factory = context.getBean(LoadBalancerClientFactory.class);
return new SubsetServiceInstanceListSupplier(delegate, resolver, factory);
};
creators.add(creator);
return this;
}

jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
/**
* Support {@link ServiceInstanceListSupplierBuilder} can be added to the expansion
* implementation of {@link ServiceInstanceListSupplier} by this method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2012-2023 the original author or 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
*
* https://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.springframework.cloud.loadbalancer.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import reactor.core.publisher.Flux;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;

/**
* A {@link ServiceInstanceListSupplier} implementation that uses
* <a href="https://sre.google/sre-book/load-balancing-datacenter/">deterministic
* subsetting algorithm</a> to limit the number of instances provided by delegate.
*
* @author Zhuozhi Ji
* @since 4.1.0
*/
jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
public class SubsetServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {

private final String instanceId;

private final int size;

public SubsetServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, PropertyResolver resolver,
ReactiveLoadBalancer.Factory<ServiceInstance> factory) {
super(delegate);
LoadBalancerProperties properties = factory.getProperties(getServiceId());
this.instanceId = resolveInstanceId(properties, resolver);
this.size = properties.getSubset().getSize();
}

@Override
public Flux<List<ServiceInstance>> get() {
jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
return delegate.get().map(instances -> {
if (instances.size() <= size) {
return instances;
}

instances = new ArrayList<>(instances);

int instanceId = this.instanceId.hashCode() & Integer.MAX_VALUE;
int count = instances.size() / size;
int round = instanceId / count;

Random random = new Random(round);
Collections.shuffle(instances, random);

int bucket = instanceId % count;
int start = bucket * size;
return instances.subList(start, start + size);
});
}

private static String resolveInstanceId(LoadBalancerProperties properties, PropertyResolver resolver) {
String instanceId = properties.getSubset().getInstanceId();
if (StringUtils.hasText(instanceId)) {
return resolver.resolvePlaceholders(properties.getSubset().getInstanceId());
}
return IdUtils.getDefaultInstanceId(resolver);
}

public String getInstanceId() {
return instanceId;
}

public int getSize() {
return size;
}

}
Loading
Loading