-
Notifications
You must be signed in to change notification settings - Fork 716
Add RandomLoadBalancer, along with tests and docs. #868
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
Merged
Merged
Changes from all commits
Commits
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
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
121 changes: 121 additions & 0 deletions
121
...alancer/src/main/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancer.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,121 @@ | ||
/* | ||
* Copyright 2012-2020 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.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.reactive.DefaultResponse; | ||
import org.springframework.cloud.client.loadbalancer.reactive.EmptyResponse; | ||
import org.springframework.cloud.client.loadbalancer.reactive.Request; | ||
import org.springframework.cloud.client.loadbalancer.reactive.Response; | ||
|
||
/** | ||
* A random-based implementation of {@link ReactorServiceInstanceLoadBalancer}. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 2.2.7 | ||
*/ | ||
public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer { | ||
|
||
private static final Log log = LogFactory.getLog(RandomLoadBalancer.class); | ||
|
||
private final String serviceId; | ||
|
||
@Deprecated | ||
private ObjectProvider<ServiceInstanceSupplier> serviceInstanceSupplier; | ||
|
||
private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider; | ||
|
||
/** | ||
* @param serviceInstanceListSupplierProvider a provider of | ||
* {@link ServiceInstanceListSupplier} that will be used to get available instances | ||
* @param serviceId id of the service for which to choose an instance | ||
*/ | ||
public RandomLoadBalancer( | ||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, | ||
String serviceId) { | ||
this.serviceId = serviceId; | ||
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider; | ||
} | ||
|
||
/** | ||
* @param serviceId id of the service for which to choose an instance | ||
* @param serviceInstanceSupplier a provider of {@link ServiceInstanceSupplier} that | ||
* will be used to get available instances | ||
* @deprecated Use {@link #RandomLoadBalancer(ObjectProvider, String)}} instead. | ||
*/ | ||
@Deprecated | ||
public RandomLoadBalancer(String serviceId, | ||
ObjectProvider<ServiceInstanceSupplier> serviceInstanceSupplier) { | ||
this.serviceId = serviceId; | ||
this.serviceInstanceSupplier = serviceInstanceSupplier; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public Mono<org.springframework.cloud.client.loadbalancer.reactive.Response<ServiceInstance>> choose( | ||
Request request) { | ||
// TODO: move supplier to Request? | ||
// Temporary conditional logic till deprecated members are removed. | ||
if (serviceInstanceListSupplierProvider != null) { | ||
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider | ||
.getIfAvailable(NoopServiceInstanceListSupplier::new); | ||
return supplier.get().next() | ||
.map(serviceInstances -> processInstanceResponse(supplier, | ||
serviceInstances)); | ||
} | ||
ServiceInstanceSupplier supplier = this.serviceInstanceSupplier | ||
.getIfAvailable(NoopServiceInstanceSupplier::new); | ||
return supplier.get().collectList().map(this::getInstanceResponse); | ||
} | ||
|
||
private org.springframework.cloud.client.loadbalancer.reactive.Response<ServiceInstance> processInstanceResponse( | ||
ServiceInstanceListSupplier supplier, | ||
List<ServiceInstance> serviceInstances) { | ||
org.springframework.cloud.client.loadbalancer.reactive.Response<ServiceInstance> serviceInstanceResponse = getInstanceResponse( | ||
serviceInstances); | ||
if (supplier instanceof SelectedInstanceCallback | ||
&& serviceInstanceResponse.hasServer()) { | ||
((SelectedInstanceCallback) supplier) | ||
.selectedServiceInstance(serviceInstanceResponse.getServer()); | ||
} | ||
return serviceInstanceResponse; | ||
} | ||
|
||
private Response<ServiceInstance> getInstanceResponse( | ||
List<ServiceInstance> instances) { | ||
if (instances.isEmpty()) { | ||
if (log.isWarnEnabled()) { | ||
log.warn("No servers available for service: " + serviceId); | ||
} | ||
return new EmptyResponse(); | ||
} | ||
int index = ThreadLocalRandom.current().nextInt(instances.size()); | ||
|
||
ServiceInstance instance = instances.get(index); | ||
|
||
return new DefaultResponse(instance); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...er/src/test/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancerTests.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,81 @@ | ||
/* | ||
* Copyright 2012-2020 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.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Flux; | ||
|
||
import org.springframework.cloud.client.DefaultServiceInstance; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.Response; | ||
import org.springframework.cloud.loadbalancer.support.SimpleObjectProvider; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Tests for {@link RandomLoadBalancer}. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
*/ | ||
class RandomLoadBalancerTests { | ||
|
||
private final ServiceInstance serviceInstance = new DefaultServiceInstance(); | ||
|
||
private RandomLoadBalancer loadBalancer; | ||
|
||
@Test | ||
void shouldReturnOneServiceInstance() { | ||
DiscoveryClientServiceInstanceListSupplier supplier = mock( | ||
DiscoveryClientServiceInstanceListSupplier.class); | ||
when(supplier.get()).thenReturn( | ||
Flux.just(Arrays.asList(serviceInstance, new DefaultServiceInstance()))); | ||
loadBalancer = new RandomLoadBalancer(new SimpleObjectProvider<>(supplier), | ||
"test"); | ||
|
||
Response<ServiceInstance> response = loadBalancer.choose().block(); | ||
|
||
assertThat(response.hasServer()).isTrue(); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyResponseWhenSupplierNotAvailable() { | ||
loadBalancer = new RandomLoadBalancer(new SimpleObjectProvider<>(null), "test"); | ||
|
||
Response<ServiceInstance> response = loadBalancer.choose().block(); | ||
|
||
assertThat(response.hasServer()).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyResponseWhenNoInstancesAvailable() { | ||
DiscoveryClientServiceInstanceListSupplier supplier = mock( | ||
DiscoveryClientServiceInstanceListSupplier.class); | ||
when(supplier.get()).thenReturn(Flux.just(Collections.emptyList())); | ||
loadBalancer = new RandomLoadBalancer(new SimpleObjectProvider<>(supplier), | ||
"test"); | ||
|
||
Response<ServiceInstance> response = loadBalancer.choose().block(); | ||
|
||
assertThat(response.hasServer()).isFalse(); | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so convoluted in order to maintain deprecated approach. Later, in 3.0.0, it's cleaned up: https://github.com/spring-cloud/spring-cloud-commons/blob/random-load-balancer/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancer.java