Skip to content

Optimize instance selection performance #1363

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -29,11 +29,13 @@
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.util.function.SingletonSupplier;

/**
* A random-based implementation of {@link ReactorServiceInstanceLoadBalancer}.
*
* @author Olga Maciaszek-Sharma
* @author Nan Chiu
* @since 2.2.7
*/
public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
Expand All @@ -42,7 +44,7 @@ public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {

private final String serviceId;

private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
private final SingletonSupplier<ServiceInstanceListSupplier> serviceInstanceListSingletonSupplier;

/**
* @param serviceInstanceListSupplierProvider a provider of
Expand All @@ -52,14 +54,15 @@ public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
public RandomLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
String serviceId) {
this.serviceId = serviceId;
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
this.serviceInstanceListSingletonSupplier = SingletonSupplier.of(
() -> serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new)
);
}

@SuppressWarnings("rawtypes")
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
.getIfAvailable(NoopServiceInstanceListSupplier::new);
ServiceInstanceListSupplier supplier = serviceInstanceListSingletonSupplier.obtain();
return supplier.get(request)
.next()
.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -30,13 +30,15 @@
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.util.function.SingletonSupplier;

/**
* A Round-Robin-based implementation of {@link ReactorServiceInstanceLoadBalancer}.
*
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Zhuozhi JI
* @author Nan Chiu
*/
public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {

Expand All @@ -46,7 +48,7 @@ public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalance

final String serviceId;

ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
private final SingletonSupplier<ServiceInstanceListSupplier> serviceInstanceListSingletonSupplier;

/**
* @param serviceInstanceListSupplierProvider a provider of
Expand All @@ -67,7 +69,9 @@ public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> servic
public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
String serviceId, int seedPosition) {
this.serviceId = serviceId;
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
this.serviceInstanceListSingletonSupplier = SingletonSupplier.of(
() -> serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new)
);
this.position = new AtomicInteger(seedPosition);
}

Expand All @@ -77,8 +81,7 @@ public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> servic
// https://github.com/Netflix/ocelli/blob/master/ocelli-core/
// src/main/java/netflix/ocelli/loadbalancer/RoundRobinLoadBalancer.java
public Mono<Response<ServiceInstance>> choose(Request request) {
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
.getIfAvailable(NoopServiceInstanceListSupplier::new);
ServiceInstanceListSupplier supplier = serviceInstanceListSingletonSupplier.obtain();
return supplier.get(request)
.next()
.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));
Expand Down
Loading