class DefaultLoadBalancerService implements LoadBalancerService {

    @VisibleForTesting
    void backfillTargetsToStore() {
        Observable<Single<LoadBalancer>> fetchLoadBalancerOperations = loadBalancerStore.getAssociations().stream()
                .map(a -> a.getJobLoadBalancer().getLoadBalancerId())
                .map(loadBalancerId -> loadBalancerConnector.getLoadBalancer(loadBalancerId)
                        // 404s will not error, and just return a LoadBalancer with state=REMOVED and no registered targets
                        .retryWhen(RetryHandlerBuilder.retryHandler()
                                .withRetryDelay(10, TimeUnit.MILLISECONDS)
                                .withRetryCount(10)
                                .buildExponentialBackoff()
                        )
                )
                .collect(Collectors.collectingAndThen(Collectors.toList(), Observable::from));

        try {
            int concurrency = configuration.getStoreBackfillConcurrencyLimit();
            ReactorExt.toFlux(Single.mergeDelayError(fetchLoadBalancerOperations, concurrency))
                    .flatMap(this::backfillTargetsToStore)
                    .ignoreElements()
                    .block(Duration.ofMillis(configuration.getStoreBackfillTimeoutMs()));
        } catch (Exception e) {
            Registry registry = runtime.getRegistry();
            registry.counter(registry.createId("titus.loadbalancer.service.backfillErrors",
                    "error", e.getClass().getSimpleName()
            )).increment();

            // swallow the error so we do not prevent activation of the leader
            // regular operations will not be affected by the incomplete backfill, but targets may leak until remaining
            // targets get backfilled during the next run
            logger.error("Backfill did not complete successfully, missing targets may be orphaned and leak on load balancers (i.e. never deregistered)", e);
        }
    }

    @Inject public  DefaultLoadBalancerService(TitusRuntime runtime,
                                      LoadBalancerConfiguration configuration,
                                      LoadBalancerConnector loadBalancerConnector,
                                      LoadBalancerStore loadBalancerStore,
                                      V3JobOperations v3JobOperations,
                                      LoadBalancerJobValidator validator);
    @VisibleForTesting  DefaultLoadBalancerService(TitusRuntime runtime,
                               LoadBalancerConfiguration configuration,
                               LoadBalancerConnector loadBalancerConnector,
                               LoadBalancerStore loadBalancerStore,
                               LoadBalancerJobOperations loadBalancerJobOperations,
                               LoadBalancerReconciler reconciler,
                               LoadBalancerJobValidator validator,
                               Scheduler scheduler);

}