Skip to content
Closed
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
Expand Up @@ -23,6 +23,7 @@
*/
public final class DefaultServiceDiscovererEvent<T> implements ServiceDiscovererEvent<T> {
private final T address;
private final double weight;
private final Status status;

/**
Expand All @@ -31,7 +32,21 @@ public final class DefaultServiceDiscovererEvent<T> implements ServiceDiscoverer
* @param status Value returned by {@link #status()}.
*/
public DefaultServiceDiscovererEvent(T address, Status status) {
this(address, 1.0, status);
}

/**
* Create a new instance.
* @param address The address returned by {@link #address()}.
* @param weight The relative weight of the address.
* @param status Value returned by {@link #status()}.
*/
public DefaultServiceDiscovererEvent(T address, double weight, Status status) {
if (weight < 0 || Double.isNaN(weight) || !Double.isFinite(weight)) {
throw new IllegalArgumentException("Weight value most be a finite positive number: " + weight);
}
this.address = requireNonNull(address);
this.weight = weight;
this.status = requireNonNull(status);
}

Expand All @@ -40,6 +55,11 @@ public T address() {
return address;
}

@Override
public double weight() {
return weight;
}

@Override
public Status status() {
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public interface ServiceDiscovererEvent<ResolvedAddress> {
*/
Status status();

/**
* The weight of endpoint.
* @return the weight of endpoint.
*/
default double weight() {
return 1.0;
}

/**
* Status provided by the {@link ServiceDiscoverer} system that guides the actions of {@link LoadBalancer} upon the
* bound {@link ServiceDiscovererEvent#address()} (via {@link ServiceDiscovererEvent}).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private enum State {

private final String lbDescription;
private final Addr address;
private final double weight;
@Nullable
private final HealthCheckConfig healthCheckConfig;
@Nullable
Expand All @@ -90,13 +91,14 @@ private enum State {
private final ListenableAsyncCloseable closeable;
private volatile ConnState connState = new ConnState(emptyList(), State.ACTIVE, 0, null);

DefaultHost(final String lbDescription, final Addr address,
DefaultHost(final String lbDescription, final Addr address, final double weight,
final ConnectionPoolStrategy<C> connectionPoolStrategy,
final ConnectionFactory<Addr, ? extends C> connectionFactory,
final HostObserver hostObserver, final @Nullable HealthCheckConfig healthCheckConfig,
final @Nullable HealthIndicator healthIndicator) {
this.lbDescription = requireNonNull(lbDescription, "lbDescription");
this.address = requireNonNull(address, "address");
this.weight = weight;
this.healthIndicator = healthIndicator;
this.connectionPoolStrategy = requireNonNull(connectionPoolStrategy, "connectionPoolStrategy");
requireNonNull(connectionFactory, "connectionFactory");
Expand All @@ -113,6 +115,11 @@ public Addr address() {
return address;
}

@Override
public double weight() {
return weight;
}

@Override
public boolean markActiveIfNotClosed() {
final ConnState oldState = connStateUpdater.getAndUpdate(this, oldConnState -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private void sequentialOnNext(Collection<? extends ServiceDiscovererEvent<Resolv
} else {
// It's a new host, so the set changed.
hostSetChanged = true;
nextHosts.add(createHost(event.address()));
nextHosts.add(createHost(event.address(), event.weight()));
}
} else if (EXPIRED.equals(event.status())) {
if (!host.markExpired()) {
Expand All @@ -336,7 +336,7 @@ private void sequentialOnNext(Collection<? extends ServiceDiscovererEvent<Resolv
if (AVAILABLE.equals(event.status())) {
sendReadyEvent = true;
hostSetChanged = true;
nextHosts.add(createHost(event.address()));
nextHosts.add(createHost(event.address(), event.weight()));
}
}

Expand Down Expand Up @@ -380,15 +380,15 @@ private void sequentialOnNext(Collection<? extends ServiceDiscovererEvent<Resolv
}
}

private Host<ResolvedAddress, C> createHost(ResolvedAddress addr) {
private Host<ResolvedAddress, C> createHost(ResolvedAddress addr, double weight) {
final LoadBalancerObserver.HostObserver hostObserver = loadBalancerObserver.hostObserver(addr);
// All hosts will share the health check config of the parent load balancer.
final HealthIndicator indicator = outlierDetector.newHealthIndicator(addr, hostObserver);
// We don't need the host level health check if we are either not health checking at all or if the
// failed connect threshold is negative, meaning disabled.
final HealthCheckConfig hostHealthCheckConfig =
healthCheckConfig == null || healthCheckConfig.failedThreshold < 0 ? null : healthCheckConfig;
final Host<ResolvedAddress, C> host = new DefaultHost<>(lbDescription, addr, connectionPoolStrategy,
final Host<ResolvedAddress, C> host = new DefaultHost<>(lbDescription, addr, weight, connectionPoolStrategy,
connectionFactory, hostObserver, hostHealthCheckConfig, indicator);
if (indicator != null) {
indicator.setHost(host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface Host<ResolvedAddress, C extends LoadBalancedConnection> extends Listen
*/
ResolvedAddress address();

double weight();

/**
* Determine the health status of this host.
* @return whether the host considers itself healthy enough to serve traffic. This is best effort and does not
Expand Down