netflix.ocelli.loadbalancer.DefaultLoadBalancerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ocelli-core Show documentation
Show all versions of ocelli-core Show documentation
ocelli-core developed by Netflix
package netflix.ocelli.loadbalancer;
import netflix.ocelli.ClientConnector;
import netflix.ocelli.FailureDetectorFactory;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.LoadBalancerBuilder;
import netflix.ocelli.MembershipEvent;
import netflix.ocelli.PartitionedLoadBalancer;
import netflix.ocelli.PartitionedLoadBalancerBuilder;
import netflix.ocelli.WeightingStrategy;
import netflix.ocelli.algorithm.EqualWeightStrategy;
import netflix.ocelli.functions.Connectors;
import netflix.ocelli.functions.Delays;
import netflix.ocelli.functions.Failures;
import netflix.ocelli.functions.Functions;
import netflix.ocelli.selectors.ClientsAndWeights;
import netflix.ocelli.selectors.RoundRobinSelectionStrategy;
import rx.Observable;
import rx.functions.Func1;
import java.util.concurrent.TimeUnit;
public class DefaultLoadBalancerBuilder implements LoadBalancerBuilder {
private Observable> hostSource;
private String name = "";
private WeightingStrategy weightingStrategy = new EqualWeightStrategy();
private Func1 connectedHostCountStrategy = Functions.identity();
private Func1 quaratineDelayStrategy = Delays.fixed(10, TimeUnit.SECONDS);
private Func1, Observable> selectionStrategy = new RoundRobinSelectionStrategy();
private FailureDetectorFactory failureDetector = Failures.never();
private ClientConnector clientConnector = Connectors.immediate();
public DefaultLoadBalancerBuilder(Observable> hostSource) {
this.hostSource = hostSource;
}
/**
* Arbitrary name assigned to the connection pool, mostly for debugging purposes
* @param name
*/
public LoadBalancerBuilder withName(String name) {
this.name = name;
return this;
}
/**
* Strategy used to determine the delay time in msec based on the quarantine
* count. The count is incremented by one for each failure detections and reset
* once the host is back to normal.
*/
public LoadBalancerBuilder withQuarantineStrategy(Func1 quaratineDelayStrategy) {
this.quaratineDelayStrategy = quaratineDelayStrategy;
return this;
}
/**
* Strategy used to determine how many hosts should be active.
* This strategy is invoked whenever a host is added or removed from the pool
*/
public LoadBalancerBuilder withActiveClientCountStrategy(Func1 activeClientCountStrategy) {
this.connectedHostCountStrategy = activeClientCountStrategy;
return this;
}
/**
* Source for host membership events
*/
public LoadBalancerBuilder withMembershipSource(Observable> hostSource) {
this.hostSource = hostSource;
return this;
}
/**
* Strategy use to calculate weights for active clients
*/
public LoadBalancerBuilder withWeightingStrategy(WeightingStrategy algorithm) {
this.weightingStrategy = algorithm;
return this;
}
/**
* Strategy used to select hosts from the calculated weights.
* @param selectionStrategy
*/
public LoadBalancerBuilder withSelectionStrategy(Func1, Observable> selectionStrategy) {
this.selectionStrategy = selectionStrategy;
return this;
}
/**
* The failure detector returns an Observable that will emit a Throwable for each
* failure of the client. The load balancer will quaratine the client in response.
* @param failureDetector
*/
public LoadBalancerBuilder withFailureDetector(FailureDetectorFactory failureDetector) {
this.failureDetector = failureDetector;
return this;
}
/**
* The connector can be used to prime a client prior to activating it in the connection
* pool.
* @param clientConnector
*/
public LoadBalancerBuilder withClientConnector(ClientConnector clientConnector) {
this.clientConnector = clientConnector;
return this;
}
public LoadBalancer build() {
assert hostSource != null;
return new DefaultLoadBalancer(
name,
hostSource,
clientConnector,
failureDetector,
selectionStrategy,
quaratineDelayStrategy,
connectedHostCountStrategy,
weightingStrategy);
}
@Override
public PartitionedLoadBalancerBuilder withPartitioner(final Func1> partitioner) {
return new PartitionedLoadBalancerBuilder() {
@Override
public PartitionedLoadBalancer build() {
return new DefaultPartitioningLoadBalancer(
name,
hostSource,
clientConnector,
failureDetector,
selectionStrategy,
quaratineDelayStrategy,
connectedHostCountStrategy,
weightingStrategy,
partitioner);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy