AggregatingLoadbalancerService implements LoadBalancerService { @Override public Observable<GetJobLoadBalancersResult> getLoadBalancers(JobId jobId, CallMetadata callMetadata) { ClientCall<GetJobLoadBalancersResult> getLoadBalancersForJob = (client, responseObserver) -> wrap(client, callMetadata).getJobLoadBalancers(jobId, responseObserver); return aggregatingClient.callExpectingErrors(LoadBalancerServiceGrpc::newStub, getLoadBalancersForJob) .filter(response -> response.getResult().hasError() || response.getResult().getValue().getLoadBalancersCount() > 0) .reduce(ResponseMerger.emptyResponseMarker(), ResponseMerger.singleValue()) .filter(ResponseMerger::isNotEmptyResponseMarker) .flatMap(response -> response.getResult().map(Observable::just).onErrorGet(Observable::error)) .switchIfEmpty(Observable.just(GetJobLoadBalancersResult.newBuilder().setJobId(jobId.getId()).build())); } @Inject  AggregatingLoadbalancerService(CellConnector connector,
                                          CallMetadataResolver callMetadataResolver,
                                          GrpcConfiguration grpcConfiguration,
                                          AggregatingCellClient aggregatingClient,
                                          AggregatingJobManagementServiceHelper jobManagementServiceHelper); @Override Observable<GetAllLoadBalancersResult> getAllLoadBalancers(GetAllLoadBalancersRequest request, CallMetadata callMetadata); @Override Observable<GetJobLoadBalancersResult> getLoadBalancers(JobId jobId, CallMetadata callMetadata); @Override Completable addLoadBalancer(AddLoadBalancerRequest request, CallMetadata callMetadata); @Override Completable removeLoadBalancer(RemoveLoadBalancerRequest removeLoadBalancerRequest, CallMetadata callMetadata);  }
@Test public void getLoadBalancersForJob() { JobLoadBalancer jobLoadBalancer1 = new JobLoadBalancer(JOB_1, LB_1); JobLoadBalancer jobLoadBalancer2 = new JobLoadBalancer(JOB_2, LB_2); final CellWithLoadBalancers cellWithLoadBalancersOne = new CellWithLoadBalancers(singletonList(jobLoadBalancer1)); final CellWithLoadBalancers cellWithLoadBalancersTwo = new CellWithLoadBalancers(singletonList(jobLoadBalancer2)); cellOne.getServiceRegistry().addService(cellWithLoadBalancersOne); cellTwo.getServiceRegistry().addService(cellWithLoadBalancersTwo); final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers(JobId.newBuilder().setId(JOB_2).build(), JUNIT_REST_CALL_METADATA).test(); resultSubscriber.awaitValueCount(1, 1, TimeUnit.SECONDS); resultSubscriber.assertNoErrors(); final List<GetJobLoadBalancersResult> onNextEvents = resultSubscriber.getOnNextEvents(); assertThat(onNextEvents.size()).isEqualTo(1); assertThat(onNextEvents.get(0).getJobId()).isEqualTo(JOB_2); assertThat(onNextEvents.get(0).getLoadBalancersCount()).isEqualTo(1); assertThat(onNextEvents.get(0).getLoadBalancers(0).getId()).isEqualTo(LB_2); }
@Test public void getJobLoadBalancersWithOneFailingCell() { JobLoadBalancer jobLoadBalancer1 = new JobLoadBalancer(JOB_1, LB_1); JobLoadBalancer jobLoadBalancer2 = new JobLoadBalancer(JOB_1, LB_2); final CellWithLoadBalancers cellWithLoadBalancersOne = new CellWithLoadBalancers( Arrays.asList(jobLoadBalancer1, jobLoadBalancer2)); cellOne.getServiceRegistry().addService(cellWithLoadBalancersOne); cellTwo.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.INTERNAL)); final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers( JobId.newBuilder().setId(JOB_1).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitValueCount(1, 1, TimeUnit.SECONDS); resultSubscriber.assertNoErrors(); final List<GetJobLoadBalancersResult> onNextEvents = resultSubscriber.getOnNextEvents(); assertThat(onNextEvents).hasSize(1); final List<LoadBalancerId> loadBalancersList = onNextEvents.get(0).getLoadBalancersList(); final List<String> resultLoadBalancers = loadBalancersList.stream().map(loadBalancerId -> loadBalancerId.getId()).collect(Collectors.toList()); assertThat(resultLoadBalancers).contains(LB_1, LB_2); }
@Test public void getJobLoadBalancersWithTwoFailingCell() { cellOne.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.NOT_FOUND)); cellTwo.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.INTERNAL)); final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers( JobId.newBuilder().setId(JOB_1).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitTerminalEvent(1, TimeUnit.SECONDS); resultSubscriber.assertNoValues(); final List<Throwable> onErrorEvents = resultSubscriber.getOnErrorEvents(); assertThat(onErrorEvents).hasSize(1); assertThat(Status.fromThrowable(onErrorEvents.get(0))).isEqualTo(Status.INTERNAL); }
@Test public void getJobLoadBalancersInvalidJobId() { cellOne.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.NOT_FOUND)); cellTwo.getServiceRegistry().addService(new CellWithFailingLoadBalancers(Status.NOT_FOUND)); final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers( JobId.newBuilder().setId(JOB_1).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitTerminalEvent(1, TimeUnit.SECONDS); resultSubscriber.assertNoValues(); final List<Throwable> onErrorEvents = resultSubscriber.getOnErrorEvents(); assertThat(onErrorEvents).hasSize(1); assertThat(Status.fromThrowable(onErrorEvents.get(0))).isEqualTo(Status.NOT_FOUND); }
@Test public void getJobLoadBalancersNoAssociations() { cellOne.getServiceRegistry().addService(new CellWithLoadBalancers(Collections.emptyList())); cellTwo.getServiceRegistry().addService(new CellWithLoadBalancers(Collections.emptyList())); final AssertableSubscriber<GetJobLoadBalancersResult> resultSubscriber = service.getLoadBalancers( JobId.newBuilder().setId(JOB_1).build(), JUNIT_REST_CALL_METADATA ).test(); resultSubscriber.awaitTerminalEvent(1, TimeUnit.SECONDS); resultSubscriber.assertNoErrors().assertCompleted(); resultSubscriber.assertValueCount(1); assertThat(resultSubscriber.getOnNextEvents().get(0).getLoadBalancersCount()).isZero(); }