All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.executor.ExecutorBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.executor;

import java.util.Collection;

import netflix.ocelli.CachingInstanceTransformer;
import netflix.ocelli.FailureDetectingInstanceFactory;
import netflix.ocelli.Instance;
import netflix.ocelli.InstanceCollector;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.loadbalancer.RoundRobinLoadBalancer;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Actions;
import rx.functions.Func1;
import rx.functions.Func2;

public class ExecutorBuilder {
    public static interface Configurator {
        void configure(ExecutorBuilder builder);
    }
    
    private FailureDetectingInstanceFactory.Builder fdBuilder = FailureDetectingInstanceFactory.builder();
    
    private Func1                     hostToClient;
    private Observable>         instances;
    private Func2>      operation;
    private Action1                      clientShutdown = Actions.empty();
    private LoadBalancer                 lb = RoundRobinLoadBalancer.create();
    private Func2, Func2>, Executor> strategy = SimpleExecutor.factory();

    public ExecutorBuilder withInstances(Observable> hosts) {
        this.instances = hosts;
        return this;
    }
    
    public ExecutorBuilder withClientFactory(Func1 hostToClient) {
        this.hostToClient = hostToClient;
        return this;
    }
    
    public ExecutorBuilder withClientConnector(Func1> clientConnector) {
        this.fdBuilder.withClientConnector(clientConnector);
        return this;
    }
    
    public ExecutorBuilder withClientShutdown(Action1 clientShutdown) {
        this.clientShutdown = clientShutdown;
        return this;
    }
    
    public ExecutorBuilder withFailureDetector(Func1> failureDetector) {
        fdBuilder.withFailureDetector(failureDetector);
        return this;
    }
    
    public ExecutorBuilder withQuarantineStrategy(Func1 quarantineStrategy) {
        fdBuilder.withQuarantineStrategy(quarantineStrategy);
        return this;
    }
    
    public ExecutorBuilder withRequestOperation(Func2> operation) {
        this.operation = operation;
        return this;
    }
    
    public ExecutorBuilder withLoadBalancer(LoadBalancer lb) {
        this.lb = lb;
        return this;
    }
    
    public ExecutorBuilder withExecutionStrategy(Func2, Func2>, Executor> strategy) {
        this.strategy = strategy;
        return this;
    }
    
    public Executor build() {
        CachingInstanceTransformer memberToInstance = CachingInstanceTransformer.from(
                hostToClient,
                clientShutdown,
                fdBuilder.build());

        instances
            .map(memberToInstance)
            .compose(new InstanceCollector())
            .subscribe(this.lb);

        return strategy.call(lb, operation);
    }
    
    public static  ExecutorBuilder builder() {
        return new ExecutorBuilder();
    }
    
    public static  Executor create(Collection> configs) {
        ExecutorBuilder builder = builder();
        for (Configurator config : configs) {
            config.configure(builder);
        }
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy